1 /*****************************************************************************/
5 /* File I/O for the ar65 archiver */
9 /* (C) 1998 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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. */
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: */
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 */
32 /*****************************************************************************/
44 /*****************************************************************************/
46 /*****************************************************************************/
50 void Write8 (FILE* F, unsigned char Val)
51 /* Write an 8 bit value to the file */
53 if (putc (Val, F) == EOF) {
54 Error ("Write error (disk full?)");
60 void Write16 (FILE* F, unsigned Val)
61 /* Write a 16 bit value to the file */
63 Write8 (F, (unsigned char) Val);
64 Write8 (F, (unsigned char) (Val >> 8));
69 void Write32 (FILE* F, unsigned long Val)
70 /* Write a 32 bit value to the file */
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));
80 void WriteStr (FILE* F, const char* S)
81 /* Write a string to the file */
83 unsigned Len = strlen (S);
85 Internal ("String too long");
87 Write8 (F, (unsigned char) Len);
88 WriteData (F, S, Len);
93 void WriteData (FILE* F, const void* Data, unsigned Size)
94 /* Write data to the file */
96 if (fwrite (Data, 1, Size, F) != Size) {
97 Error ("Write error (disk full?)");
103 unsigned Read8 (FILE* F)
104 /* Read an 8 bit value from the file */
108 Error ("Read error (file corrupt?)");
115 unsigned Read16 (FILE* F)
116 /* Read a 16 bit value from the file */
118 unsigned Lo = Read8 (F);
119 unsigned Hi = Read8 (F);
120 return (Hi << 8) | Lo;
125 unsigned long Read32 (FILE* F)
126 /* Read a 32 bit value from the file */
128 unsigned long Lo = Read16 (F);
129 unsigned long Hi = Read16 (F);
130 return (Hi << 16) | Lo;
135 char* ReadStr (FILE* F)
136 /* Read a string from the file (the memory will be malloc'ed) */
138 /* Read the length byte */
139 unsigned Len = Read8 (F);
141 /* Allocate memory and read the string itself */
142 char* S = Xmalloc (Len + 1);
143 ReadData (F, S, Len);
145 /* Terminate the string and return it */
152 void* ReadData (FILE* F, void* Data, unsigned Size)
153 /* Read data from the file */
155 if (fread (Data, 1, Size, F) != Size) {
156 Error ("Read error (file corrupt?)");