]> git.sur5r.net Git - cc65/blob - ar65/fileio.c
Fixed _textcolor definition.
[cc65] / ar65 / fileio.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 fileio.c                                  */
4 /*                                                                           */
5 /*                      File I/O for the ar65 archiver                       */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998     Ullrich von Bassewitz                                        */
10 /*              Wacholderweg 14                                              */
11 /*              D-70597 Stuttgart                                            */
12 /* EMail:       uz@musoftware.de                                             */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <string.h>
37
38 #include "error.h"
39 #include "mem.h"
40 #include "fileio.h"
41
42
43
44 /*****************************************************************************/
45 /*                                   Code                                    */
46 /*****************************************************************************/
47
48
49
50 void Write8 (FILE* F, unsigned char Val)
51 /* Write an 8 bit value to the file */
52 {
53     if (putc (Val, F) == EOF) {
54         Error ("Write error (disk full?)");
55     }
56 }
57
58
59
60 void Write16 (FILE* F, unsigned Val)
61 /* Write a 16 bit value to the file */
62 {
63     Write8 (F, (unsigned char) Val);
64     Write8 (F, (unsigned char) (Val >> 8));
65 }
66
67
68
69 void Write32 (FILE* F, unsigned long Val)
70 /* Write a 32 bit value to the file */
71 {
72     Write8 (F, (unsigned char) Val);
73     Write8 (F, (unsigned char) (Val >> 8));
74     Write8 (F, (unsigned char) (Val >> 16));
75     Write8 (F, (unsigned char) (Val >> 24));
76 }
77
78
79
80 void WriteStr (FILE* F, const char* S)
81 /* Write a string to the file */
82 {
83     unsigned Len = strlen (S);
84     if (Len > 255) {
85         Internal ("String too long");
86     }
87     Write8 (F, (unsigned char) Len);
88     WriteData (F, S, Len);
89 }
90
91
92
93 void WriteData (FILE* F, const void* Data, unsigned Size)
94 /* Write data to the file */
95 {
96     if (fwrite (Data, 1, Size, F) != Size) {
97         Error ("Write error (disk full?)");
98     }
99 }
100
101
102
103 unsigned Read8 (FILE* F)
104 /* Read an 8 bit value from the file */
105 {
106     int C = getc (F);
107     if (C == EOF) {
108         Error ("Read error (file corrupt?)");
109     }
110     return C;
111 }
112
113
114
115 unsigned Read16 (FILE* F)
116 /* Read a 16 bit value from the file */
117 {
118     unsigned Lo = Read8 (F);
119     unsigned Hi = Read8 (F);
120     return (Hi << 8) | Lo;
121 }
122
123
124
125 unsigned long Read32 (FILE* F)
126 /* Read a 32 bit value from the file */
127 {
128     unsigned long Lo = Read16 (F);
129     unsigned long Hi = Read16 (F);
130     return (Hi << 16) | Lo;
131 }
132
133
134
135 char* ReadStr (FILE* F)
136 /* Read a string from the file (the memory will be malloc'ed) */
137 {
138     /* Read the length byte */
139     unsigned Len = Read8 (F);
140
141     /* Allocate memory and read the string itself */
142     char* S = Xmalloc (Len + 1);
143     ReadData (F, S, Len);
144
145     /* Terminate the string and return it */
146     S [Len] = '\0';
147     return S;
148 }
149
150
151
152 void* ReadData (FILE* F, void* Data, unsigned Size)
153 /* Read data from the file */
154 {
155     if (fread (Data, 1, Size, F) != Size) {
156         Error ("Read error (file corrupt?)");
157     }
158     return Data;
159 }
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210