| 1 |
|
| 2 |
|
| 3 |
#include <stddef.h> |
| 4 |
#include "glk.h" |
| 5 |
#include "glkstart.h" |
| 6 |
#include "osemglken.h" |
| 7 |
#include "osifctyp.h" |
| 8 |
|
| 9 |
|
| 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 |
|
| 26 |
void osfcls(osfildef *fp) { |
| 27 |
glk_stream_close(fp, 0); |
| 28 |
} |
| 29 |
|
| 30 |
|
| 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 |
|
| 44 |
int osfflush(osfildef *fp) { |
| 45 |
return 0; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
int osfgetc(osfildef *fp) { |
| 50 |
return glk_get_char_stream(fp); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 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 |
|
| 76 |
osfildef *osfoprb(const char *fname, os_filetype_t typ) { |
| 77 |
return open_file_generic(fname, fileusage_BinaryMode, filemode_Read, 0); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
osfildef *osfoprt(const char *fname, os_filetype_t typ) { |
| 82 |
return open_file_generic(fname, fileusage_TextMode, filemode_Read, 0); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
osfildef *osfoprwtb(const char *fname, os_filetype_t typ) { |
| 89 |
return open_file_generic(fname, fileusage_BinaryMode, filemode_ReadWrite, 1); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
osfildef *osfoprwtt(const char *fname, os_filetype_t typ) { |
| 96 |
return open_file_generic(fname, fileusage_TextMode, filemode_ReadWrite, 1); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
osfildef *osfopwb(const char *fname, os_filetype_t typ) { |
| 101 |
return open_file_generic(fname, fileusage_BinaryMode, filemode_Write, 0); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
osfildef *osfopwt(const char *fname, os_filetype_t typ) { |
| 106 |
return open_file_generic(fname, fileusage_TextMode, filemode_Write, 0); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
long osfpos(osfildef *fp) { |
| 111 |
return glk_stream_get_position(fp); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
void os_fprintz(osfildef *fp, const char *str) { |
| 116 |
glk_put_string_stream(fp, (char*) str); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
void os_fprint(osfildef *fp, const char *str, size_t len) { |
| 121 |
glk_put_buffer_stream(fp, (char*) str, len); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
int osfputs(const char *buf, osfildef *fp) { |
| 126 |
glk_put_string_stream(fp, (char*) buf); |
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
int osfrb(osfildef *fp, void *buf, int bufl) { |
| 132 |
glk_get_buffer_stream(fp, buf, bufl); |
| 133 |
return 0; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
size_t osfrbc(osfildef *fp, void *buf, size_t bufl) { |
| 138 |
return glk_get_buffer_stream(fp, buf, bufl); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
int osfseek(osfildef *fp, long pos, int mode) { |
| 143 |
glk_stream_set_position(fp, pos, mode); |
| 144 |
return 0; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 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 |
|
| 154 |
|
| 155 |
int os_rename_file(const char *oldname, const char *newname) { |
| 156 |
return 0; |
| 157 |
} |