1 /* word_io.c -- word oriented I/O
2 Copyright (C) 2007 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
25 #include <stdbool.h> /* for bool */
35 # define _(Text) gettext (Text)
38 #define textdomain(Domain)
39 #define bindtextdomain(Package, Directory)
42 # define N_(String) gettext_noop (String)
44 /* We used to use (String) instead of just String, but apparently ISO C
45 * doesn't allow this (at least, that's what HP said when someone reported
46 * this as a compiler bug). This is HP case number 1205608192. See
47 * also http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11250 (which references
48 * ANSI 3.5.7p14-15). The Intel icc compiler also rejects constructs
49 * like: static const char buf[] = ("string");
51 # define N_(String) String
55 /* Swap bytes in 32 bit value. This code is taken from glibc-2.3.3. */
57 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
58 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
63 decode_value(const unsigned char data
[],
65 GetwordEndianState
*endian_state_flag
,
71 int ival
; /* native representation */
72 unsigned char data
[WORDBYTES
];
75 memcpy(&u
.data
, data
, WORDBYTES
);
76 swapped
= bswap_32(u
.ival
); /* byteswapped */
78 if (*endian_state_flag
== GetwordEndianStateInitial
)
84 /* the native value is inside the limit and the
85 * swapped value is not. We take this as proof
86 * that we should be using the ative byte order.
88 *endian_state_flag
= GetwordEndianStateNative
;
96 /* Aha, now we know we have to byte-swap. */
98 _("Warning: locate database %s was "
99 "built with a different byte order"),
100 quotearg_n_style(0, locale_quoting_style
, filename
));
101 *endian_state_flag
= GetwordEndianStateSwab
;
106 /* u.ival > limit and swapped > limit. For the moment, assume
115 /* We already know the byte order. */
116 if (*endian_state_flag
== GetwordEndianStateSwab
)
127 const char *filename
,
130 GetwordEndianState
*endian_state_flag
)
132 unsigned char data
[4];
136 bytes_read
= fread(data
, WORDBYTES
, 1, fp
);
139 const char * quoted_name
= quotearg_n_style(0, locale_quoting_style
,
141 /* Distinguish between a truncated database and an I/O error.
142 * Either condition is fatal.
145 error(1, 0, _("Unexpected EOF in %s"), quoted_name
);
147 error(1, errno
, "error reading a word from %s", quoted_name
);
151 return decode_value(data
, maxvalue
, endian_state_flag
, filename
);
157 putword (FILE *fp
, int word
,
158 GetwordEndianState endian_state_flag
)
160 size_t items_written
;
162 /* You must decide before calling this function which
163 * endianness you want to use.
165 assert (endian_state_flag
!= GetwordEndianStateInitial
);
166 if (GetwordEndianStateSwab
== endian_state_flag
)
168 word
= bswap_32(word
);
171 items_written
= fwrite(&word
, sizeof(word
), 1, fp
);
172 if (1 == items_written
)