@@ -156,226 +156,6 @@ std::string concat_dir_file(
156
156
#endif
157
157
}
158
158
159
- static std::string::size_type fileutl_parse_last_dir_pos (
160
- std::string const &file_pathname)
161
- {
162
- auto found = file_pathname.find_last_of (" /\\ " );
163
- if (found == std::string::npos)
164
- return 0 ;
165
- else
166
- return found + 1 ;
167
- }
168
-
169
- bool fileutl_file_exists (std::string const &pathname)
170
- {
171
- std::ifstream f{pathname, std::ios::binary};
172
- return f.good ();
173
- }
174
-
175
- bool fileutl_is_directory (std::string const &pathname)
176
- {
177
- if (!fileutl_file_exists (pathname))
178
- return false ;
179
- # if defined(WIN32)
180
- return PathIsDirectory (pathname.c_str ());
181
- # elif defined(__linux__) || defined(__APPLE__)
182
- struct stat buf;
183
- stat (pathname.c_str (), &buf);
184
- return S_ISDIR (buf.st_mode );
185
- # else
186
- # error "Unsuported platform."
187
- # endif
188
- }
189
-
190
- uint64_t fileutl_file_size (std::string const &file_pathname)
191
- {
192
- std::ifstream f{file_pathname, std::ios::binary};
193
- std::streampos const begin = f.tellg ();
194
- f.seekg (0ULL , std::ios::end);
195
- std::streampos const end = f.tellg ();
196
- return end - begin;
197
- }
198
-
199
- std::string fileutl_parse_name_in_pathname (std::string const &file_pathname)
200
- {
201
- return file_pathname.substr (fileutl_parse_last_dir_pos (file_pathname));
202
- }
203
-
204
- std::string fileutl_parse_extension_in_pathname (const std::string &pathname)
205
- {
206
- const std::size_t idx=pathname.find_last_of (' .' );
207
- # if defined(WIN32)
208
- const std::size_t fwd_slash_idx=pathname.find_last_of (' /' );
209
- const std::size_t bwd_slash_idx=pathname.find_last_of (' \\ ' );
210
- const std::size_t slash_idx=
211
- fwd_slash_idx==std::string::npos ? bwd_slash_idx :
212
- bwd_slash_idx==std::string::npos ? fwd_slash_idx :
213
- std::max (fwd_slash_idx, bwd_slash_idx);
214
- # elif defined(__linux__) || defined(__APPLE__)
215
- const std::size_t slash_idx=pathname.find_last_of (' /' );
216
- # else
217
- # error "Unsuported platform."
218
- # endif
219
- return
220
- idx==std::string::npos || (slash_idx!=std::string::npos && idx<slash_idx) ?
221
- std::string () : pathname.substr (idx);
222
- }
223
-
224
- std::string fileutl_parse_path_in_pathname (std::string const &file_pathname)
225
- {
226
- return file_pathname.substr (0U , fileutl_parse_last_dir_pos (file_pathname));
227
- }
228
-
229
- std::string fileutl_remove_extension (std::string const &filename)
230
- {
231
- return filename.substr (0U , filename.find_last_of (' .' ));
232
- }
233
-
234
- void fileutl_create_directory (std::string const &pathname)
235
- {
236
- # if defined(WIN32)
237
- char path_sep=' \\ ' ;
238
- #else
239
- char path_sep=' /' ;
240
- #endif
241
- std::size_t search_from=0 ;
242
- while (search_from!=std::string::npos)
243
- {
244
- // Search from after the previous path separator, incidentally
245
- // skipping trying to create '/' if an absolute path is given
246
- search_from=pathname.find (path_sep, search_from+1 );
247
- std::string truncated_pathname=pathname.substr (0 , search_from);
248
- #if defined(WIN32)
249
- _mkdir (truncated_pathname.c_str ());
250
- #else
251
- mkdir (truncated_pathname.c_str (), 0777 );
252
- #endif
253
- // Ignore return-- regardless of why we can't create a
254
- // path prefix, we might as well keep trying down to more
255
- // specific paths.
256
- }
257
- }
258
-
259
- std::string fileutl_concatenate_file_paths (
260
- std::string const &left_path,
261
- std::string const &right_path)
262
- {
263
- if (left_path.empty ())
264
- return right_path;
265
- if (right_path.empty ())
266
- return left_path;
267
- return left_path + " /" + right_path;
268
- }
269
-
270
- std::string fileutl_absolute_path (std::string const &path)
271
- {
272
- #if defined(WIN32)
273
- DWORD buffer_length = GetFullPathName (path.c_str (), 0 , nullptr , nullptr );
274
- std::vector<char > buffer (buffer_length);
275
- DWORD actual_length =
276
- GetFullPathName (path.c_str (), buffer_length, &(buffer[0 ]), nullptr );
277
- if (actual_length != buffer_length - 1 )
278
- throw " fileutl_absolute_path: GetFullPathName failed" ;
279
- return std::string (&(buffer[0 ]));
280
- #else
281
- char *absolute = realpath (path.c_str (), nullptr );
282
- if (absolute==NULL )
283
- {
284
- std::string error=std::strerror (errno);
285
- throw std::ios::failure (" Could not resolve absolute path (" +error+" )" );
286
- }
287
- std::string ret (absolute);
288
- free (absolute);
289
- return ret;
290
- #endif
291
- }
292
-
293
- std::string fileutl_normalise_path (std::string const &path)
294
- {
295
- std::string result = path;
296
- std::replace (result.begin (), result.end (), ' \\ ' , ' /' );
297
- std::string::size_type pos = 0ULL ;
298
- while ((pos = result.find (" //" , 0 )) != std::string::npos)
299
- result.replace (pos, 2 , " /" );
300
- while ((pos = result.find (" /./" , 0 )) != std::string::npos)
301
- result.replace (pos, 3 , " /" );
302
- while ((pos = result.find (" /../" , 0 )) != std::string::npos)
303
- {
304
- assert (pos != 0ULL );
305
- const std::string::size_type prev_pos = result.rfind (" /" , pos-1ULL );
306
- if (prev_pos == std::string::npos)
307
- break ;
308
- result.replace (prev_pos, pos - prev_pos + 4ULL , " /" );
309
- }
310
- return result;
311
- }
312
-
313
- void fileutl_split_pathname (
314
- std::string const &pathname,
315
- std::vector<std::string> &output)
316
- {
317
- std::istringstream istr (fileutl_normalise_path (pathname));
318
- std::string token;
319
- while (std::getline (istr, token, ' /' ))
320
- output.push_back (token);
321
- }
322
-
323
- std::string fileutl_join_path_parts (std::vector<std::string> const &parts)
324
- {
325
- if (parts.empty ())
326
- return " " ;
327
- std::string result = parts.at (0 );
328
- for (uint64_t i = 1ULL ; i < parts.size (); ++i)
329
- {
330
- result.push_back (' /' );
331
- result.append (parts.at (i));
332
- }
333
- return result;
334
- }
335
-
336
- std::string fileutl_get_common_prefix (
337
- std::string const &pathname1,
338
- std::string const &pathname2)
339
- {
340
- std::vector<std::string> split1;
341
- fileutl_split_pathname (pathname1, split1);
342
-
343
- std::vector<std::string> split2;
344
- fileutl_split_pathname (pathname2, split2);
345
-
346
- std::vector<std::string> common_split;
347
- for (uint64_t i = 0ULL , size = std::min (split1.size (), split2.size ());
348
- i < size && split1.at (i) == split2.at (i);
349
- ++i)
350
- common_split.push_back (split1.at (i));
351
-
352
- std::string const result = fileutl_join_path_parts (common_split);
353
- return result;
354
- }
355
-
356
- std::string fileutl_get_relative_path (
357
- std::string const &pathname,
358
- std::string const &directory)
359
- {
360
- std::vector<std::string> split1;
361
- fileutl_split_pathname (pathname, split1);
362
- std::reverse (split1.begin (), split1.end ());
363
-
364
- std::vector<std::string> split2;
365
- fileutl_split_pathname (directory, split2);
366
- std::reverse (split2.begin (), split2.end ());
367
-
368
- while (!split1.empty () && !split2.empty () && split1.back () == split2.back ())
369
- {
370
- split1.pop_back ();
371
- split2.pop_back ();
372
- }
373
-
374
- std::reverse (split1.begin (), split1.end ());
375
- std::string const result = fileutl_join_path_parts (split1);
376
- return result;
377
- }
378
-
379
159
// / Replaces invalid characters in a file name using a hard-coded list of
380
160
// / replacements.
381
161
// / This is not designed to operate on path names and will replace folder
0 commit comments