Line data Source code
1 :
2 : /* Copyright (c) 2009, Cedric Stalder <cedric.stalder@gmail.com>
3 : * 2009-2013, Stefan Eilemann <eile@equalizergraphics.com>
4 : *
5 : * This library is free software; you can redistribute it and/or modify it under
6 : * the terms of the GNU Lesser General Public License version 2.1 as published
7 : * by the Free Software Foundation.
8 : *
9 : * This library is distributed in the hope that it will be useful, but WITHOUT
10 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 : * details.
13 : *
14 : * You should have received a copy of the GNU Lesser General Public License
15 : * along with this library; if not, write to the Free Software Foundation, Inc.,
16 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 : */
18 :
19 : #include "file.h"
20 :
21 : #include "debug.h"
22 : #include "os.h"
23 :
24 : #include <boost/regex.hpp>
25 : #include <sys/stat.h>
26 : #ifndef _MSC_VER
27 : # include <dirent.h>
28 : #endif
29 :
30 : namespace lunchbox
31 : {
32 54 : Strings searchDirectory( const std::string& directory,
33 : const std::string& pattern )
34 : {
35 54 : Strings files;
36 108 : const boost::regex regex( pattern );
37 :
38 : #ifdef _MSC_VER
39 : WIN32_FIND_DATA file;
40 : const std::string search = directory.empty() ? "*.*" : directory + "\\*.*";
41 : HANDLE hSearch = FindFirstFile( search.c_str(), &file );
42 :
43 : if( hSearch == INVALID_HANDLE_VALUE )
44 : {
45 : LBVERB << "Error finding the first file to match " << pattern << " in "
46 : << directory << std::endl;
47 : FindClose( hSearch );
48 : return files;
49 : }
50 :
51 : if( boost::regex_match( file.cFileName, regex ))
52 : files.push_back( file.cFileName );
53 : while( FindNextFile( hSearch, &file ))
54 : {
55 : if( boost::regex_match( file.cFileName, regex ))
56 : files.push_back( file.cFileName );
57 : }
58 : FindClose( hSearch );
59 :
60 : #else
61 :
62 54 : DIR* dir = opendir( directory.c_str() );
63 54 : if( !dir )
64 : {
65 6 : LBVERB << "Can't open directory " << directory << std::endl;
66 6 : return files;
67 : }
68 :
69 : struct dirent* entry;
70 :
71 1430 : while(( entry = readdir( dir )) != 0 )
72 : {
73 1334 : const std::string candidate( entry->d_name );
74 1334 : if( boost::regex_match( candidate, regex ))
75 137 : files.push_back( entry->d_name );
76 1334 : }
77 :
78 48 : closedir(dir);
79 : #endif
80 48 : return files;
81 : }
82 :
83 120 : std::string getFilename( const std::string& filename )
84 : {
85 120 : size_t lastSeparator = 0;
86 120 : const size_t length = filename.length();
87 :
88 4576 : for( size_t i = 0; i < length; ++i )
89 4456 : if( filename[ i ] == '/' || filename[i] == '\\' )
90 492 : lastSeparator = i+1;
91 :
92 : return lastSeparator == 0 ? filename :
93 120 : filename.substr( lastSeparator, length );
94 : }
95 :
96 0 : std::string getDirname( const std::string& filename )
97 : {
98 0 : size_t lastSeparator = 0;
99 0 : const size_t length = filename.length();
100 :
101 0 : for( size_t i = 0; i < length; ++i )
102 0 : if( filename[ i ] == '/' || filename[i] == '\\' )
103 0 : lastSeparator = i+1;
104 :
105 0 : return lastSeparator == 0 ? "." : filename.substr( 0, lastSeparator );
106 : }
107 :
108 90 : }
|