| 366 | } |
| 367 | |
| 368 | std::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) && \ |