| 1 |
|
| 2 |
|
| 3 |
#include <ctype.h> |
| 4 |
#include <string.h> |
| 5 |
#include <stdlib.h> |
| 6 |
#include <stdio.h> |
| 7 |
#include <stddef.h> |
| 8 |
#include "IgorBin.h" |
| 9 |
#include "CrossPlatformFileIO.h" |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
static int |
| 17 |
Checksum(short *data, int oldcksum, int numbytes) |
| 18 |
{ |
| 19 |
numbytes >>= 1; |
| 20 |
while(numbytes-- > 0) |
| 21 |
oldcksum += *data++; |
| 22 |
return oldcksum&0xffff; |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
static int |
| 30 |
NumBytesPerPoint(int type) |
| 31 |
{ |
| 32 |
int numBytesPerPoint; |
| 33 |
|
| 34 |
|
| 35 |
switch(type & ~(NT_CMPLX | NT_UNSIGNED)) { |
| 36 |
case NT_I8: |
| 37 |
numBytesPerPoint = 1; |
| 38 |
break; |
| 39 |
case NT_I16: |
| 40 |
numBytesPerPoint = 2; |
| 41 |
break; |
| 42 |
case NT_I32: |
| 43 |
numBytesPerPoint = 4; |
| 44 |
break; |
| 45 |
case NT_FP32: |
| 46 |
numBytesPerPoint = 4; |
| 47 |
break; |
| 48 |
case NT_FP64: |
| 49 |
numBytesPerPoint = 8; |
| 50 |
break; |
| 51 |
default: |
| 52 |
return 0; |
| 53 |
break; |
| 54 |
} |
| 55 |
|
| 56 |
if (type & NT_CMPLX) |
| 57 |
numBytesPerPoint *= 2; |
| 58 |
|
| 59 |
return numBytesPerPoint; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
static int |
| 71 |
WriteVersion2NumericWave(CP_FILE_REF fr, WaveHeader2* whp, const void* data, const char* waveNote, long noteSize) |
| 72 |
{ |
| 73 |
unsigned long numBytesToWrite; |
| 74 |
unsigned long numBytesWritten; |
| 75 |
unsigned long waveDataSize; |
| 76 |
int numBytesPerPoint; |
| 77 |
short cksum; |
| 78 |
BinHeader2 bh; |
| 79 |
char padding[16]; |
| 80 |
int err; |
| 81 |
|
| 82 |
numBytesPerPoint = NumBytesPerPoint(whp->type); |
| 83 |
if (numBytesPerPoint <= 0) { |
| 84 |
printf("Invalid wave type (0x%x).\n", whp->type); |
| 85 |
return -1; |
| 86 |
} |
| 87 |
waveDataSize = whp->npnts * numBytesPerPoint; |
| 88 |
|
| 89 |
|
| 90 |
memset(&bh,0,sizeof(struct BinHeader2)); |
| 91 |
bh.version = 2; |
| 92 |
bh.wfmSize = offsetof(WaveHeader2, wData) + waveDataSize + 16; |
| 93 |
bh.noteSize = noteSize; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
cksum = Checksum((short *)&bh, 0, sizeof(struct BinHeader2)); |
| 101 |
cksum = Checksum((short *)whp, cksum, sizeof(struct WaveHeader2)); |
| 102 |
bh.checksum = -cksum; |
| 103 |
|
| 104 |
do { |
| 105 |
|
| 106 |
numBytesToWrite = sizeof(struct BinHeader2); |
| 107 |
if (err = CPWriteFile(fr, numBytesToWrite, &bh, &numBytesWritten)) |
| 108 |
break; |
| 109 |
|
| 110 |
|
| 111 |
numBytesToWrite = offsetof(WaveHeader2, wData); |
| 112 |
if (err = CPWriteFile(fr, numBytesToWrite, whp, &numBytesWritten)) |
| 113 |
break; |
| 114 |
|
| 115 |
|
| 116 |
numBytesToWrite = waveDataSize; |
| 117 |
if (err = CPWriteFile(fr, numBytesToWrite, data, &numBytesWritten)) |
| 118 |
break; |
| 119 |
|
| 120 |
|
| 121 |
memset(padding, 0, 16); |
| 122 |
numBytesToWrite = 16; |
| 123 |
if (err = CPWriteFile(fr, numBytesToWrite, padding, &numBytesWritten)) |
| 124 |
break; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
numBytesToWrite = noteSize; |
| 130 |
if (numBytesToWrite > 0) { |
| 131 |
if (err = CPWriteFile(fr, numBytesToWrite, waveNote, &numBytesWritten)) |
| 132 |
break; |
| 133 |
} |
| 134 |
|
| 135 |
} while(0); |
| 136 |
|
| 137 |
return err; |
| 138 |
} |
| 139 |
|
| 140 |
#define WAVEPOINTS 100 |
| 141 |
|
| 142 |
int DoWriteTest(const char* filePath); |
| 143 |
int |
| 144 |
DoWriteTest(const char* filePath) |
| 145 |
{ |
| 146 |
WaveHeader2 wh; |
| 147 |
unsigned long now; |
| 148 |
float data[WAVEPOINTS]; |
| 149 |
char waveNote[256]; |
| 150 |
CP_FILE_REF fr; |
| 151 |
long p; |
| 152 |
int err; |
| 153 |
|
| 154 |
#ifdef WIN32 |
| 155 |
now = 0; |
| 156 |
#else |
| 157 |
GetDateTime(&now); |
| 158 |
#endif |
| 159 |
|
| 160 |
memset(&wh, 0, sizeof(wh)); |
| 161 |
wh.type = NT_FP32; |
| 162 |
strcpy(wh.bname, "TestWave"); |
| 163 |
strcpy(wh.dataUnits, "v"); |
| 164 |
strcpy(wh.xUnits, "s"); |
| 165 |
wh.npnts = WAVEPOINTS; |
| 166 |
wh.hsA = 1e-6; |
| 167 |
wh.hsB = 0.0e0; |
| 168 |
wh.modDate = now; |
| 169 |
|
| 170 |
|
| 171 |
for (p = 0; p < WAVEPOINTS; p++) |
| 172 |
data[p] = (float)p; |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
memcpy(wh.wData, data, 16); |
| 177 |
|
| 178 |
|
| 179 |
strcpy(waveNote, "This is a test, this is only a test."); |
| 180 |
|
| 181 |
do { |
| 182 |
|
| 183 |
if (err = CPCreateFile(filePath, 1, 'IGR0', 'IGBW')) { |
| 184 |
printf("Error %d creating the file.\n", err); |
| 185 |
break; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
if (err = CPOpenFile(filePath, 1, &fr)) { |
| 190 |
printf("Error %d opening the file.\n", err); |
| 191 |
break; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
if (err = WriteVersion2NumericWave(fr, &wh, data, waveNote, strlen(waveNote))) |
| 196 |
printf("Error %d writing the file.\n", err); |
| 197 |
|
| 198 |
CPCloseFile(fr); |
| 199 |
} while(0); |
| 200 |
|
| 201 |
if (err == 0) |
| 202 |
printf("Successfully wrote the file %s.\n", filePath); |
| 203 |
|
| 204 |
return err; |
| 205 |
} |