@@ -11,15 +11,20 @@ Date: January 2012
11
11
#include < cerrno>
12
12
#include < cassert>
13
13
14
- #if defined(__linux__) || \
15
- defined (__FreeBSD_kernel__) || \
16
- defined(__GNU__) || \
17
- defined(__unix__) || \
18
- defined(__CYGWIN__) || \
19
- defined(__MACH__)
14
+ #include < fstream>
15
+ #include < cstdlib>
16
+ #include < algorithm>
17
+ #include < sstream>
18
+ #include < vector>
19
+
20
+ #if defined(__linux__) || \
21
+ defined (__FreeBSD_kernel__) || \
22
+ defined(__GNU__) || \
23
+ defined(__unix__) || \
24
+ defined(__CYGWIN__) || \
25
+ defined(__MACH__)
20
26
#include < unistd.h>
21
27
#include < dirent.h>
22
- #include < cstdlib>
23
28
#include < cstdio>
24
29
#include < sys/stat.h>
25
30
#endif
@@ -89,7 +94,7 @@ Function: delete_directory
89
94
90
95
void delete_directory (const std::string &path)
91
96
{
92
- #ifdef _WIN32
97
+ #ifdef _WIN32
93
98
94
99
std::string pattern=path+" \\ *" ;
95
100
@@ -106,7 +111,7 @@ void delete_directory(const std::string &path)
106
111
unlink (info.name );
107
112
}
108
113
109
- #else
114
+ #else
110
115
111
116
DIR *dir=opendir (path.c_str ());
112
117
@@ -120,7 +125,7 @@ void delete_directory(const std::string &path)
120
125
closedir (dir);
121
126
}
122
127
123
- #endif
128
+ #endif
124
129
125
130
rmdir (path.c_str ());
126
131
}
@@ -142,137 +147,107 @@ std::string concat_dir_file(
142
147
const std::string &directory,
143
148
const std::string &file_name)
144
149
{
145
- #ifdef _WIN32
146
- return (file_name.size ()>1 &&
147
- file_name[0 ]!=' /' &&
148
- file_name[1 ]!=' :' ) ?
149
- file_name : directory+" \\ " +file_name;
150
- #else
150
+ #ifdef _WIN32
151
+ return (file_name.size ()>1 &&
152
+ file_name[0 ]!=' /' &&
153
+ file_name[1 ]!=' :' ) ?
154
+ file_name : directory+" \\ " +file_name;
155
+ #else
151
156
return (!file_name.empty () && file_name[0 ]==' /' ) ?
152
- file_name : directory+" /" +file_name;
153
- #endif
154
- }
155
-
156
-
157
-
158
-
159
-
160
- #include < fstream>
161
- #include < cstdlib>
162
- #include < algorithm>
163
- #include < sstream>
164
- #include < vector>
165
-
166
- #if defined(WIN32)
167
- # include < windows.h>
168
- #elif defined(__linux__) || defined(__APPLE__)
169
- # include < sys/stat.h>
157
+ file_name : directory+" /" +file_name;
170
158
#endif
159
+ }
171
160
172
-
173
- static std::string::size_type fileutl_parse_last_dir_pos (
174
- std::string const & file_pathname
175
- )
161
+ static std::string::size_type fileutl_parse_last_dir_pos (
162
+ std::string const &file_pathname)
176
163
{
177
- std::string::size_type const last_slash_pos =
178
- file_pathname.find_last_of (' /' );
179
- std::string::size_type const last_backslash_pos =
180
- file_pathname.find_last_of (' \\ ' );
181
- std::string::size_type const last_dir_pos =
182
- last_slash_pos == std::string::npos ?
183
- (last_backslash_pos == std::string::npos ?
184
- 0ULL :
185
- last_backslash_pos + 1ULL ) :
186
- (last_backslash_pos == std::string::npos ?
187
- last_slash_pos + 1ULL :
188
- (last_slash_pos < last_backslash_pos ?
189
- last_backslash_pos :
190
- last_slash_pos))
191
- ;
192
- return last_dir_pos;
164
+ auto found = file_pathname.find_last_of (" /\\ " );
165
+ if (found == std::string::npos)
166
+ return 0 ;
167
+ else
168
+ return found + 1 ;
193
169
}
194
170
195
-
196
- bool fileutl_file_exists (std::string const & pathname)
171
+ bool fileutl_file_exists (std::string const &pathname)
197
172
{
198
- std::ifstream f{pathname,std::ios::binary};
173
+ std::ifstream f{pathname, std::ios::binary};
199
174
return f.good ();
200
175
}
201
176
202
- bool fileutl_is_directory (std::string const & pathname)
177
+ bool fileutl_is_directory (std::string const & pathname)
203
178
{
204
- if (!fileutl_file_exists (pathname))
205
- return false ;
179
+ if (!fileutl_file_exists (pathname))
180
+ return false ;
206
181
# if defined(WIN32)
207
- return PathIsDirectory (pathname.c_str ());
182
+ return PathIsDirectory (pathname.c_str ());
208
183
# elif defined(__linux__) || defined(__APPLE__)
209
184
struct stat buf;
210
185
stat (pathname.c_str (), &buf);
211
186
return S_ISDIR (buf.st_mode );
212
- // return S_ISREG(buf.st_mode); // is file ?
213
187
# else
214
188
# error "Unsuported platform."
215
189
# endif
216
190
}
217
191
218
- uint64_t fileutl_file_size (std::string const & file_pathname)
192
+ uint64_t fileutl_file_size (std::string const & file_pathname)
219
193
{
220
- std::ifstream f{file_pathname,std::ios::binary};
221
- std::streampos const begin = f.tellg ();
222
- f.seekg (0ULL ,std::ios::end);
223
- std::streampos const end = f.tellg ();
194
+ std::ifstream f{file_pathname, std::ios::binary};
195
+ std::streampos const begin = f.tellg ();
196
+ f.seekg (0ULL , std::ios::end);
197
+ std::streampos const end = f.tellg ();
224
198
return end - begin;
225
199
}
226
200
227
- std::string fileutl_parse_name_in_pathname (std::string const & file_pathname)
201
+ std::string fileutl_parse_name_in_pathname (std::string const & file_pathname)
228
202
{
229
203
return file_pathname.substr (fileutl_parse_last_dir_pos (file_pathname));
230
204
}
231
205
232
- std::string fileutl_parse_path_in_pathname (std::string const & file_pathname)
206
+ std::string fileutl_parse_path_in_pathname (std::string const & file_pathname)
233
207
{
234
- return file_pathname.substr (0U ,fileutl_parse_last_dir_pos (file_pathname));
208
+ return file_pathname.substr (0U , fileutl_parse_last_dir_pos (file_pathname));
235
209
}
236
210
237
- std::string fileutl_remove_extension (std::string const & filename)
211
+ std::string fileutl_remove_extension (std::string const & filename)
238
212
{
239
- return filename.substr (0U ,filename.find_last_of (' .' ));
213
+ return filename.substr (0U , filename.find_last_of (' .' ));
240
214
}
241
215
242
- void fileutl_create_directory (std::string const & pathname)
216
+ void fileutl_create_directory (std::string const & pathname)
243
217
{
244
218
# if defined(WIN32)
245
- std::system ((std::string (" mkdir \" " ) + pathname + " \" " ).c_str ());
219
+ std::system ((std::string (" mkdir \" " ) + pathname + " \" " ).c_str ());
246
220
# elif defined(__linux__) || defined(__APPLE__)
247
221
#ifdef USE_BOOST
248
222
boost::filesystem::create_directories (pathname);
249
223
#else
250
224
auto ignore = std::system (
251
- (std::string (" mkdir -p \" " ) + pathname + " \" " ).c_str ()
252
- );
225
+ (std::string (" mkdir -p \" " ) + pathname + " \" " ).c_str ());
253
226
(void )ignore;
254
227
#endif
255
228
# else
256
229
# error "Unsuported platform."
257
230
# endif
258
231
}
259
232
260
- std::string fileutl_concatenate_file_paths (std::string const & left_path,
261
- std::string const & right_path)
233
+ std::string fileutl_concatenate_file_paths (
234
+ std::string const &left_path,
235
+ std::string const &right_path)
262
236
{
263
- if (left_path.empty ())
237
+ if (left_path.empty ())
264
238
return right_path;
265
- if (right_path.empty ())
239
+ if (right_path.empty ())
266
240
return left_path;
267
241
return left_path + " /" + right_path;
268
242
}
269
243
270
- std::string fileutl_absolute_path (std::string const & path)
244
+ std::string fileutl_absolute_path (std::string const & path)
271
245
{
272
246
#if defined(WIN32)
273
247
DWORD buffer_length = GetFullPathName (path.c_str (), 0 , nullptr , nullptr );
274
248
std::vector<char > buffer (buffer_length);
275
- DWORD actual_length = GetFullPathName (path.c_str (), buffer_length, &(buffer[0 ]), nullptr );
249
+ DWORD actual_length =
250
+ GetFullPathName (path.c_str (), buffer_length, &(buffer[0 ]), nullptr );
276
251
if (actual_length != buffer_length - 1 )
277
252
throw " fileutl_absolute_path: GetFullPathName failed" ;
278
253
return std::string (&(buffer[0 ]));
@@ -284,85 +259,88 @@ std::string fileutl_absolute_path(std::string const& path)
284
259
#endif
285
260
}
286
261
287
- std::string fileutl_normalise_path (std::string const & path)
262
+ std::string fileutl_normalise_path (std::string const & path)
288
263
{
289
- std::string result = path;
290
- std::replace (result.begin (),result.end (),' \\ ' ,' /' );
291
- std::string::size_type pos = 0ULL ;
292
- while ((pos = result.find (" //" ,0 )) != std::string::npos)
293
- result.replace (pos,2 , " /" );
294
- while ((pos = result.find (" /./" ,0 )) != std::string::npos)
295
- result.replace (pos,3 , " /" );
296
- while ((pos = result.find (" /../" ,0 )) != std::string::npos)
264
+ std::string result = path;
265
+ std::replace (result.begin (), result.end (), ' \\ ' , ' /' );
266
+ std::string::size_type pos = 0ULL ;
267
+ while ((pos = result.find (" //" , 0 )) != std::string::npos)
268
+ result.replace (pos, 2 , " /" );
269
+ while ((pos = result.find (" /./" , 0 )) != std::string::npos)
270
+ result.replace (pos, 3 , " /" );
271
+ while ((pos = result.find (" /../" , 0 )) != std::string::npos)
297
272
{
298
273
assert (pos != 0ULL );
299
- const std::string::size_type prev_pos = result.rfind (" /" ,pos-1ULL );
274
+ const std::string::size_type prev_pos = result.rfind (" /" , pos-1ULL );
300
275
if (prev_pos == std::string::npos)
301
276
break ;
302
- result.replace (prev_pos,pos - prev_pos + 4ULL ," /" );
277
+ result.replace (prev_pos, pos - prev_pos + 4ULL , " /" );
303
278
}
304
279
return result;
305
280
}
306
281
307
- void fileutl_split_pathname (std::string const & pathname,
308
- std::vector<std::string>& output)
282
+ void fileutl_split_pathname (
283
+ std::string const &pathname,
284
+ std::vector<std::string> &output)
309
285
{
310
- std::istringstream istr (fileutl_normalise_path (pathname));
311
- std::string token;
312
- while (std::getline (istr,token,' /' ))
286
+ std::istringstream istr (fileutl_normalise_path (pathname));
287
+ std::string token;
288
+ while (std::getline (istr, token, ' /' ))
313
289
output.push_back (token);
314
290
}
315
291
316
- std::string fileutl_join_path_parts (std::vector<std::string> const & parts)
292
+ std::string fileutl_join_path_parts (std::vector<std::string> const & parts)
317
293
{
318
- if (parts.empty ())
294
+ if (parts.empty ())
319
295
return " " ;
320
- std::string result = parts.at (0 );
321
- for (uint64_t i = 1ULL ; i < parts.size (); ++i)
296
+ std::string result = parts.at (0 );
297
+ for (uint64_t i = 1ULL ; i < parts.size (); ++i)
322
298
{
323
299
result.push_back (' /' );
324
300
result.append (parts.at (i));
325
301
}
326
302
return result;
327
303
}
328
304
329
- std::string fileutl_get_common_preffix (std::string const & pathname1,
330
- std::string const & pathname2)
305
+ std::string fileutl_get_common_prefix (
306
+ std::string const &pathname1,
307
+ std::string const &pathname2)
331
308
{
332
309
std::vector<std::string> split1;
333
- fileutl_split_pathname (pathname1,split1);
310
+ fileutl_split_pathname (pathname1, split1);
334
311
335
312
std::vector<std::string> split2;
336
- fileutl_split_pathname (pathname2,split2);
313
+ fileutl_split_pathname (pathname2, split2);
337
314
338
315
std::vector<std::string> common_split;
339
- for (uint64_t i = 0ULL , size = std::min (split1.size (),split2.size ());
340
- i < size && split1.at (i) == split2.at (i);
341
- ++i)
316
+ for (uint64_t i = 0ULL , size = std::min (split1.size (), split2.size ());
317
+ i < size && split1.at (i) == split2.at (i);
318
+ ++i)
342
319
common_split.push_back (split1.at (i));
343
320
344
- std::string const result = fileutl_join_path_parts (common_split);
321
+ std::string const result = fileutl_join_path_parts (common_split);
345
322
return result;
346
323
}
347
324
348
- std::string fileutl_get_relative_path (std::string const & pathname,
349
- std::string const & directory)
325
+ std::string fileutl_get_relative_path (
326
+ std::string const &pathname,
327
+ std::string const &directory)
350
328
{
351
329
std::vector<std::string> split1;
352
- fileutl_split_pathname (pathname,split1);
353
- std::reverse (split1.begin (),split1.end ());
330
+ fileutl_split_pathname (pathname, split1);
331
+ std::reverse (split1.begin (), split1.end ());
354
332
355
333
std::vector<std::string> split2;
356
- fileutl_split_pathname (directory,split2);
357
- std::reverse (split2.begin (),split2.end ());
334
+ fileutl_split_pathname (directory, split2);
335
+ std::reverse (split2.begin (), split2.end ());
358
336
359
- while (!split1.empty () && !split2.empty () && split1.back () == split2.back ())
337
+ while (!split1.empty () && !split2.empty () && split1.back () == split2.back ())
360
338
{
361
339
split1.pop_back ();
362
340
split2.pop_back ();
363
341
}
364
342
365
- std::reverse (split1.begin (),split1.end ());
366
- std::string const result = fileutl_join_path_parts (split1);
343
+ std::reverse (split1.begin (), split1.end ());
344
+ std::string const result = fileutl_join_path_parts (split1);
367
345
return result;
368
346
}
0 commit comments