annotate th_endian.c @ 184:b256db93cf25

Initial import of th_file module.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 20 Jan 2016 16:40:05 +0200
parents 51eec969b07a
children 5c7de25666e7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * Endianess handling
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Programmed and designed by Matti 'ccr' Hamalainen
144
51eec969b07a Update copyrights.
Matti Hamalainen <ccr@tnsp.org>
parents: 134
diff changeset
4 * (C) Copyright 2002-2015 Tecnic Software productions (TNSP)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 *
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 * Please read file 'COPYING' for information on license and distribution.
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 */
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 #include "th_endian.h"
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10
134
4094fcfd4783 Rename functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
11 BOOL th_fread_str(FILE *f, uint8_t *s, size_t l)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 {
5
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
13 return (fread(s, sizeof(uint8_t), l, f) == l);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16
134
4094fcfd4783 Rename functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
17 BOOL th_fwrite_str(FILE * f, uint8_t * s, size_t l)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 {
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
19 return (fwrite(s, sizeof(uint8_t), l, f) == l);
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22
134
4094fcfd4783 Rename functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
23 BOOL th_fread_byte(FILE *f, uint8_t *v)
63
5ab4c28b85cd Add th_{read,write}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
24 {
65
0a260f0c34e5 Fix the previously implemented functions .. urgh.
Matti Hamalainen <ccr@tnsp.org>
parents: 63
diff changeset
25 return (fread(v, sizeof(uint8_t), 1, f) == 1);
63
5ab4c28b85cd Add th_{read,write}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
26 }
5ab4c28b85cd Add th_{read,write}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
27
5ab4c28b85cd Add th_{read,write}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
28
134
4094fcfd4783 Rename functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
29 BOOL th_fwrite_byte(FILE * f, uint8_t v)
63
5ab4c28b85cd Add th_{read,write}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
30 {
65
0a260f0c34e5 Fix the previously implemented functions .. urgh.
Matti Hamalainen <ccr@tnsp.org>
parents: 63
diff changeset
31 return (fputc(v, f) != EOF);
63
5ab4c28b85cd Add th_{read,write}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
32 }
5ab4c28b85cd Add th_{read,write}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
33
5ab4c28b85cd Add th_{read,write}_byte() convenience functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
34
5
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
35 /* File routines for endian-dependant data
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
36 */
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
37 #define TH_DEFINE_FUNC(xname, xtype, xmacro) \
134
4094fcfd4783 Rename functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
38 BOOL th_fread_ ## xname (FILE *f, xtype *v) { \
5
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
39 xtype result; \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
40 if (fread(&result, sizeof( xtype ), 1, f) != 1) \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
41 return FALSE; \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
42 *v = TH_ ## xmacro ## _TO_NATIVE (result); \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
43 return TRUE; \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
44 } \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
45 \
134
4094fcfd4783 Rename functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 66
diff changeset
46 BOOL th_fwrite_ ## xname (FILE *f, xtype v) { \
5
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
47 xtype result = TH_NATIVE_TO_ ## xmacro (v); \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
48 if (fwrite(&result, sizeof( xtype ), 1, f) != 1) \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
49 return FALSE; \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
50 return TRUE; \
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52
5
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
53 TH_DEFINE_FUNC(le16, uint16_t, LE16)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
54 TH_DEFINE_FUNC(le32, uint32_t, LE32)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55
5
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
56 TH_DEFINE_FUNC(be16, uint16_t, BE16)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
57 TH_DEFINE_FUNC(be32, uint32_t, BE32)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58
5
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
59 #ifdef TH_HAVE_64BIT
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
60 TH_DEFINE_FUNC(le64, uint64_t, LE64)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
61 TH_DEFINE_FUNC(be64, uint64_t, BE64)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
62 #endif
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
63
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
64 #undef TH_DEFINE_FUNC