fix debug
[AROS.git] / tools / MetaMake / io.c
blob090e584b4df28fa8cd3e93da13ee55f2bd361b89
1 /* MetaMake - A Make extension
2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
4 This file is part of MetaMake.
6 MetaMake is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 MetaMake is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "config.h"
23 #include <inttypes.h>
24 #include <stdio.h>
25 #include <string.h> /* for strlen() */
26 #ifdef HAVE_NETINET_IN_H
27 # include <netinet/in.h> /* for htonl/ntohl() */
28 #endif
29 #ifdef _WIN32
30 #include "winsock2.h"
31 #endif
33 #include "mem.h"
35 int
36 writestring (FILE * fh, const char * s)
38 uint32_t out;
39 int32_t len;
41 if (s != NULL)
42 len = strlen(s);
43 else
44 len = -1;
46 out = htonl(len);
47 fwrite(&out, sizeof(out), 1, fh);
49 if (!ferror(fh) && len>0)
50 fwrite(s, len, 1, fh);
52 return !ferror(fh);
55 int
56 readstring (FILE * fh, char **strptr)
58 uint32_t in;
59 int32_t len;
61 if (fread(&in, sizeof(in), 1, fh) != 1)
62 return 0;
64 len = ntohl(in);
65 if (len>0)
67 *strptr = xmalloc (len+1);
68 if (fread (*strptr, len, 1, fh) != 1)
70 xfree (*strptr);
71 return 0;
73 (*strptr)[len] = 0;
75 else if (len == 0)
76 *strptr = xstrdup("");
77 else /* len < 0 */
78 *strptr = NULL;
80 return 1;
83 int
84 writeint32 (FILE * fh, int32_t i)
86 uint32_t out;
88 out = htonl(i);
89 fwrite(&out, sizeof(out), 1, fh);
91 return !ferror(fh);
94 int
95 readint32 (FILE * fh, int32_t * iptr)
97 uint32_t in;
99 if (fread(&in, sizeof(in), 1, fh) == 1)
100 *iptr = ntohl(in);
101 else
102 *iptr = 0;
105 return !ferror(fh);
109 writeuint32 (FILE * fh, uint32_t i)
111 uint32_t out;
113 out = htonl(i);
114 fwrite(&out, sizeof(out), 1, fh);
116 return !ferror(fh);
120 readuint32 (FILE * fh, uint32_t * iptr)
122 uint32_t in;
124 if (fread(&in, sizeof(in), 1, fh) == 1)
125 *iptr = ntohl(in);
126 else
127 *iptr = 0;
129 return !ferror(fh);