annotate dmfile.c @ 567:b2b461829c61

Move utility function dmReadDataFile() to dmfile module instead.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 04 Jan 2013 16:48:15 +0200
parents c1f6def0c1da
children 8b727fa3f529
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * DMLib
414
c452a459e552 Clear up the file descriptions.
Matti Hamalainen <ccr@tnsp.org>
parents: 405
diff changeset
3 * -- Standard I/O (stdio) file write/read endianess helpers
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 * Programmed and designed by Matti 'ccr' Hamalainen
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 * (C) Copyright 2011 Tecnic Software productions (TNSP)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 */
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 #include "dmfile.h"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9
405
d0257d0004f6 Implement dm_{read,write}_byte() and constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
10 BOOL dm_fread_str(FILE *f, void *buf, const size_t len)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 return fread(buf, len, 1, f) == 1;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15
405
d0257d0004f6 Implement dm_{read,write}_byte() and constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
16 BOOL dm_fread_byte(FILE *f, Uint8 *val)
d0257d0004f6 Implement dm_{read,write}_byte() and constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
17 {
438
c1f6def0c1da Adjust dm_fread_byte() and dm_fwrite_byte() stdio helper functions to match
Matti Hamalainen <ccr@tnsp.org>
parents: 414
diff changeset
18 int tmp = fgetc(f);
c1f6def0c1da Adjust dm_fread_byte() and dm_fwrite_byte() stdio helper functions to match
Matti Hamalainen <ccr@tnsp.org>
parents: 414
diff changeset
19 *val = tmp;
c1f6def0c1da Adjust dm_fread_byte() and dm_fwrite_byte() stdio helper functions to match
Matti Hamalainen <ccr@tnsp.org>
parents: 414
diff changeset
20 return tmp != EOF;
405
d0257d0004f6 Implement dm_{read,write}_byte() and constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
21 }
d0257d0004f6 Implement dm_{read,write}_byte() and constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
22
d0257d0004f6 Implement dm_{read,write}_byte() and constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
23
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 #define DM_DEFINE_FFUNC(xname, xtype, xmacro) \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 BOOL dm_fread_ ## xname (FILE *f, xtype *v) { \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 xtype result; \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 if (fread(&result, sizeof( xtype ), 1, f) != 1) \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 return FALSE; \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 *v = DM_ ## xmacro ## _TO_NATIVE (result); \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30 return TRUE; \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 #include "dmfiletmpl.h"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 #undef DM_DEFINE_FFUNC
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37
405
d0257d0004f6 Implement dm_{read,write}_byte() and constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
38
d0257d0004f6 Implement dm_{read,write}_byte() and constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
39 BOOL dm_fwrite_str(FILE *f, const void *buf, const size_t len)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 return fwrite(buf, len, 1, f) == 1;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44
405
d0257d0004f6 Implement dm_{read,write}_byte() and constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
45 BOOL dm_fwrite_byte(FILE *f, const Uint8 val)
d0257d0004f6 Implement dm_{read,write}_byte() and constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
46 {
438
c1f6def0c1da Adjust dm_fread_byte() and dm_fwrite_byte() stdio helper functions to match
Matti Hamalainen <ccr@tnsp.org>
parents: 414
diff changeset
47 return fputc(val, f) == val;
405
d0257d0004f6 Implement dm_{read,write}_byte() and constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
48 }
d0257d0004f6 Implement dm_{read,write}_byte() and constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
49
d0257d0004f6 Implement dm_{read,write}_byte() and constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
50
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 #define DM_DEFINE_FFUNC(xname, xtype, xmacro) \
405
d0257d0004f6 Implement dm_{read,write}_byte() and constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
52 BOOL dm_fwrite_ ## xname (FILE *f, const xtype v) { \
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 xtype result = DM_NATIVE_TO_ ## xmacro (v); \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 if (fwrite(&result, sizeof( xtype ), 1, f) != 1) \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 return FALSE; \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 return TRUE; \
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 #include "dmfiletmpl.h"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 #undef DM_DEFINE_FFUNC
567
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
62
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
63
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
64 #define BUF_SIZE_INITIAL (16*1024)
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
65 #define BUF_SIZE_GROW (4*1024)
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
66
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
67 int dmReadDataFile(FILE *inFile, const char *filename, Uint8 **pbuf, size_t *pbufSize)
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
68 {
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
69 FILE *f;
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
70 int res = DMERR_OK;
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
71 Uint8 *dataBuf = NULL;
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
72 size_t readSize, dataSize, dataRead, dataPos;
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
73
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
74 if (inFile != NULL)
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
75 f = inFile;
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
76 else
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
77 if (filename != NULL)
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
78 {
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
79 if ((f = fopen(filename, "rb")) == NULL)
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
80 {
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
81 dmError("Could not open '%s' for reading.\n", filename);
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
82 return DMERR_FOPEN;
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
83 }
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
84 }
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
85 else
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
86 {
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
87 dmError("NULL filename and stream pointers.\n");
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
88 return DMERR_NULLPTR;
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
89 }
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
90
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
91 // Allocate initial data buffer
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
92 readSize = dataSize = BUF_SIZE_INITIAL;
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
93 if ((dataBuf = dmMalloc(dataSize)) == NULL)
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
94 {
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
95 dmError("Error allocating memory for data, %d bytes.\n", dataSize);
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
96 res = DMERR_MALLOC;
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
97 goto error;
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
98 }
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
99
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
100 dataPos = 0;
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
101 dataRead = 0;
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
102
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
103 while (!feof(f) && !ferror(f))
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
104 {
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
105 size_t read = fread(dataBuf + dataPos, 1, readSize, f);
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
106 dataPos += read;
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
107 dataRead += read;
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
108
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
109 if (dataRead >= dataSize)
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
110 {
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
111 readSize = BUF_SIZE_GROW;
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
112 dataSize += BUF_SIZE_GROW;
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
113 if ((dataBuf = dmRealloc(dataBuf, dataSize)) == NULL)
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
114 {
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
115 dmError("Error reallocating memory for data, %d bytes.\n", dataSize);
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
116 res = DMERR_MALLOC;
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
117 goto error;
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
118 }
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
119 }
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
120 else
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
121 break;
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
122 }
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
123
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
124 *pbufSize = dataRead;
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
125 *pbuf = dataBuf;
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
126
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
127 error:
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
128 if (f != inFile)
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
129 fclose(f);
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
130
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
131 return res;
b2b461829c61 Move utility function dmReadDataFile() to dmfile module instead.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
132 }