| 1 |
/*
|
| 2 |
Copyright (C) 2000 Marco Ziech (mmz@gmx.net)
|
| 3 |
Copyright (C) 2000 Ross Combs (rocombs@cs.nmsu.edu)
|
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify
|
| 6 |
it under the terms of the GNU General Public License as published by
|
| 7 |
the Free Software Foundation; either version 2 of the License, or
|
| 8 |
(at your option) any later version.
|
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful,
|
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 13 |
GNU General Public License for more details.
|
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License
|
| 16 |
along with this program; if not, write to the Free Software
|
| 17 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
| 18 |
*/
|
| 19 |
#include "common/setup_before.h"
|
| 20 |
#include <stdio.h>
|
| 21 |
#ifdef STDC_HEADERS
|
| 22 |
# include <stdlib.h>
|
| 23 |
#else
|
| 24 |
# ifdef HAVE_MALLOC_H
|
| 25 |
# include <malloc.h>
|
| 26 |
# endif
|
| 27 |
#endif
|
| 28 |
#include "compat/uint.h"
|
| 29 |
#include "fileio.h"
|
| 30 |
#include "common/setup_after.h"
|
| 31 |
|
| 32 |
typedef struct t_file {
|
| 33 |
FILE *f;
|
| 34 |
struct t_file *next;
|
| 35 |
} t_file;
|
| 36 |
|
| 37 |
static t_file *r_file = NULL;
|
| 38 |
static t_file *w_file = NULL;
|
| 39 |
|
| 40 |
/* ----------------------------------------------------------------- */
|
| 41 |
|
| 42 |
extern void file_rpush(FILE *f) {
|
| 43 |
t_file *tf = malloc(sizeof(t_file));
|
| 44 |
tf->next = r_file;
|
| 45 |
tf->f = f;
|
| 46 |
r_file = tf;
|
| 47 |
return;
|
| 48 |
}
|
| 49 |
|
| 50 |
extern void file_rpop(void) {
|
| 51 |
t_file *tf;
|
| 52 |
tf = r_file;
|
| 53 |
r_file = tf->next;
|
| 54 |
free(tf);
|
| 55 |
return;
|
| 56 |
}
|
| 57 |
|
| 58 |
/* ----------------------------------------------------------------- */
|
| 59 |
|
| 60 |
extern void file_wpush(FILE *f) {
|
| 61 |
t_file *tf = malloc(sizeof(t_file));
|
| 62 |
tf->next = w_file;
|
| 63 |
tf->f = f;
|
| 64 |
w_file = tf;
|
| 65 |
return;
|
| 66 |
}
|
| 67 |
|
| 68 |
extern void file_wpop(void) {
|
| 69 |
t_file *tf;
|
| 70 |
tf = w_file;
|
| 71 |
w_file = tf->next;
|
| 72 |
free(tf);
|
| 73 |
return;
|
| 74 |
}
|
| 75 |
|
| 76 |
/* ----------------------------------------------------------------- */
|
| 77 |
|
| 78 |
extern t_uint8 file_readb(void) {
|
| 79 |
unsigned char buff[1];
|
| 80 |
if (fread(buff,1,sizeof(buff),r_file->f) < sizeof(buff)) {
|
| 81 |
if (ferror(r_file->f)) perror("file_readb: fread");
|
| 82 |
return 0;
|
| 83 |
}
|
| 84 |
return (((t_uint8)buff[0]) );
|
| 85 |
}
|
| 86 |
|
| 87 |
|
| 88 |
extern t_uint16 file_readw_le(void) {
|
| 89 |
unsigned char buff[2];
|
| 90 |
if (fread(buff,1,sizeof(buff),r_file->f) < sizeof(buff)) {
|
| 91 |
if (ferror(r_file->f)) perror("file_readw_le: fread");
|
| 92 |
return 0;
|
| 93 |
}
|
| 94 |
return (((t_uint16)buff[0]) )|
|
| 95 |
(((t_uint16)buff[1])<< 8);
|
| 96 |
}
|
| 97 |
|
| 98 |
|
| 99 |
extern t_uint16 file_readw_be(void) {
|
| 100 |
unsigned char buff[2];
|
| 101 |
if (fread(buff,1,sizeof(buff),r_file->f) < sizeof(buff)) {
|
| 102 |
if (ferror(r_file->f)) perror("file_readw_be: fread");
|
| 103 |
return 0;
|
| 104 |
}
|
| 105 |
return (((t_uint16)buff[0])<< 8)|
|
| 106 |
(((t_uint16)buff[1]) );
|
| 107 |
}
|
| 108 |
|
| 109 |
|
| 110 |
extern t_uint32 file_readd_le(void) {
|
| 111 |
unsigned char buff[4];
|
| 112 |
if (fread(buff,1,sizeof(buff),r_file->f) < sizeof(buff)) {
|
| 113 |
if (ferror(r_file->f)) perror("file_readd_le: fread");
|
| 114 |
return 0;
|
| 115 |
}
|
| 116 |
return (((t_uint32)buff[0]) )|
|
| 117 |
(((t_uint32)buff[1])<< 8)|
|
| 118 |
(((t_uint32)buff[2])<<16)|
|
| 119 |
(((t_uint32)buff[3])<<24);
|
| 120 |
}
|
| 121 |
|
| 122 |
|
| 123 |
extern t_uint32 file_readd_be(void) {
|
| 124 |
unsigned char buff[4];
|
| 125 |
if (fread(buff,1,sizeof(buff),r_file->f) < sizeof(buff)) {
|
| 126 |
if (ferror(r_file->f)) perror("file_readd_be: fread");
|
| 127 |
return 0;
|
| 128 |
}
|
| 129 |
return (((t_uint32)buff[0])<<24)|
|
| 130 |
(((t_uint32)buff[1])<<16)|
|
| 131 |
(((t_uint32)buff[2])<< 8)|
|
| 132 |
(((t_uint32)buff[3]) );
|
| 133 |
}
|
| 134 |
|
| 135 |
|
| 136 |
extern int file_writeb(t_uint8 u) {
|
| 137 |
unsigned char buff[1];
|
| 138 |
buff[0] = (u );
|
| 139 |
if (fwrite(buff,1,sizeof(buff),w_file->f) < sizeof(buff)) {
|
| 140 |
if (ferror(w_file->f)) perror("file_writeb: fwrite");
|
| 141 |
return -1;
|
| 142 |
}
|
| 143 |
return 0;
|
| 144 |
}
|
| 145 |
|
| 146 |
|
| 147 |
extern int file_writew_le(t_uint16 u) {
|
| 148 |
unsigned char buff[2];
|
| 149 |
buff[0] = (u );
|
| 150 |
buff[1] = (u>> 8);
|
| 151 |
if (fwrite(buff,1,sizeof(buff),w_file->f) < sizeof(buff)) {
|
| 152 |
if (ferror(w_file->f)) perror("file_writew_le: fwrite");
|
| 153 |
return -1;
|
| 154 |
}
|
| 155 |
return 0;
|
| 156 |
}
|
| 157 |
|
| 158 |
|
| 159 |
extern int file_writew_be(t_uint16 u) {
|
| 160 |
unsigned char buff[2];
|
| 161 |
buff[0] = (u>> 8);
|
| 162 |
buff[1] = (u );
|
| 163 |
if (fwrite(buff,1,sizeof(buff),w_file->f) < sizeof(buff)) {
|
| 164 |
if (ferror(w_file->f)) perror("file_writew_be: fwrite");
|
| 165 |
return -1;
|
| 166 |
}
|
| 167 |
return 0;
|
| 168 |
}
|
| 169 |
|
| 170 |
|
| 171 |
extern int file_writed_le(t_uint32 u) {
|
| 172 |
unsigned char buff[4];
|
| 173 |
buff[0] = (u );
|
| 174 |
buff[1] = (u>> 8);
|
| 175 |
buff[2] = (u>>16);
|
| 176 |
buff[3] = (u>>24);
|
| 177 |
if (fwrite(buff,1,sizeof(buff),w_file->f) < sizeof(buff)) {
|
| 178 |
if (ferror(w_file->f)) perror("file_writed_le: fwrite");
|
| 179 |
return -1;
|
| 180 |
}
|
| 181 |
return 0;
|
| 182 |
}
|
| 183 |
|
| 184 |
|
| 185 |
extern int file_writed_be(t_uint32 u) {
|
| 186 |
unsigned char buff[4];
|
| 187 |
buff[0] = (u>>24);
|
| 188 |
buff[1] = (u>>16);
|
| 189 |
buff[2] = (u>> 8);
|
| 190 |
buff[3] = (u );
|
| 191 |
if (fwrite(buff,1,sizeof(buff),w_file->f) < sizeof(buff)) {
|
| 192 |
if (ferror(w_file->f)) perror("file_writed_be: fwrite");
|
| 193 |
return -1;
|
| 194 |
}
|
| 195 |
return 0;
|
| 196 |
}
|