| 86 | } |
| 87 | |
| 88 | bool CommandLineInterface::DoSource(std::string path, SourceBitset* pOptions) |
| 89 | { |
| 90 | if (m_SourceFileStack.size() >= 100) |
| 91 | { |
| 92 | return SetError("Source depth (100) exceeded, possible recursive source."); |
| 93 | } |
| 94 | |
| 95 | normalize_separators(path); |
| 96 | |
| 97 | // Separate the path out of the filename if any |
| 98 | std::string filename; |
| 99 | std::string folder; |
| 100 | std::string::size_type lastSeparator = path.rfind('/'); |
| 101 | if (lastSeparator == std::string::npos) |
| 102 | { |
| 103 | filename.assign(path); |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | ++lastSeparator; |
| 108 | if (lastSeparator < path.length()) |
| 109 | { |
| 110 | folder = path.substr(0, lastSeparator); |
| 111 | filename.assign(path.substr(lastSeparator, path.length() - lastSeparator)); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if (!folder.empty()) if (!DoPushD(folder)) |
| 116 | { |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | FILE* pFile = fopen(filename.c_str() , "rb"); |
| 121 | if (!pFile) |
| 122 | { |
| 123 | if (!folder.empty()) |
| 124 | { |
| 125 | DoPopD(); |
| 126 | } |
| 127 | return SetError("Failed to open file for reading: " + path); |
| 128 | } |
| 129 | |
| 130 | // obtain file size: |
| 131 | fseek(pFile, 0, SEEK_END); |
| 132 | long lSize = ftell(pFile); |
| 133 | rewind(pFile); |
| 134 | |
| 135 | // allocate memory to contain the whole file: |
| 136 | char* buffer = reinterpret_cast<char*>(malloc(sizeof(char) * (lSize + 1))); |
| 137 | if (!buffer) |
| 138 | { |
| 139 | if (!folder.empty()) |
| 140 | { |
| 141 | DoPopD(); |
| 142 | } |
| 143 | path.insert(0, "Memory allocation failed: "); |
| 144 | fclose(pFile); |
| 145 | return SetError("Failed to open file for reading: " + path); |
no test coverage detected