Debian Codegraph @

terps/tads/glk/osemglken.c

raw symbols SCIP
1 /* Emglken implementation */
2
3 #include <stddef.h>
4 #include "glk.h"
5 #include "glkstart.h"
6 #include "osemglken.h"
7 #include "osifctyp.h"
8
9 /* Access a file - determine if the file exists. */
10 int osfacc(const char *fname) {
11 if (fname[0] == '\0') {
12 return 1;
13 }
14 frefid_t fref = glkunix_fileref_create_by_name_uncleaned(0, fname, 0);
15 if (fref) {
16 int result = glk_fileref_does_file_exist(fref);
17 glk_fileref_destroy(fref);
18 if (result) {
19 return 0;
20 }
21 }
22 return 1;
23 }
24
25 /* Close a file. */
26 void osfcls(osfildef *fp) {
27 glk_stream_close(fp, 0);
28 }
29
30 /* Delete a file. */
31 int osfdel(const char *fname) {
32 if (fname[0] == '\0') {
33 return -1;
34 }
35 frefid_t fref = glkunix_fileref_create_by_name_uncleaned(0, fname, 0);
36 if (fref) {
37 glk_fileref_delete_file(fref);
38 glk_fileref_destroy(fref);
39 }
40 return 0;
41 }
42
43 /* Flush buffered writes to a file. */
44 int osfflush(osfildef *fp) {
45 return 0;
46 }
47
48 /* Get a character from a file. */
49 int osfgetc(osfildef *fp) {
50 return glk_get_char_stream(fp);
51 }
52
53 /* Get a line of text from a text file. */
54 char *osfgets(char *buf, size_t len, osfildef *fp) {
55 glui32 count = glk_get_line_stream(fp, buf, len);
56 if (count == 0) {
57 return NULL;
58 }
59 return buf;
60 }
61
62 osfildef *open_file_generic(const char *fname, glui32 binary, glui32 fmode, glui32 trunc) {
63 frefid_t fref = glkunix_fileref_create_by_name_uncleaned(binary, fname, 0);
64 if (fref) {
65 if (trunc) {
66 glk_fileref_delete_file(fref);
67 }
68 osfildef *result = glk_stream_open_file(fref, fmode, 0);
69 glk_fileref_destroy(fref);
70 return result;
71 }
72 return NULL;
73 }
74
75 /* Open binary file for reading. */
76 osfildef *osfoprb(const char *fname, os_filetype_t typ) {
77 return open_file_generic(fname, fileusage_BinaryMode, filemode_Read, 0);
78 }
79
80 /* Open text file for reading. */
81 osfildef *osfoprt(const char *fname, os_filetype_t typ) {
82 return open_file_generic(fname, fileusage_TextMode, filemode_Read, 0);
83 }
84
85 /* Open binary file for reading/writing. If the file already exists,
86 * truncate the existing contents. Create a new file if it doesn't
87 * already exist. */
88 osfildef *osfoprwtb(const char *fname, os_filetype_t typ) {
89 return open_file_generic(fname, fileusage_BinaryMode, filemode_ReadWrite, 1);
90 }
91
92 /* Open text file for reading/writing. If the file already exists,
93 * truncate the existing contents. Create a new file if it doesn't
94 * already exist. */
95 osfildef *osfoprwtt(const char *fname, os_filetype_t typ) {
96 return open_file_generic(fname, fileusage_TextMode, filemode_ReadWrite, 1);
97 }
98
99 /* Open binary file for writing. */
100 osfildef *osfopwb(const char *fname, os_filetype_t typ) {
101 return open_file_generic(fname, fileusage_BinaryMode, filemode_Write, 0);
102 }
103
104 /* Open text file for writing. */
105 osfildef *osfopwt(const char *fname, os_filetype_t typ) {
106 return open_file_generic(fname, fileusage_TextMode, filemode_Write, 0);
107 }
108
109 /* Get the current seek location in the file. */
110 long osfpos(osfildef *fp) {
111 return glk_stream_get_position(fp);
112 }
113
114 /* print a null-terminated string to osfildef* file */
115 void os_fprintz(osfildef *fp, const char *str) {
116 glk_put_string_stream(fp, (char*) str);
117 }
118
119 /* print a counted-length string (which might not be null-terminated) to a file */
120 void os_fprint(osfildef *fp, const char *str, size_t len) {
121 glk_put_buffer_stream(fp, (char*) str, len);
122 }
123
124 /* Write a line of text to a text file. */
125 int osfputs(const char *buf, osfildef *fp) {
126 glk_put_string_stream(fp, (char*) buf);
127 return 0;
128 }
129
130 /* Read bytes from file. */
131 int osfrb(osfildef *fp, void *buf, int bufl) {
132 glk_get_buffer_stream(fp, buf, bufl);
133 return 0;
134 }
135
136 /* Read bytes from file and return the number of bytes read. */
137 size_t osfrbc(osfildef *fp, void *buf, size_t bufl) {
138 return glk_get_buffer_stream(fp, buf, bufl);
139 }
140
141 /* Seek to a location in the file. */
142 int osfseek(osfildef *fp, long pos, int mode) {
143 glk_stream_set_position(fp, pos, mode);
144 return 0;
145 }
146
147 /* Write bytes to file. */
148 int osfwb(osfildef *fp, const void *buf, int bufl) {
149 glk_put_buffer_stream(fp, (void*) buf, bufl);
150 return 0;
151 }
152
153 /* Rename a file. */
154 // I don't think we should really need this, as it's only in net code, but TADS won't compile without it
155 int os_rename_file(const char *oldname, const char *newname) {
156 return 0;
157 }