]> git.sur5r.net Git - cc65/blob - src/ar65/fileio.c
goto.c warning fix for implicit truncation
[cc65] / src / ar65 / fileio.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 fileio.c                                  */
4 /*                                                                           */
5 /*                      File I/O for the ar65 archiver                       */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2003 Ullrich von Bassewitz                                       */
10 /*               Roemerstrasse 52                                            */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
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 /* common */
39 #include "xmalloc.h"
40
41 /* ar65 */
42 #include "error.h"
43 #include "fileio.h"
44
45
46
47 /*****************************************************************************/
48 /*                                   Code                                    */
49 /*****************************************************************************/
50
51
52
53 void Write8 (FILE* F, unsigned char Val)
54 /* Write an 8 bit value to the file */
55 {
56     if (putc (Val, F) == EOF) {
57         Error ("Write error (disk full?)");
58     }
59 }
60
61
62
63 void Write16 (FILE* F, unsigned Val)
64 /* Write a 16 bit value to the file */
65 {
66     Write8 (F, (unsigned char) Val);
67     Write8 (F, (unsigned char) (Val >> 8));
68 }
69
70
71
72 void Write32 (FILE* F, unsigned long Val)
73 /* Write a 32 bit value to the file */
74 {
75     Write8 (F, (unsigned char) Val);
76     Write8 (F, (unsigned char) (Val >> 8));
77     Write8 (F, (unsigned char) (Val >> 16));
78     Write8 (F, (unsigned char) (Val >> 24));
79 }
80
81
82
83 void WriteVar (FILE* F, unsigned long V)
84 /* Write a variable sized value to the file in special encoding */
85 {
86     /* We will write the value to the file in 7 bit chunks. If the 8th bit
87     ** is clear, we're done, if it is set, another chunk follows. This will
88     ** allow us to encode smaller values with less bytes, at the expense of
89     ** needing 5 bytes if a 32 bit value is written to file.
90     */
91     do {
92         unsigned char C = (V & 0x7F);
93         V >>= 7;
94         if (V) {
95             C |= 0x80;
96         }
97         Write8 (F, C);
98     } while (V != 0);
99 }
100
101
102
103 void WriteStr (FILE* F, const char* S)
104 /* Write a string to the file */
105 {
106     unsigned Len = strlen (S);
107     WriteVar (F, Len);
108     WriteData (F, S, Len);
109 }
110
111
112
113 void WriteData (FILE* F, const void* Data, unsigned Size)
114 /* Write data to the file */
115 {
116     if (fwrite (Data, 1, Size, F) != Size) {
117         Error ("Write error (disk full?)");
118     }
119 }
120
121
122
123 unsigned Read8 (FILE* F)
124 /* Read an 8 bit value from the file */
125 {
126     int C = getc (F);
127     if (C == EOF) {
128         Error ("Read error (file corrupt?)");
129     }
130     return C;
131 }
132
133
134
135 unsigned Read16 (FILE* F)
136 /* Read a 16 bit value from the file */
137 {
138     unsigned Lo = Read8 (F);
139     unsigned Hi = Read8 (F);
140     return (Hi << 8) | Lo;
141 }
142
143
144
145 unsigned long Read32 (FILE* F)
146 /* Read a 32 bit value from the file */
147 {
148     unsigned long Lo = Read16 (F);
149     unsigned long Hi = Read16 (F);
150     return (Hi << 16) | Lo;
151 }
152
153
154
155 unsigned long ReadVar (FILE* F)
156 /* Read a variable size value from the file */
157 {
158     /* The value was written to the file in 7 bit chunks LSB first. If there
159     ** are more bytes, bit 8 is set, otherwise it is clear.
160     */
161     unsigned char C;
162     unsigned long V = 0;
163     unsigned Shift = 0;
164     do {
165         /* Read one byte */
166         C = Read8 (F);
167         /* Encode it into the target value */
168         V |= ((unsigned long)(C & 0x7F)) << Shift;
169         /* Next value */
170         Shift += 7;
171     } while (C & 0x80);
172
173     /* Return the value read */
174     return V;
175 }
176
177
178
179 char* ReadStr (FILE* F)
180 /* Read a string from the file (the memory will be malloc'ed) */
181 {
182     /* Read the length */
183     unsigned Len = ReadVar (F);
184
185     /* Allocate memory and read the string itself */
186     char* S = xmalloc (Len + 1);
187     ReadData (F, S, Len);
188
189     /* Terminate the string and return it */
190     S [Len] = '\0';
191     return S;
192 }
193
194
195
196 void* ReadData (FILE* F, void* Data, unsigned Size)
197 /* Read data from the file */
198 {
199     if (fread (Data, 1, Size, F) != Size) {
200         Error ("Read error (file corrupt?)");
201     }
202     return Data;
203 }