Debian Codegraph @

biosig4c++/igor/ptn003/ReadPackedFile.c

raw symbols SCIP
1 /* The code in this file writes a sample Igor Pro packed experiment file.
2
3 See Igor Pro Tech Note PTN#003 for details.
4 */
5
6 #include <stdlib.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <stddef.h> // For offsetof macro.
10
11 #include "PackedFile.h"
12 #include "Variables.h"
13 #include "IgorBin.h"
14 #include "CrossPlatformFileIO.h"
15 #include "IgorSupport.h"
16
17 #include "PackedFileDemo.h"
18
19 // Constants
20 #define NOMEM 1 // Error code for out of memory.
21 #define DEMO_WAVE_LENGTH 10 // Number of points in the demo wave.
22
23 static void
24 PrintResult(const char* message, int error)
25 {
26 char buffer[256];
27
28 if (error)
29 sprintf(buffer, "Error %d: %s", error, message);
30 else
31 strcpy(buffer, message);
32 printf("%s\n", buffer);
33 }
34
35 static void
36 ReorderBytes(void *p, int bytesPerPoint, long numValues) // Reverses byte order.
37 {
38 unsigned char ch, *p1, *p2, *pEnd;
39
40 pEnd = (unsigned char *)p + numValues*bytesPerPoint;
41 while (p < (void *)pEnd) {
42 p1 = p;
43 p2 = (unsigned char *)p + bytesPerPoint-1;
44 while (p1 < p2) {
45 ch = *p1;
46 *p1++ = *p2;
47 *p2-- = ch;
48 }
49 p = (unsigned char *)p + bytesPerPoint;
50 }
51 }
52
53 static void
54 ReorderShort(void* sp)
55 {
56 ReorderBytes(sp, 2, 1);
57 }
58
59 static void
60 ReorderLong(void* lp)
61 {
62 ReorderBytes(lp, 4, 1);
63 }
64
65 static void
66 ReorderDouble(void* dp)
67 {
68 ReorderBytes(dp, 8, 1);
69 }
70
71 static void
72 ReorderPackedFileRecordHeader(PackedFileRecordHeader* php)
73 {
74 ReorderShort(&php->recordType);
75 ReorderShort(&php->version);
76 ReorderLong(&php->numDataBytes);
77 }
78
79 static void
80 ReorderVarHeader2(VarHeader2* vhp)
81 {
82 ReorderBytes(vhp, 2, 6); // 6 shorts
83 }
84
85 static void
86 ReorderUserNumVarRec(UserNumVarRec* nvp)
87 {
88 // The name field does not need to be reordered.
89 ReorderShort(&nvp->type);
90 ReorderShort(&nvp->num.numType);
91 ReorderDouble(&nvp->num.realPart);
92 ReorderDouble(&nvp->num.imagPart);
93 }
94
95 static void
96 ReorderUserDependentVarRec(UserDependentVarRec* dvp)
97 {
98 // The name field does not need to be reordered.
99 ReorderShort(&dvp->type);
100 ReorderShort(&dvp->num.numType);
101 ReorderDouble(&dvp->num.realPart);
102 ReorderDouble(&dvp->num.imagPart);
103 ReorderShort(&dvp->formulaLen);
104 // The formula field does not need to be reordered.
105 }
106
107 static void
108 DisplayTextLines(const char* text, unsigned long textLength)
109 {
110 const char* p1;
111 const char* p;
112 long bytesInLine, bytesToDisplay, count;
113 char line[512];
114 int i;
115
116 p1 = text;
117 for(i=0; i <= 3; i++) {
118 if (i == 3) {
119 printf(" Skipping the rest of the text.\n");
120 break;
121 }
122
123 p = strchr(p1, 13); // Look for next CR.
124 if (p == NULL) {
125 bytesInLine = text + textLength - p1;
126 bytesToDisplay = bytesInLine;
127 }
128 else {
129 bytesInLine = p+1 - p1;
130 bytesToDisplay = bytesInLine - 1; // Exclude CR.
131 }
132 count = bytesToDisplay;
133 if (count > sizeof(line)-1)
134 count = sizeof(line)-1;
135 strncpy(line, p1, count);
136 line[count] = 0;
137 printf(" Line %d: %s\n", i, line);
138 p1 += bytesInLine;
139 if (p1 < text + textLength) {
140 if (*p1 == 10)
141 p1 += 1; // Skip linefeed following carriage return.
142 }
143 if (p1 >= text + textLength)
144 break; // All done.
145 }
146 }
147
148
149 static int
150 ReadPackedHeader(CP_FILE_REF fr, int needToReorderBytes, PackedFileRecordHeader* php)
151 {
152 unsigned long numBytesToRead, numBytesRead;
153 int err;
154
155 numBytesToRead = sizeof(PackedFileRecordHeader);
156 if (err = CPReadFile(fr, numBytesToRead, php, &numBytesRead))
157 return err;
158
159 if (needToReorderBytes)
160 ReorderPackedFileRecordHeader(php);
161
162 return 0;
163 }
164
165 static int
166 ReadVariablesRecord(CP_FILE_REF fr, long numDataBytes, int needToReorderBytes)
167 {
168 VarHeader2 vh;
169 short version;
170 unsigned long numBytesToRead, numBytesRead;
171 int i;
172 int err;
173
174 memset(&vh, 0, sizeof(vh));
175
176 // First determine if we have a version 1 or version 2 VarHeader structure.
177 numBytesToRead = 2;
178 if (err = CPReadFile(fr, numBytesToRead, &version, &numBytesRead)) {
179 PrintResult("ReadVariablesRecord error", err);
180 return err;
181 }
182 if (needToReorderBytes)
183 ReorderShort(&version);
184 CPSetFilePosition(fr, -2, 0); // Back up to the starting point.
185
186 switch(version) {
187 case 1:
188 numBytesToRead = sizeof(VarHeader1);
189 err = CPReadFile(fr, numBytesToRead, &vh, &numBytesRead);
190 break;
191
192 case 2:
193 numBytesToRead = sizeof(VarHeader2);
194 err = CPReadFile(fr, numBytesToRead, &vh, &numBytesRead);
195 break;
196
197 default:
198 printf("Unknown VarHeader version: %d\n", version);
199 err = -1;
200 break;
201 }
202 if (err != 0) {
203 PrintResult("Error reading VarHeader record", err);
204 return err;
205 }
206 if (needToReorderBytes)
207 ReorderVarHeader2(&vh);
208
209 printf("--Variables record--\n");
210 printf(" System: %d, numeric: %d, string: %d, dependent numeric: %d, dependent string: %d\n", vh.numSysVars, vh.numUserVars, vh.numUserStrs, vh.numDependentVars, vh.numDependentStrs);
211
212 // Read system variables.
213 for(i = 0; i < vh.numSysVars; i++) {
214 float f;
215 numBytesToRead = sizeof(float);
216 if (err = CPReadFile(fr, numBytesToRead, &f, &numBytesRead)) {
217 PrintResult("Error reading system variable", err);
218 return err;
219 }
220 if (needToReorderBytes)
221 ReorderLong(&f);
222 }
223
224 // Read user numeric variables.
225 for(i = 0; i < vh.numUserVars; i++) {
226 UserNumVarRec nv;
227 numBytesToRead = sizeof(nv);
228 if (err = CPReadFile(fr, numBytesToRead, &nv, &numBytesRead)) {
229 PrintResult("Error reading user numeric variable", err);
230 return err;
231 }
232 if (needToReorderBytes)
233 ReorderUserNumVarRec(&nv);
234 if (i <= 3) {
235 if (i == 3) {
236 printf(" Skipping remaining numeric variables.\n");
237 }
238 else {
239 if (nv.num.numType & NT_CMPLX)
240 printf(" Numeric variable %s, value=(%g,%g)\n", nv.name, nv.num.realPart, nv.num.imagPart);
241 else
242 printf(" Numeric variable %s, value=%g\n", nv.name, nv.num.realPart);
243 }
244 }
245 }
246
247 // Read user string variables.
248 for(i = 0; i < vh.numUserStrs; i++) {
249 UserStrVarRec1 sv1;
250 UserStrVarRec2 sv2;
251 char buffer[32];
252 char ellipsis[16];
253 unsigned long pos;
254
255 if (version == 1) {
256 numBytesToRead = offsetof(UserStrVarRec1, data);
257 err = CPReadFile(fr, numBytesToRead, &sv1, &numBytesRead);
258 if (err == 0) {
259 if (needToReorderBytes)
260 ReorderShort(&sv1.strLen);
261 // Copy sv1 record to sv2. Only sv2 is used below.
262 strcpy(sv2.name, sv1.name);
263 sv2.strLen = sv1.strLen;
264 }
265 }
266 else {
267 numBytesToRead = offsetof(UserStrVarRec2, data);
268 err = CPReadFile(fr, numBytesToRead, &sv2, &numBytesRead);
269 if (err == 0) {
270 if (needToReorderBytes)
271 ReorderLong(&sv2.strLen);
272 }
273 }
274 if (err != 0) {
275 PrintResult("Error reading user string variable header", err);
276 return err;
277 }
278
279 CPGetFilePosition(fr, &pos);
280 numBytesToRead = sv2.strLen;
281 if (numBytesToRead > sizeof(buffer)-1)
282 numBytesToRead = sizeof(buffer)-1;
283 if (err = CPReadFile(fr, numBytesToRead, buffer, &numBytesRead)) {
284 PrintResult("Error reading user string variable", err);
285 return err;
286 }
287 buffer[numBytesRead] = 0;
288 if (numBytesRead < (unsigned long)sv2.strLen) // Read only part of the string?
289 strcpy(ellipsis, "..."); // This is used below in printout.
290 else
291 *ellipsis = 0;
292
293 CPSetFilePosition(fr, pos + sv2.strLen, -1); // Move to the end of the string.
294
295 if (i <= 3) {
296 if (i == 3)
297 printf(" Skipping remaining string variables.\n");
298 else
299 printf(" String variable %s, value=\"%s%s\"\n", sv2.name, buffer, ellipsis);
300 }
301 }
302
303 // Read user dependent numeric variables.
304 for(i = 0; i < vh.numDependentVars; i++) {
305 UserDependentVarRec dv;
306 numBytesToRead = sizeof(dv);
307 if (err = CPReadFile(fr, numBytesToRead, &dv, &numBytesRead)) {
308 PrintResult("Error reading user dependent numeric variable", err);
309 return err;
310 }
311 if (needToReorderBytes)
312 ReorderUserDependentVarRec(&dv);
313 if (i <= 3) {
314 if (i == 3) {
315 printf(" Skipping remaining numeric dependent variables.\n");
316 }
317 else {
318 if (dv.num.numType & NT_CMPLX)
319 printf(" Numeric dependent variable %s, value=(%g,%g)\n", dv.name, dv.num.realPart, dv.num.imagPart);
320 else
321 printf(" Numeric dependent variable %s, value=%g\n", dv.name, dv.num.realPart);
322 }
323 }
324 }
325
326 // Read user dependent string variables.
327 for(i = 0; i < vh.numDependentStrs; i++) {
328 UserDependentVarRec dv;
329 numBytesToRead = sizeof(dv);
330 if (err = CPReadFile(fr, numBytesToRead, &dv, &numBytesRead)) {
331 PrintResult("Error reading user dependent string variable", err);
332 return err;
333 }
334 if (needToReorderBytes)
335 ReorderUserDependentVarRec(&dv);
336 if (i <= 3) {
337 if (i == 3) {
338 printf(" Skipping remaining string dependent variables.\n");
339 }
340 else {
341 printf(" String dependent variable %s\n", dv.name);
342 }
343 }
344 }
345
346 return 0;
347 }
348
349 static int
350 ReadHistoryRecord(CP_FILE_REF fr, long numDataBytes, int needToReorderBytes)
351 {
352 char* text;
353 unsigned long numBytesToRead, numBytesRead;
354 int err;
355
356 printf("--History record--\n");
357
358 if (numDataBytes > 0) {
359 text = malloc(numDataBytes);
360 if (text == NULL) {
361 printf("ReadHistoryRecord unable to allocate %ld bytes\n", numDataBytes);
362 return -1;
363 }
364 numBytesToRead = numDataBytes;
365 if (err = CPReadFile(fr, numBytesToRead, text, &numBytesRead)) {
366 PrintResult("Error reading history", err);
367 free(text);
368 return err;
369 }
370
371 DisplayTextLines(text, numDataBytes);
372
373 free(text);
374 }
375
376 return 0;
377 }
378
379 static int
380 ReadWaveRecord(CP_FILE_REF fr, long numDataBytes, int needToReorderBytes)
381 {
382 int type;
383 long npnts;
384 void* waveDataPtr;
385 int err;
386
387 printf("--Wave record--\n");
388
389 err = ReadWave(fr, &type, &npnts, &waveDataPtr);
390
391 // If no error occurred, the main wave data is now available via waveDataPtr.
392
393 if (waveDataPtr != NULL)
394 free(waveDataPtr);
395
396 if (err) {
397 PrintResult("Error reading wave", err);
398 return err;
399 }
400
401 return 0;
402 }
403
404 static int
405 ReadRecreationRecord(CP_FILE_REF fr, long numDataBytes, int needToReorderBytes)
406 {
407 char* text;
408 unsigned long numBytesToRead, numBytesRead;
409 int err;
410
411 printf("--Recreation macros record--\n");
412
413 if (numDataBytes > 0) {
414 text = malloc(numDataBytes);
415 if (text == NULL) {
416 printf("ReadRecreationRecord unable to allocate %ld bytes\n", numDataBytes);
417 return -1;
418 }
419 numBytesToRead = numDataBytes;
420 if (err = CPReadFile(fr, numBytesToRead, text, &numBytesRead)) {
421 PrintResult("Error reading recreation procedures", err);
422 free(text);
423 return err;
424 }
425
426 DisplayTextLines(text, numDataBytes);
427
428 free(text);
429 }
430
431 return 0;
432 }
433
434
435 static int
436 ReadProcedureRecord(CP_FILE_REF fr, long numDataBytes, int needToReorderBytes)
437 {
438 char* text;
439 unsigned long numBytesToRead, numBytesRead;
440 int err;
441
442 printf("--Procedures record--\n");
443
444 if (numDataBytes > 0) {
445 text = malloc(numDataBytes);
446 if (text == NULL) {
447 printf("ReadProcedureRecord unable to allocate %ld bytes\n", numDataBytes);
448 return -1;
449 }
450 numBytesToRead = numDataBytes;
451 if (err = CPReadFile(fr, numBytesToRead, text, &numBytesRead)) {
452 PrintResult("Error reading procedures", err);
453 free(text);
454 return err;
455 }
456
457 DisplayTextLines(text, numDataBytes);
458
459 free(text);
460 }
461
462 return 0;
463 }
464
465 static int
466 ReadGetHistoryRecord(CP_FILE_REF fr, long numDataBytes, int needToReorderBytes)
467 {
468 printf("--Get history record--\n");
469
470 return 0;
471 }
472
473 static int
474 ReadPackedFileRecord(CP_FILE_REF fr, long numDataBytes, int needToReorderBytes)
475 {
476 printf("--Packed file record--\n");
477 printf(" This type of record is used to store packed notebooks and procedure windows.\n");
478 printf(" The format of the record is quite complex and is not documented in PTN003.\n");
479
480 return 0;
481 }
482
483 static int
484 ReadDataFolderStartRecord(CP_FILE_REF fr, long numDataBytes, int needToReorderBytes)
485 {
486 unsigned long numBytesToRead, numBytesRead;
487 char name[32];
488 int err;
489
490 printf("--Data folder start record--\n");
491
492 numBytesToRead = 32;
493 if (err = CPReadFile(fr, numBytesToRead, name, &numBytesRead)) {
494 PrintResult("Error reading data folder name", err);
495 return err;
496 }
497
498 printf(" Entering data folder \'%s\'.\n", name);
499
500 return 0;
501 }
502
503 static int
504 ReadDataFolderEndRecord(CP_FILE_REF fr, long numDataBytes, int needToReorderBytes)
505 {
506 printf("--Data folder end record--\n");
507
508 return 0;
509 }
510
511 static int
512 ReadPackedRecords(CP_FILE_REF fr)
513 {
514 unsigned long numberOfBytesInFile;
515 unsigned long recordStartPos;
516 unsigned long numBytesToRead, numBytesRead;
517 PackedFileRecordHeader ph;
518 unsigned short stemp;
519 int needToReorderBytes;
520 int err;
521
522 /* First we must determine if the file uses the wrong byte ordering, in which
523 case we will need to do reordering. We do this by reading the recordType
524 field of the first record. All record types have 0 in the high byte,
525 except for a possibly set SUPERCEDED_MASK.
526 */
527 numBytesToRead = 2;
528 if (err = CPReadFile(fr, numBytesToRead, &stemp, &numBytesRead)) {
529 PrintResult("Read file error", err);
530 return err;
531 }
532 needToReorderBytes = 0;
533 stemp &= PACKEDRECTYPE_MASK; // Clear SUPERCEDED_MASK bit.
534 if ((stemp & 0xFF00) != 0) // High byte is non-zero.
535 needToReorderBytes = 1; // The file uses the wrong byte ordering.
536 CPSetFilePosition(fr, -2, 0); // Back up to the starting point.
537
538 if (needToReorderBytes) {
539 #ifdef WIN32
540 printf("This file uses big-endian byte ordering.\n");
541 #else
542 printf("This file uses little-endian byte ordering.\n");
543 #endif
544 }
545
546 // Now read the records.
547 CPNumberOfBytesInFile(fr, &numberOfBytesInFile);
548 while(1) {
549 CPGetFilePosition(fr, &recordStartPos);
550
551 if (recordStartPos >= numberOfBytesInFile)
552 break; // No more data in the file.
553
554 if (err = ReadPackedHeader(fr, needToReorderBytes, &ph)) {
555 PrintResult("ReadPackedHeader error", err);
556 return err;
557 }
558
559 switch(ph.recordType) {
560 case kVariablesRecord: // 1: Contains system numeric variables (e.g., K0) and user numeric and string variables.
561 if (err = ReadVariablesRecord(fr, ph.numDataBytes, needToReorderBytes))
562 return err;
563 break;
564
565 case kHistoryRecord: // 2: Contains the experiment's history as plain text.
566 if (err = ReadHistoryRecord(fr, ph.numDataBytes, needToReorderBytes))
567 return err;
568 break;
569
570 case kWaveRecord: // 3: Contains the data for a wave
571 if (err = ReadWaveRecord(fr, ph.numDataBytes, needToReorderBytes))
572 return err;
573 break;
574
575 case kRecreationRecord: // 4: Contains the experiment's recreation procedures as plain text.
576 if (err = ReadRecreationRecord(fr, ph.numDataBytes, needToReorderBytes))
577 return err;
578 break;
579
580 case kProcedureRecord: // 5: Contains the experiment's main procedure window text as plain text.
581 if (err = ReadProcedureRecord(fr, ph.numDataBytes, needToReorderBytes))
582 return err;
583 break;
584
585 case kGetHistoryRecord: // 6: Not a real record but rather a message to go back and read the history text.
586 if (err = ReadGetHistoryRecord(fr, ph.numDataBytes, needToReorderBytes))
587 return err;
588 break;
589
590 case kPackedFileRecord: // 7: Contains the data for a procedure file or notebook in a packed form.
591 if (err = ReadPackedFileRecord(fr, ph.numDataBytes, needToReorderBytes))
592 return err;
593 break;
594
595 case kDataFolderStartRecord: // 8: Marks the start of a new data folder.
596 if (err = ReadDataFolderStartRecord(fr, ph.numDataBytes, needToReorderBytes))
597 return err;
598 break;
599
600 case kDataFolderEndRecord: // 10: Marks the end of a data folder.
601 if (err = ReadDataFolderEndRecord(fr, ph.numDataBytes, needToReorderBytes))
602 return err;
603 break;
604
605 default:
606 // Here if we have an unknown record type or a superceded record.
607 break;
608 }
609
610 // Skip to next record.
611 CPSetFilePosition(fr, recordStartPos + sizeof(ph) + ph.numDataBytes, -1);
612 }
613
614 return 0;
615 }
616
617 int
618 ReadPackedFile(const char* packedFilePath)
619 {
620 CP_FILE_REF fr; // Ref number for the file we are writing to.
621 unsigned long numberOfBytesInFile;
622 char temp[256];
623 int err;
624
625 if (err = CPOpenFile(packedFilePath, 0, &fr)) {
626 PrintResult("Open file error", err);
627 return err;
628 }
629
630 CPNumberOfBytesInFile(fr, &numberOfBytesInFile);
631 printf("The file consists of %ld bytes.\n", numberOfBytesInFile);
632
633 // Read the records.
634 if (err = ReadPackedRecords(fr))
635 return err;
636
637 if (err = CPCloseFile(fr)) {
638 PrintResult("Close file error", err);
639 return err;
640 }
641
642 sprintf(temp, "\"%s\" read successfully.", packedFilePath);
643 PrintResult(temp, 0);
644
645 return 0;
646 }