1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | #include "nrrd.h" |
25 | #include "privateNrrd.h" |
26 | |
27 | static FILE *_fileSave = NULL((void*)0); |
28 | |
29 | static int |
30 | _nrrdEncodingAscii_available(void) { |
31 | |
32 | return AIR_TRUE1; |
33 | } |
34 | |
35 | static int |
36 | _nrrdEncodingAscii_read(FILE *file, void *_data, size_t elNum, |
37 | Nrrd *nrrd, NrrdIoState *nio) { |
38 | static const char me[]="_nrrdEncodingAscii_read"; |
39 | char numbStr[AIR_STRLEN_HUGE(1024+1)]; |
40 | char *nstr; |
41 | size_t I; |
42 | char *data; |
43 | int tmp; |
44 | |
45 | AIR_UNUSED(nio)(void)(nio); |
46 | _fileSave = file; |
47 | if (nrrdTypeBlock == nrrd->type) { |
48 | biffAddf(NRRDnrrdBiffKey, "%s: can't read nrrd type %s from %s", me, |
49 | airEnumStr(nrrdType, nrrdTypeBlock), |
50 | nrrdEncodingAscii->name); |
51 | return 1; |
52 | } |
53 | data = (char*)_data; |
54 | I = 0; |
55 | while (I < elNum) { |
56 | char stmp1[AIR_STRLEN_SMALL(128+1)], stmp2[AIR_STRLEN_SMALL(128+1)]; |
57 | |
58 | |
59 | |
60 | |
61 | |
62 | |
63 | |
64 | |
65 | |
66 | |
67 | if (1 != fscanf(file, "%s", numbStr)) { |
68 | biffAddf(NRRDnrrdBiffKey, "%s: couldn't parse element %s of %s", me, |
69 | airSprintSize_t(stmp1, I+1), |
70 | airSprintSize_t(stmp2, elNum)); |
71 | return 1; |
72 | } |
73 | if (file != _fileSave) { |
74 | fprintf(stderr__stderrp, "%s: PANIC memory corruption detected\n", me); |
75 | |
76 | biffAddf(NRRDnrrdBiffKey, "%s: PANIC memory corruption detected", me); |
77 | return 1; |
78 | } |
79 | if (!strcmp(",", numbStr)) { |
80 | |
81 | continue; |
82 | } |
83 | |
84 | nstr = numbStr + strspn(numbStr, ","); |
85 | if (nrrd->type >= nrrdTypeInt) { |
86 | |
87 | if (1 != airSingleSscanf(nstr, nrrdTypePrintfStr[nrrd->type], |
88 | (void*)(data + I*nrrdElementSize(nrrd)))) { |
89 | biffAddf(NRRDnrrdBiffKey, "%s: couldn't parse %s %s of %s (\"%s\")", me, |
90 | airEnumStr(nrrdType, nrrd->type), |
91 | airSprintSize_t(stmp1, I+1), |
92 | airSprintSize_t(stmp2, elNum), nstr); |
93 | return 1; |
94 | } |
95 | } else { |
96 | |
97 | if (1 != airSingleSscanf(nstr, "%d", &tmp)) { |
98 | biffAddf(NRRDnrrdBiffKey, "%s: couldn't parse element %s of %s (\"%s\")", me, |
99 | airSprintSize_t(stmp1, I+1), |
100 | airSprintSize_t(stmp2, elNum), nstr); |
101 | return 1; |
102 | } |
103 | nrrdIInsert[nrrd->type](data, I, tmp); |
104 | } |
105 | I++; |
106 | } |
107 | |
108 | return 0; |
109 | } |
110 | |
111 | static int |
112 | _nrrdEncodingAscii_write(FILE *file, const void *_data, size_t elNum, |
113 | const Nrrd *nrrd, NrrdIoState *nio) { |
114 | static const char me[]="_nrrdEncodingAscii_write"; |
115 | char buff[AIR_STRLEN_MED(256+1)]; |
116 | size_t bufflen, linelen; |
117 | const char *data; |
118 | size_t I; |
119 | int newlined; |
| 1 | 'newlined' declared without an initial value | |
|
120 | |
121 | if (nrrdTypeBlock == nrrd->type) { |
| |
122 | biffAddf(NRRDnrrdBiffKey, "%s: can't write nrrd type %s to %s", me, |
123 | airEnumStr(nrrdType, nrrdTypeBlock), |
124 | nrrdEncodingAscii->name); |
125 | return 1; |
126 | } |
127 | data = AIR_CAST(const char*, _data)((const char*)(_data)); |
128 | linelen = 0; |
129 | for (I=0; I<elNum; I++) { |
| 3 | | Assuming 'I' is >= 'elNum' | |
|
| 4 | | Loop condition is false. Execution continues on line 152 | |
|
130 | nrrdSprint[nrrd->type](buff, data); |
131 | if (1 == nrrd->dim) { |
132 | fprintf(file, "%s\n", buff); |
133 | newlined = AIR_TRUE1; |
134 | } else if (nrrd->dim == 2 |
135 | && nrrd->axis[0].size <= nio->valsPerLine) { |
136 | int nonewline = (I+1)%(nrrd->axis[0].size); |
137 | fprintf(file, "%s%c", buff, nonewline ? ' ' : '\n'); |
138 | newlined = !nonewline; |
139 | } else { |
140 | bufflen = strlen(buff); |
141 | if (linelen+bufflen+1 <= nio->charsPerLine) { |
142 | fprintf(file, "%s%s", I ? " " : "", buff); |
143 | linelen += (I ? 1 : 0) + bufflen; |
144 | } else { |
145 | fprintf(file, "\n%s", buff); |
146 | linelen = bufflen; |
147 | } |
148 | newlined = AIR_FALSE0; |
149 | } |
150 | data += nrrdElementSize(nrrd); |
151 | } |
152 | if (!newlined) { |
| 5 | | Branch condition evaluates to a garbage value |
|
153 | |
154 | |
155 | fprintf(file, "\n"); |
156 | } |
157 | fflush(file); |
158 | |
159 | return 0; |
160 | } |
161 | |
162 | const NrrdEncoding |
163 | _nrrdEncodingAscii = { |
164 | "ASCII", |
165 | "ascii", |
166 | AIR_FALSE0, |
167 | AIR_FALSE0, |
168 | _nrrdEncodingAscii_available, |
169 | _nrrdEncodingAscii_read, |
170 | _nrrdEncodingAscii_write |
171 | }; |
172 | |
173 | const NrrdEncoding *const |
174 | nrrdEncodingAscii = &_nrrdEncodingAscii; |