SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FileUtils.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2011 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 
30 ///
31 ///@file FileUtils.h
32 ///
33 ///@author
34 /// Chris Moulding
35 /// University of Utah
36 ///@date Sept 2000
37 ///
38 
39 #ifndef FILEUTILS_H
40 #define FILEUTILS_H 1
41 
42 #include <map>
43 #include <string>
44 #include <vector>
45 
46 #include <boost/filesystem.hpp>
47 
49 
50 namespace SCIRun {
51 
52 ////////////////////////////////////
53 /// InsertStringInFile()
54 /// Inserts "insert" in front of all occurrances of
55 /// "match" within the file named "filename"
56 
57 SCISHARE void InsertStringInFile(char* filename, const char* match, const char* insert);
58 
59 
60 ////////////////////////////////////
61 /// GetFilenamesEndingWith()
62 /// returns a std::map of strings that contains
63 /// all the files with extension "ext" inside
64 /// the directory named "dir"
65 
66 SCISHARE std::map<int,char*>* GetFilenamesEndingWith(const char* dir,
67  const char* ext);
68 
69 SCISHARE std::vector<std::string> GetFilenamesStartingWith(const std::string & dir,
70  const std::string & prefix);
71 
72 /// Return all of the filenames that appear to be in the same numeric
73 /// sequence as the reference one. This means all the letters in each
74 /// are the same and only the numbers are different. No sorting is done.
75 SCISHARE std::vector<std::string> GetFilenamesInSequence(const std::string &dir,
76  const std::string &ref);
77 
78 SCISHARE std::pair<std::string, std::string> split_filename(std::string fname);
79 
80 SCISHARE std::string findFileInPath(const std::string &filename,
81  const std::string &path);
82 
83 SCISHARE bool validFile( const std::string& filename );
84 SCISHARE bool validFile( const boost::filesystem::path& filename );
85 
86 SCISHARE bool validDir( const std::string& dirname );
87 SCISHARE bool validDir( const boost::filesystem::path& dirname );
88 
89 SCISHARE bool isSymLink( const boost::filesystem::path& filename );
90 
91 // Creates a temp file (in directoryPath), writes to it, checks the resulting files size, and then deletes it...
92 SCISHARE bool testFilesystem( std::string directoryPath );
93 
94 SCISHARE std::string autocomplete(const std::string &);
95 SCISHARE std::string canonicalize(std::string);
96 SCISHARE std::string substituteTilde(const std::string &dirstr);
97 
98 // Replaces '/' with '\' or vice-versa between unix and windows paths
99 SCISHARE void convertToWindowsPath( std::string & unixPath );
100 inline void convertToUnixPath( std::string & winPath )
101 {
102  std::replace(winPath.begin(), winPath.end(), '\\', '/');
103 }
104 
105 SCISHARE int copyFile(const boost::filesystem::path& src,
106  const boost::filesystem::path& dest);
107 SCISHARE int moveFile(const boost::filesystem::path& src,
108  const boost::filesystem::path& dest);
109 SCISHARE int deleteFile(const boost::filesystem::path& filename);
110 /// @todo: re-implement with boost
111 // The strings are not references since windows has to convert
112 // '/' to '\\', and we do that in the same string
113 SCISHARE int copyDir(std::string src, std::string dest);
114 SCISHARE int deleteDir(const boost::filesystem::path& filename);
115 
116 // Count the number of non-empty, non-whitespace-only lines in file
117 SCISHARE int getNumNonEmptyLines(char *filename);
118 
119 } // End namespace SCIRun
120 
121 #ifdef _WIN32
122 // windows doesn't have dirent... make them here
123 struct dirent
124 {
125  char *d_name;
126 };
127 
128 struct DIR;
129 
130 SCISHARE DIR *opendir(const char *);
131 SCISHARE int closedir(DIR *);
132 SCISHARE dirent *readdir(DIR *);
133 
134 // not implemented yet...
135 SCISHARE void rewinddir(DIR *);
136 #endif
137 
138 #endif
139 
SCISHARE void InsertStringInFile(char *filename, const char *match, const char *insert)
SCISHARE bool validFile(const std::string &filename)
SCISHARE std::pair< std::string, std::string > split_filename(std::string fname)
SCISHARE bool testFilesystem(std::string directoryPath)
SCISHARE std::string substituteTilde(const std::string &dirstr)
SCISHARE int getNumNonEmptyLines(char *filename)
SCISHARE int deleteDir(const boost::filesystem::path &filename)
SCISHARE int moveFile(const boost::filesystem::path &src, const boost::filesystem::path &dest)
#define SCISHARE
Definition: share.h:39
void convertToUnixPath(std::string &winPath)
Definition: FileUtils.h:100
SCISHARE std::map< int, char * > * GetFilenamesEndingWith(const char *dir, const char *ext)
string dir
Definition: eab.py:5
SCISHARE int deleteFile(const boost::filesystem::path &filename)
SCISHARE std::string autocomplete(const std::string &)
SCISHARE std::string canonicalize(std::string)
SCISHARE bool validDir(const std::string &dirname)
SCISHARE void convertToWindowsPath(std::string &unixPath)
SCISHARE int copyDir(std::string src, std::string dest)
SCISHARE int copyFile(const boost::filesystem::path &src, const boost::filesystem::path &dest)
SCISHARE std::string findFileInPath(const std::string &filename, const std::string &path)
SCISHARE std::vector< std::string > GetFilenamesStartingWith(const std::string &dir, const std::string &prefix)
SCISHARE bool isSymLink(const boost::filesystem::path &filename)
SCISHARE std::vector< std::string > GetFilenamesInSequence(const std::string &dir, const std::string &ref)