/
parseNml.c
executable file
·1544 lines (1408 loc) · 65.3 KB
/
parseNml.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* *************************************************************************************************** */
/* parseNML.c NML parser for Knossos and Oxalis */
/* Copyright 2013 Max Planck Institute of Neurobiology, Martinsried */
/* Version 0.11 Martin Zauser */
/* *************************************************************************************************** */
/* 26.07.2013 V0.10 MAX_NUMBER_OF_COMMENTS increased from 2000 to 8000 because iris tracing is ~3000 */
/* parseNML XML-Parser */
#include "mex.h"
#include "stdio.h"
#include "time.h"
#include "string.h"
#define VERSION "0.11"
#define VERSION_DATE "19.09.2013"
/* without DEBUG_MODE for high speed parsing (for DEBUG_MODE just remove the double slash) */
/*#define DEBUG_MODE */
/* memory variables */
#define MEMORY_SIZE 40000000
#define MAX_NUMBER_OF_ELEMENTS 600000
#define MAX_NUMBER_OF_ATTRIBUTES 10000
#define MAX_NUMBER_OF_NODES 600000
#define MAX_NUMBER_OF_EDGES 600000
#define MAX_NUMBER_OF_BRANCHPOINTS 2000
#define MAX_NUMBER_OF_THINGS 2000
#define MAX_NUMBER_OF_PARAMETERS 500
#define MAX_NUMBER_OF_PARAMETER_ATTRIBUTES_TOTAL 10000
#define MAX_NUMBER_OF_COMMENTS 8000
unsigned char nmlMemory[MEMORY_SIZE];
unsigned char *pElementList[MAX_NUMBER_OF_ELEMENTS];
int iNumberOfAttributes[MAX_NUMBER_OF_ELEMENTS];
unsigned char *pAttributeName[MAX_NUMBER_OF_ATTRIBUTES];
unsigned char *pAttributeValue[MAX_NUMBER_OF_ATTRIBUTES];
long int gMemorypointer;
long int gMemorypointerCurrent;
long int nmlElementCounter;
long int nmlAttributeCounter;
long int nmlParameterCounter;
long int nmlThingCounter;
long int nmlCommentCounter;
int iNumberOfParameterAttributes[MAX_NUMBER_OF_PARAMETERS];
unsigned char *pParameterName[MAX_NUMBER_OF_PARAMETERS];
unsigned char *pParameterAttributeName[MAX_NUMBER_OF_PARAMETER_ATTRIBUTES_TOTAL];
unsigned char *pParameterAttributeValue[MAX_NUMBER_OF_PARAMETER_ATTRIBUTES_TOTAL];
long int gLineCounter;
#define NUM_OF_NODE_ATTRIBUTES_NML_FILE 8
double dNode[MAX_NUMBER_OF_NODES][NUM_OF_NODE_ATTRIBUTES_NML_FILE];
int iNodeThingNumber[MAX_NUMBER_OF_NODES];
int iNodeIdConversion[MAX_NUMBER_OF_NODES + 1]; /* +1 because it starts with 0 */
int iNodeIdConversionAllThings[MAX_NUMBER_OF_NODES + 1]; /* +1 because it starts with 0 */
unsigned char *pNode[MAX_NUMBER_OF_NODES][NUM_OF_NODE_ATTRIBUTES_NML_FILE];
unsigned char *pNodeComment[MAX_NUMBER_OF_NODES];
int iEdge[MAX_NUMBER_OF_EDGES][2];
int iBranchpoint[MAX_NUMBER_OF_BRANCHPOINTS];
double dThingID[MAX_NUMBER_OF_THINGS];
unsigned char *pThingName[MAX_NUMBER_OF_THINGS];
int iCommentNodeID[MAX_NUMBER_OF_COMMENTS];
unsigned char *pCommentContent[MAX_NUMBER_OF_COMMENTS];
int iNumberOfNodes;
int iNumberOfNodesThing[MAX_NUMBER_OF_THINGS];
int iNumberOfEdges;
int iNumberOfEdgesThing[MAX_NUMBER_OF_THINGS];
int iNumberOfBranchpoints;
/* input-output variables */
#define MESSAGE_BUFFER_SIZE 200
unsigned char szMessageBuffer[MESSAGE_BUFFER_SIZE];
/* element variables */
#define MAX_LENGTH_NMLELEMENT 100
#define MAX_LENGTH_ATTRIBUTE_NAME 100
#define MAX_LENGTH_ATTRIBUTE_VALUE 100
unsigned char szNmlElement[MAX_LENGTH_NMLELEMENT + 1 + 1]; /* +1 for ending slash and +1 for ending zero */
int statusElementClosed; /* 0 if open, 1 if closed with backslash */
/* prototypes */
void mexFunction (int nlhs , mxArray *plhs[], int nrhs , const mxArray *prhs[]);
int readCharacterFromFile (FILE* file);
int readNmlElement (FILE* file);
int readNmlString (FILE* file, unsigned char *szEndOfElement, unsigned char **pString);
int getIntegerAttribute (const char *szAttribute, int *iValue, int bShowErrorMessage);
int getDoubleAttribute (const char *szAttribute, double *dValue, unsigned char **pAttribute, int bShowErrorMessage);
int getStringAttribute (const char *szAttribute, unsigned char **pAttribute, int bShowErrorMessage);
/* DEBUG_MODE defines */
#ifdef DEBUG_MODE
#define getCharacter readCharacterFromFile
#else
#define getCharacter fgetc
#endif
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* ~~~~~~~~~~~~ CONSTANTS ~~~~~~~~~~~~~~~~ */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* coding sections */
#define SECTION_MAIN 1
#define SECTION_READNMLSTRING 2
#define SECTION_READNMLELEMENT 3
#define SECTION_COMMENTSSTRING 4
#define SECTION_BRANCHPOINTSSTRING 5
/* error codes */
#define ERROR_OUT_OF_MEMORY 1
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* ~~~~~~~~~~~~ FUNCTIONS ~~~~~~~~~~~~~~~~ */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* error function */
void errorMessage (int iCodingSection, int iErrorType) {
/* define text for coding section */
switch (iCodingSection) {
case SECTION_READNMLSTRING:
sprintf(szMessageBuffer, "Braintracing:parseNML:readNmlString");
break;
case SECTION_READNMLELEMENT:
sprintf(szMessageBuffer, "Braintracing:parseNML:readNmlElement");
break;
case SECTION_COMMENTSSTRING:
sprintf(szMessageBuffer, "Braintracing:parseNML:commentsString");
break;
case SECTION_BRANCHPOINTSSTRING:
sprintf(szMessageBuffer, "Braintracing:parseNML:branchpointsString");
break;
case SECTION_MAIN:
default:
sprintf(szMessageBuffer, "Braintracing:parseNML:mainFunction");
break;
}
/* send error message */
switch (iErrorType) {
case ERROR_OUT_OF_MEMORY:
if (iCodingSection == SECTION_COMMENTSSTRING) {
mexErrMsgIdAndTxt(szMessageBuffer, "Out of memory. Cannot write comments. Please increase constant MEMORY_SIZE (line 19).");
} else {
mexErrMsgIdAndTxt(szMessageBuffer, "Out of memory. Please increase constant MEMORY_SIZE (line 19).");
}
break;
default:
mexErrMsgIdAndTxt(szMessageBuffer, "Unknown internal error.");
break;
}
return;
}
/* read character from file (DEBUG_MODE) */
int readCharacterFromFile (FILE* file) {
int character;
character = fgetc (file);
if (character == 0x0A) {
gLineCounter++;
}
return character;
}
/* read attribute as integer value from memory (from last element) */
int getIntegerAttribute (const char *szAttribute, int *iValue, int bShowErrorMessage) {
int i;
int bFoundAttributeName;
/* initialize value */
*iValue = 0;
/* initialize other variables */
bFoundAttributeName = false;
/* check number of elements */
if (nmlElementCounter <= 0) {
mexErrMsgIdAndTxt("Braintracing:parseNML:getIntegerAttribute", "No elements available.");
return 1;
}
/* check number of attributes */
if (iNumberOfAttributes[nmlElementCounter - 1] <= 0) {
mexErrMsgIdAndTxt("Braintracing:parseNML:getIntegerAttribute", "No attributes available.");
return 1;
}
/* search attribute name */
for (i = 0; i < iNumberOfAttributes[nmlElementCounter - 1]; i++) {
if (strcmp(pAttributeName[i], szAttribute) == 0) {
/* convert attribute value to double */
*iValue = atoi(pAttributeValue[i]);
/* set "found" flag */
bFoundAttributeName = true;
break;
}
}
/* attribute name not found */
if (!bFoundAttributeName) {
if (bShowErrorMessage) {
/* show line number in debug mode */
#ifdef DEBUG_MODE
sprintf(szMessageBuffer, "Attribute %s not found in line %ld.", szAttribute, gLineCounter);
#else
sprintf(szMessageBuffer, "Attribute %s not found.", szAttribute);
#endif
mexErrMsgIdAndTxt("Braintracing:parseNML:getIntegerAttribute", szMessageBuffer);
}
return 1;
}
/* return OK */
return 0;
}
/* read attribute as double value from memory (from last element) */
int getDoubleAttribute (const char *szAttribute, double *dValue, unsigned char **pAttribute, int bShowErrorMessage) {
int i;
int bFoundAttributeName;
/* initialize value */
*dValue = 0;
*pAttribute = NULL;
/* initialize other variables */
bFoundAttributeName = false;
/* check number of elements */
if (nmlElementCounter <= 0) {
mexErrMsgIdAndTxt("Braintracing:parseNML:getDoubleAttribute", "No elements available.");
return 1;
}
/* check number of attributes */
if (iNumberOfAttributes[nmlElementCounter - 1] <= 0) {
mexErrMsgIdAndTxt("Braintracing:parseNML:getDoubleAttribute", "No attributes available.");
return 1;
}
/* search attribute name */
for (i = 0; i < iNumberOfAttributes[nmlElementCounter - 1]; i++) {
if (strcmp(pAttributeName[i], szAttribute) == 0) {
/* store pointer */
*pAttribute = pAttributeValue[i];
/* convert attribute value to double */
*dValue = atof(pAttributeValue[i]);
/* set "found" flag */
bFoundAttributeName = true;
break;
}
}
/* attribute name not found */
if (!bFoundAttributeName) {
if (bShowErrorMessage) {
/* show line number in debug mode */
#ifdef DEBUG_MODE
sprintf(szMessageBuffer, "Attribute %s not found in line %ld.", szAttribute, gLineCounter);
#else
sprintf(szMessageBuffer, "Attribute %s not found.", szAttribute);
#endif
mexErrMsgIdAndTxt("Braintracing:parseNML:getDoubleAttribute", szMessageBuffer);
}
return 1;
}
/* return OK */
return 0;
}
/* read attribute as string value from memory (from last element) */
int getStringAttribute (const char *szAttribute, unsigned char **pAttribute, int bShowErrorMessage) {
int i;
int bFoundAttributeName;
/* initialize value */
*pAttribute = NULL;
/* initialize other variables */
bFoundAttributeName = false;
/* check number of elements */
if (nmlElementCounter <= 0) {
mexErrMsgIdAndTxt("Braintracing:parseNML:getStringAttribute", "No elements available.");
return 1;
}
/* check number of attributes */
if (iNumberOfAttributes[nmlElementCounter - 1] <= 0) {
mexErrMsgIdAndTxt("Braintracing:parseNML:getStringAttribute", "No attributes available.");
return 1;
}
/* search attribute name */
for (i = 0; i < iNumberOfAttributes[nmlElementCounter - 1]; i++) {
if (strcmp(pAttributeName[i], szAttribute) == 0) {
/* store pointer */
*pAttribute = pAttributeValue[i];
/* set "found" flag */
bFoundAttributeName = true;
break;
}
}
/* attribute name not found */
if (!bFoundAttributeName) {
if (bShowErrorMessage) {
/* show line number in debug mode */
#ifdef DEBUG_MODE
sprintf(szMessageBuffer, "Attribute %s not found in line %ld.", szAttribute, gLineCounter);
#else
sprintf(szMessageBuffer, "Attribute %s not found.", szAttribute);
#endif
mexErrMsgIdAndTxt("Braintracing:parseNML:getStringAttribute", szMessageBuffer);
}
/* return NOT OK */
return 1;
}
/* return OK */
return 0;
}
/* read single string until end and set pointer to string */
int readNmlString (FILE* file, unsigned char *szEndOfElement, unsigned char **pString) {
unsigned char c;
int iLengthEndOfString;
int bRemoveSpaces;
int bBeginning;
long int gMemorypointerEnd;
int i;
/* store memory pointer */
*pString = &nmlMemory[gMemorypointer];
i = 0;
gMemorypointerEnd = gMemorypointer;
bRemoveSpaces = 1; /* 1 = section between > and < 0 = section between < and > */
bBeginning = 1;
do {
c = getCharacter (file);
if (c == EOF) {
mexErrMsgIdAndTxt("Braintracing:parseNML:readNmlString", "Unexpected end of file in XML element.");
return 1;
}
/* check for beginning */
if (bBeginning) {
if ((c == ' ') || (c == 0x09) || (c == 0x0D) || (c == 0x0A) || (c == 0x00)) {
continue;
}
/* end of beginning ;-) */
bBeginning = 0;
}
/* check for end */
if ((i == 0) && (c == '>')) {
gMemorypointerEnd = gMemorypointer;
}
/* check for "space mode" --> remove spaces and tabs between > and < */
if (((c == ' ') || (c == 0x09)) && bRemoveSpaces) {
continue;
}
if (bRemoveSpaces) {
if (c == '<') {
bRemoveSpaces = 0;
}
} else {
if (c == '>') {
bRemoveSpaces = 1;
}
}
/* store character in memory */
if (gMemorypointer >= MEMORY_SIZE) {
errorMessage(SECTION_READNMLSTRING, ERROR_OUT_OF_MEMORY);
return 1;
}
nmlMemory[gMemorypointer++] = c;
/* check for < sign */
if (i == 0) {
if (c == '<') {
i = 1;
iLengthEndOfString = 1;
}
continue;
}
/* check for end of element */
if ((i == 1) || (i == ((int)strlen(szEndOfElement) + 1))) {
if (c == ' ') {
iLengthEndOfString++;
continue;
}
if ((i == ((int)strlen(szEndOfElement) + 1)) && (c == '>')) {
/* found end of element */
/* correct memory pointer */
gMemorypointer -= (iLengthEndOfString + 1);
/* remove whitespaces from the end */
if ((gMemorypointerEnd + 1) <= gMemorypointer) {
while (((gMemorypointerEnd + 1) <= gMemorypointer) && ((nmlMemory[gMemorypointer - 1] == ' ') ||
(nmlMemory[gMemorypointer - 1] == 0x0D) || (nmlMemory[gMemorypointer - 1] == 0x0A) ||
(nmlMemory[gMemorypointer - 1] == 0x09) || (nmlMemory[gMemorypointer - 1] == 0x00))) {
gMemorypointer--;
}
}
/* store trailing zero in memory */
nmlMemory[gMemorypointer++] = 0;
/* return OK; */
return 0;
}
}
/* compare string */
if (c == szEndOfElement[i - 1]) {
i++;
iLengthEndOfString++;
} else {
i = 0;
iLengthEndOfString = 0;
}
} while (1);
/* dummy return */
return 1;
}
/* read single NML element (if an ending slash exists it will be added to the name of the element !!! <name id="xx" value=0"/> --> "name/") */
int readNmlElement (FILE* file) {
char c;
int i;
int iEndOfElement;
char cTypeOfQuotationMark;
/* delete previous element */
szNmlElement[0] = 0;
statusElementClosed = 0; /* 0 = open */
/* read leading '<' */
i = 0;
do {
c = getCharacter (file);
if (c == EOF) {
/* reached end of file --> return empty element */
return 0;
}
} while (c != '<');
/* read element name (i = number of characters) */
do {
/* read character */
c = getCharacter (file);
if (c == EOF) {
mexErrMsgIdAndTxt("Braintracing:parseNML:readNmlElement", "Unexpected end of file in XML element.");
return 1;
}
/* check for special parameters beginning with ? and ignore it (for example: ?xml version="1.0") */
/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ?xml special >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
if ((i == 0) && (c == '?')) {
do {
c = getCharacter (file);
if (c == EOF) {
mexErrMsgIdAndTxt("Braintracing:parseNML:readNmlElement", "Unexpected end of file in XML element.");
return 1;
}
} while (c != '>');
/* read leading '<' again */
do {
c = getCharacter (file);
if (c == EOF) {
/* reached end of file --> return empty element */
return 0;
}
} while (c != '<');
/* read first character of element name again */
c = getCharacter (file);
if (c == EOF) {
mexErrMsgIdAndTxt("Braintracing:parseNML:readNmlElement", "Unexpected end of file in XML element.");
return 1;
}
}
/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ?xml special >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
/* store character */
if ((c != ' ') && (c != 0x00) && (c != 0x09) && (c != 0x0A) && (c != 0x0D) && (c != '>')) {
/* +++++++++++++++++++++++++ */
/* +++ element found !!! +++ */
/* +++++++++++++++++++++++++ */
/* store element pointer */
if (i == 0) {
if (nmlElementCounter >= MAX_NUMBER_OF_ELEMENTS) {
mexErrMsgIdAndTxt("Braintracing:parseNML:readNmlElement", "Two many elements Please increase MAX_NUMBER_OF_ELEMENTS.");
return 1;
}
/* store element pointer */
pElementList[nmlElementCounter] = &nmlMemory[gMemorypointer];
/* reset number of attributes */
iNumberOfAttributes[nmlElementCounter] = 0;
}
/* store character */
szNmlElement[i++] = c;
/* store character in memory */
if (gMemorypointer >= MEMORY_SIZE) {
errorMessage(SECTION_READNMLELEMENT, ERROR_OUT_OF_MEMORY);
return 1;
}
nmlMemory[gMemorypointer++] = c;
}
/* check length */
if (i >= MAX_LENGTH_NMLELEMENT) {
szNmlElement[MAX_LENGTH_NMLELEMENT] = 0;
sprintf(szMessageBuffer, "NML element too long: %s... Maximal length is %d characters.\n", szNmlElement, MAX_LENGTH_NMLELEMENT);
mexErrMsgIdAndTxt("Braintracing:parseNML:readNmlElement", szMessageBuffer);
return 1;
}
} while ((c != ' ') && (c != 0x00) && (c != 0x09) && (c != 0x0A) && (c != 0x0D) && (c != '>'));
/* set trailing zero (name of element is now available in 'szNmlElement') */
iEndOfElement = i;
szNmlElement[i] = 0;
/* store trailing zero in memory */
if (gMemorypointer >= MEMORY_SIZE) {
errorMessage(SECTION_READNMLELEMENT, ERROR_OUT_OF_MEMORY);
return 1;
}
nmlMemory[gMemorypointer++] = 0;
/* check element size (has to be at least 1 character) */
if (i == 0) {
mexErrMsgIdAndTxt("Braintracing:parseNML:readNmlElement", "Empty NML element. Leading spaces not allowed in NML element.");
return 1;
}
/* no attributes? fine, then return without attributes ;-) */
if (c == '>') {
/* store double trailing zero in memory */
if (gMemorypointer >= MEMORY_SIZE) {
errorMessage(SECTION_READNMLELEMENT, ERROR_OUT_OF_MEMORY);
return 1;
}
nmlMemory[gMemorypointer++] = 0;
/* increase element counter */
nmlElementCounter++;
/* return OK */
return 0;
}
/* loop: read all attribute names and values */
do {
/* read attribute name */
i = 0;
do {
c = getCharacter (file);
if (c == EOF) {
mexErrMsgIdAndTxt("Braintracing:parseNML:readNmlElement", "Unexpected end of file in XML element.");
return 1;
}
/* ending slash --> add slash to name of element !!! */
if ((c == '/') && (i == 0)) {
szNmlElement[iEndOfElement] = c;
szNmlElement[iEndOfElement + 1] = 0;
statusElementClosed = 1; /* 1 = closed */
/* store slash in memory */
if (gMemorypointer >= MEMORY_SIZE) {
errorMessage(SECTION_READNMLELEMENT, ERROR_OUT_OF_MEMORY);
return 1;
}
nmlMemory[gMemorypointer++] = c;
/* read '>' */
do {
c = getCharacter (file);
if (c == EOF) {
mexErrMsgIdAndTxt("Braintracing:parseNML:readNmlElement", "Unexpected end of file in XML element.");
return 1;
}
} while (c != '>');
/* store double trailing zero in memory */
if (gMemorypointer >= MEMORY_SIZE) {
errorMessage(SECTION_READNMLELEMENT, ERROR_OUT_OF_MEMORY);
return 1;
}
nmlMemory[gMemorypointer++] = 0;
/* increase element counter */
nmlElementCounter++;
/* return OK */
return 0;
}
/* end of element --> error, if there is an attribute without '=' */
if (c == '>') {
if (i == 0) {
/* store double trailing zero in memory */
if (gMemorypointer >= MEMORY_SIZE) {
errorMessage(SECTION_READNMLELEMENT, ERROR_OUT_OF_MEMORY);
return 1;
}
nmlMemory[gMemorypointer++] = 0;
/* increase element counter */
nmlElementCounter++;
/* return OK */
return 0;
}
mexErrMsgIdAndTxt("Braintracing:parseNML:readNmlElement", "Unexpected end of file in XML element.");
return 1;
}
/* store attribute name (ignore spaces in attribute name) */
if ((c != ' ') && (c != 0x00) && (c != 0x09) && (c != 0x0A) && (c != 0x0D) && (c != '=')) {
/* check forbidden characters */
if ((c == '>') || (c == '/')) {
sprintf(szMessageBuffer, "Forbidden character in attribute name. May be attribute value is missing.\n");
mexErrMsgIdAndTxt("Braintracing:parseNML:readNmlElement", szMessageBuffer);
return 1;
}
/* check max number of attributes */
if (i == 0) {
if (nmlAttributeCounter >= MAX_NUMBER_OF_ATTRIBUTES) {
sprintf(szMessageBuffer, "Too many attributes in element %s.\n", szNmlElement);
mexErrMsgIdAndTxt("Braintracing:parseNML:readNmlElement", szMessageBuffer);
return 1;
}
/* store pointer to attribute name */
pAttributeName[nmlAttributeCounter] = &nmlMemory[gMemorypointer];
}
i++;
/* store character in memory */
if (gMemorypointer >= MEMORY_SIZE) {
errorMessage(SECTION_READNMLELEMENT, ERROR_OUT_OF_MEMORY);
return 1;
}
nmlMemory[gMemorypointer++] = c;
}
} while (c != '=');
/* store double zero in memory */
if (gMemorypointer >= MEMORY_SIZE) {
errorMessage(SECTION_READNMLELEMENT, ERROR_OUT_OF_MEMORY);
return 1;
}
nmlMemory[gMemorypointer++] = 0;
/* *********** QUOTATION MARK ********** */
/* read quotation mark and ignore spaces */
do {
c = getCharacter (file);
if (c == EOF) {
mexErrMsgIdAndTxt("Braintracing:parseNML:readNmlElement", "Unexpected end of file in XML element.");
return 1;
}
} while ((c == ' ') || (c == 0x00) || (c == 0x09) || (c == 0x0A) || (c == 0x0D));
/* check quotation mark " (hex22) or ' (hex27) */
if ((c != 0x22) && (c != 0x27)) {
mexErrMsgIdAndTxt("Braintracing:parseNML:readNmlElement", "Argument value is missing. Quotation mark expected. Forgot it?");
return 1;
}
/* remember type of quotation mark */
cTypeOfQuotationMark = c;
/* reset character counter */
i = 0;
/* read attribute value */
do {
c = getCharacter (file);
if (c == EOF) {
mexErrMsgIdAndTxt("Braintracing:parseNML:readNmlElement", "Unexpected end of file in XML element.");
return 1;
}
/* store character */
if (c != cTypeOfQuotationMark) {
/* store pointer to attribute value */
if (i == 0) {
pAttributeValue[nmlAttributeCounter] = &nmlMemory[gMemorypointer];
}
i++;
/* store character in memory */
if (gMemorypointer >= MEMORY_SIZE) {
errorMessage(SECTION_READNMLELEMENT, ERROR_OUT_OF_MEMORY);
return 1;
}
nmlMemory[gMemorypointer++] = c;
}
} while (c != cTypeOfQuotationMark);
/* increase number of attributes per element and attribute counter */
iNumberOfAttributes[nmlElementCounter]++;
nmlAttributeCounter++;
/* store double zero in memory */
if (gMemorypointer >= MEMORY_SIZE) {
errorMessage(SECTION_READNMLELEMENT, ERROR_OUT_OF_MEMORY);
return 1;
}
nmlMemory[gMemorypointer++] = 0;
} while (1); /* endless loop --> reading attributes */
/* dummy return value (to avoid warnings) */
return 1;
}
void mexFunction (int nlhs , mxArray *plhs[], int nrhs , const mxArray *prhs[]) {
/* declare variables */
#define MAX_LENGTH_FILENAME 200
#define MAX_LENGTH_PATH 200
unsigned char szPathAndFilename[MAX_LENGTH_FILENAME + MAX_LENGTH_PATH + 1];
unsigned char szFilename[MAX_LENGTH_FILENAME + 1];
unsigned char szPath[MAX_LENGTH_PATH + 1];
mxArray *mxGetFileOutput[2];
mxArray *mxGetFileInput[2];
const char *nmlStructFirstThing[] = { "parameters", "nodes", "nodesAsStruct", "nodesNumDataAll",
"edges", "thingID", "name", "commentsString", "branchpointsString", "branchpoints"};
const char *nmlStructLastThing[] = { "nodes", "nodesAsStruct", "nodesNumDataAll", "edges", "thingID", "name", "commentsString"};
const char *nmlStructOtherThings[] = { "nodes", "nodesAsStruct", "nodesNumDataAll", "edges", "thingID", "name"};
const char *nmlNodeAttributes[] = { "id", "radius", "x", "y", "z", "inVp", "inMag", "time", "comment" };
const char *nmlEdgeAttributes[] = { "source", "target" };
const char *nmlBranchpointAttributes[] = { "id" };
int nmlNodeAttributeOrder[] = { 2, 3, 4, 1 };
#define NUM_OF_NODE_ATTRIBUTES_ALL (sizeof (nmlNodeAttributes) / sizeof (const char *))
#define NUM_OF_NODE_ATTRIBUTES (sizeof (nmlNodeAttributeOrder) / sizeof (int))
#define NUM_OF_EDGE_ATTRIBUTES (sizeof (nmlEdgeAttributes) / sizeof (const char *))
#define NUM_OF_BRANCHPOINT_ATTRIBUTES (sizeof (nmlBranchpointAttributes) / sizeof (const char *))
/* general variables */
int i, j, k;
double d;
unsigned char *p;
time_t time_start;
int iLength;
int iNumberOfBytesWritten;
int bCommentsAvailable;
int iNumberOfNodesOffset;
int iNumberOfEdgesOffset;
int iDimensions[1];
int iDimensionsNodes[2];
int iKeepNodeAsStruct;
int iNodeIDConverted;
long int nmlElementCounterCurrent;
/* xml variables */
FILE* file;
/* MATLAB variables */
mwSize mwPointer;
mxArray *nmlCell;
mxArray *nmlCellCommentsString;
mxArray *nmlCellLastCommentsString;
mxArray *nmlCellBranchpointsString;
mxArray *nmlStruct[MAX_NUMBER_OF_THINGS];
mxArray *nmlParameterElementStruct;
mxArray *nmlParameterAttributeStruct;
mxArray *nmlCellNodeAsStruct[MAX_NUMBER_OF_THINGS];
mxArray *nmlStructNodeAsStruct[MAX_NUMBER_OF_NODES];
mxArray *nmlArrayNodesNumDataAll[MAX_NUMBER_OF_THINGS];
double *pArrayNodesNumDataAll;
mxArray *nmlArrayNodes[MAX_NUMBER_OF_THINGS];
double *pArrayNodes;
mxArray *nmlArrayEdges[MAX_NUMBER_OF_THINGS];
double *pArrayEdges;
mxArray *nmlArrayBranchpoints;
double *pArrayBranchpoints;
/* send welcome message */
printf("This is Braintracing NML parser version %s, Copyright 2013 MPI of Neurobiology, Martinsried.\n", VERSION);
/* check number of input arguments */
if (nrhs > 2) {
mexErrMsgIdAndTxt("Braintracing:parseNML:nrhs", "Only two parameters allowed: filename[character] and keepNodeAsStruct[0/1].");
return;
}
if (nrhs < 1) {
/* ************ */
/* get filename */
/* ************ */
mxGetFileInput[0] = mxCreateString ("*.nml");
mxGetFileInput[1] = mxCreateString ("Please select KNOSSOS or OXALIS .nml file");
/* open file dialog */
if (mexCallMATLAB(2, mxGetFileOutput, 2, mxGetFileInput, "uigetfile")) {
mexErrMsgIdAndTxt("Braintracing:parseNML:FileDialog", "Filename missing. Could not select file.");
return;
}
/* check for user abort */
if ((mxIsClass(mxGetFileOutput[0], "double")) && (mxGetScalar(mxGetFileOutput[0]) == 0)) {
mexErrMsgIdAndTxt("Braintracing:parseNML:FileDialog", "Filename missing. File dialog canceled by user.");
return;
}
if (mxGetString(mxGetFileOutput[0], szFilename, MAX_LENGTH_FILENAME)) {
mexErrMsgIdAndTxt("Braintracing:parseNML:FileDialog", "Filename invalid or too long.");
return;
}
if (mxGetString(mxGetFileOutput[1], szPath, MAX_LENGTH_PATH)) {
mexErrMsgIdAndTxt("Braintracing:parseNML:FileDialog", "Path invalid or too long.");
return;
}
/* concatenate path and filename */
sprintf(szPathAndFilename, "%s%s", szPath, szFilename);
} else {
/* check type of input argument */
if( !mxIsClass(prhs[0], "char")) {
mexErrMsgIdAndTxt("Braintracing:parseNML:notString", "Input parameter 'filename' must be a string.");
return;
}
/* get file name */
if (mxGetString(prhs[0], szPathAndFilename, MAX_LENGTH_FILENAME)) {
mexErrMsgIdAndTxt("Braintracing:parseNML:FilenameTooLong", "Input parameter 'filename' too long.");
return;
}
}
/* intialize optional parameter */
iKeepNodeAsStruct = 1;
/* check second argument */
if (nrhs == 2) {
/* check type of secondinput argument */
if( !mxIsClass(prhs[1], "double")) {
mexErrMsgIdAndTxt("Braintracing:parseNML:notString", "Input parameter 'keepNodeAsStruct' must be 0 or 1.");
return;
}
/* get parameter */
iKeepNodeAsStruct = (int)mxGetScalar(prhs[1]);
}
/* check number of output arguments */
if (nlhs != 1) {
mexErrMsgIdAndTxt("Braintracing:parseNML:nlhs", "Output required. Proper use of function is 'MyVar = parseNML FileName'");
return;
}
/* start time */
time_start = time(NULL);
/* open file */
if (!(file = fopen (szPathAndFilename, "r"))) {
sprintf(szMessageBuffer, "Cannot open file %s.\n", szPathAndFilename);
mexErrMsgIdAndTxt("Braintracing:parseNML:OpenFile", szMessageBuffer);
return;
}
/* ************** */
/* parse nml file */
/* ***************/
/* initialize global variables */
iNumberOfNodes = 0;
iNumberOfEdges = 0;
iNumberOfBranchpoints = 0;
gLineCounter = 0;
/* initialize local variables */
bCommentsAvailable = false;
/* initialize dimensions */
iDimensions[0] = 1;
/* initialize node ID conversion and node thing id */
for (i = 0; i < MAX_NUMBER_OF_NODES; i++) {
iNodeIdConversion[i] = 0;
pNodeComment[i] = NULL;
iNodeThingNumber[i] = -1;
}
/* reset memory pointers and counters */
gMemorypointer = 0;
gMemorypointerCurrent = 0;
nmlElementCounter = 0;
nmlAttributeCounter = 0;
nmlParameterCounter = 0;
nmlThingCounter = 0;
nmlCommentCounter = 0;
/* things */
readNmlElement(file);
if (strcmp(szNmlElement, "things")) {
sprintf(szMessageBuffer, "Expected element things. Got %s.\n", szNmlElement);
mexErrMsgIdAndTxt("Braintracing:parseNML:things", szMessageBuffer);
return;
}
/* reset memory pointer */
gMemorypointer = gMemorypointerCurrent;
/* oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo*/
/* oooooooo read all elements until end (= "/things") ooooooooooooo */
/* oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo*/
do {
/* reset counters*/
nmlElementCounter = 0;
nmlAttributeCounter = 0;
/* save memory pointer (last position) */
gMemorypointerCurrent = gMemorypointer;
/* read element */
readNmlElement(file);
/* ignore empty elements */
if ((strcmp(szNmlElement, "parameters/") == 0) || (strcmp(szNmlElement, "comments/") == 0) || (strcmp(szNmlElement, "branchpoints/") == 0) ||
(strcmp(szNmlElement, "thing/") == 0)) {
/* reset memory pointer */
gMemorypointer = gMemorypointerCurrent;
continue;
}
/* read comments */
if (strcmp(szNmlElement, "comments") == 0) {
/* reset element and attribute counters */
nmlElementCounter = 0;
nmlAttributeCounter = 0;
bCommentsAvailable = true;
/* reset memory pointer */
gMemorypointer = gMemorypointerCurrent;
do {
/* reset attribute counter */
nmlAttributeCounter = 0;
/* save memory pointer (last position) */
gMemorypointerCurrent = gMemorypointer;
/* read element */
if (readNmlElement(file)) {
/* stop program after error */
fclose (file);
plhs[0] = mxCreateCellMatrix (1, 1);
return;
}
/* ignore end of comment elements) */
if (strcmp(szNmlElement, "/comment") == 0) {
/* reset memory pointer */
gMemorypointer = gMemorypointerCurrent;
continue;
}
/* check element */
if ((strcmp(szNmlElement, "comment") != 0) && (strcmp(szNmlElement, "comment/") != 0) && (strcmp(szNmlElement, "/comments") != 0)) {
sprintf(szMessageBuffer, "Illegal element %s in comment line %ld.", szNmlElement, (nmlCommentCounter + 1));
mexErrMsgIdAndTxt("Braintracing:parseNML:comment", szMessageBuffer);
return;
}
if ((strcmp(szNmlElement, "comment") == 0) || (strcmp(szNmlElement, "comment/") == 0)) {
/* check node counter */
if (nmlCommentCounter >= MAX_NUMBER_OF_COMMENTS) {
mexErrMsgIdAndTxt("Braintracing:parseNML:comment", "Too many comments in nml file. Increasing MAX_NUMBER_OF_COMMENTS could help.");
return;
}
/* get comment attributes (id and content) */
if (getIntegerAttribute ("node", &i, true)) {
/* stop program after error */
fclose (file);
plhs[0] = mxCreateCellMatrix (1, 1);
return;
}
/* check node id value */
if ((i <= 0) || (i > MAX_NUMBER_OF_NODES)) {
sprintf(szMessageBuffer, "Illegal node id %d in comment line %ld. Must be >= 1 and <= %d.\n",
i, (nmlCommentCounter + 1), MAX_NUMBER_OF_NODES);
mexErrMsgIdAndTxt("Braintracing:parseNML:comment", szMessageBuffer);
return;
}
/* store integer value of id in memory */
iCommentNodeID[nmlCommentCounter] = i;
/* get comment content */
if (getStringAttribute ("content", &p, true)) {
/* stop program after error */
fclose (file);
plhs[0] = mxCreateCellMatrix (1, 1);
return;
}
/* store comment in memory */
pCommentContent[nmlCommentCounter] = p;
pNodeComment[iNodeIdConversionAllThings[i]] = p;
/* increase comment counter */
nmlCommentCounter++;
}
} while (strcmp(szNmlElement, "/comments"));
/* reset memory pointer */
gMemorypointer = gMemorypointerCurrent;
continue;
}
/* read parameters */
if (strcmp(szNmlElement, "parameters") == 0) {
/* decrease element counter by 1 (because "parameters" is supposed to have no attributes and therefore the element itself is not needed) */
nmlElementCounter--;
/* save element counter */
nmlElementCounterCurrent = nmlElementCounter;
/* read parameters */
nmlParameterCounter = 0;
if (!statusElementClosed) {
do {
readNmlElement(file);
if ((szNmlElement[0] != '/') && (strcmp(szNmlElement, "/parameters"))) {
if (nmlParameterCounter >= MAX_NUMBER_OF_PARAMETERS) {
mexErrMsgIdAndTxt("Braintracing:parseNML:parameters", "Too many parameters in nml file. "
"Increasing MAX_NUMBER_OF_PARAMETERS could help.");
return;
}
if (nmlElementCounter <= 0) {
mexErrMsgIdAndTxt("Braintracing:parseNML:parameters", "Internal error. No parameter section found.");
return;
}
pParameterName[nmlParameterCounter] = pElementList[nmlElementCounter - 1];
nmlParameterCounter++;
} else {
/* decrease element counter by 1 (because /parameters is no element) */
nmlElementCounter--;