project_files/hwc/rtl/fileio.c
branchwebgl
changeset 7983 02f36c3e7f6c
child 9966 01e198990211
equal deleted inserted replaced
7981:aac257b77842 7983:02f36c3e7f6c
       
     1 /*
       
     2  * XXX: assume all files are text files
       
     3  */
       
     4 
       
     5 #include "misc.h"
       
     6 #include "fileio.h"
       
     7 #include <string.h>
       
     8 #include <stdlib.h>
       
     9 #include <assert.h>
       
    10 #include <sys/stat.h>
       
    11 
       
    12 io_result_t IOResult;
       
    13 int FileMode;
       
    14 
       
    15 static void init(File f) {
       
    16     f->fp = NULL;
       
    17     f->eof = 0;
       
    18     f->mode = NULL;
       
    19     f->record_len = 0;
       
    20 }
       
    21 
       
    22 void fpcrtl_assign__vars(File *f, string255 name) {
       
    23     FIX_STRING(name);
       
    24     *f = (File) malloc(sizeof(file_wrapper_t));
       
    25     strcpy((*f)->file_name, name.str);
       
    26     init(*f);
       
    27 }
       
    28 
       
    29 void fpcrtl_reset1(File f) {
       
    30     f->fp = fopen(f->file_name, "r");
       
    31     if (!f->fp) {
       
    32         IOResult = IO_ERROR_DUMMY;
       
    33         printf("Failed to open %s\n", f->file_name);
       
    34         return;
       
    35     } else {
       
    36 #ifdef FPCRTL_DEBUG
       
    37         printf("Opened %s\n", f->file_name);
       
    38 #endif
       
    39     }
       
    40     IOResult = IO_NO_ERROR;
       
    41     f->mode = "r";
       
    42 }
       
    43 
       
    44 void fpcrtl_reset2(File f, int l) {
       
    45     f->eof = 0;
       
    46     f->fp = fopen(f->file_name, "rb");
       
    47     if (!f->fp) {
       
    48         IOResult = IO_ERROR_DUMMY;
       
    49         printf("Failed to open %s\n", f->file_name);
       
    50         return;
       
    51     }
       
    52     IOResult = IO_NO_ERROR;
       
    53     f->mode = "rb";
       
    54     f->record_len = l;
       
    55 }
       
    56 
       
    57 void __attribute__((overloadable)) fpcrtl_rewrite(File f) {
       
    58     f->fp = fopen(f->file_name, "w+");
       
    59     if (!f->fp) {
       
    60         IOResult = IO_ERROR_DUMMY;
       
    61         return;
       
    62     }
       
    63     IOResult = IO_NO_ERROR;
       
    64     f->mode = "w+";
       
    65 }
       
    66 
       
    67 void __attribute__((overloadable)) fpcrtl_rewrite(File f, Integer l) {
       
    68     IOResult = IO_NO_ERROR;
       
    69     fpcrtl_rewrite(f);
       
    70     if (f->fp) {
       
    71         f->record_len = l;
       
    72     }
       
    73 }
       
    74 
       
    75 void fpcrtl_close(File f) {
       
    76     IOResult = IO_NO_ERROR;
       
    77     fclose(f->fp);
       
    78     free(f);
       
    79 }
       
    80 
       
    81 boolean fpcrtl_eof(File f) {
       
    82     IOResult = IO_NO_ERROR;
       
    83     if (f->eof || f->fp == NULL || feof(f->fp)) {
       
    84         return true;
       
    85     } else {
       
    86         return false;
       
    87     }
       
    88 }
       
    89 
       
    90 void __attribute__((overloadable)) fpcrtl_readLn(File f) {
       
    91     IOResult = IO_NO_ERROR;
       
    92     char line[256];
       
    93     if (fgets(line, sizeof(line), f->fp) == NULL) {
       
    94         f->eof = 1;
       
    95     }
       
    96     if (feof(f->fp)) {
       
    97         f->eof = 1;
       
    98     }
       
    99 }
       
   100 
       
   101 void __attribute__((overloadable)) fpcrtl_readLn__vars(File f, Integer *i) {
       
   102     string255 s;
       
   103 
       
   104     if (feof(f->fp)) {
       
   105         f->eof = 1;
       
   106         return;
       
   107     }
       
   108 
       
   109     fpcrtl_readLn__vars(f, &s);
       
   110 
       
   111     *i = atoi(s.str);
       
   112 }
       
   113 
       
   114 void __attribute__((overloadable)) fpcrtl_readLn__vars(File f, LongWord *i) {
       
   115     string255 s;
       
   116 
       
   117     if (feof(f->fp)) {
       
   118         f->eof = 1;
       
   119         return;
       
   120     }
       
   121 
       
   122     fpcrtl_readLn__vars(f, &s);
       
   123 
       
   124     *i = atoi(s.str);
       
   125 }
       
   126 
       
   127 void __attribute__((overloadable)) fpcrtl_readLn__vars(File f, string255 *s) {
       
   128 
       
   129     if (fgets(s->str, 255, f->fp) == NULL) {
       
   130 
       
   131         s->len = 0;
       
   132         s->str[0] = 0;
       
   133 
       
   134         f->eof = 1;
       
   135         return;
       
   136     }
       
   137 
       
   138     if (feof(f->fp)) {
       
   139         s->len = 0;
       
   140         f->eof = 1;
       
   141         return;
       
   142     }
       
   143 
       
   144     IOResult = IO_NO_ERROR;
       
   145 
       
   146     s->len = strlen(s->str);
       
   147     if ((s->len > 0) && (s->str[s->len - 1] == '\n')) {
       
   148         s->str[s->len - 1] = 0;
       
   149         s->len--;
       
   150     }
       
   151 }
       
   152 
       
   153 void __attribute__((overloadable)) fpcrtl_write(File f, string255 s) {
       
   154     FIX_STRING(s);
       
   155     fprintf(f->fp, "%s", s.str);
       
   156 }
       
   157 
       
   158 void __attribute__((overloadable)) fpcrtl_write(FILE *f, string255 s) {
       
   159     FIX_STRING(s);
       
   160     fprintf(f, "%s", s.str);
       
   161 }
       
   162 
       
   163 void __attribute__((overloadable)) fpcrtl_writeLn(File f, string255 s) {
       
   164     FIX_STRING(s);
       
   165     fprintf(f->fp, "%s\n", s.str);
       
   166 }
       
   167 
       
   168 void __attribute__((overloadable)) fpcrtl_writeLn(FILE *f, string255 s) {
       
   169     FIX_STRING(s);
       
   170     fprintf(f, "%s\n", s.str);
       
   171 }
       
   172 
       
   173 void fpcrtl_blockRead__vars(File f, void *buf, Integer count, Integer *result) {
       
   174     assert(f->record_len > 0);
       
   175     *result = fread(buf, f->record_len, count, f->fp);
       
   176 }
       
   177 
       
   178 /*
       
   179  * XXX: dummy blockWrite
       
   180  */
       
   181 void fpcrtl_blockWrite__vars(File f, const void *buf, Integer count,
       
   182         Integer *result) {
       
   183     assert(0);
       
   184 }
       
   185 
       
   186 bool fpcrtl_directoryExists(string255 dir) {
       
   187 
       
   188     struct stat st;
       
   189     FIX_STRING(dir);
       
   190 
       
   191     IOResult = IO_NO_ERROR;
       
   192 
       
   193 #ifdef FPCRTL_DEBUG
       
   194     printf("Warning: directoryExists is called. This may not work when compiled to js.\n");
       
   195 #endif
       
   196 
       
   197     if (stat(dir.str, &st) == 0) {
       
   198         return true;
       
   199     }
       
   200 
       
   201     return false;
       
   202 }
       
   203 
       
   204 bool fpcrtl_fileExists(string255 filename) {
       
   205 
       
   206     FIX_STRING(filename);
       
   207 
       
   208     IOResult = IO_NO_ERROR;
       
   209 
       
   210     FILE *fp = fopen(filename.str, "r");
       
   211     if (fp) {
       
   212         fclose(fp);
       
   213         return true;
       
   214     }
       
   215     return false;
       
   216 }
       
   217 
       
   218 void __attribute__((overloadable)) fpcrtl_flush(Text f) {
       
   219     printf("flush not implemented\n");
       
   220     assert(0);
       
   221 }
       
   222 
       
   223 void __attribute__((overloadable)) fpcrtl_flush(FILE *f) {
       
   224     fflush(f);
       
   225 }
       
   226