project_files/hwc/rtl/fileio.c
author unC0Rr
Sat, 28 Sep 2024 22:27:13 +0200
changeset 16037 2b4f361e3891
parent 14913 68e1783762bc
permissions -rw-r--r--
Wrap chat lines into div
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     1
/*
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     2
 * XXX: assume all files are text files
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     3
 */
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     4
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     5
#include "misc.h"
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     6
#include "fileio.h"
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     7
#include <string.h>
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     8
#include <stdlib.h>
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     9
#include <assert.h>
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    10
#include <sys/stat.h>
11682
2c21bc80c95d Implement GetCurrentDir in the c rtl
koda
parents: 10575
diff changeset
    11
#include <unistd.h>
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    12
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    13
io_result_t IOResult;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    14
int FileMode;
11682
2c21bc80c95d Implement GetCurrentDir in the c rtl
koda
parents: 10575
diff changeset
    15
char cwd[1024];
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    16
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    17
static void init(File f) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    18
    f->fp = NULL;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    19
    f->eof = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    20
    f->mode = NULL;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    21
    f->record_len = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    22
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    23
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    24
void fpcrtl_assign__vars(File *f, string255 name) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    25
    FIX_STRING(name);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    26
    *f = (File) malloc(sizeof(file_wrapper_t));
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    27
    strcpy((*f)->file_name, name.str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    28
    init(*f);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    29
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    30
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    31
void fpcrtl_reset1(File f) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    32
    f->fp = fopen(f->file_name, "r");
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    33
    if (!f->fp) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    34
        IOResult = IO_ERROR_DUMMY;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    35
        printf("Failed to open %s\n", f->file_name);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    36
        return;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    37
    } else {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    38
#ifdef FPCRTL_DEBUG
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    39
        printf("Opened %s\n", f->file_name);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    40
#endif
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    41
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    42
    IOResult = IO_NO_ERROR;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    43
    f->mode = "r";
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    44
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    45
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    46
void fpcrtl_reset2(File f, int l) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    47
    f->eof = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    48
    f->fp = fopen(f->file_name, "rb");
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    49
    if (!f->fp) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    50
        IOResult = IO_ERROR_DUMMY;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    51
        printf("Failed to open %s\n", f->file_name);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    52
        return;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    53
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    54
    IOResult = IO_NO_ERROR;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    55
    f->mode = "rb";
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    56
    f->record_len = l;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    57
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    58
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    59
void __attribute__((overloadable)) fpcrtl_rewrite(File f) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    60
    f->fp = fopen(f->file_name, "w+");
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    61
    if (!f->fp) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    62
        IOResult = IO_ERROR_DUMMY;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    63
        return;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    64
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    65
    IOResult = IO_NO_ERROR;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    66
    f->mode = "w+";
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    67
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    68
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    69
void __attribute__((overloadable)) fpcrtl_rewrite(File f, Integer l) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    70
    IOResult = IO_NO_ERROR;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    71
    fpcrtl_rewrite(f);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    72
    if (f->fp) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    73
        f->record_len = l;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    74
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    75
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    76
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    77
void fpcrtl_close(File f) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    78
    IOResult = IO_NO_ERROR;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    79
    fclose(f->fp);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    80
    free(f);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    81
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    82
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    83
boolean fpcrtl_eof(File f) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    84
    IOResult = IO_NO_ERROR;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    85
    if (f->eof || f->fp == NULL || feof(f->fp)) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    86
        return true;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    87
    } else {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    88
        return false;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    89
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    90
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    91
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    92
void __attribute__((overloadable)) fpcrtl_readLn(File f) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    93
    IOResult = IO_NO_ERROR;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    94
    char line[256];
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    95
    if (fgets(line, sizeof(line), f->fp) == NULL) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    96
        f->eof = 1;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    97
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    98
    if (feof(f->fp)) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    99
        f->eof = 1;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   100
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   101
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   102
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   103
void __attribute__((overloadable)) fpcrtl_readLn__vars(File f, Integer *i) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   104
    string255 s;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   105
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   106
    if (feof(f->fp)) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   107
        f->eof = 1;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   108
        return;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   109
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   110
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   111
    fpcrtl_readLn__vars(f, &s);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   112
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   113
    *i = atoi(s.str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   114
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   115
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   116
void __attribute__((overloadable)) fpcrtl_readLn__vars(File f, LongWord *i) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   117
    string255 s;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   118
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   119
    if (feof(f->fp)) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   120
        f->eof = 1;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   121
        return;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   122
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   123
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   124
    fpcrtl_readLn__vars(f, &s);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   125
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   126
    *i = atoi(s.str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   127
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   128
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   129
void __attribute__((overloadable)) fpcrtl_readLn__vars(File f, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   130
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   131
    if (fgets(s->str, 255, f->fp) == NULL) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   132
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   133
        s->len = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   134
        s->str[0] = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   135
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   136
        f->eof = 1;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   137
        return;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   138
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   139
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   140
    if (feof(f->fp)) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   141
        s->len = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   142
        f->eof = 1;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   143
        return;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   144
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   145
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   146
    IOResult = IO_NO_ERROR;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   147
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   148
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   149
    if ((s->len > 0) && (s->str[s->len - 1] == '\n')) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   150
        s->str[s->len - 1] = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   151
        s->len--;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   152
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   153
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   154
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   155
void __attribute__((overloadable)) fpcrtl_write(File f, string255 s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   156
    FIX_STRING(s);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   157
    fprintf(f->fp, "%s", s.str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   158
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   159
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   160
void __attribute__((overloadable)) fpcrtl_write(FILE *f, string255 s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   161
    FIX_STRING(s);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   162
    fprintf(f, "%s", s.str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   163
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   164
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   165
void __attribute__((overloadable)) fpcrtl_writeLn(File f, string255 s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   166
    FIX_STRING(s);
10575
13b1e9008f4b super-filthy hack to allow pas2c to fallback to writing to stderr.
sheepluva
parents: 9966
diff changeset
   167
    // filthy hack to write to stderr
13b1e9008f4b super-filthy hack to allow pas2c to fallback to writing to stderr.
sheepluva
parents: 9966
diff changeset
   168
    if (!f->fp)
13b1e9008f4b super-filthy hack to allow pas2c to fallback to writing to stderr.
sheepluva
parents: 9966
diff changeset
   169
        fprintf(stderr, "%s\n", s.str);
13b1e9008f4b super-filthy hack to allow pas2c to fallback to writing to stderr.
sheepluva
parents: 9966
diff changeset
   170
    else
13b1e9008f4b super-filthy hack to allow pas2c to fallback to writing to stderr.
sheepluva
parents: 9966
diff changeset
   171
        fprintf(f->fp, "%s\n", s.str);
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   172
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   173
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   174
void __attribute__((overloadable)) fpcrtl_writeLn(FILE *f, string255 s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   175
    FIX_STRING(s);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   176
    fprintf(f, "%s\n", s.str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   177
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   178
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   179
void fpcrtl_blockRead__vars(File f, void *buf, Integer count, Integer *result) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   180
    assert(f->record_len > 0);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   181
    *result = fread(buf, f->record_len, count, f->fp);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   182
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   183
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   184
/*
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   185
 * XXX: dummy blockWrite
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   186
 */
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   187
void fpcrtl_blockWrite__vars(File f, const void *buf, Integer count,
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   188
        Integer *result) {
14423
a32b967f1341 This seems to be all that was needed for bug #719
nemo
parents: 11682
diff changeset
   189
    assert(f->record_len > 0);
a32b967f1341 This seems to be all that was needed for bug #719
nemo
parents: 11682
diff changeset
   190
    *result = fwrite(buf, f->record_len, count, f->fp);
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   191
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   192
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   193
bool fpcrtl_directoryExists(string255 dir) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   194
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   195
    struct stat st;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   196
    FIX_STRING(dir);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   197
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   198
    IOResult = IO_NO_ERROR;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   199
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   200
#ifdef FPCRTL_DEBUG
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   201
    printf("Warning: directoryExists is called. This may not work when compiled to js.\n");
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   202
#endif
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   203
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   204
    if (stat(dir.str, &st) == 0) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   205
        return true;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   206
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   207
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   208
    return false;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   209
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   210
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   211
bool fpcrtl_fileExists(string255 filename) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   212
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   213
    FIX_STRING(filename);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   214
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   215
    IOResult = IO_NO_ERROR;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   216
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   217
    FILE *fp = fopen(filename.str, "r");
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   218
    if (fp) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   219
        fclose(fp);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   220
        return true;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   221
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   222
    return false;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   223
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   224
11682
2c21bc80c95d Implement GetCurrentDir in the c rtl
koda
parents: 10575
diff changeset
   225
char * fpcrtl_getCurrentDir(void) {
2c21bc80c95d Implement GetCurrentDir in the c rtl
koda
parents: 10575
diff changeset
   226
2c21bc80c95d Implement GetCurrentDir in the c rtl
koda
parents: 10575
diff changeset
   227
    IOResult = IO_NO_ERROR;
2c21bc80c95d Implement GetCurrentDir in the c rtl
koda
parents: 10575
diff changeset
   228
2c21bc80c95d Implement GetCurrentDir in the c rtl
koda
parents: 10575
diff changeset
   229
    if (getcwd(cwd, sizeof(cwd)) != NULL)
2c21bc80c95d Implement GetCurrentDir in the c rtl
koda
parents: 10575
diff changeset
   230
        return cwd;
2c21bc80c95d Implement GetCurrentDir in the c rtl
koda
parents: 10575
diff changeset
   231
2c21bc80c95d Implement GetCurrentDir in the c rtl
koda
parents: 10575
diff changeset
   232
    IOResult = IO_ERROR_DUMMY;
2c21bc80c95d Implement GetCurrentDir in the c rtl
koda
parents: 10575
diff changeset
   233
    return "";
2c21bc80c95d Implement GetCurrentDir in the c rtl
koda
parents: 10575
diff changeset
   234
}
2c21bc80c95d Implement GetCurrentDir in the c rtl
koda
parents: 10575
diff changeset
   235
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   236
void __attribute__((overloadable)) fpcrtl_flush(Text f) {
9966
01e198990211 pas2c engine now writes debug log
unc0rr
parents: 7983
diff changeset
   237
    fflush(f->fp);
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   238
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   239
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   240
void __attribute__((overloadable)) fpcrtl_flush(FILE *f) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   241
    fflush(f);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   242
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   243
14913
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   244
Int64 fpcrtl_fileSize(File f)
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   245
{
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   246
    assert(f->record_len > 0);
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   247
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   248
    IOResult = IO_NO_ERROR;
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   249
    int i = fseek(f->fp, 0, SEEK_END);
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   250
    if (i == -1) {
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   251
        IOResult = IO_ERROR_DUMMY;
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   252
        return -1;
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   253
    }
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   254
    long size = ftell(f->fp);
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   255
    if (size == -1) {
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   256
        IOResult = IO_ERROR_DUMMY;
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   257
        return -1;
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   258
    }
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   259
    return size / f->record_len;
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   260
}
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   261
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   262
bool fpcrtl_deleteFile(string255 filename)
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   263
{
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   264
    FIX_STRING(filename);
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   265
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   266
    int ret = remove(filename.str);
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   267
    if(ret == 0) {
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   268
       IOResult = IO_NO_ERROR;
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   269
       return true;
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   270
    } else {
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   271
       IOResult = IO_ERROR_DUMMY;
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   272
       return false;
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   273
    }
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   274
}
68e1783762bc Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
Wuzzy <Wuzzy2@mail.ru>
parents: 14423
diff changeset
   275