Debian Codegraph @

biosig4c++/t210/sopen_nicolet.c

raw symbols SCIP
1 /*
2 sandbox is used for development and under constraction work
3 The functions here are either under construction or experimental.
4 The functions will be either fixed, then they are moved to another place;
5 or the functions are discarded. Do not rely on the interface in this function
6
7 Copyright (C) 2025 Alois Schloegl <[email protected]>
8 This file is part of the "BioSig for C/C++" repository
9 (biosig4c++) at http://biosig.sf.net/
10
11 BioSig is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License
13 as published by the Free Software Foundation; either version 3
14 of the License, or (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #define _GNU_SOURCE
26 #include <assert.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include <iconv.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "../biosig.h"
34 #define min(a,b) (((a) < (b)) ? (a) : (b))
35 #define max(a,b) (((a) > (b)) ? (a) : (b))
36
37 /*
38 guid_* are adapted from util-linux/uuid for the use of
39 windows-formated guid.
40 */
41
42 #ifndef UUID_DEFINED
43 typedef unsigned char uuid_t[16];
44 #endif
45
46 struct uuid {
47 uint32_t time_low;
48 uint16_t time_mid;
49 uint16_t time_hi_and_version;
50 uint16_t clock_seq;
51 uint8_t node[6];
52 };
53
54 int guid_parse_range(const char *in_start, const char *in_end, uuid_t uu) {
55 struct uuid uuid;
56 int i;
57 const char *cp;
58 char buf[3];
59
60 if ((in_end - in_start) != 36)
61 return -1;
62 for (i=0, cp = in_start; i < 36; i++,cp++) {
63 if ((i == 8) || (i == 13) || (i == 18) ||
64 (i == 23)) {
65 if (*cp == '-')
66 continue;
67 return -1;
68 }
69
70 if (!isxdigit(*cp))
71 return -1;
72 }
73 errno = 0;
74
75 cp = in_start;
76 buf[2] = 0;
77 for (i=0; i < 16; i++) {
78 buf[0] = *cp++;
79 buf[1] = *cp++;
80
81 errno = 0;
82 uuid.node[i] = strtoul(buf, NULL, 16);
83 if (errno)
84 return -1;
85 }
86
87 return 0;
88 }
89
90
91 /*
92 code list derived from
93 https://github.com/ieeg-portal/Nicolet-Reader/blob/master/%40NicoletFile/NicoletFile.m
94 */
95 const char* TagTable[] = {
96 "{A271CCCB-515D-4590-B6A1-DC170C8D6EE2}", "TSGUID",
97 "{8A19AA48-BEA0-40D5-B89F-667FC578D635}", "DERIVATIONGUID",
98 "{F824D60C-995E-4D94-9578-893C755ECB99}", "FILTERGUID",
99 "{02950361-35BB-4A22-9F0B-C78AAA5DB094}", "DISPLAYGUID",
100 "{8E94EF21-70F5-11D3-8F72-00105A9AFD56}", "FILEINFOGUID",
101 "{E4138BC0-7733-11D3-8685-0050044DAAB1}", "SRINFOGUID",
102 "{C728E565-E5A0-4419-93D2-F6CFC69F3B8F}", "EVENTTYPEINFOGUID",
103 "{D01B34A0-9DBD-11D3-93D3-00500400C148}", "AUDIOINFOGUID",
104 "{BF7C95EF-6C3B-4E70-9E11-779BFFF58EA7}", "CHANNELGUID",
105 "{2DEB82A1-D15F-4770-A4A4-CF03815F52DE}", "INPUTGUID",
106 "{5B036022-2EDC-465F-86EC-C0A4AB1A7A91}", "INPUTSETTINGSGUID",
107 "{99A636F2-51F7-4B9D-9569-C7D45058431A}", "PHOTICGUID",
108 "{55C5E044-5541-4594-9E35-5B3004EF7647}", "ERRORGUID",
109 "{223A3CA0-B5AC-43FB-B0A8-74CF8752BDBE}", "VIDEOGUID",
110 "{0623B545-38BE-4939-B9D0-55F5E241278D}", "DETECTIONPARAMSGUID",
111 "{CE06297D-D9D6-4E4B-8EAC-305EA1243EAB}", "PAGEGUID",
112 "{782B34E8-8E51-4BB9-9701-3227BB882A23}", "ACCINFOGUID",
113 "{3A6E8546-D144-4B55-A2C7-40DF579ED11E}", "RECCTRLGUID",
114 "{D046F2B0-5130-41B1-ABD7-38C12B32FAC3}", "GUID TRENDINFOGUID",
115 "{CBEBA8E6-1CDA-4509-B6C2-6AC2EA7DB8F8}", "HWINFOGUID",
116 "{E11C4CBA-0753-4655-A1E9-2B2309D1545B}", "VIDEOSYNCGUID",
117 "{B9344241-7AC1-42B5-BE9B-B7AFA16CBFA5}", "SLEEPSCOREINFOGUID",
118 "{15B41C32-0294-440E-ADFF-DD8B61C8B5AE}", "FOURIERSETTINGSGUID",
119 "{024FA81F-6A83-43C8-8C82-241A5501F0A1}", "SPECTRUMGUID",
120 "{8032E68A-EA3E-42E8-893E-6E93C59ED515}", "SIGNALINFOGUID",
121 "{30950D98-C39C-4352-AF3E-CB17D5B93DED}", "SENSORINFOGUID",
122 "{F5D39CD3-A340-4172-A1A3-78B2CDBCCB9F}", "DERIVEDSIGNALINFOGUID",
123 "{969FBB89-EE8E-4501-AD40-FB5A448BC4F9}", "ARTIFACTINFOGUID",
124 "{02948284-17EC-4538-A7FA-8E18BD65E167}", "STUDYINFOGUID",
125 "{D0B3FD0B-49D9-4BF0-8929-296DE5A55910}", "PATIENTINFOGUID",
126 "{7842FEF5-A686-459D-8196-769FC0AD99B3}", "DOCUMENTINFOGUID",
127 "{BCDAEE87-2496-4DF4-B07C-8B4E31E3C495}", "USERSINFOGUID",
128 "{B799F680-72A4-11D3-93D3-00500400C148}", "EVENTGUID",
129 "{AF2B3281-7FCE-11D2-B2DE-00104B6FC652}", "SHORTSAMPLESGUID",
130 "{89A091B3-972E-4DA2-9266-261B186302A9}", "DELAYLINESAMPLESGUID",
131 "{291E2381-B3B4-44D1-BB77-8CF5C24420D7}", "GENERALSAMPLESGUID",
132 "{5F11C628-FCCC-4FDD-B429-5EC94CB3AFEB}", "FILTERSAMPLESGUID",
133 "{728087F8-73E1-44D1-8882-C770976478A2}", "DATEXDATAGUID",
134 "{35F356D9-0F1C-4DFE-8286-D3DB3346FD75}", "TESTINFOGUID",
135 };
136
137 const char* infoProps[] = {
138 "patientID", "firstName", "middleName", "lastName",
139 "altID", "mothersMaidenName", "DOB", "DOD",
140 "street", "sexID", "phone", "notes",
141 "dominance", "siteID", "suffix", "prefix",
142 "degree", "apartment", "city", "state",
143 "country", "language", "height", "weight",
144 "race", "religion", "maritalStatus", ""
145 };
146
147
148 int guid_parse(const char *in, uuid_t uu) {
149 size_t len = strlen(in);
150 if (len != 36)
151 return -1;
152
153 return guid_parse_range(in, in + len, uu);
154 }
155
156 #ifdef __cplusplus
157 extern "C" {
158 #endif
159
160 int sopen_nicoletE_read(HDRTYPE* hdr) {
161 /* TODO: implement Nicolet-E format support
162 define all fields in hdr->....
163 currently only the first hdr->HeadLen bytes are stored in
164 hdr->AS.Header, all other fields still need to be defined.
165 */
166
167 const int LABELSIZE = 32;
168 const int TSLABELSIZE = 64;
169 const int UNITSIZE = 16;
170 const int ITEMNAMESIZE = 64;
171
172 if (VERBOSE_LEVEL > 8) fprintf(stderr,"%s line %ld: NICOLET:E\n",__FILE__,__LINE__);
173 // Nicolet_E
174 uint16_t gdftyp=3; // int16_t
175 size_t pos=0;
176 size_t count = hdr->HeadLen;
177 while (!ifeof(hdr)) {
178 hdr->AS.Header = (uint8_t*)realloc(hdr->AS.Header, count*2 + 1);
179 count += ifread(hdr->AS.Header+count, 1, count, hdr);
180 }
181 hdr->HeadLen=count;
182 hdr->AS.Header[count]=0;
183 hdr->NS=0;
184
185 fprintf(stderr,"%s line %d: NICOLET:E\n",__FILE__,__LINE__);
186 fflush(stdout);
187
188 typedef struct {
189 uint16_t index[2];
190 uint32_t misc1;
191 uint32_t indexIdx;
192 uint32_t misc2[3];
193 uint32_t sectionIdx;
194 uint32_t misc3;
195 uint32_t offset;
196 uint32_t blockL;
197 uint32_t dataL;
198 } Qindex_t;
199
200 typedef struct {
201 uint64_t sectionIdx;
202 uint64_t offset;
203 uint32_t blockL;
204 uint32_t sectionL;
205 } NicoletIndex_t;
206 NicoletIndex_t *NicoletIndex = NULL;
207
208 typedef struct {
209 gdf_time gdfTime;
210 gdf_time gdfTime2;
211 uint64_t internalOffset;
212 uint32_t packetSize;
213 char* IDStr;
214 uint8_t *data;
215 size_t datasize;
216 } dynamicPacket_t;
217
218 // format definition obtained from https://github.com/ieeg-portal/Nicolet-Reader
219 hdr->FILE.LittleEndian = 1; // default little endian
220 uint64_t indexIdx = leu32p(hdr->AS.Header+6*4);
221 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s line %d: NICOLET:E # Get tags and datastructure\n",__FILE__,__LINE__);
222
223 // Get TAGS structure and Channel IDS
224 int32_t nrTags = lei32p(hdr->AS.Header+172);
225
226 char** Taglist = (char**)alloca(nrTags * sizeof(char*));
227
228 iconv_t CD=iconv_open("UTF8", "UTF-16LE");
229 char BUFFER[(LABELSIZE+TSLABELSIZE+UNITSIZE+ITEMNAMESIZE)*2];
230 for (size_t k=0; k<nrTags; k++) {
231 size_t outbytesleft = sizeof(BUFFER);
232 char *tag=BUFFER;
233 char *wctag = hdr->AS.Header + 176 + k*84;
234 uint32_t tagIndex = leu32p(hdr->AS.Header + 176 + 80 + k*84);
235 size_t inbytesleft = 80;
236 size_t ret=iconv(CD, &wctag, &inbytesleft, &tag, &outbytesleft);
237
238 if (k!=tagIndex) {
239 biosigERROR(hdr, B4C_DATATYPE_UNSUPPORTED, "Error SOPEN(Nicolet_E): invalid tagIndex - file seems corrupted");
240 }
241
242 Taglist[k] = (BUFFER[0]=='{') ? "UNKNOWN" : NULL;
243 const int TL = (sizeof(TagTable)/sizeof(char*));
244 for (int l=0; l < TL ; l+=2) {
245 if (!strcmp(BUFFER, TagTable[l])) {
246 Taglist[k] = TagTable[l+1];
247 break;
248 }
249 }
250 if (Taglist[k]==NULL) {
251 Taglist[k] = alloca(strlen(BUFFER)+1);
252 strcpy(Taglist[k], BUFFER);
253 }
254 if (VERBOSE_LEVEL>7) fprintf(stdout, "%s (line %d) NICOLET_E: %d/%d #%d tag: <%s> <%s> %ld\n", __FILE__, __LINE__, k, nrTags, tagIndex, BUFFER, Taglist[k], ret);
255
256 }
257
258 if (VERBOSE_LEVEL > 7) fprintf(stdout,"\n#------- %s line %d: NICOLET:E\n",__FILE__,__LINE__);
259
260 // QI index
261 pos=172208;
262 uint32_t Qi_nrEntries = leu32p(hdr->AS.Header+pos);
263 uint32_t Qi_misc1 = leu32p(hdr->AS.Header+pos+4);
264 uint32_t Qi_indexIdx = leu32p(hdr->AS.Header+pos+8);
265 uint32_t Qi_misc3 = leu32p(hdr->AS.Header+pos+12);
266 uint32_t Qi_LQi = leu64p(hdr->AS.Header+pos+16);
267 uint32_t Qi_firstIdx = leu64p(hdr->AS.Header+pos+24);
268
269 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s line %d: NICOLET:E\n",__FILE__,__LINE__);
270 Qindex_t *Qindex = (Qindex_t*)(hdr->AS.Header+188664);
271
272 // TODO: do we need Qindex, or can we omit it ? If needed, Endianity conversion of Qindex->FIELDS
273 for (size_t k=0; k<Qi_LQi; k++) {
274 // offset 48
275 }
276
277 // Get Main Index
278 if (VERBOSE_LEVEL > 7) fprintf(stdout,"\n-------- %s line %d: NICOLET:E get Main index indexIdx=%ld,%ld\n",__FILE__,__LINE__,indexIdx,Qi_nrEntries);
279 NicoletIndex = (NicoletIndex_t*)alloca(Qi_nrEntries*sizeof(NicoletIndex_t));
280 uint64_t nextIndexPointer = indexIdx;
281 int curIdx=0;
282 int curIdx2=1; // TODO: can be omitted
283
284 uint64_t *spr=NULL; // spr[i] number of samples of sections (i.e. sum of sectionL) with sectionIdx==i,
285 uint64_t *nsi=NULL; // nsi[i] number of sections with sectionIdx==i
286 uint64_t numSectionType=0;
287 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s line %d: NICOLET:E %d %d next:%ld %ld\n",__FILE__,__LINE__,curIdx, Qi_nrEntries,nextIndexPointer,count);
288 while (curIdx < Qi_nrEntries) {
289 size_t nrIdx = leu64p(hdr->AS.Header+nextIndexPointer);
290 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s line %d: NICOLET:E nrIdx=%d next=%lx\n",__FILE__,__LINE__, nrIdx, nextIndexPointer);
291 for (size_t k=0; k < nrIdx; k++, curIdx++) {
292 uint64_t ns = leu64p(hdr->AS.Header + nextIndexPointer + k*24 + 8);
293 NicoletIndex[curIdx].sectionIdx = ns;
294 NicoletIndex[curIdx].offset = leu64p(hdr->AS.Header + nextIndexPointer + k*24 + 16);
295 NicoletIndex[curIdx].blockL = leu32p(hdr->AS.Header + nextIndexPointer + k*24 + 24);
296 NicoletIndex[curIdx].sectionL = leu32p(hdr->AS.Header + nextIndexPointer + k*24 + 28);
297
298 if (VERBOSE_LEVEL > 8) fprintf(stdout,"%s line %d: NICOLET:E %d %ld %ld| %9ld %9ld %9d %9d \n", __FILE__, __LINE__, curIdx, k, ns,
299 NicoletIndex[curIdx].sectionIdx, NicoletIndex[curIdx].offset, NicoletIndex[curIdx].blockL, NicoletIndex[curIdx].sectionL);
300
301 if (numSectionType < (ns+1)) {
302 //fprintf(stdout,"%s line %d: NICOLET:E ns=%d,%ld\n",__FILE__, __LINE__, hdr->NS,ns);
303 spr = realloc(spr, (ns+1)*sizeof(uint64_t));
304 nsi = realloc(nsi, (ns+1)*sizeof(uint64_t));
305 for (size_t k=numSectionType; k<=ns; k++) {
306 spr[k]=0;
307 nsi[k]=0;
308 }
309 numSectionType = ns;
310 }
311 spr[ns]+= NicoletIndex[curIdx].sectionL;
312 nsi[ns]++;
313 };
314 nextIndexPointer = leu64p(hdr->AS.Header + nextIndexPointer + nrIdx*24);
315 curIdx2++;
316 }
317
318 #if 1
319 if (VERBOSE_LEVEL > 7)
320 for (size_t k=0; k < numSectionType; k++) {
321 fprintf(stdout, "#%d spr/nsi=%lu/%lu\n", k, spr[k], nsi[k]);
322 }
323 #endif
324
325
326 for (int k=0; k<Qi_nrEntries; k++) {
327 if (!strcmp(Taglist[NicoletIndex[k].sectionIdx],"InfoChangeStream")) {
328 curIdx=k;
329 break;
330 }
331 }
332
333 // dynamic packages
334 int nrDynamicPackets = NicoletIndex[curIdx].sectionL / 48;
335 pos = NicoletIndex[curIdx].offset;
336 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s line %d: NICOLET:E DynPKG: %d %d 0x%0x \n",__FILE__,__LINE__,nrDynamicPackets,pos,pos);
337
338 dynamicPacket_t *dynamicPacket = alloca(nrDynamicPackets * sizeof(dynamicPacket_t));
339 for (size_t k=0; k < nrDynamicPackets; k++) {
340 uint8_t *ptr = hdr->AS.Header + pos + k*48;
341
342 dynamicPacket[k].gdfTime = t_time2gdf_time((lef64p(ptr+16) + lef64p(ptr+24) - 25569) * 24 * 3600);
343 dynamicPacket[k].gdfTime2 = ldexp(lef64p(ptr+16) + lef64p(ptr+24) + 693961,32);
344 dynamicPacket[k].internalOffset = leu64p(ptr+32);
345 dynamicPacket[k].packetSize = lef64p(ptr+40);
346 dynamicPacket[k].data = NULL;
347 dynamicPacket[k].datasize = 0;
348 dynamicPacket[k].IDStr = NULL;
349
350 uint8_t *guid = ptr;
351 char BUFFER[40];
352 snprintf(BUFFER, sizeof(BUFFER), "{%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
353 guid[3],guid[2],guid[1],guid[0], guid[5],guid[4],guid[7],guid[6],
354 guid[8],guid[9],guid[10],guid[11], guid[12],guid[13],guid[14],guid[15]);
355
356 const int TL = (sizeof(TagTable)/sizeof(char*));
357 for (int j=0; j < TL ; j+=2) {
358 if (!strcmp(BUFFER, TagTable[j])) {
359 dynamicPacket[k].IDStr = TagTable[j+1];
360 break;
361 }
362 }
363 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s line %d: k=%d ptr=<%s> <%s>\n",__FILE__,__LINE__,k,BUFFER, dynamicPacket[k].IDStr);
364 }
365
366
367 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s line %d: %s(...)\n",__FILE__,__LINE__,__func__);
368
369 for (size_t k=0; k < nrDynamicPackets; k++) {
370
371 if (dynamicPacket[k].IDStr==NULL) continue;
372
373 // const char *BUFFER=dynamicPacket[k].IDStr; //
374 const char *BUFFER=dynamicPacket[k].IDStr; // this is equialent to dynamicPacket[k].IDStr
375
376 size_t internalOffset = 0;
377 size_t remainingDataToRead = dynamicPacket[k].packetSize;
378 size_t currentTargetStart = dynamicPacket[k].internalOffset;
379 for (int j = 0; j < Qi_nrEntries; j++) {
380 if (k==NicoletIndex[j].sectionIdx) {
381 NicoletIndex_t *currentInstance = NicoletIndex+j;
382 if ((internalOffset <= currentTargetStart) && ((internalOffset+currentInstance->sectionL) >= currentTargetStart)) {
383
384 size_t startAt = currentTargetStart;
385 size_t stopAt = min(startAt+remainingDataToRead, internalOffset+currentInstance->sectionL);
386 size_t readLength = stopAt-startAt;
387
388 size_t filePosStart = currentInstance->offset + startAt - internalOffset;
389 uint8_t *dataPart = hdr->AS.Header+filePosStart;
390 dynamicPacket[k].data = realloc(dynamicPacket[k].data, dynamicPacket[k].datasize + readLength);
391 memcpy(dynamicPacket[k].data + dynamicPacket[k].datasize, dataPart, readLength);
392 dynamicPacket[k].datasize += readLength;
393
394 remainingDataToRead -= readLength;
395 currentTargetStart += readLength;
396 }
397 internalOffset += currentInstance->sectionL;
398 }
399 }
400 }
401
402 curIdx=-1;
403 for (int k=0; k<Qi_nrEntries; k++) {
404 if (!strcmp(Taglist[NicoletIndex[k].sectionIdx],"PATIENTINFOGUID")) {
405 curIdx=k;
406 break;
407 }
408 }
409 if (curIdx != -1) {
410 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s line %d: %s(...) idx=%d \n",__FILE__,__LINE__,__func__,curIdx);
411 pos = NicoletIndex[curIdx].offset;
412 NicoletIndex_t *currentInstance = NicoletIndex+curIdx;
413 uint8_t* ptr = hdr->AS.Header + pos;
414 uint8_t *guid16 = ptr;
415 ptr += 16;
416 uint64_t lSection = leu64p(ptr);
417 ptr += 8;
418 // uint64_t reserved = leu64_t(ptr);
419 //ptr += 6;
420 uint64_t nrValues = leu64p(ptr);
421 ptr += 8;
422 uint64_t nrBstr = leu64p(ptr);
423 ptr += 8;
424 for (size_t k=0; k < nrValues; k++) {
425
426 uint64_t id = leu64p(ptr);
427 ptr += 8;
428 double value = lef64p(ptr);
429 switch (id) {
430 case 7: // DOB
431 case 8: // DOD
432 case 23: // height
433 case 24: // weight
434 ptr += 8;
435 };
436
437 switch (id) {
438 case 7: // DOB
439 hdr->Patient.Birthday = t_time2gdf_time((value - 25569) * 24 * 3600);
440 break;
441 case 8: // DOD
442 hdr->T0 = t_time2gdf_time((value - 25569) * 24 * 3600);
443 break;
444 case 23: // height
445 hdr->Patient.Height = value;
446 break;
447 case 24: // weight
448 hdr->Patient.Weight = value;
449 break;
450 };
451 }
452
453 // strSetup = fread(h,nrBstr*2,'uint64');
454 // ptr += nrBstr*2*sizeof(uint64_t);
455 char* pos2 = ptr + nrBstr * 16;
456 char* name1 = NULL;
457 char* name2 = NULL;
458 char* name3 = NULL;
459 for (size_t k=0; k < 2*nrBstr; k+=2) {
460 uint64_t id = leu64p(ptr+k*8);
461 uint64_t len = (leu64p(ptr+k*8+8)+1)*2;
462 size_t inbytesleft = len;
463
464 size_t outbytesleft = ITEMNAMESIZE*2+1;
465 uint8_t BUFFER[ITEMNAMESIZE*2+1];
466 char* ptr2=BUFFER;
467 size_t ret=iconv(CD, &pos2, &inbytesleft, &ptr2, &outbytesleft);
468
469 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s line %d: %s(...) %d <%s><%s><%s>\n",__FILE__,__LINE__,__func__, id, name1, name2, name3 );
470
471 switch (id) {
472 case 1: // patientID
473 strncpy(hdr->Patient.Id, BUFFER, MAX_LENGTH_PID);
474 hdr->Patient.Id[MAX_LENGTH_PID]=0;
475 break;
476 case 2: // first name
477 // FIXME
478 name1 = alloca(strlen(BUFFER)+1);
479 strcpy(name1,BUFFER);
480 break;
481 case 3: // middle name
482 // FIXME
483 name2 = alloca(strlen(BUFFER)+1);
484 strcpy(name2,BUFFER);
485 break;
486 case 4: // last name
487 // FIXME
488 name3 = alloca(strlen(BUFFER)+1);
489 strcpy(name3,BUFFER);
490 break;
491 case 10: // Sex
492 hdr->Patient.Sex = 0; // FIXME
493 break;
494 }
495 pos2 += len;
496 }
497 // FIXME
498 snprintf(hdr->Patient.Name, MAX_LENGTH_NAME+1,"%s\x1f%s\x01f%s\0",name3,name1,name2);
499 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s line %d: %s(...)\n",__FILE__,__LINE__,__func__);
500 }
501
502 curIdx=-1;
503 for (int k=0; k<Qi_nrEntries; k++) {
504 if (!strcmp(Taglist[NicoletIndex[k].sectionIdx],"InfoGuids")) {
505 curIdx=k;
506 break;
507 }
508 }
509 if (curIdx != -1) {
510 NicoletIndex_t *currentInstance = NicoletIndex+curIdx;
511 uint8_t* ptr = hdr->AS.Header + NicoletIndex[curIdx].offset;
512 // ignored
513 }
514
515 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s line %d: %s(...)\n",__FILE__,__LINE__,__func__);
516
517 curIdx = -1;
518 for (int k=0; k < nrTags; k++) {
519 if (!strcmp(Taglist[k],"SIGNALINFOGUID")) {
520 curIdx=k;
521 break;
522 }
523 }
524 if (curIdx != -1) {
525 // %% Get SignalInfo (SIGNALINFOGUID): One per file
526 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s line %d: %s(...) %d\n",__FILE__,__LINE__,__func__,curIdx);
527 pos = NicoletIndex[curIdx].offset;
528 pos = NicoletIndex[curIdx].offset+16+ITEMNAMESIZE+152+512;
529 hdr->NS = leu16p(hdr->AS.Header + pos);
530 uint32_t nrIdx = leu32p(hdr->AS.Header + pos);
531
532 if (hdr->NS != nrIdx) biosigERROR(hdr, B4C_DATATYPE_UNSUPPORTED, "Error SOPEN(Nicolet_E): unexpected/unsupported values - possible file corruption ");
533
534 if (VERBOSE_LEVEL > 7) fprintf(stdout,"# -------- %s line %d: NICOLET:E get SignalInfo idx=%d NS=%d 0x%016x\n",__FILE__,__LINE__,curIdx,hdr->NS, nrIdx);
535 hdr->CHANNEL = (CHANNEL_TYPE*) realloc(hdr->CHANNEL,hdr->NS * sizeof(CHANNEL_TYPE));
536
537 pos = NicoletIndex[curIdx].offset+16+ITEMNAMESIZE+152+512+4*2;
538 for (int ch=0; ch < hdr->NS; ch++) {
539 CHANNEL_TYPE *hc = hdr->CHANNEL+ch;
540 hc->GDFTYP=0;
541 // TODO: initialize all fields
542
543 size_t pos2 = pos + ch*(512-64-8); //(LABELSIZE+UNITSIZE+16+4*4+256);
544 char *wctag = hdr->AS.Header + pos2;
545 size_t inbytesleft = LABELSIZE*2;
546 size_t outbytesleft = MAX_LENGTH_LABEL+1;
547 char *tag = hc->Label;
548 size_t ret=iconv(CD, &wctag, &inbytesleft, &tag, &outbytesleft);
549 *tag=0;
550
551 wctag = hdr->AS.Header + pos2 + LABELSIZE*2; //(LABELSIZE+UNITSIZE+16+4*4+256);
552 outbytesleft = MAX_LENGTH_TRANSDUCER+1;
553 tag = hc->Transducer;
554 inbytesleft = UNITSIZE*2;
555 ret=iconv(CD, &wctag, &inbytesleft, &tag, &outbytesleft);
556 *tag=0;
557
558 hc->LowPass = leu32p(hdr->AS.Header + pos2 + LABELSIZE*2+UNITSIZE*2+16*2+4);
559 hc->HighPass = leu32p(hdr->AS.Header + pos2 + LABELSIZE*2+UNITSIZE*2+16*2+4+4);
560
561 // FIXME
562 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s line %d: NICOLET:E @%08x #%-5d/%d <%s>:<%s> filter:[%g,%g]\n",__FILE__,__LINE__,pos,ch,hdr->NS,hc->Label,hc->Transducer,hc->LowPass,hc->HighPass);
563 }
564 }
565
566 for (int k=0; k<Qi_nrEntries; k++) {
567 if (strcmp(Taglist[NicoletIndex[k].sectionIdx],"CHANNELGUID")) continue;
568 curIdx=k;
569 // Get CHANNELINFO (CHANNELGUID)
570 pos = NicoletIndex[curIdx].offset;
571 // guid
572 pos = NicoletIndex[curIdx].offset+16+ITEMNAMESIZE+152+16+16+488;
573 //TODO: check pos = NicoletIndex[idxSIGNALINFOGUID].offset+16+ITEMNAMESIZE*2+152+16+16+488;
574 uint32_t nrIdx1 = leu32p(hdr->AS.Header + pos);
575 uint32_t nrIdx2 = leu32p(hdr->AS.Header + pos + 4);
576
577 if (VERBOSE_LEVEL > 7) fprintf(stdout,"# --------- %s line %d: NS=%d %d %d \n",__FILE__,__LINE__,hdr->NS,nrIdx1,nrIdx2);
578
579 // TODO: check handling of disabled channels, see max(hdr->NS,nrIdx2), bOn, etc.
580 hdr->CHANNEL = (CHANNEL_TYPE*) realloc(hdr->CHANNEL,max(hdr->NS,nrIdx2)*sizeof(CHANNEL_TYPE));
581 pos += 8;
582 for (int ch=0; ch<nrIdx2; ch++) {
583 CHANNEL_TYPE *hc = hdr->CHANNEL+ch;
584
585 char *wctag = hdr->AS.Header + pos;
586 size_t inbytesleft = LABELSIZE*2;
587 size_t outbytesleft = MAX_LENGTH_LABEL+1;
588 char *tag = hc->Label;
589 size_t ret=iconv(CD, &wctag, &inbytesleft, &tag, &outbytesleft);
590 *tag=0;
591
592 double Fs = lef64p(hdr->AS.Header + pos + LABELSIZE*2);
593 uint32_t bOn = leu32p(hdr->AS.Header + pos + LABELSIZE*2+8);
594 hc->OnOff = bOn>0;
595
596 uint32_t lInputID = leu32p(hdr->AS.Header + pos + LABELSIZE*2+8+4);
597 uint32_t lInputSettingID = leu32p(hdr->AS.Header + pos + LABELSIZE*2+8+8);
598
599 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s line %d: NICOLET:E @%08x #%d/%d %dHz <%s> [%d,%d,%d]\n",__FILE__,__LINE__,pos,ch,hdr->NS,Fs,hc->Label,hc->Transducer,bOn, lInputID, lInputSettingID);
600 pos += LABELSIZE*2+8+4*4+128;
601 }
602 }
603
604 curIdx=-1;
605 for (size_t k=0; k < nrDynamicPackets; k++) {
606 if (strcmp("TSGUID", dynamicPacket[k].IDStr)) continue;
607 dynamicPacket_t* tsPacket = dynamicPacket + k;
608 uint32_t elems = leu32p(tsPacket->data+752);
609 uint32_t alloc = leu32p(tsPacket->data+756);
610 uint32_t offset = 760;
611
612 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s line %d: %s(...) %d %d TSGUID\n",__FILE__,__LINE__,__func__,k,elems);
613
614 for (uint32_t i=0; i<elems; i++, offset+=552) {
615 uint32_t internalOffset = offset;
616
617 char *inbuf;
618 char *outbuf;
619 size_t inbytesleft,outbytesleft;
620
621 inbuf=tsPacket->data+internalOffset; inbytesleft = TSLABELSIZE*2;
622 outbuf=BUFFER; outbytesleft = sizeof(BUFFER);
623 size_t ret=iconv(CD, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
624 char *label = alloca(strlen(BUFFER)+1);
625 strcpy(label,BUFFER);
626
627 internalOffset += TSLABELSIZE*2;
628 inbuf=tsPacket->data+internalOffset; inbytesleft = LABELSIZE*2;
629 outbuf=BUFFER; outbytesleft = sizeof(BUFFER);
630 ret=iconv(CD, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
631 char *activeSensor = alloca(strlen(BUFFER)+1);
632 strcpy(label,BUFFER);
633
634 internalOffset += LABELSIZE*2;
635 inbuf=tsPacket->data+internalOffset; inbytesleft = 8*2;
636 outbuf=BUFFER; outbytesleft = sizeof(BUFFER);
637 ret=iconv(CD, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
638 char *refSensor = alloca(strlen(BUFFER)+1);
639 strcpy(label,BUFFER);
640
641 internalOffset += 8+56;
642 double dLowCut = lef64p(tsPacket->data+offset+internalOffset);
643 double dHighCut = lef64p(tsPacket->data+offset+internalOffset+8);
644 double dSamplingRate = lef64p(tsPacket->data+offset+internalOffset+16);
645 double dResolution = lef64p(tsPacket->data+offset+internalOffset+24);
646 double bMark = leu16p(tsPacket->data+offset+internalOffset+32);
647 double bNotch = leu16p(tsPacket->data+offset+internalOffset+34);
648 double dEegOffset = lef64p(tsPacket->data+offset+internalOffset+36);
649
650 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s line %d: %s(...) elem %d/%d Label:%s A:%s Ref:%s [%g,%g] %gHz Cal:%g Off:%g M:%d Notch:%d\n",
651 __FILE__, __LINE__, __func__, i, elems, label, activeSensor, refSensor,
652 dLowCut, dHighCut, dSamplingRate, dResolution, dEegOffset, bMark, bNotch);
653 }
654 }
655
656 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s line %d: %s(...)\n",__FILE__,__LINE__,__func__);
657
658 curIdx=-1;
659 for (int k=0; k<Qi_nrEntries; k++) {
660 if (!strcmp(Taglist[NicoletIndex[k].sectionIdx],"SegmentStream")) {
661 curIdx=k;
662 break;
663 }
664 }
665 if (curIdx != -1) {
666 size_t nrSegments = NicoletIndex[curIdx].sectionL/152;
667
668 size_t N = hdr->EVENT.N + nrSegments - 1;
669 hdr->EVENT.POS = realloc(hdr->EVENT.POS, N * sizeof(hdr->EVENT.POS[0]));
670 hdr->EVENT.TYP = realloc(hdr->EVENT.TYP, N * sizeof(hdr->EVENT.TYP[0]));
671 hdr->EVENT.CHN = realloc(hdr->EVENT.CHN, N * sizeof(hdr->EVENT.CHN[0]));
672 hdr->EVENT.DUR = realloc(hdr->EVENT.DUR, N * sizeof(hdr->EVENT.DUR[0]));
673 hdr->EVENT.TimeStamp= realloc(hdr->EVENT.TimeStamp, N * sizeof(hdr->EVENT.TimeStamp[0]));
674
675 pos = NicoletIndex[curIdx].offset;
676 for (size_t i = 0; i<nrSegments; i++) {
677 double dateOLE = lef64p(hdr->AS.Header + pos + i*152);
678 double duration = lef64p(hdr->AS.Header + pos + i*152 + 16);
679
680 gdf_time TimeStamp = t_time2gdf_time( (dateOLE - 25569) * 24 * 3600);
681 if (i==0)
682 hdr->T0 = TimeStamp;
683 else {
684 size_t n = hdr->EVENT.N + i - 1;
685 hdr->EVENT.TimeStamp[n] = TimeStamp;
686 hdr->EVENT.CHN[n] = 0;
687 hdr->EVENT.TYP[n] = 0x7ffe;
688 hdr->EVENT.DUR[n] = duration; // TODO: convert to samples
689 hdr->EVENT.POS[n] = 0; // TODO: get samples
690 }
691 }
692 }
693
694 curIdx=-1;
695 for (int k=0; k<Qi_nrEntries; k++) {
696 if (!strcmp(Taglist[NicoletIndex[k].sectionIdx],"Events")) {
697 curIdx=k;
698 break;
699 }
700 }
701 if (curIdx != -1) {
702 size_t nrSegments = NicoletIndex[curIdx].sectionL/152;
703 // TODO: get evnets
704 }
705
706 // Get montage - TODO
707 // there is more TODO
708
709 if (VERBOSE_LEVEL > 7) fprintf(stdout,"%s line %d: NICOLET:E --- parsing has stopped here \n",__FILE__,__LINE__);
710
711 iconv_close(CD);
712 if (spr) free(spr);
713 if (nsi) free(nsi);
714 for (size_t k=0; k < nrDynamicPackets; k++) {
715 uint8_t* ptr = dynamicPacket[k].data;
716 if (ptr) free(ptr);
717 }
718
719 biosigERROR(hdr, B4C_DATATYPE_UNSUPPORTED, "Error SOPEN(Nicolet_E):not supported (yet)");
720 return 0;
721 }
722
723 #ifdef __cplusplus
724 }
725 #endif
726