-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcgfref.c
371 lines (306 loc) · 8.34 KB
/
cgfref.c
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> /* for unlink() */
#include <sys/stat.h> /* for stat() */
#include "glk.h"
#include "cheapglk.h"
/* This file implements filerefs as they work in a stdio system: a
fileref contains a pathname, a text/binary flag, and a file
type.
*/
/* Linked list of all filerefs */
static fileref_t *gli_filereflist = NULL;
#define BUFLEN (256)
static char workingdir[BUFLEN] = ".";
fileref_t *gli_new_fileref(char *filename, glui32 usage, glui32 rock)
{
fileref_t *fref = (fileref_t *)malloc(sizeof(fileref_t));
if (!fref)
return NULL;
fref->magicnum = MAGIC_FILEREF_NUM;
fref->rock = rock;
fref->filename = malloc(1 + strlen(filename));
strcpy(fref->filename, filename);
fref->textmode = ((usage & fileusage_TextMode) != 0);
fref->filetype = (usage & fileusage_TypeMask);
fref->prev = NULL;
fref->next = gli_filereflist;
gli_filereflist = fref;
if (fref->next) {
fref->next->prev = fref;
}
if (gli_register_obj)
fref->disprock = (*gli_register_obj)(fref, gidisp_Class_Fileref);
else
fref->disprock.ptr = NULL;
return fref;
}
void gli_delete_fileref(fileref_t *fref)
{
fileref_t *prev, *next;
if (gli_unregister_obj) {
(*gli_unregister_obj)(fref, gidisp_Class_Fileref, fref->disprock);
fref->disprock.ptr = NULL;
}
fref->magicnum = 0;
if (fref->filename) {
free(fref->filename);
fref->filename = NULL;
}
prev = fref->prev;
next = fref->next;
fref->prev = NULL;
fref->next = NULL;
if (prev)
prev->next = next;
else
gli_filereflist = next;
if (next)
next->prev = prev;
free(fref);
}
void glk_fileref_destroy(fileref_t *fref)
{
if (!fref) {
gli_strict_warning("fileref_destroy: invalid ref");
return;
}
gli_delete_fileref(fref);
}
static char *gli_suffix_for_usage(glui32 usage)
{
switch (usage & fileusage_TypeMask) {
case fileusage_Data:
return ".glkdata";
case fileusage_SavedGame:
return ".glksave";
case fileusage_Transcript:
case fileusage_InputRecord:
return ".txt";
default:
return "";
}
}
frefid_t glk_fileref_create_temp(glui32 usage, glui32 rock)
{
char filename[BUFLEN];
fileref_t *fref;
sprintf(filename, "/tmp/glktempfref-XXXXXX");
close(mkstemp(filename));
fref = gli_new_fileref(filename, usage, rock);
if (!fref) {
gli_strict_warning("fileref_create_temp: unable to create fileref.");
return NULL;
}
return fref;
}
frefid_t glk_fileref_create_from_fileref(glui32 usage, frefid_t oldfref,
glui32 rock)
{
fileref_t *fref;
if (!oldfref) {
gli_strict_warning("fileref_create_from_fileref: invalid ref");
return NULL;
}
fref = gli_new_fileref(oldfref->filename, usage, rock);
if (!fref) {
gli_strict_warning("fileref_create_from_fileref: unable to create fileref.");
return NULL;
}
return fref;
}
frefid_t glk_fileref_create_by_name(glui32 usage, char *name,
glui32 rock)
{
fileref_t *fref;
char buf[BUFLEN];
char buf2[2*BUFLEN+10];
int len;
char *cx;
char *suffix;
/* The new spec recommendations: delete all characters in the
string "/\<>:|?*" (including quotes). Truncate at the first
period. Change to "null" if there's nothing left. Then append
an appropriate suffix: ".glkdata", ".glksave", ".txt".
*/
for (cx=name, len=0; (*cx && *cx!='.' && len<BUFLEN-1); cx++) {
switch (*cx) {
case '"':
case '\\':
case '/':
case '>':
case '<':
case ':':
case '|':
case '?':
case '*':
break;
default:
buf[len++] = *cx;
}
}
buf[len] = '\0';
if (len == 0) {
strcpy(buf, "null");
len = strlen(buf);
}
suffix = gli_suffix_for_usage(usage);
sprintf(buf2, "%s/%s%s", workingdir, buf, suffix);
fref = gli_new_fileref(buf2, usage, rock);
if (!fref) {
gli_strict_warning("fileref_create_by_name: unable to create fileref.");
return NULL;
}
return fref;
}
frefid_t glk_fileref_create_by_prompt(glui32 usage, glui32 fmode,
glui32 rock)
{
fileref_t *fref;
char buf[BUFLEN];
char newbuf[2*BUFLEN+10];
char *res;
char *cx;
int val, gotdot;
char *prompt, *prompt2;
switch (usage & fileusage_TypeMask) {
case fileusage_SavedGame:
prompt = "Enter saved game";
break;
case fileusage_Transcript:
prompt = "Enter transcript file";
break;
case fileusage_InputRecord:
prompt = "Enter command record file";
break;
case fileusage_Data:
default:
prompt = "Enter data file";
break;
}
if (fmode == filemode_Read)
prompt2 = "to load";
else
prompt2 = "to store";
printf("%s %s: ", prompt, prompt2);
res = fgets(buf, BUFLEN-1, stdin);
if (!res) {
printf("\n<end of input>\n");
glk_exit();
}
/* Trim whitespace from end and beginning. */
val = strlen(buf);
while (val
&& (buf[val-1] == '\n'
|| buf[val-1] == '\r'
|| buf[val-1] == ' '))
val--;
buf[val] = '\0';
for (cx = buf; *cx == ' '; cx++) { }
val = strlen(cx);
if (!val) {
/* The player just hit return. It would be nice to provide a
default value, but this implementation is too cheap. */
return NULL;
}
if (cx[0] == '/')
strcpy(newbuf, cx);
else
sprintf(newbuf, "%s/%s", workingdir, cx);
/* If there is no dot-suffix, add a standard one. */
val = strlen(newbuf);
gotdot = FALSE;
while (val && (buf[val-1] != '/')) {
if (buf[val-1] == '.') {
gotdot = TRUE;
break;
}
val--;
}
if (!gotdot) {
char *suffix = gli_suffix_for_usage(usage);
strcat(newbuf, suffix);
}
if (fmode == filemode_Read) {
/* According to recent spec discussion, we must silently return NULL if no such file exists. */
if (access(newbuf, R_OK)) {
return NULL;
}
}
fref = gli_new_fileref(newbuf, usage, rock);
if (!fref) {
gli_strict_warning("fileref_create_by_prompt: unable to create fileref.");
return NULL;
}
return fref;
}
frefid_t glk_fileref_iterate(fileref_t *fref, glui32 *rock)
{
if (!fref) {
fref = gli_filereflist;
}
else {
fref = fref->next;
}
if (fref) {
if (rock)
*rock = fref->rock;
return fref;
}
if (rock)
*rock = 0;
return NULL;
}
glui32 glk_fileref_get_rock(fileref_t *fref)
{
if (!fref) {
gli_strict_warning("fileref_get_rock: invalid ref.");
return 0;
}
return fref->rock;
}
glui32 glk_fileref_does_file_exist(fileref_t *fref)
{
struct stat buf;
if (!fref) {
gli_strict_warning("fileref_does_file_exist: invalid ref");
return FALSE;
}
/* This is sort of Unix-specific, but probably any stdio library
will implement at least this much of stat(). */
if (stat(fref->filename, &buf))
return 0;
if (S_ISREG(buf.st_mode))
return 1;
else
return 0;
}
void glk_fileref_delete_file(fileref_t *fref)
{
if (!fref) {
gli_strict_warning("fileref_delete_file: invalid ref");
return;
}
/* If you don't have the unlink() function, obviously, change it
to whatever file-deletion function you do have. */
unlink(fref->filename);
}
/* This should only be called from startup code. */
void glkunix_set_base_file(char *filename)
{
int ix;
for (ix=strlen(filename)-1; ix >= 0; ix--)
if (filename[ix] == '/')
break;
if (ix >= 0) {
/* There is a slash. */
strncpy(workingdir, filename, ix);
workingdir[ix] = '\0';
ix++;
}
else {
/* No slash, just a filename. */
ix = 0;
}
}