[go: up one dir, main page]

Menu

[r95]: / aambuilding / lv_io.cpp  Maximize  Restore  History

Download this file

159 lines (121 with data), 4.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/****************************************************************************
*
* Copyright (c) 2006 by JIA Pei, all rights reserved.
* Copyright (c) 2006 by Vision Open: http://www.visionopen.com/
*
* Author: JIA Pei
* Contact: jp4work@gmail.com
* URL: http://www.visionopen.com/members/jiapei.php
* The author administrates Vision Open -- http://www.visionopen.com
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* This software is partly based on the following open source:
*
* - Boost
* - OpenCV
*
* This software is using IMM Face Database, which can be downloaded from
* http://www2.imm.dtu.dk/~aam/.
*
* M. B. Stegmann, B. K. Ersb{\o}ll, and R. Larsen. FAME - a flexible appearance
* modelling environment. IEEE Trans. on Medical Imaging, 22(10):1319-1331, 2003
*
****************************************************************************/
// $Id: lv_io.cpp,v 1.1.1.1 2006-09-03 17:49:04 JIA Pei Exp $
#include "lv_io.h"
#include <iostream>
#include <fstream>
#include <string>
#include <boost/filesystem/operations.hpp> // includes boost/filesystem/path.hpp
#include <boost/filesystem/fstream.hpp> // ditto
#include <iostream> // for std::cout
#include <boost/regex/v4/fileiter.hpp>
using namespace std;
// local (static) compare function for the qsort() call below
static int str_compare( const void *arg1, const void *arg2 )
{
return strcmp( ( *(string*)arg1).c_str (), ( *(string*)arg2).c_str () );
}
/**
@author JIA Pei
@version 2006-09-01
@brief Scans a directory and sorts all files with a specified extension.
@param path Path to read from.
@param extension The file extension to search for.
@return The filenames found without any path.
*/
vector<string> lv_io::ScanNSortDirectory( const string &path, const string &extension )
{
boost::re_detail::file_iterator_ref *ref = new boost::re_detail::file_iterator_ref ();
BOOST_REGEX_NOEH_ASSERT(ref);
ref->count = 1;
string searchPath;
int nbFiles;
vector<string> vFilenames;
// add termination backslash (if needed)
string pathBS = lv_io::AddBackSlash(path);
// build and sort list of filenames
searchPath = pathBS + string("*.") + extension;
ref->hf = FindFirstFileA(searchPath.c_str (), &(ref->_data));
vFilenames.push_back( pathBS+string(ref->_data.cFileName) );
nbFiles=1;
while( FindNextFileA ( ref->hf, &(ref->_data) ) )
{
vFilenames.push_back( pathBS+string(ref->_data.cFileName) );
nbFiles++;
}
FindClose( ref->hf );
// sort the filenames
qsort( (void *)&(vFilenames[0]), (size_t)nbFiles, sizeof( string), str_compare );
// return
return vFilenames;
}
/**
@author JIA Pei
@version 2006-09-01
@brief Ensures that a string is terminated with a backslash.
If the already has a terminating backslash, nothing is done.
@param path Input string.
@return Backslash-terminated output string.
*/
string lv_io::AddBackSlash( const string &path )
{
int len = path.length();
// For Windows
#ifdef BOOST_REGEX_FI_WIN32_MAP
{
if (len <= 0 || (len>0 && path[len-1]=='\\'))
{
return path;
}
else
{
return path+"\\";
}
}
// For Posix -- Portable Operating System Interface for uniX
#elif defined(BOOST_REGEX_FI_POSIX_MAP)
{
if (len <= 0 || (len>0 && path[len-1]=='/'))
{
return path;
}
else
{
return path+"/";
}
}
#endif
}