-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.h
More file actions
48 lines (41 loc) · 1001 Bytes
/
script.h
File metadata and controls
48 lines (41 loc) · 1001 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#ifndef SCRIPT_H
#define SCRIPT_H
#include <stdbool.h>
#include "array.h"
#define SCRIPT_HEADER 8
static char *script_header(char *script) {
char *header = malloc(sizeof(char) * (SCRIPT_HEADER + 1));
int i = 0;
for (;
i < SCRIPT_HEADER
&& script[i] != '\0'
&& script[i] != '\n';
i++
) header[i] = script[i];
header[i] = '\0';
return header;
}
/* Loads a script from `filename`. */
static char *script_load(char *filename) {
FILE *f = fopen(filename, "r");
if (!f)
return NULL;
fseek(f, 0, SEEK_END);
long fs = ftell(f);
rewind(f);
char *script = (char*)malloc(fs + 1);
fread(script, 1, fs, f);
script[fs] = '\0';
fclose(f);
return script;
}
/* Exports `script` to `filename`. */
static bool script_export(char *script, char *filename) {
FILE *f = fopen(filename, "w");
if (!f)
return false;
fwrite(script, 1, strlen(script), f);
fclose(f);
return true;
}
#endif