| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <stdlib.h> |
| 7 |
#include <string.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include <stddef.h> |
| 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 |
|
| 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 int |
| 36 |
WriteDemoVariablesRecord(CP_FILE_REF fr) |
| 37 |
{ |
| 38 |
unsigned long recordStartPos; |
| 39 |
char temp[256]; |
| 40 |
int err; |
| 41 |
|
| 42 |
if (err = CPGetFilePosition(fr, &recordStartPos)) |
| 43 |
return err; |
| 44 |
|
| 45 |
|
| 46 |
if (err = WriteRecordHeader(fr, kVariablesRecord, 0, 0)) |
| 47 |
return err; |
| 48 |
|
| 49 |
|
| 50 |
if (err = WriteVariablesHeader(fr, 0, 1, 1, 0, 0)) |
| 51 |
return err; |
| 52 |
|
| 53 |
|
| 54 |
if (err = WriteNumericVar(fr, "userNumVar", 0, 1234.0, 0.0)) |
| 55 |
return err; |
| 56 |
|
| 57 |
|
| 58 |
strcpy(temp, "This is a string variable."); |
| 59 |
if (err = WriteStringVar(fr, "userStrVar", temp, strlen(temp))) |
| 60 |
return err; |
| 61 |
|
| 62 |
|
| 63 |
if (err = SetRecordHeaderNumDataBytesField(fr, recordStartPos)) |
| 64 |
return err; |
| 65 |
|
| 66 |
return 0; |
| 67 |
} |
| 68 |
|
| 69 |
static void |
| 70 |
SetDemoWaveDataContents(float* fp, long numPoints) |
| 71 |
{ |
| 72 |
float val; |
| 73 |
|
| 74 |
val = 0.0; |
| 75 |
while(numPoints > 0) { |
| 76 |
*fp++ = val; |
| 77 |
val += 1.0; |
| 78 |
numPoints -= 1; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
static int |
| 83 |
WriteDemoWave1DNumericWaveRecord(CP_FILE_REF fr) |
| 84 |
{ |
| 85 |
WaveHeader2 wh; |
| 86 |
float* data; |
| 87 |
unsigned long recordStartPos; |
| 88 |
unsigned long creationDate; |
| 89 |
long waveMainDataSize, noteSize; |
| 90 |
char note[256]; |
| 91 |
int err; |
| 92 |
|
| 93 |
if (err = CPGetFilePosition(fr, &recordStartPos)) |
| 94 |
return err; |
| 95 |
|
| 96 |
|
| 97 |
if (err = WriteRecordHeader(fr, kWaveRecord, 0, 0)) |
| 98 |
return err; |
| 99 |
|
| 100 |
|
| 101 |
#ifdef WIN32 |
| 102 |
creationDate = 0; |
| 103 |
#else |
| 104 |
GetDateTime(&creationDate); |
| 105 |
#endif |
| 106 |
|
| 107 |
|
| 108 |
waveMainDataSize = DEMO_WAVE_LENGTH * sizeof(float); |
| 109 |
data = malloc(waveMainDataSize); |
| 110 |
if (data == NULL) |
| 111 |
return NOMEM; |
| 112 |
SetDemoWaveDataContents(data, DEMO_WAVE_LENGTH); |
| 113 |
|
| 114 |
|
| 115 |
InitWaveHeader2(&wh, "wave0", creationDate, DEMO_WAVE_LENGTH, NT_FP32); |
| 116 |
|
| 117 |
|
| 118 |
wh.hsA = 0.2; |
| 119 |
wh.hsB = 0.0; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
{ |
| 126 |
int tmp = 16; |
| 127 |
if (waveMainDataSize < tmp) |
| 128 |
tmp = waveMainDataSize; |
| 129 |
memcpy(&wh.wData, data, tmp); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
strcpy(note, "This is wave0."); |
| 134 |
noteSize = strlen(note); |
| 135 |
|
| 136 |
|
| 137 |
err = WriteVersion2NumericWave(fr, &wh, data, note, strlen(note)); |
| 138 |
free(data); |
| 139 |
if (err) |
| 140 |
return err; |
| 141 |
|
| 142 |
|
| 143 |
if (err = SetRecordHeaderNumDataBytesField(fr, recordStartPos)) |
| 144 |
return err; |
| 145 |
|
| 146 |
return 0; |
| 147 |
} |
| 148 |
|
| 149 |
static int |
| 150 |
WriteDemoRecreationProcedures(CP_FILE_REF fr) |
| 151 |
{ |
| 152 |
char text[1024]; |
| 153 |
|
| 154 |
|
| 155 |
strcpy(text, "Table0()"CR_STR); |
| 156 |
strcat(text, CR_STR); |
| 157 |
|
| 158 |
|
| 159 |
strcat(text, "Window Table0() : Table"CR_STR); |
| 160 |
strcat(text, "\tPauseUpdate; Silent 1 | building window..."CR_STR); |
| 161 |
strcat(text, "\tEdit wave0"CR_STR); |
| 162 |
strcat(text, "End"CR_STR); |
| 163 |
|
| 164 |
return WritePlainTextRecord(fr, kRecreationRecord, text, strlen(text)); |
| 165 |
} |
| 166 |
|
| 167 |
static int |
| 168 |
WriteDemoProcedureWindowProcedures(CP_FILE_REF fr) |
| 169 |
{ |
| 170 |
char text[1024]; |
| 171 |
|
| 172 |
|
| 173 |
strcpy(text, "Macro Demo()"CR_STR); |
| 174 |
strcat(text, "\tDoAlert 0, \"Greetings!\""CR_STR); |
| 175 |
strcat(text, "End"CR_STR); |
| 176 |
|
| 177 |
return WritePlainTextRecord(fr, kProcedureRecord, text, strlen(text)); |
| 178 |
} |
| 179 |
|
| 180 |
static int |
| 181 |
WriteDemoHistory(CP_FILE_REF fr) |
| 182 |
{ |
| 183 |
char text[1024]; |
| 184 |
int err; |
| 185 |
|
| 186 |
|
| 187 |
strcpy(text, "// This experiment was created using Igor Pro Tech Note PTN003"CR_STR); |
| 188 |
strcat(text, "// This tech note shows how to write a packed Igor Pro 3.0 file from your own program."CR_STR); |
| 189 |
|
| 190 |
if (err = WritePlainTextRecord(fr, kHistoryRecord, text, strlen(text))) |
| 191 |
return err; |
| 192 |
return WriteRecordHeader(fr, kGetHistoryRecord, 0, 0); |
| 193 |
} |
| 194 |
|
| 195 |
static int |
| 196 |
WriteDemoPackedFile(CP_FILE_REF fr) |
| 197 |
{ |
| 198 |
int err; |
| 199 |
|
| 200 |
if (err = WriteDemoVariablesRecord(fr)) |
| 201 |
return err; |
| 202 |
if (err = WriteDemoWave1DNumericWaveRecord(fr)) |
| 203 |
return err; |
| 204 |
if (err = WriteDemoRecreationProcedures(fr)) |
| 205 |
return err; |
| 206 |
if (err = WriteDemoProcedureWindowProcedures(fr)) |
| 207 |
return err; |
| 208 |
if (err = WriteDemoHistory(fr)) |
| 209 |
return err; |
| 210 |
return 0; |
| 211 |
} |
| 212 |
|
| 213 |
int |
| 214 |
WritePackedFile(const char* packedFilePath) |
| 215 |
{ |
| 216 |
CP_FILE_REF fr; |
| 217 |
char temp[256]; |
| 218 |
int err; |
| 219 |
|
| 220 |
|
| 221 |
if (err = CPCreateFile(packedFilePath, 1, IGOR_CREATOR_CODE, IGOR_PACKED_FILE_TYPE)) { |
| 222 |
PrintResult("Create file error", err); |
| 223 |
return err; |
| 224 |
} |
| 225 |
|
| 226 |
if (err = CPOpenFile(packedFilePath, 1, &fr)) { |
| 227 |
PrintResult("Open file error", err); |
| 228 |
return err; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
if (err = WriteDemoPackedFile(fr)) { |
| 233 |
PrintResult("WritePackedFile error", err); |
| 234 |
return err; |
| 235 |
} |
| 236 |
|
| 237 |
if (err = CPCloseFile(fr)) { |
| 238 |
PrintResult("Close file error", err); |
| 239 |
return err; |
| 240 |
} |
| 241 |
|
| 242 |
sprintf(temp, "\"%s\" written successfully.", packedFilePath); |
| 243 |
PrintResult(temp, 0); |
| 244 |
|
| 245 |
return 0; |
| 246 |
} |