HPCToolkit
FileUtil.hpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 #ifndef support_FileUtil_hpp
48 #define support_FileUtil_hpp
49 
50 //************************* System Include Files ****************************
51 
52 #include <string>
53 #include <vector>
54 
55 #include <fnmatch.h>
56 
57 //*************************** User Include Files ****************************
58 
59 //*************************** Forward Declarations ***************************
60 
61 //****************************************************************************
62 
63 namespace FileUtil {
64 
65 // ---------------------------------------------------------
66 // file names
67 // ---------------------------------------------------------
68 
69 // 'basename': returns the 'fname.ext' component of '/path/fname.ext'
70 extern std::string
71 basename(const char* fname);
72 
73 inline std::string
74 basename(const std::string& fname)
75 {
76  return basename(fname.c_str());
77 }
78 
79 
80 // 'rmSuffix': returns the 'fname' component of 'fname.ext'
81 extern std::string
82 rmSuffix(const char* fname);
83 
84 inline std::string
85 rmSuffix(const std::string& fname)
86 {
87  return rmSuffix(fname.c_str());
88 }
89 
90 
91 // 'dirname': returns the '/path' component of "/path/fname.ext"
92 extern std::string
93 dirname(const char* fname);
94 
95 inline std::string
96 dirname(const std::string& fname)
97 {
98  return dirname(fname.c_str());
99 }
100 
101 
102 static inline bool
103 fnmatch(const std::string pattern, const char* string, int flags = 0)
104 {
105  int fnd = ::fnmatch(pattern.c_str(), string, flags);
106  return (fnd == 0);
107 #if 0
108  if (fnd == 0) {
109  return true;
110  }
111  else if (fnd != FNM_NOMATCH) {
112  // error
113  }
114 #endif
115 }
116 
117 
118 bool
119 fnmatch(const std::vector<std::string>& patternVec,
120  const char* string,
121  int flags = 0);
122 
123 
124 // ---------------------------------------------------------
125 // file tests
126 // ---------------------------------------------------------
127 
128 extern bool
129 isReadable(const char* path);
130 
131 inline bool
132 isReadable(const std::string& path)
133 {
134  return isReadable(path.c_str());
135 }
136 
137 
138 bool
139 isDir(const char* path);
140 
141 inline bool
142 isDir(const std::string& path)
143 {
144  return isDir(path.c_str());
145 }
146 
147 
148 // count how often char appears in file
149 // return that number or -1 upon failure to open file for reading
150 extern int
151 countChar(const char* file, char c);
152 
153 
154 // ---------------------------------------------------------
155 // file operations
156 // ---------------------------------------------------------
157 
158 // copy: takes a NULL terminated list of file name and appends these
159 // files into destFile.
160 extern void
161 copy(const char* destFile, ...);
162 
163 inline void
164 copy(const std::string& dst, const std::string& src)
165 {
166  copy(dst.c_str(), src.c_str(), NULL);
167 }
168 
169 
170 void
171 move(const char* dst, const char* src);
172 
173 inline void
174 move(const std::string& dst, const std::string& src)
175 {
176  move(dst.c_str(), src.c_str());
177 }
178 
179 
180 
181 // deletes fname (unlink)
182 extern int
183 remove(const char* fname);
184 
185 
186 // mkdir: makes 'dir' (including all intermediate directories)
187 extern int
188 mkdir(const char* dir);
189 
190 inline void
191 mkdir(const std::string& dir)
192 {
193  FileUtil::mkdir(dir.c_str());
194 }
195 
196 
197 // mkdirUnique:
198 std::pair<std::string, bool>
199 mkdirUnique(const char* dirnm);
200 
201 inline std::pair<std::string, bool>
202 mkdirUnique(const std::string& dirnm)
203 {
204  return mkdirUnique(dirnm.c_str());
205 }
206 
207 
208 // retuns a name that can safely be used for a temporary file
209 // in a static variable, which is overwritten with each call to
210 // tmpname
211 extern const char*
212 tmpname();
213 
214 
215 } // end of FileUtil namespace
216 
217 
218 #endif // support_FileUtil_hpp
const char * tmpname()
Definition: FileUtil.cpp:418
void copy(const char *dst,...)
Definition: FileUtil.cpp:233
string dirname(const char *fName)
Definition: FileUtil.cpp:134
bool isReadable(const char *path)
Definition: FileUtil.cpp:171
bool fnmatch(const std::vector< std::string > &patternVec, const char *string, int flags)
Definition: FileUtil.cpp:147
void move(const char *dst, const char *src)
Definition: FileUtil.cpp:272
bool isDir(const char *path)
Definition: FileUtil.cpp:182
std::pair< string, bool > mkdirUnique(const char *dirnm)
Definition: FileUtil.cpp:364
int countChar(const char *path, char c)
Definition: FileUtil.cpp:194
int mkdir(const char *dir)
Definition: FileUtil.cpp:289
#define NULL
Definition: ElfHelper.cpp:85
string basename(const char *fName)
Definition: FileUtil.cpp:90
string rmSuffix(const char *fName)
Definition: FileUtil.cpp:121