sj4/src/sj4mkdic/file.c

137 lines
3.1 KiB
C
Raw Normal View History

2008-01-11 11:57:08 +00:00
/*
* Copyright (c) 1991-1994 Sony Corporation
2026-06-19 01:49:53 +09:00
*
2008-01-11 11:57:08 +00:00
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
2026-06-19 01:49:53 +09:00
*
2008-01-11 11:57:08 +00:00
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
2026-06-19 01:49:53 +09:00
*
2008-01-11 11:57:08 +00:00
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL SONY CORPORATION BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2026-06-19 01:49:53 +09:00
*
2008-01-11 11:57:08 +00:00
* Except as contained in this notice, the name of Sony Corporation
* shall not be used in advertising or otherwise to promote the sale, use
* or other dealings in this Software without prior written authorization
* from Sony Corporation.
*
*/
2026-06-20 02:12:04 +09:00
#include "sj4mkdic.h"
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
typedef struct filelist {
FILE* fp;
char* fname;
struct filelist* next;
2008-01-11 11:57:08 +00:00
} FileList;
2026-06-19 01:49:53 +09:00
static FileList* flist = NULL;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
static char*
get_fname(FILE* fp) {
FileList* p;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
for(p = flist; p; p = p->next)
if(p->fp == fp) return p->fname;
2008-01-11 11:57:08 +00:00
return "some file";
}
2026-06-19 01:49:53 +09:00
FILE* Fopen(char* filename, char* type) {
FILE* fp;
char* p;
FileList* fl;
size_t len;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
if(!(fp = fopen(filename, type))) {
2008-01-11 11:57:08 +00:00
fprintf(stderr, "Can't open %s mode \"%s\"\n", filename, type);
exit(1);
}
2026-06-19 01:49:53 +09:00
fl = (FileList*)Malloc(sizeof(*fl));
len = strlen(filename) + 1;
fl->fname = Malloc(len);
sj4_strlcpy(fl->fname, filename, len);
2026-06-19 01:49:53 +09:00
fl->fp = fp;
fl->next = flist;
2026-06-20 21:16:45 +09:00
flist = fl;
2008-01-11 11:57:08 +00:00
return fp;
}
2026-06-19 01:49:53 +09:00
void Fclose(FILE* fp) {
2008-01-11 11:57:08 +00:00
FileList *p, *q;
2026-06-19 01:49:53 +09:00
if(fclose(fp) == EOF) {
2008-01-11 11:57:08 +00:00
fprintf(stderr, "Close error in %s\n", get_fname(fp));
exit(1);
}
2026-06-19 01:49:53 +09:00
for(p = flist, q = NULL; p; q = p, p = p->next) {
if(p->fp == fp) {
if(q)
q->next = p->next;
2008-01-11 11:57:08 +00:00
else
2026-06-19 01:49:53 +09:00
flist = p->next;
free(p->fname);
2008-01-11 11:57:08 +00:00
free(p);
break;
}
}
}
2026-06-19 01:49:53 +09:00
int Fseek(FILE* fp, long ofs, int ptr) {
if(fseek(fp, ofs, ptr) == 0) return 0;
2008-01-11 11:57:08 +00:00
fprintf(stderr, "Seek error in %s\n", get_fname(fp));
exit(1);
}
2026-06-19 01:49:53 +09:00
long Ftell(FILE* fp) {
2008-01-11 11:57:08 +00:00
return ftell(fp);
}
2026-06-19 01:49:53 +09:00
long Fsize(char* filename) {
struct stat buf;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
if(stat(filename, &buf) == 0) return (long)buf.st_size;
2008-01-11 11:57:08 +00:00
fprintf(stderr, "Stat error in %s\n", filename);
exit(1);
}
size_t
2026-06-19 01:49:53 +09:00
Fread(char* p, int s, int n, FILE* fp) {
if(fread(p, s, n, fp) == n) return n;
2008-01-11 11:57:08 +00:00
2026-06-19 06:25:17 +09:00
printf("!\n");
2008-01-11 11:57:08 +00:00
fprintf(stderr, "Read error in %s\n", get_fname(fp));
exit(1);
}
size_t
2026-06-19 01:49:53 +09:00
Fwrite(char* p, int s, int n, FILE* fp) {
if(fwrite(p, s, n, fp) == n) return n;
2008-01-11 11:57:08 +00:00
fprintf(stderr, "Write error in %s\n", get_fname(fp));
exit(1);
}
2026-06-19 01:49:53 +09:00
int Fgetc(FILE* fp) {
2008-01-11 11:57:08 +00:00
return fgetc(fp);
}
2026-06-19 01:49:53 +09:00
int Fflush(FILE* fp) {
return fflush(fp);
2008-01-11 11:57:08 +00:00
}