MCPcopy Create free account
hub / github.com/WolfireGames/overgrowth / ReadScriptFile

Function ReadScriptFile

Source/Scripting/scriptfile.cpp:138–188  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

136}
137
138static std::string ReadScriptFile(const Path& path) {
139 int max_tries = 10;
140 int tries = 0;
141 int len = 0;
142 bool file_exists = false;
143 FILE* file;
144
145 while (len == 0 && tries < max_tries) {
146 file = robust_fopen(path.GetFullPath(), "rb");
147
148 if (file == NULL) {
149 FatalError("Error", "Failed to open the script file: %s\n%s",
150 path.GetFullPath(), strerror(errno));
151 }
152
153 file_exists = true;
154
155 // Determine the size of the file
156 fseek(file, 0, SEEK_END);
157 len = ftell(file);
158 fseek(file, 0, SEEK_SET);
159
160 if (len == 0) {
161 fclose(file);
162 tries++;
163 BusyWaitMilliseconds(50);
164 }
165 }
166
167 // Read the entire file
168 std::string script;
169 script.resize(len);
170 int c = fread(&script[0], len, 1, file);
171
172 if (file_exists) {
173 if (len == 0) {
174 FatalError("Error", "Script file was present, but empty:\n%s\n%s",
175 path.GetFullPath(), strerror(errno));
176 } else if (c == 0) {
177 FatalError("Error", "Failed to load script file:\n%s\n%s",
178 path.GetFullPath(), strerror(errno));
179 }
180 } else {
181 FatalError("Error", "Failed to load script file:\n%s\n%s",
182 path.GetFullPath(), strerror(errno));
183 }
184
185 fclose(file);
186
187 return script;
188}
189
190static void UpdateFile(const ScriptFile* parent_script_file, ScriptFile& file, const std::string& source, const Path& path) {
191 file.parent = parent_script_file;

Callers 1

GetScriptFileRecursiveFunction · 0.85

Calls 5

robust_fopenFunction · 0.85
BusyWaitMillisecondsFunction · 0.85
GetFullPathMethod · 0.80
FatalErrorFunction · 0.50
resizeMethod · 0.45

Tested by

no test coverage detected