Mercurial > octave-nkf
comparison src/DLD-FUNCTIONS/dlmread.cc @ 10154:40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 20 Jan 2010 17:33:41 -0500 |
parents | 56e850e3b06f |
children | 12884915a8e4 |
comparison
equal
deleted
inserted
replaced
10153:2c28f9d0360f | 10154:40dfc0c99116 |
---|---|
46 if (is.peek () == std::istream::traits_type::eof ()) | 46 if (is.peek () == std::istream::traits_type::eof ()) |
47 stat = true; | 47 stat = true; |
48 else | 48 else |
49 { | 49 { |
50 if (::isalpha (is.peek ())) | 50 if (::isalpha (is.peek ())) |
51 { | 51 { |
52 col = 0; | 52 col = 0; |
53 while (is && ::isalpha (is.peek ())) | 53 while (is && ::isalpha (is.peek ())) |
54 { | 54 { |
55 char ch = is.get (); | 55 char ch = is.get (); |
56 col *= 26; | 56 col *= 26; |
57 if (ch >= 'a') | 57 if (ch >= 'a') |
58 col += ch - 'a'; | 58 col += ch - 'a'; |
59 else | 59 else |
60 col += ch - 'A'; | 60 col += ch - 'A'; |
61 } | 61 } |
62 | 62 |
63 if (is) | 63 if (is) |
64 { | 64 { |
65 is >> row; | 65 is >> row; |
66 row --; | 66 row --; |
67 if (is) | 67 if (is) |
68 stat = true; | 68 stat = true; |
69 } | 69 } |
70 } | 70 } |
71 } | 71 } |
72 | 72 |
73 return stat; | 73 return stat; |
74 } | 74 } |
75 | 75 |
76 static bool | 76 static bool |
77 parse_range_spec (const octave_value& range_spec, | 77 parse_range_spec (const octave_value& range_spec, |
78 unsigned long& rlo, unsigned long& clo, | 78 unsigned long& rlo, unsigned long& clo, |
79 unsigned long& rup, unsigned long& cup) | 79 unsigned long& rup, unsigned long& cup) |
80 { | 80 { |
81 bool stat = true; | 81 bool stat = true; |
82 | 82 |
83 if (range_spec.is_string ()) | 83 if (range_spec.is_string ()) |
84 { | 84 { |
85 std::istringstream is (range_spec.string_value ()); | 85 std::istringstream is (range_spec.string_value ()); |
86 char ch = is.peek (); | 86 char ch = is.peek (); |
87 | 87 |
88 if (ch == '.' || ch == ':') | 88 if (ch == '.' || ch == ':') |
89 { | 89 { |
90 rlo = 0; | 90 rlo = 0; |
91 clo = 0; | 91 clo = 0; |
92 ch = is.get (); | 92 ch = is.get (); |
93 if (ch == '.') | 93 if (ch == '.') |
94 { | 94 { |
95 ch = is.get (); | 95 ch = is.get (); |
96 if (ch != '.') | 96 if (ch != '.') |
97 stat = false; | 97 stat = false; |
98 } | 98 } |
99 } | 99 } |
100 else | 100 else |
101 { | 101 { |
102 stat = read_cell_spec (is, rlo, clo); | 102 stat = read_cell_spec (is, rlo, clo); |
103 | 103 |
104 if (stat) | 104 if (stat) |
105 { | 105 { |
106 ch = is.peek (); | 106 ch = is.peek (); |
107 | 107 |
108 if (ch == '.' || ch == ':') | 108 if (ch == '.' || ch == ':') |
109 { | 109 { |
110 ch = is.get (); | 110 ch = is.get (); |
111 if (ch == '.') | 111 if (ch == '.') |
112 { | 112 { |
113 ch = is.get (); | 113 ch = is.get (); |
114 if (!is || ch != '.') | 114 if (!is || ch != '.') |
115 stat = false; | 115 stat = false; |
116 } | 116 } |
117 | 117 |
118 rup = ULONG_MAX - 1; | 118 rup = ULONG_MAX - 1; |
119 cup = ULONG_MAX - 1; | 119 cup = ULONG_MAX - 1; |
120 } | 120 } |
121 else | 121 else |
122 { | 122 { |
123 rup = rlo; | 123 rup = rlo; |
124 cup = clo; | 124 cup = clo; |
125 if (!is || !is.eof ()) | 125 if (!is || !is.eof ()) |
126 stat = false; | 126 stat = false; |
127 } | 127 } |
128 } | 128 } |
129 } | 129 } |
130 | 130 |
131 if (stat && is && !is.eof ()) | 131 if (stat && is && !is.eof ()) |
132 stat = read_cell_spec (is, rup, cup); | 132 stat = read_cell_spec (is, rup, cup); |
133 | 133 |
134 if (!is || !is.eof ()) | 134 if (!is || !is.eof ()) |
135 stat = false; | 135 stat = false; |
136 } | 136 } |
137 else if (range_spec.is_real_matrix () && range_spec.numel () == 4) | 137 else if (range_spec.is_real_matrix () && range_spec.numel () == 4) |
138 { | 138 { |
139 ColumnVector range(range_spec.vector_value ()); | 139 ColumnVector range(range_spec.vector_value ()); |
140 // double --> unsigned int | 140 // double --> unsigned int |
200 // Set default separator. | 200 // Set default separator. |
201 std::string sep; | 201 std::string sep; |
202 if (nargin > 1) | 202 if (nargin > 1) |
203 { | 203 { |
204 if (args(1).is_sq_string ()) | 204 if (args(1).is_sq_string ()) |
205 sep = do_string_escapes (args(1).string_value ()); | 205 sep = do_string_escapes (args(1).string_value ()); |
206 else | 206 else |
207 sep = args(1).string_value (); | 207 sep = args(1).string_value (); |
208 | 208 |
209 if (error_state) | 209 if (error_state) |
210 return retval; | 210 return retval; |
211 } | 211 } |
212 | 212 |
213 // Take a subset if a range was given. | 213 // Take a subset if a range was given. |
214 unsigned long r0 = 0, c0 = 0, r1 = ULONG_MAX-1, c1 = ULONG_MAX-1; | 214 unsigned long r0 = 0, c0 = 0, r1 = ULONG_MAX-1, c1 = ULONG_MAX-1; |
215 if (nargin > 2) | 215 if (nargin > 2) |
216 { | 216 { |
217 if (nargin == 3) | 217 if (nargin == 3) |
218 { | 218 { |
219 if (!parse_range_spec (args (2), r0, c0, r1, c1)) | 219 if (!parse_range_spec (args (2), r0, c0, r1, c1)) |
220 error ("dlmread: error parsing range"); | 220 error ("dlmread: error parsing range"); |
221 } | 221 } |
222 else if (nargin == 4) | 222 else if (nargin == 4) |
223 { | 223 { |
224 r0 = args(2).ulong_value (); | 224 r0 = args(2).ulong_value (); |
225 c0 = args(3).ulong_value (); | 225 c0 = args(3).ulong_value (); |
226 | 226 |
227 if (error_state) | 227 if (error_state) |
228 return retval; | 228 return retval; |
229 } | 229 } |
230 } | 230 } |
231 | 231 |
232 if (!error_state) | 232 if (!error_state) |
233 { | 233 { |
234 unsigned long i = 0, j = 0, r = 1, c = 1, rmax = 0, cmax = 0; | 234 unsigned long i = 0, j = 0, r = 1, c = 1, rmax = 0, cmax = 0; |
243 | 243 |
244 std::string line; | 244 std::string line; |
245 | 245 |
246 // Skip the r0 leading lines as these might be a header. | 246 // Skip the r0 leading lines as these might be a header. |
247 for (unsigned long m = 0; m < r0; m++) | 247 for (unsigned long m = 0; m < r0; m++) |
248 getline (file, line); | 248 getline (file, line); |
249 r1 -= r0; | 249 r1 -= r0; |
250 | 250 |
251 // Read in the data one field at a time, growing the data matrix | 251 // Read in the data one field at a time, growing the data matrix |
252 // as needed. | 252 // as needed. |
253 while (getline (file, line)) | 253 while (getline (file, line)) |
254 { | 254 { |
255 // Skip blank lines for compatibility. | 255 // Skip blank lines for compatibility. |
256 if (line.find_first_not_of (" \t") == std::string::npos) | 256 if (line.find_first_not_of (" \t") == std::string::npos) |
257 continue; | 257 continue; |
258 | 258 |
259 // To be compatible with matlab, blank separator should | 259 // To be compatible with matlab, blank separator should |
260 // correspond to whitespace as delimter. | 260 // correspond to whitespace as delimter. |
261 if (!sep.length ()) | 261 if (!sep.length ()) |
262 { | 262 { |
263 size_t n = line.find_first_of (",:; \t", | 263 size_t n = line.find_first_of (",:; \t", |
264 line.find_first_of ("0123456789")); | 264 line.find_first_of ("0123456789")); |
265 if (n == std::string::npos) | 265 if (n == std::string::npos) |
266 { | 266 { |
267 sep = " \t"; | 267 sep = " \t"; |
268 sepflag = true; | 268 sepflag = true; |
269 } | 269 } |
270 else | 270 else |
271 { | 271 { |
272 char ch = line.at (n); | 272 char ch = line.at (n); |
273 | 273 |
274 switch (line.at (n)) | 274 switch (line.at (n)) |
275 { | 275 { |
276 case ' ': | 276 case ' ': |
277 case '\t': | 277 case '\t': |
278 sepflag = true; | 278 sepflag = true; |
279 sep = " \t"; | 279 sep = " \t"; |
280 break; | 280 break; |
281 | 281 |
282 default: | 282 default: |
283 sep = ch; | 283 sep = ch; |
284 break; | 284 break; |
285 } | 285 } |
286 } | 286 } |
287 } | 287 } |
288 | 288 |
289 r = (r > i + 1 ? r : i + 1); | 289 r = (r > i + 1 ? r : i + 1); |
290 j = 0; | 290 j = 0; |
291 size_t pos1 = 0; | 291 size_t pos1 = 0; |
292 do | 292 do |
293 { | 293 { |
294 size_t pos2 = line.find_first_of (sep, pos1); | 294 size_t pos2 = line.find_first_of (sep, pos1); |
295 std::string str = line.substr (pos1, pos2 - pos1); | 295 std::string str = line.substr (pos1, pos2 - pos1); |
296 | 296 |
297 if (sepflag && pos2 != std::string::npos) | 297 if (sepflag && pos2 != std::string::npos) |
298 // Treat consecutive separators as one. | 298 // Treat consecutive separators as one. |
299 pos2 = line.find_first_not_of (sep, pos2) - 1; | 299 pos2 = line.find_first_not_of (sep, pos2) - 1; |
300 | 300 |
301 c = (c > j + 1 ? c : j + 1); | 301 c = (c > j + 1 ? c : j + 1); |
302 if (r > rmax || c > cmax) | 302 if (r > rmax || c > cmax) |
303 { | 303 { |
304 // Use resize_and_fill for the case of not-equal | 304 // Use resize_and_fill for the case of not-equal |
305 // length rows. | 305 // length rows. |
306 if (iscmplx) | 306 if (iscmplx) |
307 cdata.resize_fill (r, c, 0); | 307 cdata.resize_fill (r, c, 0); |
308 else | 308 else |
309 rdata.resize_fill (r, c, 0); | 309 rdata.resize_fill (r, c, 0); |
310 rmax = r; | 310 rmax = r; |
311 cmax = c; | 311 cmax = c; |
312 } | 312 } |
313 | 313 |
314 std::istringstream tmp_stream (str); | 314 std::istringstream tmp_stream (str); |
315 double x = octave_read_double (tmp_stream); | 315 double x = octave_read_double (tmp_stream); |
316 if (tmp_stream) | 316 if (tmp_stream) |
317 { | 317 { |
318 if (tmp_stream.eof ()) | 318 if (tmp_stream.eof ()) |
319 if (iscmplx) | 319 if (iscmplx) |
320 cdata(i,j++) = x; | 320 cdata(i,j++) = x; |
321 else | 321 else |
322 rdata(i,j++) = x; | 322 rdata(i,j++) = x; |
323 else | 323 else |
324 { | 324 { |
325 double y = octave_read_double (tmp_stream); | 325 double y = octave_read_double (tmp_stream); |
326 | 326 |
327 if (!iscmplx && y != 0.) | 327 if (!iscmplx && y != 0.) |
328 { | 328 { |
329 iscmplx = true; | 329 iscmplx = true; |
330 cdata = ComplexMatrix (rdata); | 330 cdata = ComplexMatrix (rdata); |
331 } | 331 } |
332 | 332 |
333 if (iscmplx) | 333 if (iscmplx) |
334 cdata(i,j++) = Complex (x, y); | 334 cdata(i,j++) = Complex (x, y); |
335 else | 335 else |
336 rdata(i,j++) = x; | 336 rdata(i,j++) = x; |
337 } | 337 } |
338 } | 338 } |
339 else if (iscmplx) | 339 else if (iscmplx) |
340 cdata(i,j++) = 0.; | 340 cdata(i,j++) = 0.; |
341 else | 341 else |
342 rdata(i,j++) = 0.; | 342 rdata(i,j++) = 0.; |
343 | 343 |
344 if (pos2 != std::string::npos) | 344 if (pos2 != std::string::npos) |
345 pos1 = pos2 + 1; | 345 pos1 = pos2 + 1; |
346 else | 346 else |
347 pos1 = std::string::npos; | 347 pos1 = std::string::npos; |
348 | 348 |
349 } | 349 } |
350 while (pos1 != std::string::npos); | 350 while (pos1 != std::string::npos); |
351 | 351 |
352 if (nargin == 3 && i == maxrows) | 352 if (nargin == 3 && i == maxrows) |
353 break; | 353 break; |
354 | 354 |
355 i++; | 355 i++; |
356 } | 356 } |
357 | 357 |
358 if (nargin > 2) | 358 if (nargin > 2) |
359 { | 359 { |
360 if (nargin == 3) | 360 if (nargin == 3) |
361 { | 361 { |
362 if (r1 >= r) | 362 if (r1 >= r) |
363 r1 = r - 1; | 363 r1 = r - 1; |
364 if (c1 >= c) | 364 if (c1 >= c) |
365 c1 = c - 1; | 365 c1 = c - 1; |
366 } | 366 } |
367 else if (nargin == 4) | 367 else if (nargin == 4) |
368 { | 368 { |
369 // If r1 and c1 are not given, use what was found to be | 369 // If r1 and c1 are not given, use what was found to be |
370 // the maximum. | 370 // the maximum. |
371 r1 = r - 1; | 371 r1 = r - 1; |
372 c1 = c - 1; | 372 c1 = c - 1; |
373 } | 373 } |
374 | 374 |
375 // Now take the subset of the matrix. | 375 // Now take the subset of the matrix. |
376 if (iscmplx) | 376 if (iscmplx) |
377 { | 377 { |
378 cdata = cdata.extract (0, c0, r1, c1); | 378 cdata = cdata.extract (0, c0, r1, c1); |
379 cdata.resize (r1 + 1, c1 - c0 + 1); | 379 cdata.resize (r1 + 1, c1 - c0 + 1); |
380 } | 380 } |
381 else | 381 else |
382 { | 382 { |
383 rdata = rdata.extract (0, c0, r1, c1); | 383 rdata = rdata.extract (0, c0, r1, c1); |
384 rdata.resize (r1 + 1, c1 - c0 + 1); | 384 rdata.resize (r1 + 1, c1 - c0 + 1); |
385 } | 385 } |
386 } | 386 } |
387 | 387 |
388 if (iscmplx) | 388 if (iscmplx) |
389 retval(0) = cdata; | 389 retval(0) = cdata; |
390 else | 390 else |
391 retval(0) = rdata; | 391 retval(0) = rdata; |
392 } | 392 } |
393 | 393 |
394 return retval; | 394 return retval; |
395 } | 395 } |
396 | 396 |