| 33 | } |
| 34 | |
| 35 | static int32_t normalizeFilePath(char* _dst, int32_t _dstSize, const char* _src, int32_t _num) |
| 36 | { |
| 37 | // Reference(s): |
| 38 | // - Lexical File Names in Plan 9 or Getting Dot-Dot Right |
| 39 | // https://web.archive.org/web/20180629044444/https://9p.io/sys/doc/lexnames.html |
| 40 | |
| 41 | const int32_t num = strLen(_src, _num); |
| 42 | |
| 43 | if (0 == num) |
| 44 | { |
| 45 | return strCopy(_dst, _dstSize, "."); |
| 46 | } |
| 47 | |
| 48 | int32_t size = 0; |
| 49 | |
| 50 | StaticMemoryBlockWriter writer(_dst, _dstSize); |
| 51 | Error err; |
| 52 | |
| 53 | int32_t idx = 0; |
| 54 | int32_t dotdot = 0; |
| 55 | |
| 56 | if (2 <= num |
| 57 | && ':' == _src[1]) |
| 58 | { |
| 59 | size += write(&writer, toUpper(_src[idx]), &err); |
| 60 | size += write(&writer, ':', &err); |
| 61 | idx += 2; |
| 62 | dotdot = size; |
| 63 | } |
| 64 | |
| 65 | const int32_t slashIdx = idx; |
| 66 | |
| 67 | bool rooted = isPathSeparator(_src[idx]); |
| 68 | if (rooted) |
| 69 | { |
| 70 | size += write(&writer, '/', &err); |
| 71 | ++idx; |
| 72 | dotdot = size; |
| 73 | } |
| 74 | |
| 75 | bool trailingSlash = false; |
| 76 | |
| 77 | while (idx < num && err.isOk() ) |
| 78 | { |
| 79 | switch (_src[idx]) |
| 80 | { |
| 81 | case '/': |
| 82 | case '\\': |
| 83 | ++idx; |
| 84 | trailingSlash = idx == num; |
| 85 | break; |
| 86 | |
| 87 | case '.': |
| 88 | if (idx+1 == num |
| 89 | || isPathSeparator(_src[idx+1]) ) |
| 90 | { |
| 91 | ++idx; |
| 92 | break; |