Debian Codegraph @

biosig4c++/igor/ptn003/IgorSupport.c

raw symbols SCIP
1 /* This file provides support for Igor-related things, such as writing
2 variable and wave records, for the Packed File demo program.
3
4 See main.c for examples showing how these routines are called.
5
6 See Igor Pro Tech Note PTN#003 for details.
7 */
8
9 #include <string.h>
10 #include <stdio.h>
11 #include <stddef.h> // For offsetof macro.
12
13 #include "PackedFile.h"
14 #include "Variables.h"
15 #include "IgorBin.h"
16 #include "CrossPlatformFileIO.h"
17 #include "IgorSupport.h"
18
19
20 // Support for writing packed file records.
21
22 int
23 WriteRecordHeader(CP_FILE_REF fr, int recordType, int recordVersion, long recordBytes)
24 {
25 PackedFileRecordHeader h;
26 unsigned long numBytesToWrite, numBytesWritten;
27 int err;
28
29 h.recordType = recordType;
30 h.version = recordVersion;
31 h.numDataBytes = recordBytes;
32 numBytesToWrite = sizeof(PackedFileRecordHeader);
33 err = CPWriteFile(fr, numBytesToWrite, &h, &numBytesWritten);
34 return err;
35 }
36
37 /* SetRecordHeaderNumDataBytesField(...)
38
39 This is used to set the numDataBytes field of the PackedFileRecordHeader
40 structure on disk, after the data section of a record has been written.
41 This must be called after writing the data section so that the current
42 file mark is just after the last data byte written.
43 */
44 int
45 SetRecordHeaderNumDataBytesField(CP_FILE_REF fr, long recordStartPos)
46 {
47 unsigned long currentFilePos;
48 long numDataBytes;
49 unsigned long numBytesToWrite, numBytesWritten;
50 long filePos;
51 int err;
52
53 if (err = CPGetFilePosition(fr, &currentFilePos))
54 return err;
55 numDataBytes = currentFilePos - recordStartPos - sizeof(PackedFileRecordHeader);
56 filePos = recordStartPos + offsetof(PackedFileRecordHeader, numDataBytes);
57 if (err = CPSetFilePosition(fr, filePos, -1))
58 return err;
59 numBytesToWrite = sizeof(numDataBytes);
60 if (err = CPWriteFile(fr, numBytesToWrite, &numDataBytes, &numBytesWritten))
61 return err;
62 if (err = CPSetFilePosition(fr, currentFilePos, -1))
63 return err;
64 return 0;
65 }
66
67
68 // Support for writing variables.
69
70 int
71 WriteNumericVar(CP_FILE_REF fr, const char* name, int isComplex, double realPart, double imagPart)
72 {
73 UserNumVarRec nv;
74 unsigned long numBytesToWrite, numBytesWritten;
75 int err;
76
77 strcpy(nv.name, name);
78 nv.type = 1; // Signifies numeric variable.
79 if (isComplex)
80 nv.num.numType = NT_FP64 | NT_CMPLX; // Double-precision, complex.
81 else
82 nv.num.numType = NT_FP64; // Double-precision, real.
83 nv.num.realPart = realPart;
84 nv.num.imagPart = imagPart;
85 nv.num.reserved = 0;
86
87 numBytesToWrite = sizeof(UserNumVarRec);
88 err = CPWriteFile(fr, numBytesToWrite, &nv, &numBytesWritten);
89 return err;
90 }
91
92 int
93 WriteStringVar(CP_FILE_REF fr, const char* name, const char* contents, long numBytes)
94 {
95 UserStrVarRec2 sv;
96 unsigned long numBytesToWrite, numBytesWritten;
97 int err;
98
99 strcpy(sv.name, name);
100 sv.strLen = numBytes;
101
102 numBytesToWrite = offsetof(UserStrVarRec2, data);
103 if (err = CPWriteFile(fr, numBytesToWrite, &sv, &numBytesWritten)) // Write up to but not including the data field.
104 return err;
105
106 numBytesToWrite = numBytes;
107 err = CPWriteFile(fr, numBytesToWrite, contents, &numBytesWritten); // Write the string data.
108 return err;
109 }
110
111 int
112 WriteVariablesHeader(CP_FILE_REF fr, int numSysVars, int numUserVars, int numUserStrs, int numDependentVars, int numDependentStrs)
113 {
114 VarHeader2 vh;
115 unsigned long numBytesToWrite, numBytesWritten;
116 int err;
117
118 // Write the specific header for variables data.
119 vh.version = 2; // Version 2 variables header.
120 vh.numSysVars = numSysVars;
121 vh.numUserVars = numUserVars;
122 vh.numUserStrs = numUserStrs;
123 vh.numDependentVars = numDependentVars;
124 vh.numDependentStrs = numDependentStrs;
125 numBytesToWrite = sizeof(vh);
126 err = CPWriteFile(fr, numBytesToWrite, &vh, &numBytesWritten);
127 return err;
128
129 return 0;
130 }
131
132
133 // Support for writing waves.
134
135 static long
136 Checksum(
137 short *data,
138 long oldcksum,
139 long numBytes)
140 {
141 numBytes >>= 1; // 2 bytes to a short -- ignore trailing odd byte.
142 while(numBytes-- > 0)
143 oldcksum += *data++;
144 return oldcksum&0xffff;
145 }
146
147
148 /* WriteWaveBinHeader2(...)
149
150 This writes a version 2 BinHeader to disk.
151
152 The wave BinHeader contains a checksum over the BinHeader itself plus
153 the following wave WaveHeader2 structure. The wData field of the WaveHeader2
154 structure is included in the checksum. Therefore, the wData field must
155 contain the same data as the first 16 bytes of the actual wave data.
156
157 The wfmSize parameter is the size of the WaveHeader2 structure up to
158 but not including the wData field plus the size of the wave's main data
159 bytes plus 16 bytes of padding. Main data bytes means the numeric data
160 for numeric waves.
161
162 The noteSize parameters can be 0.
163 */
164 int
165 WriteWaveBinHeader2(
166 CP_FILE_REF fr,
167 WavePtr2 wp, // Pointer to wave structure for wave to be written
168 long wfmSize, // Size of wave structure plus wave data proper plus 16 bytes of padding
169 long noteSize)
170 {
171 BinHeader2 bh;
172 long checksum;
173 unsigned long numBytesToWrite, numBytesWritten;
174 int err;
175
176 memset(&bh, 0, sizeof(BinHeader2));
177
178 bh.version = 2; // This is a version 2 wave record.
179 bh.wfmSize = wfmSize;
180 bh.noteSize = noteSize;
181
182 // Do checksum over wave structure and then add checksum over wave header.
183 checksum = Checksum((short *)wp, 0, sizeof(WaveHeader2)); // Includes wData field which must contain the same data as the first 16 bytes of the main wave data.
184 checksum = -Checksum((short *)&bh, checksum, sizeof(struct BinHeader2));
185 bh.checksum = (short)checksum;
186
187 numBytesToWrite = sizeof(BinHeader2);
188 err = CPWriteFile(fr, numBytesToWrite, &bh, &numBytesWritten);
189 return err;
190 }
191
192 /* InitWaveHeader2(...)
193
194 Initializes the wave structure pointed to by wp to valid values
195 for a 1D numeric wave.
196
197 Since this sets all fields to safe values, you should call this first
198 for all waves and then change the structure as needed for multi-dimensional
199 or text waves. See PTN003 for details.
200
201 If you don't care about setting the creation and modification dates,
202 pass 0 for creationDate.
203
204 numPoints is the number of data points in all dimensions of the wave.
205 */
206 void
207 InitWaveHeader2(
208 WavePtr2 wp, // Pointer to wave structure to be inited.
209 const char* name, // C string. Up to 31 characters.
210 unsigned long creationDate, // Number of seconds since midnight January 1, 1904.
211 long numPoints, // Number of points in the wave.
212 int type) // Valid Igor number type. See IgorBin.h.
213 {
214 memset(wp, 0, sizeof(WaveHeader2));
215
216 wp->creationDate = creationDate;
217 wp->modDate = creationDate;
218 wp->npnts = numPoints;
219 wp->type = type;
220 wp->whVersion = 0;
221 strcpy(wp->bname, name);
222 wp->hsA = 1.0;
223 wp->hsB = 0.0;
224 }
225
226 /* WriteWaveBinHeader5(...)
227
228 This writes a version 5 BinHeader to disk.
229
230 Because the wave BinHeader contains a checksum over the header itself
231 plus the following wave WaveHeader5 structure, this routine requires a
232 pointer to the WaveHeader5 structure to be written. The structure must
233 contain the values to be written so that the checksum will be correct.
234
235 The wfmSize parameter is the size of the WaveHeader5 structure up to
236 but not including the wData field plus the size of the wave's main data
237 bytes. Main data bytes means the numeric data for numeric waves or the
238 text data for text waves but does not include any optional data or the
239 string indices.
240
241 All of the size parameters, except for wfmSize, can be 0.
242 For text waves, the sIndicesSize must be as described in TN003 (not PTN003).
243 */
244 int
245 WriteWaveBinHeader5(
246 CP_FILE_REF fr,
247 WavePtr5 wp, // Pointer to wave structure for wave to be written
248 long wfmSize, // size of wave structure plus wave data proper
249 long formulaSize,
250 long noteSize,
251 long dataEUnitsSize,
252 long dimEUnitsSize[], // Array of 4 longs or NULL if no extended dimension units
253 long dimLabelsSize[], // Array of 4 longs or NULL if no dimension labels
254 long sIndicesSize) // Size of string indices if this is a text wave
255 {
256 BinHeader5 bh;
257 long checksum;
258 unsigned long numBytesToWrite, numBytesWritten;
259 int err;
260
261 memset(&bh, 0, sizeof(BinHeader5));
262
263 bh.version = 5; // This is a version 5 wave record.
264 bh.wfmSize = wfmSize;
265 bh.formulaSize = formulaSize;
266 bh.noteSize = noteSize;
267 bh.dataEUnitsSize = dataEUnitsSize;
268 if (dimEUnitsSize != NULL)
269 memcpy(bh.dimEUnitsSize, dimEUnitsSize, MAXDIMS*sizeof(long));
270 if (dimLabelsSize != NULL)
271 memcpy(bh.dimLabelsSize, dimLabelsSize, MAXDIMS*sizeof(long));
272 bh.sIndicesSize = sIndicesSize;
273
274 // Do checksum over wave structure and then add checksum over wave header.
275 checksum = Checksum((short *)wp, 0, offsetof(WaveHeader5, wData));
276 checksum = -Checksum((short *)&bh, checksum, sizeof(struct BinHeader5));
277 bh.checksum = (short)checksum;
278
279 numBytesToWrite = sizeof(BinHeader5);
280 err = CPWriteFile(fr, numBytesToWrite, &bh, &numBytesWritten);
281 return err;
282 }
283
284 /* InitWaveHeader5(...)
285
286 Initializes the wave structure pointed to by wp to valid values
287 for a 1D numeric wave.
288
289 Since this sets all fields to safe values, you should call this first
290 for all waves and then change the structure as needed for multi-dimensional
291 or text waves. See PTN003 for details.
292
293 If you don't care about setting the creation and modification dates,
294 pass 0 for creationDate.
295
296 numPoints is the number of data points in all dimensions of the wave.
297 */
298 void
299 InitWaveHeader5(
300 WavePtr5 wp, // Pointer to wave structure to be inited.
301 const char* name, // C string. Up to 31 characters.
302 unsigned long creationDate, // Number of seconds since midnight January 1, 1904.
303 long numPoints, // Number of points in the wave.
304 int type) // Valid Igor number type. See IgorBin.h.
305 {
306 int i;
307
308 memset(wp, 0, sizeof(WaveHeader5));
309
310 wp->creationDate = creationDate;
311 wp->modDate = creationDate;
312 wp->npnts = numPoints;
313 wp->type = type;
314 wp->whVersion = 1;
315 strcpy(wp->bname, name);
316 wp->nDim[0] = numPoints; // This assumes a 1D wave.
317
318 for(i = 0; i < MAXDIMS; i++) {
319 wp->sfA[i] = 1.0; // Dimension scaling delta value.
320 wp->sfB[i] = 0.0; // Dimension scaling offset value.
321 }
322 }
323
324
325 // Support for data folders.
326
327 int
328 WriteDataFolderStartRecord(CP_FILE_REF fr, const char* name)
329 {
330 char temp[32];
331 unsigned long numBytesToWrite, numBytesWritten;
332 int err;
333
334 if (err = WriteRecordHeader(fr, kDataFolderStartRecord, 0, 32))
335 return err;
336
337 memset(temp, 0, sizeof(temp));
338 strncpy(temp, name, 31);
339 numBytesToWrite = 32;
340 err = CPWriteFile(fr, numBytesToWrite, temp, &numBytesWritten);
341 return err;
342 }
343
344 int
345 WriteDataFolderEndRecord(CP_FILE_REF fr)
346 {
347 return WriteRecordHeader(fr, kDataFolderEndRecord, 0, 0);
348 }
349
350
351 // Support for plain text records (kRecreationRecord, kHistoryRecord, kProcedureRecord).
352
353 int
354 WritePlainTextRecord(CP_FILE_REF fr, int recordType, const char* text, long numBytes)
355 {
356 unsigned long recordStartPos;
357 unsigned long numBytesToWrite, numBytesWritten;
358 int err;
359
360 if (err = CPGetFilePosition(fr, &recordStartPos)) // Remember where record starts in the file.
361 return err;
362
363 // Write the generic packed file record header. numDataBytes field will be updated later.
364 if (err = WriteRecordHeader(fr, recordType, 0, 0))
365 return err;
366
367 // Write the text.
368 numBytesToWrite = numBytes;
369 if (err = CPWriteFile(fr, numBytesToWrite, text, &numBytesWritten))
370 return err;
371
372 // Update the numDataBytes field to record the actual length of the data.
373 if (err = SetRecordHeaderNumDataBytesField(fr, recordStartPos))
374 return err;
375
376 return 0;
377 }