MCPcopy Create free account
hub / github.com/catboost/catboost / AbsolutePath

Function AbsolutePath

contrib/libs/flatbuffers/src/util.cpp:343–475  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

341}
342
343std::string AbsolutePath(const std::string &filepath) {
344 // clang-format off
345
346 #ifdef FLATBUFFERS_NO_ABSOLUTE_PATH_RESOLUTION
347 return filepath;
348 #else
349 #if defined(_WIN32) || defined(__MINGW32__) || defined(__MINGW64__) || defined(__CYGWIN__)
350 char abs_path[MAX_PATH];
351 return GetFullPathNameA(filepath.c_str(), MAX_PATH, abs_path, nullptr)
352 #else
353 char *abs_path_temp = realpath(filepath.c_str(), nullptr);
354 bool success = abs_path_temp != nullptr;
355 std::string abs_path;
356 if(success) {
357 abs_path = abs_path_temp;
358 free(abs_path_temp);
359 }
360 return success
361 #endif
362 ? abs_path
363 : filepath;
364 #endif // FLATBUFFERS_NO_ABSOLUTE_PATH_RESOLUTION
365 // clang-format on
366}
367
368std::string RelativeToRootPath(const std::string &project,
369 const std::string &filepath) {
370 std::string absolute_project = PosixPath(AbsolutePath(project));
371 if (absolute_project.back() != '/') absolute_project += "/";
372 std::string absolute_filepath = PosixPath(AbsolutePath(filepath));
373
374 // Find the first character where they disagree.
375 // The previous directory is the lowest common ancestor;
376 const char *a = absolute_project.c_str();
377 const char *b = absolute_filepath.c_str();
378 size_t common_prefix_len = 0;
379 while (*a != '\0' && *b != '\0' && *a == *b) {
380 if (*a == '/') common_prefix_len = a - absolute_project.c_str();
381 a++;
382 b++;
383 }
384 // the number of ../ to prepend to b depends on the number of remaining
385 // directories in A.
386 const char *suffix = absolute_project.c_str() + common_prefix_len;
387 size_t num_up = 0;
388 while (*suffix != '\0')
389 if (*suffix++ == '/') num_up++;
390 num_up--; // last one is known to be '/'.
391 std::string result = "//";
392 for (size_t i = 0; i < num_up; i++) result += "../";
393 result += absolute_filepath.substr(common_prefix_len + 1);
394
395 return result;
396}
397
398// Locale-independent code.
399#if defined(FLATBUFFERS_LOCALE_INDEPENDENT) && \
400 (FLATBUFFERS_LOCALE_INDEPENDENT > 0)

Callers 3

MessageMethod · 0.85
FilePathFunction · 0.85
RelativeToRootPathFunction · 0.85

Calls 3

realpathFunction · 0.50
freeFunction · 0.50
c_strMethod · 0.45

Tested by

no test coverage detected