Debian Codegraph @

datrie/fileutils.c

raw
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * libdatrie - Double-Array Trie Library
4 * Copyright (C) 2006 Theppitak Karoonboonyanan <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /*
22 * fileutils.h - File utility functions
23 * Created: 2006-08-15
24 * Author: Theppitak Karoonboonyanan <[email protected]>
25 */
26
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include "fileutils.h"
31
32 /* ==================== BEGIN IMPLEMENTATION PART ==================== */
33
34 /*--------------------------------*
35 * FUNCTIONS IMPLEMENTATIONS *
36 *--------------------------------*/
37
38 static int32
39 parse_int32_be (const uint8 *buff)
40 {
41 return (buff[0] << 24) | (buff[1] << 16) | (buff[2] << 8) | buff[3];
42 }
43
44 Bool
45 file_read_int32 (FILE *file, int32 *o_val)
46 {
47 uint8 buff[4];
48
49 if (fread (buff, 4, 1, file) == 1) {
50 *o_val = parse_int32_be (buff);
51 return TRUE;
52 }
53
54 return FALSE;
55 }
56
57 static void
58 serialize_int32_be (uint8 *buff, int32 val)
59 {
60 buff[0] = (val >> 24) & 0xff;
61 buff[1] = (val >> 16) & 0xff;
62 buff[2] = (val >> 8) & 0xff;
63 buff[3] = val & 0xff;
64 }
65
66
67 void
68 serialize_int32_be_incr (uint8 **buff, int32 val)
69 {
70 serialize_int32_be (*buff, val);
71 *buff += 4; /* don't sizeof! */
72 }
73
74 Bool
75 file_write_int32 (FILE *file, int32 val)
76 {
77 uint8 buff[4];
78 serialize_int32_be (buff, val);
79 return (fwrite (buff, 4, 1, file) == 1);
80 }
81
82 static int16
83 parse_int16_be (const uint8 *buff)
84 {
85 return (buff[0] << 8) | buff[1];
86 }
87
88 Bool
89 file_read_int16 (FILE *file, int16 *o_val)
90 {
91 uint8 buff[2];
92
93 if (fread (buff, 2, 1, file) == 1) {
94 *o_val = parse_int16_be (buff);
95 return TRUE;
96 }
97
98 return FALSE;
99 }
100
101 static void
102 serialize_int16_be (uint8 *buff, int16 val)
103 {
104 buff[0] = val >> 8;
105 buff[1] = val & 0xff;
106 }
107
108 void
109 serialize_int16_be_incr (uint8 **buff, int16 val)
110 {
111 serialize_int16_be (*buff, val);
112 *buff += 2; /* don't sizeof! */
113 }
114
115 Bool
116 file_write_int16 (FILE *file, int16 val)
117 {
118 uint8 buff[2];
119 serialize_int16_be (buff, val);
120 return (fwrite (buff, 2, 1, file) == 1);
121 }
122
123 Bool
124 file_read_int8 (FILE *file, int8 *o_val)
125 {
126 return (fread (o_val, sizeof (int8), 1, file) == 1);
127 }
128
129 Bool
130 file_write_int8 (FILE *file, int8 val)
131 {
132 return (fwrite (&val, sizeof (int8), 1, file) == 1);
133 }
134
135 Bool
136 file_read_chars (FILE *file, char *buff, int len)
137 {
138 return (fread (buff, sizeof (char), len, file) == len);
139 }
140
141 Bool
142 file_write_chars (FILE *file, const char *buff, int len)
143 {
144 return (fwrite (buff, sizeof (char), len, file) == len);
145 }
146
147 /*
148 vi:ts=4:ai:expandtab
149 */