Mercurial > hg > th-libs
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 |
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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 35 /* File routines for endian-dependant data |
36 */ | |
37 #define TH_DEFINE_FUNC(xname, xtype, xmacro) \ | |
134 | 38 BOOL th_fread_ ## xname (FILE *f, xtype *v) { \ |
5 | 39 xtype result; \ |
40 if (fread(&result, sizeof( xtype ), 1, f) != 1) \ | |
41 return FALSE; \ | |
42 *v = TH_ ## xmacro ## _TO_NATIVE (result); \ | |
43 return TRUE; \ | |
44 } \ | |
45 \ | |
134 | 46 BOOL th_fwrite_ ## xname (FILE *f, xtype v) { \ |
5 | 47 xtype result = TH_NATIVE_TO_ ## xmacro (v); \ |
48 if (fwrite(&result, sizeof( xtype ), 1, f) != 1) \ | |
49 return FALSE; \ | |
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 | 53 TH_DEFINE_FUNC(le16, uint16_t, LE16) |
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 | 56 TH_DEFINE_FUNC(be16, uint16_t, BE16) |
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 | 59 #ifdef TH_HAVE_64BIT |
60 TH_DEFINE_FUNC(le64, uint64_t, LE64) | |
61 TH_DEFINE_FUNC(be64, uint64_t, BE64) | |
62 #endif | |
63 | |
64 #undef TH_DEFINE_FUNC |