-
Notifications
You must be signed in to change notification settings - Fork 23
/
FileIOBin.c
executable file
·234 lines (187 loc) · 6.38 KB
/
FileIOBin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* SUMMARY: FileIOBin.c - Functions for binary IO
* USAGE: Part of DHSVM
*
* AUTHOR: Bart Nijssen
* ORG: University of Washington, Department of Civil Engineering
* E-MAIL: nijssen@u.washington.edu
* ORIG-DATE: Apr-96
* DESCRIPTION: Functions for binary IO
* DESCRIP-END.
* FUNCTIONS: CreateMapFileBin()
* Read2DMatrixBin()
* Read2DMatrixByteSwapBin()
* Write2DMatrixBin()
* Write2DMatrixByteSwapBin()
* SizeOfNumberType()
* byte_swap_long()
* byte_swap_short()
* COMMENTS:
* $Id: FileIOBin.c,v 1.4 2003/07/01 21:26:14 olivier Exp $
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "fifobin.h"
#include "fileio.h"
#include "sizeofnt.h"
#include "settings.h"
#include "DHSVMerror.h"
/*****************************************************************************
Function name: CreateMapFileBin()
Purpose : Open and close a new file. If the file already exists it
will be overwritten.
Required :
FileName - Name of the new file
Returns : void
Modifies :
Comments :
*****************************************************************************/
void CreateMapFileBin(char *FileName, ...)
{
FILE *NewFile;
OpenFile(&NewFile, FileName, "w", TRUE);
}
/*****************************************************************************
Function name: Read2DMatrixBin()
Purpose : Function to read a 2D array from a file.
Required :
FileName - name of input file
Matrix - address of array data into
NumberType - code for number type (taken from HDF, see comments at the
beginning of InitFileIO.c for more detail)
NY - Number of rows
NX - Number of columns
NDataSet - number of the dataset to read, i.e. the first matrix in a
file is number 0, etc.
Any remaining arguments are not used in straight binary
Returns : Number of elements read
Modifies : Matrix
Comments :
*****************************************************************************/
int Read2DMatrixBin(char *FileName, void *Matrix, int NumberType, int NY,
int NX, int NDataSet, ...)
{
FILE *InFile;
int NElements = 0; /* number of elements read */
size_t ElemSize;
unsigned long OffSet; /* number of bytes to OffSet (is non-zero when
reading matrices other than the first one
in the file */
OpenFile(&InFile, FileName, "rb", FALSE);
ElemSize = SizeOfNumberType(NumberType);
OffSet = NY * NX * ElemSize * NDataSet;
if (fseek(InFile, OffSet, SEEK_SET))
ReportError(FileName, 39);
NElements = fread(Matrix, ElemSize, NY * NX, InFile);
if (NElements != NY * NX)
ReportError(FileName, 2);
fclose(InFile);
return NElements;
}
/******************************************************************************/
int Read2DMatrixByteSwapBin(char *FileName, void *Matrix, int NumberType,
int NY, int NX, int NDataSet, ...)
{
FILE *InFile;
int NElements = 0; /* number of elements read */
size_t ElemSize;
unsigned long OffSet; /* number of bytes to OffSet (is non-zero when
reading matrices other than the first one
in the file */
OpenFile(&InFile, FileName, "rb", FALSE);
ElemSize = SizeOfNumberType(NumberType);
OffSet = NY * NX * ElemSize * NDataSet;
if (fseek(InFile, OffSet, SEEK_SET)) {
ReportError(FileName, 39);
}
NElements = fread(Matrix, ElemSize, NY * NX, InFile);
if (NElements != NY * NX) {
ReportError(FileName, 2);
}
fclose(InFile);
if (ElemSize == 4) {
byte_swap_long(Matrix, NElements);
}
else if (ElemSize == 2) {
byte_swap_short(Matrix, NElements);
}
else if (ElemSize != 1) {
ReportError(FileName, 61);
}
return NElements;
}
/*****************************************************************************
Function name: Write2DMatrixBin()
Purpose : Function to write a 2D array to a file. Data is appended to
the end of the file.
Required :
FileName - name of output file
Matrix - address of array containing matrix elements
NumberType - code for number type (see comments at the
beginning of InitFileIO.c for more detail)
NY - Number of rows
NX - Number of columns
Returns : Number of elements written
Modifies :
Comments :
*****************************************************************************/
int Write2DMatrixBin(char *FileName, void *Matrix, int NumberType, int NY,
int NX, ...)
{
FILE *OutFile; /* output file */
size_t ElemSize = 0; /* size of number type in bytes */
OpenFile(&OutFile, FileName, "ab", FALSE);
ElemSize = SizeOfNumberType(NumberType);
if (!(fwrite(Matrix, ElemSize, NY * NX, OutFile)))
ReportError(FileName, 41);
fclose(OutFile);
return NY * NX;
}
/******************************************************************************/
int Write2DMatrixByteSwapBin(char *FileName, void *Matrix, int NumberType,
int NY, int NX, ...)
{
FILE *OutFile; /* output file */
size_t ElemSize = 0; /* size of number type in bytes */
int NElements;
NElements = NX * NY;
OpenFile(&OutFile, FileName, "ab", FALSE);
ElemSize = SizeOfNumberType(NumberType);
if (ElemSize == 4) {
byte_swap_long(Matrix, NElements);
}
else if (ElemSize == 2) {
byte_swap_short(Matrix, NElements);
}
else if (ElemSize != 1) {
ReportError(FileName, 61);
}
if (!(fwrite(Matrix, ElemSize, NY * NX, OutFile))) {
ReportError(FileName, 41);
}
fclose(OutFile);
return NY * NX;
}
/******************************************************************************/
void byte_swap_short(short *buffer, int number_of_swaps)
{
short *temp;
int swap_loop;
for (swap_loop = 0, temp = buffer; swap_loop < number_of_swaps;
swap_loop++, temp++) {
*temp = ((*temp & 0x00ff) << 8) | ((*temp & 0xff00) >> 8);
}
}
/******************************************************************************/
void byte_swap_long(long *buffer, int number_of_swaps)
{
long *temp;
int swap_loop;
for (swap_loop = 0, temp = buffer; swap_loop < number_of_swaps;
swap_loop++, temp++) {
*temp = ((*temp & 0x000000ff) << 24) | ((*temp & 0x0000ff00) << 8) |
((*temp & 0x00ff0000) >> 8) | ((*temp & 0xff000000) >> 24);
}
}