MCPcopy Create free account
hub / github.com/DescentDevelopers/Descent3 / LoadScript

Function LoadScript

editor/ObjCScript.cpp:199–244  ·  view source on GitHub ↗

Loads the script into into memory (Don't forget to call FreeScript() to free the memory when done) Returns true on success

Source from the content-addressed store, hash-verified

197// Loads the script into into memory (Don't forget to call FreeScript() to free the memory when done)
198// Returns true on success
199char *LoadScript(const char *filename) {
200 char *source;
201 char file_path[256];
202 CFILE *file;
203 CString temp;
204 int size;
205 char buffer[MAX_SCRIPT_LINE_SIZE];
206
207 memset(buffer, 0, MAX_SCRIPT_LINE_SIZE);
208
209 ddio_MakePath(file_path, LocalLevelsDir, filename, NULL);
210 // mprintf(0,"Loading script from %s\n",file_path);
211 if (!cfexist(file_path)) {
212 return false;
213 }
214
215 file = cfopen(file_path, "rt");
216 do {
217 size = cf_ReadString(buffer, MAX_SCRIPT_LINE_SIZE - 2, file);
218 buffer[size] = '\xd';
219 buffer[size + 1] = '\xa';
220 buffer[size + 2] = '\0';
221 temp += buffer;
222 memset(buffer, 0, size);
223 } while (!cfeof(file));
224
225 char *tmp_script;
226 size = strlen(temp.GetBuffer(1));
227 tmp_script = (char *)mem_malloc(size + 1);
228 ASSERT(tmp_script);
229 strcpy(tmp_script, temp.GetBuffer(1));
230 if (size > 2)
231 tmp_script[size - 2] = '\0';
232 temp = tmp_script;
233 if (tmp_script)
234 mem_free(tmp_script);
235
236 cfclose(file);
237
238 source = (char *)mem_malloc(strlen(temp.GetBuffer(1)) + 1);
239 if (!source)
240 return false;
241 strcpy(source, temp.GetBuffer(1));
242
243 return source;
244}
245
246void SaveScript(const char *filename, char *script) {
247 CFILE *file;

Callers 5

OnEditScriptMethod · 0.85
LoadCurrentModuleMethod · 0.85
OnScriptselAddMethod · 0.85
InvokeScriptStudioFunction · 0.85

Calls 7

cfexistFunction · 0.85
cfopenFunction · 0.85
cf_ReadStringFunction · 0.85
cfeofFunction · 0.85
cfcloseFunction · 0.85
GetBufferMethod · 0.80
ddio_MakePathFunction · 0.50

Tested by

no test coverage detected