| 63 | } |
| 64 | |
| 65 | bool CheckPath( |
| 66 | char const* commandLineArg, |
| 67 | std::wstring* str, |
| 68 | wchar_t const* path, |
| 69 | bool directory, |
| 70 | bool* exists) |
| 71 | { |
| 72 | // If path not specified in command line, use default value |
| 73 | if (path == nullptr) { |
| 74 | path = str->c_str(); |
| 75 | } |
| 76 | |
| 77 | // Get full path |
| 78 | wchar_t fullPath[MAX_PATH]; |
| 79 | auto r = GetFullPathName(path, _countof(fullPath), fullPath, nullptr); |
| 80 | if (r == 0 || r > _countof(fullPath)) { |
| 81 | fprintf(stderr, "error: could not get full path for: %ls\n", path); |
| 82 | fprintf(stderr, " Specify a new path using the %s command line argument.\n", commandLineArg); |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | // Make sure file exists and is the right type (file vs directory). |
| 87 | auto attr = GetFileAttributes(fullPath); |
| 88 | if (attr == INVALID_FILE_ATTRIBUTES) { |
| 89 | if (exists == nullptr) { // must exist |
| 90 | fprintf(stderr, "error: path does not exist: %ls\n", fullPath); |
| 91 | fprintf(stderr, " Specify a new path using the %s command line argument.\n", commandLineArg); |
| 92 | return false; |
| 93 | } |
| 94 | *exists = false; |
| 95 | } else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != (directory ? FILE_ATTRIBUTE_DIRECTORY : 0u)) { |
| 96 | fprintf(stderr, "error: path is not a %s: %ls\n", directory ? "directory" : "file", fullPath); |
| 97 | fprintf(stderr, " Specify a new path using the %s command line argument.\n", commandLineArg); |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | // Update the string with the full path and end directories with separator. |
| 102 | str->assign(fullPath); |
| 103 | if (directory) { |
| 104 | *str += L'\\'; |
| 105 | } |
| 106 | |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | } |
| 111 | |