@@ -163,174 +163,177 @@ std::string concat_dir_file(
163
163
# include < sys/stat.h>
164
164
#endif
165
165
166
- namespace fileutl { namespace {
167
166
168
-
169
- std::string::size_type parse_last_dir_pos (std::string const & file_pathname)
167
+ static std::string::size_type fileutl_parse_last_dir_pos (
168
+ std::string const & file_pathname
169
+ )
170
170
{
171
- std::string::size_type const last_slash_pos = file_pathname.find_last_of (' /' );
172
- std::string::size_type const last_backslash_pos = file_pathname.find_last_of (' \\ ' );
173
- std::string::size_type const last_dir_pos =
174
- last_slash_pos == std::string::npos ?
175
- (last_backslash_pos == std::string::npos ? 0ULL :
176
- last_backslash_pos + 1ULL ) :
177
- (last_backslash_pos == std::string::npos ? last_slash_pos + 1ULL :
178
- (last_slash_pos < last_backslash_pos ? last_backslash_pos :
179
- last_slash_pos))
180
- ;
181
- return last_dir_pos;
171
+ std::string::size_type const last_slash_pos =
172
+ file_pathname.find_last_of (' /' );
173
+ std::string::size_type const last_backslash_pos =
174
+ file_pathname.find_last_of (' \\ ' );
175
+ std::string::size_type const last_dir_pos =
176
+ last_slash_pos == std::string::npos ?
177
+ (last_backslash_pos == std::string::npos ?
178
+ 0ULL :
179
+ last_backslash_pos + 1ULL ) :
180
+ (last_backslash_pos == std::string::npos ?
181
+ last_slash_pos + 1ULL :
182
+ (last_slash_pos < last_backslash_pos ?
183
+ last_backslash_pos :
184
+ last_slash_pos))
185
+ ;
186
+ return last_dir_pos;
182
187
}
183
188
184
189
185
- }}
186
-
187
- namespace fileutl {
188
-
189
-
190
- bool file_exists (std::string const & pathname)
190
+ bool fileutl_file_exists (std::string const & pathname)
191
191
{
192
- std::ifstream f{pathname,std::ios::binary};
193
- return f.good ();
192
+ std::ifstream f{pathname,std::ios::binary};
193
+ return f.good ();
194
194
}
195
195
196
- bool is_directory (std::string const & pathname)
196
+ bool fileutl_is_directory (std::string const & pathname)
197
197
{
198
- if (!file_exists (pathname))
199
- return false ;
200
- # if defined(WIN32)
198
+ if (!fileutl_file_exists (pathname))
199
+ return false ;
200
+ # if defined(WIN32)
201
201
return PathIsDirectory (pathname.c_str ());
202
- # elif defined(__linux__) || defined(__APPLE__)
203
- struct stat buf;
204
- stat (pathname.c_str (), &buf);
205
- return S_ISDIR (buf.st_mode );
206
- // return S_ISREG(buf.st_mode); // is file ?
207
- # else
208
- # error "Unsuported platform."
209
- # endif
202
+ # elif defined(__linux__) || defined(__APPLE__)
203
+ struct stat buf;
204
+ stat (pathname.c_str (), &buf);
205
+ return S_ISDIR (buf.st_mode );
206
+ // return S_ISREG(buf.st_mode); // is file ?
207
+ # else
208
+ # error "Unsuported platform."
209
+ # endif
210
210
}
211
211
212
- uint64_t file_size (std::string const & file_pathname)
212
+ uint64_t fileutl_file_size (std::string const & file_pathname)
213
213
{
214
- std::ifstream f{file_pathname,std::ios::binary};
215
- std::streampos const begin = f.tellg ();
216
- f.seekg (0ULL ,std::ios::end);
217
- std::streampos const end = f.tellg ();
218
- return end - begin;
214
+ std::ifstream f{file_pathname,std::ios::binary};
215
+ std::streampos const begin = f.tellg ();
216
+ f.seekg (0ULL ,std::ios::end);
217
+ std::streampos const end = f.tellg ();
218
+ return end - begin;
219
219
}
220
220
221
- std::string parse_name_in_pathname (std::string const & file_pathname)
221
+ std::string fileutl_parse_name_in_pathname (std::string const & file_pathname)
222
222
{
223
- return file_pathname.substr (parse_last_dir_pos (file_pathname));
223
+ return file_pathname.substr (fileutl_parse_last_dir_pos (file_pathname));
224
224
}
225
225
226
- std::string parse_path_in_pathname (std::string const & file_pathname)
226
+ std::string fileutl_parse_path_in_pathname (std::string const & file_pathname)
227
227
{
228
- return file_pathname.substr (0U ,parse_last_dir_pos (file_pathname));
228
+ return file_pathname.substr (0U ,fileutl_parse_last_dir_pos (file_pathname));
229
229
}
230
230
231
- std::string remove_extension (std::string const & filename)
231
+ std::string fileutl_remove_extension (std::string const & filename)
232
232
{
233
233
return filename.substr (0U ,filename.find_last_of (' .' ));
234
234
}
235
235
236
- void create_directory (std::string const & pathname)
236
+ void fileutl_create_directory (std::string const & pathname)
237
237
{
238
- # if defined(WIN32)
239
- std::system ((std::string (" mkdir \" " ) + pathname + " \" " ).c_str ());
240
- # elif defined(__linux__) || defined(__APPLE__)
241
- auto ignore = std::system ((std::string (" mkdir -p \" " ) + pathname + " \" " ).c_str ());
242
- (void )ignore;
243
- # else
244
- # error "Unsuported platform."
245
- # endif
238
+ # if defined(WIN32)
239
+ std::system ((std::string (" mkdir \" " ) + pathname + " \" " ).c_str ());
240
+ # elif defined(__linux__) || defined(__APPLE__)
241
+ auto ignore = std::system (
242
+ (std::string (" mkdir -p \" " ) + pathname + " \" " ).c_str ()
243
+ );
244
+ (void )ignore;
245
+ # else
246
+ # error "Unsuported platform."
247
+ # endif
246
248
}
247
249
248
- std::string concatenate_file_paths (std::string const & left_path, std::string const & right_path)
250
+ std::string fileutl_concatenate_file_paths (std::string const & left_path,
251
+ std::string const & right_path)
249
252
{
250
- if (left_path.empty ())
251
- return right_path;
252
- if (right_path.empty ())
253
- return left_path;
254
- return left_path + " /" + right_path;
253
+ if (left_path.empty ())
254
+ return right_path;
255
+ if (right_path.empty ())
256
+ return left_path;
257
+ return left_path + " /" + right_path;
255
258
}
256
259
257
- std::string absolute_path (std::string const & path)
260
+ std::string fileutl_absolute_path (std::string const & path)
258
261
{
259
- // TODO: portability - this implementation won't probably work on Windows...
260
- std::vector<char > buffer (10000 ,0 );
261
- return realpath (path.c_str (),&buffer.at (0 ));
262
+ // TODO: portability - this implementation won't probably work on Windows...
263
+ std::vector<char > buffer (10000 ,0 );
264
+ return realpath (path.c_str (),&buffer.at (0 ));
262
265
}
263
266
264
- std::string normalise_path (std::string const & path)
267
+ std::string fileutl_normalise_path (std::string const & path)
265
268
{
266
- std::string result = path;
267
- std::replace (result.begin (),result.end (),' \\ ' ,' /' );
268
- std::string::size_type pos = 0 ;
269
- while ((pos = result.find (" /./" ,0 )) != std::string::npos)
270
- result.replace (pos,3 ," /" );
271
- // TODO: more fixes should be applied (e.g. /../, //, etc.)
272
- return result;
269
+ std::string result = path;
270
+ std::replace (result.begin (),result.end (),' \\ ' ,' /' );
271
+ std::string::size_type pos = 0 ;
272
+ while ((pos = result.find (" /./" ,0 )) != std::string::npos)
273
+ result.replace (pos,3 ," /" );
274
+ // TODO: more fixes should be applied (e.g. /../, //, etc.)
275
+ return result;
273
276
}
274
277
275
- void split_pathname (std::string const & pathname, std::vector<std::string>& output)
278
+ void fileutl_split_pathname (std::string const & pathname,
279
+ std::vector<std::string>& output)
276
280
{
277
- std::istringstream istr (normalise_path (pathname));
278
- std::string token;
279
- while (std::getline (istr,token,' /' ))
280
- output.push_back (token);
281
+ std::istringstream istr (fileutl_normalise_path (pathname));
282
+ std::string token;
283
+ while (std::getline (istr,token,' /' ))
284
+ output.push_back (token);
281
285
}
282
286
283
- std::string join_path_parts (std::vector<std::string> const & parts)
287
+ std::string fileutl_join_path_parts (std::vector<std::string> const & parts)
284
288
{
285
- if (parts.empty ())
286
- return " " ;
287
- std::string result = parts.at (0 );
288
- for (uint64_t i = 1ULL ; i < parts.size (); ++i)
289
- {
290
- result.push_back (' /' );
291
- result.append (parts.at (i));
292
- }
293
- return result;
289
+ if (parts.empty ())
290
+ return " " ;
291
+ std::string result = parts.at (0 );
292
+ for (uint64_t i = 1ULL ; i < parts.size (); ++i)
293
+ {
294
+ result.push_back (' /' );
295
+ result.append (parts.at (i));
296
+ }
297
+ return result;
294
298
}
295
299
296
- std::string get_common_preffix (std::string const & pathname1, std::string const & pathname2)
300
+ std::string fileutl_get_common_preffix (std::string const & pathname1,
301
+ std::string const & pathname2)
297
302
{
298
- std::vector<std::string> split1;
299
- split_pathname (pathname1,split1);
303
+ std::vector<std::string> split1;
304
+ fileutl_split_pathname (pathname1,split1);
300
305
301
- std::vector<std::string> split2;
302
- split_pathname (pathname2,split2);
306
+ std::vector<std::string> split2;
307
+ fileutl_split_pathname (pathname2,split2);
303
308
304
- std::vector<std::string> common_split;
305
- for (uint64_t i = 0ULL , size = std::min (split1.size (),split2.size ());
306
- i < size && split1.at (i) == split2.at (i);
307
- ++i)
308
- common_split.push_back (split1.at (i));
309
+ std::vector<std::string> common_split;
310
+ for (uint64_t i = 0ULL , size = std::min (split1.size (),split2.size ());
311
+ i < size && split1.at (i) == split2.at (i);
312
+ ++i)
313
+ common_split.push_back (split1.at (i));
309
314
310
- std::string const result = join_path_parts (common_split);
311
- return result;
315
+ std::string const result = fileutl_join_path_parts (common_split);
316
+ return result;
312
317
}
313
318
314
- std::string get_relative_path (std::string const & pathname, std::string const & directory)
319
+ std::string fileutl_get_relative_path (std::string const & pathname,
320
+ std::string const & directory)
315
321
{
316
- std::vector<std::string> split1;
317
- split_pathname (pathname,split1);
318
- std::reverse (split1.begin (),split1.end ());
319
-
320
- std::vector<std::string> split2;
321
- split_pathname (directory,split2);
322
- std::reverse (split2.begin (),split2.end ());
323
-
324
- while (!split1.empty () && !split2.empty () && split1.back () == split2.back ())
325
- {
326
- split1.pop_back ();
327
- split2.pop_back ();
328
- }
329
-
330
- std::reverse (split1.begin (),split1.end ());
331
- std::string const result = join_path_parts (split1);
332
- return result;
333
- }
322
+ std::vector<std::string> split1;
323
+ fileutl_split_pathname (pathname,split1);
324
+ std::reverse (split1.begin (),split1.end ());
325
+
326
+ std::vector<std::string> split2;
327
+ fileutl_split_pathname (directory,split2);
328
+ std::reverse (split2.begin (),split2.end ());
334
329
330
+ while (!split1.empty () && !split2.empty () && split1.back () == split2.back ())
331
+ {
332
+ split1.pop_back ();
333
+ split2.pop_back ();
334
+ }
335
335
336
+ std::reverse (split1.begin (),split1.end ());
337
+ std::string const result = fileutl_join_path_parts (split1);
338
+ return result;
336
339
}
0 commit comments