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 /*****************************************************************************/
38 #include "../common/xmalloc.h"
45 /*****************************************************************************/
47 /*****************************************************************************/
51 void Write8 (FILE* F, unsigned char Val)
52 /* Write an 8 bit value to the file */
54 if (putc (Val, F) == EOF) {
55 Error ("Write error (disk full?)");
61 void Write16 (FILE* F, unsigned Val)
62 /* Write a 16 bit value to the file */
64 Write8 (F, (unsigned char) Val);
65 Write8 (F, (unsigned char) (Val >> 8));
70 void Write32 (FILE* F, unsigned long Val)
71 /* Write a 32 bit value to the file */
73 Write8 (F, (unsigned char) Val);
74 Write8 (F, (unsigned char) (Val >> 8));
75 Write8 (F, (unsigned char) (Val >> 16));
76 Write8 (F, (unsigned char) (Val >> 24));
81 void WriteStr (FILE* F, const char* S)
82 /* Write a string to the file */
84 unsigned Len = strlen (S);
86 Internal ("String too long");
88 Write8 (F, (unsigned char) Len);
89 WriteData (F, S, Len);
94 void WriteData (FILE* F, const void* Data, unsigned Size)
95 /* Write data to the file */
97 if (fwrite (Data, 1, Size, F) != Size) {
98 Error ("Write error (disk full?)");
104 unsigned Read8 (FILE* F)
105 /* Read an 8 bit value from the file */
109 Error ("Read error (file corrupt?)");
116 unsigned Read16 (FILE* F)
117 /* Read a 16 bit value from the file */
119 unsigned Lo = Read8 (F);
120 unsigned Hi = Read8 (F);
121 return (Hi << 8) | Lo;
126 unsigned long Read32 (FILE* F)
127 /* Read a 32 bit value from the file */
129 unsigned long Lo = Read16 (F);
130 unsigned long Hi = Read16 (F);
131 return (Hi << 16) | Lo;
136 char* ReadStr (FILE* F)
137 /* Read a string from the file (the memory will be malloc'ed) */
139 /* Read the length byte */
140 unsigned Len = Read8 (F);
142 /* Allocate memory and read the string itself */
143 char* S = xmalloc (Len + 1);
144 ReadData (F, S, Len);
146 /* Terminate the string and return it */
153 void* ReadData (FILE* F, void* Data, unsigned Size)
154 /* Read data from the file */
156 if (fread (Data, 1, Size, F) != Size) {
157 Error ("Read error (file corrupt?)");