1
|
1 // file-io.cc -*- C++ -*- |
|
2 /* |
|
3 |
332
|
4 Copyright (C) 1993, 1994 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 // Written by John C. Campbell <jcc@che.utexas.edu>. |
|
25 |
240
|
26 #ifdef HAVE_CONFIG_H |
|
27 #include "config.h" |
1
|
28 #endif |
|
29 |
|
30 #include <DLList.h> |
|
31 #include <unistd.h> |
|
32 #include <string.h> |
|
33 #include <stdio.h> |
444
|
34 #include <errno.h> |
1
|
35 #include <stdlib.h> |
|
36 #include <strstream.h> |
|
37 #include <ctype.h> |
|
38 |
456
|
39 #include "dMatrix.h" |
444
|
40 |
1
|
41 #include "statdefs.h" |
|
42 #include "file-io.h" |
|
43 #include "input.h" |
|
44 #include "octave-hist.h" |
|
45 #include "tree-const.h" |
|
46 #include "error.h" |
542
|
47 #include "help.h" |
1
|
48 #include "utils.h" |
|
49 #include "pager.h" |
529
|
50 #include "defun.h" |
444
|
51 #include "sysdep.h" |
|
52 #include "mappers.h" |
529
|
53 #include "variables.h" |
1
|
54 |
|
55 // keeps a count of how many files are open and in the file list |
|
56 static int file_count = 0; |
|
57 |
|
58 // keeps a count of args sent to printf or scanf |
|
59 static int fmt_arg_count = 0; |
|
60 |
759
|
61 // double linked list containing relevant information about open files |
|
62 static DLList <file_info> file_list; |
1
|
63 |
240
|
64 file_info::file_info (void) |
1
|
65 { |
334
|
66 file_number = -1; |
529
|
67 file_name = 0; |
|
68 file_fptr = 0; |
|
69 file_mode = 0; |
1
|
70 } |
|
71 |
240
|
72 file_info::file_info (int n, const char *nm, FILE *t, const char *md) |
|
73 { |
334
|
74 file_number = n; |
|
75 file_name = strsave (nm); |
|
76 file_fptr = t; |
|
77 file_mode = strsave (md); |
240
|
78 } |
|
79 |
|
80 file_info::file_info (const file_info& f) |
1
|
81 { |
367
|
82 file_number = f.file_number; |
|
83 file_name = strsave (f.file_name); |
|
84 file_fptr = f.file_fptr; |
|
85 file_mode = strsave (f.file_mode); |
1
|
86 } |
|
87 |
240
|
88 file_info& |
|
89 file_info::operator = (const file_info& f) |
1
|
90 { |
240
|
91 if (this != & f) |
|
92 { |
367
|
93 file_number = f.file_number; |
334
|
94 delete [] file_name; |
|
95 file_name = strsave (f.file_name); |
|
96 file_fptr = f.file_fptr; |
|
97 delete [] file_mode; |
|
98 file_mode = strsave (f.file_mode); |
240
|
99 } |
1
|
100 return *this; |
|
101 } |
|
102 |
240
|
103 file_info::~file_info (void) |
1
|
104 { |
334
|
105 delete [] file_name; |
|
106 delete [] file_mode; |
1
|
107 } |
|
108 |
|
109 int |
240
|
110 file_info::number (void) const |
1
|
111 { |
334
|
112 return file_number; |
1
|
113 } |
|
114 |
240
|
115 const char * |
|
116 file_info::name (void) const |
1
|
117 { |
334
|
118 return file_name; |
1
|
119 } |
|
120 |
|
121 FILE * |
240
|
122 file_info::fptr (void) const |
1
|
123 { |
334
|
124 return file_fptr; |
1
|
125 } |
|
126 |
240
|
127 const char * |
|
128 file_info::mode (void) const |
1
|
129 { |
334
|
130 return file_mode; |
1
|
131 } |
|
132 |
|
133 void |
164
|
134 initialize_file_io (void) |
1
|
135 { |
334
|
136 file_info octave_stdin (0, "stdin", stdin, "r"); |
|
137 file_info octave_stdout (1, "stdout", stdout, "w"); |
|
138 file_info octave_stderr (2, "stderr", stderr, "w"); |
1
|
139 |
334
|
140 file_list.append (octave_stdin); |
|
141 file_list.append (octave_stdout); |
|
142 file_list.append (octave_stderr); |
1
|
143 |
|
144 file_count = 3; |
|
145 } |
|
146 |
761
|
147 // Given a file name or number, return a pointer to the corresponding |
|
148 // open file. If the file has not already been opened, return NULL. |
|
149 |
1
|
150 Pix |
164
|
151 return_valid_file (const tree_constant& arg) |
1
|
152 { |
610
|
153 if (arg.is_string ()) |
1
|
154 { |
|
155 Pix p = file_list.first (); |
240
|
156 file_info file; |
1
|
157 for (int i = 0; i < file_count; i++) |
|
158 { |
|
159 char *file_name = arg.string_value (); |
|
160 file = file_list (p); |
|
161 if (strcmp (file.name (), file_name) == 0) |
|
162 return p; |
|
163 file_list.next (p); |
|
164 } |
|
165 } |
636
|
166 else |
1
|
167 { |
|
168 double file_num = arg.double_value (); |
636
|
169 |
|
170 if (! error_state) |
1
|
171 { |
636
|
172 if ((double) NINT (file_num) != file_num) |
|
173 error ("file number not an integer value"); |
|
174 else |
1
|
175 { |
636
|
176 Pix p = file_list.first (); |
|
177 file_info file; |
|
178 for (int i = 0; i < file_count; i++) |
|
179 { |
|
180 file = file_list (p); |
|
181 if (file.number () == file_num) |
|
182 return p; |
|
183 file_list.next (p); |
|
184 } |
|
185 error ("no file with that number"); |
1
|
186 } |
|
187 } |
636
|
188 else |
|
189 error ("inapproriate file specifier"); |
|
190 } |
1
|
191 |
529
|
192 return 0; |
1
|
193 } |
|
194 |
|
195 static Pix |
636
|
196 fopen_file_for_user (const char *name, const char *mode, |
370
|
197 const char *warn_for) |
1
|
198 { |
636
|
199 FILE *file_ptr = fopen (name, mode); |
529
|
200 if (file_ptr) |
1
|
201 { |
636
|
202 file_info file (++file_count, name, file_ptr, mode); |
1
|
203 file_list.append (file); |
|
204 |
|
205 Pix p = file_list.first (); |
240
|
206 file_info file_from_list; |
1
|
207 |
|
208 for (int i = 0; i < file_count; i++) |
|
209 { |
|
210 file_from_list = file_list (p); |
636
|
211 if (strcmp (file_from_list.name (), name) == 0) |
1
|
212 return p; |
|
213 file_list.next (p); |
|
214 } |
|
215 } |
|
216 |
636
|
217 error ("%s: unable to open file `%s'", warn_for, name); |
240
|
218 |
529
|
219 return 0; |
1
|
220 } |
|
221 |
368
|
222 static Pix |
636
|
223 file_io_get_file (const tree_constant& arg, const char *mode, |
368
|
224 const char *warn_for) |
|
225 { |
|
226 Pix p = return_valid_file (arg); |
|
227 |
529
|
228 if (! p) |
368
|
229 { |
610
|
230 if (arg.is_string ()) |
368
|
231 { |
|
232 char *name = arg.string_value (); |
|
233 |
|
234 struct stat buffer; |
|
235 int status = stat (name, &buffer); |
|
236 |
372
|
237 if (status == 0) |
368
|
238 { |
|
239 if ((buffer.st_mode & S_IFREG) == S_IFREG) |
636
|
240 p = fopen_file_for_user (name, mode, warn_for); |
368
|
241 else |
|
242 error ("%s: invalid file type", warn_for); |
|
243 } |
372
|
244 else if (status < 0 && *mode != 'r') |
636
|
245 p = fopen_file_for_user (name, mode, warn_for); |
368
|
246 else |
|
247 error ("%s: can't stat file `%s'", warn_for, name); |
|
248 } |
|
249 else |
|
250 error ("%s: invalid file specifier", warn_for); |
|
251 } |
|
252 |
|
253 return p; |
|
254 } |
1
|
255 |
712
|
256 DEFUN ("fclose", Ffclose, Sfclose, 1, 1, |
529
|
257 "fclose (FILENAME or FILENUM): close a file") |
|
258 { |
|
259 Octave_object retval; |
|
260 |
|
261 int nargin = args.length (); |
|
262 |
712
|
263 if (nargin != 1) |
529
|
264 print_usage ("fclose"); |
|
265 else |
|
266 retval = fclose_internal (args); |
|
267 |
|
268 return retval; |
|
269 } |
|
270 |
497
|
271 Octave_object |
|
272 fclose_internal (const Octave_object& args) |
1
|
273 { |
497
|
274 Octave_object retval; |
1
|
275 |
712
|
276 Pix p = return_valid_file (args(0)); |
1
|
277 |
529
|
278 if (! p) |
1
|
279 return retval; |
|
280 |
240
|
281 file_info file = file_list (p); |
1
|
282 |
|
283 if (file.number () < 3) |
|
284 { |
|
285 warning ("fclose: can't close stdin, stdout, or stderr!"); |
|
286 return retval; |
|
287 } |
|
288 |
|
289 int success = fclose (file.fptr ()); |
|
290 file_list.del (p); |
|
291 file_count--; |
|
292 |
|
293 if (success == 0) |
636
|
294 retval(0) = 1.0; // succeeded |
1
|
295 else |
|
296 { |
|
297 error ("fclose: error on closing file"); |
636
|
298 retval(0) = 0.0; // failed |
1
|
299 } |
|
300 |
|
301 return retval; |
|
302 } |
|
303 |
712
|
304 DEFUN ("fflush", Ffflush, Sfflush, 1, 1, |
529
|
305 "fflush (FILENAME or FILENUM): flush buffered data to output file") |
|
306 { |
|
307 Octave_object retval; |
|
308 |
|
309 int nargin = args.length (); |
|
310 |
712
|
311 if (nargin != 1) |
529
|
312 print_usage ("fflush"); |
|
313 else |
|
314 retval = fflush_internal (args); |
|
315 |
|
316 return retval; |
|
317 } |
|
318 |
497
|
319 Octave_object |
|
320 fflush_internal (const Octave_object& args) |
1
|
321 { |
497
|
322 Octave_object retval; |
1
|
323 |
712
|
324 Pix p = return_valid_file (args(0)); |
1
|
325 |
529
|
326 if (! p) |
1
|
327 return retval; |
|
328 |
240
|
329 file_info file = file_list (p); |
1
|
330 |
|
331 if (strcmp (file.mode (), "r") == 0) |
|
332 { |
|
333 warning ("can't flush an input stream"); |
|
334 return retval; |
|
335 } |
|
336 |
|
337 int success = 0; |
240
|
338 |
1
|
339 if (file.number () == 1) |
|
340 flush_output_to_pager (); |
|
341 else |
|
342 success = fflush (file.fptr ()); |
|
343 |
|
344 if (success == 0) |
636
|
345 retval(0) = 1.0; // succeeded |
1
|
346 else |
|
347 { |
|
348 error ("fflush: write error"); |
636
|
349 retval(0) = 0.0; // failed |
1
|
350 } |
|
351 |
|
352 return retval; |
|
353 } |
|
354 |
|
355 static int |
164
|
356 valid_mode (const char *mode) |
1
|
357 { |
529
|
358 if (mode) |
1
|
359 { |
|
360 char m = mode[0]; |
|
361 if (m == 'r' || m == 'w' || m == 'a') |
|
362 { |
|
363 m = mode[1]; |
|
364 return (m == '\0' || (m == '+' && mode[2] == '\0')); |
|
365 } |
|
366 } |
|
367 return 0; |
|
368 } |
|
369 |
712
|
370 DEFUN ("fgets", Ffgets, Sfgets, 2, 2, |
529
|
371 "[STRING, LENGTH] = fgets (FILENAME or FILENUM, LENGTH)\n\ |
|
372 \n\ |
|
373 read a string from a file") |
|
374 { |
|
375 Octave_object retval; |
|
376 |
|
377 int nargin = args.length (); |
|
378 |
712
|
379 if (nargin != 2) |
529
|
380 print_usage ("fgets"); |
|
381 else |
|
382 retval = fgets_internal (args, nargout); |
|
383 |
|
384 return retval; |
|
385 } |
|
386 |
497
|
387 Octave_object |
|
388 fgets_internal (const Octave_object& args, int nargout) |
1
|
389 { |
497
|
390 Octave_object retval; |
1
|
391 |
712
|
392 Pix p = file_io_get_file (args(0), "r", "fgets"); |
1
|
393 |
529
|
394 if (! p) |
368
|
395 return retval; |
|
396 |
636
|
397 |
712
|
398 double dlen = args(1).double_value (); |
636
|
399 |
|
400 if (error_state) |
|
401 return retval; |
|
402 |
|
403 int length = NINT (dlen); |
|
404 |
|
405 if ((double) length != dlen) |
1
|
406 { |
636
|
407 error ("fgets: length not an integer value"); |
|
408 return retval; |
1
|
409 } |
|
410 |
368
|
411 file_info file = file_list (p); |
|
412 |
636
|
413 char string [length+1]; |
1
|
414 char *success = fgets (string, length+1, file.fptr ()); |
|
415 |
529
|
416 if (! success) |
1
|
417 { |
636
|
418 retval(0) = -1.0; |
1
|
419 return retval; |
|
420 } |
|
421 |
|
422 if (nargout == 2) |
636
|
423 retval(1) = (double) strlen (string); |
1
|
424 |
636
|
425 retval(0) = string; |
1
|
426 |
|
427 return retval; |
|
428 } |
|
429 |
712
|
430 DEFUN ("fopen", Ffopen, Sfopen, 2, 1, |
529
|
431 "FILENUM = fopen (FILENAME, MODE): open a file\n\ |
|
432 \n\ |
|
433 Valid values for mode include:\n\ |
|
434 \n\ |
|
435 r : open text file for reading\n\ |
|
436 w : open text file for writing; discard previous contents if any\n\ |
|
437 a : append; open or create text file for writing at end of file\n\ |
|
438 r+ : open text file for update (i.e., reading and writing)\n\ |
|
439 w+ : create text file for update; discard previous contents if any\n\ |
|
440 a+ : append; open or create text file for update, writing at end\n\n\ |
|
441 Update mode permits reading from and writing to the same file.") |
|
442 { |
|
443 Octave_object retval; |
|
444 |
|
445 int nargin = args.length (); |
|
446 |
712
|
447 if (nargin != 2) |
529
|
448 print_usage ("fopen"); |
|
449 else |
|
450 retval = fopen_internal (args); |
|
451 |
|
452 return retval; |
|
453 } |
|
454 |
497
|
455 Octave_object |
|
456 fopen_internal (const Octave_object& args) |
1
|
457 { |
497
|
458 Octave_object retval; |
1
|
459 Pix p; |
|
460 |
712
|
461 if (! args(0).is_string ()) |
1
|
462 { |
|
463 error ("fopen: file name must be a string"); |
|
464 return retval; |
|
465 } |
|
466 |
712
|
467 p = return_valid_file (args(0)); |
1
|
468 |
529
|
469 if (p) |
1
|
470 { |
240
|
471 file_info file = file_list (p); |
1
|
472 |
636
|
473 retval(0) = (double) file.number (); |
1
|
474 |
|
475 return retval; |
|
476 } |
|
477 |
712
|
478 if (! args(1).is_string ()) |
1
|
479 { |
370
|
480 error ("fopen: file mode must be a string"); |
1
|
481 return retval; |
|
482 } |
|
483 |
712
|
484 char *name = args(0).string_value (); |
|
485 char *mode = args(1).string_value (); |
1
|
486 |
|
487 if (! valid_mode (mode)) |
|
488 { |
|
489 error ("fopen: invalid mode"); |
|
490 return retval; |
|
491 } |
|
492 |
|
493 struct stat buffer; |
|
494 if (stat (name, &buffer) == 0 && (buffer.st_mode & S_IFDIR) == S_IFDIR) |
|
495 { |
|
496 error ("fopen: can't open directory"); |
|
497 return retval; |
|
498 } |
|
499 |
|
500 FILE *file_ptr = fopen (name, mode); |
|
501 |
529
|
502 if (! file_ptr) |
1
|
503 { |
370
|
504 error ("fopen: unable to open file `%s'", name); |
1
|
505 return retval; |
|
506 } |
|
507 |
|
508 int number = file_count++; |
|
509 |
240
|
510 file_info file (number, name, file_ptr, mode); |
1
|
511 file_list.append (file); |
|
512 |
636
|
513 retval(0) = (double) number; |
1
|
514 |
|
515 return retval; |
|
516 } |
|
517 |
712
|
518 DEFUN ("freport", Ffreport, Sfreport, 0, 1, |
529
|
519 "freport (): list open files and their status") |
|
520 { |
|
521 Octave_object retval; |
|
522 |
|
523 int nargin = args.length (); |
|
524 |
712
|
525 if (nargin > 0) |
529
|
526 warning ("freport: ignoring extra arguments"); |
|
527 |
|
528 retval = freport_internal (); |
|
529 |
|
530 return retval; |
|
531 } |
|
532 |
497
|
533 Octave_object |
164
|
534 freport_internal (void) |
1
|
535 { |
497
|
536 Octave_object retval; |
1
|
537 Pix p = file_list.first (); |
|
538 |
|
539 ostrstream output_buf; |
|
540 |
|
541 output_buf << "\n number mode name\n\n"; |
|
542 for (int i = 0; i < file_count; i++) |
|
543 { |
240
|
544 file_info file = file_list (p); |
1
|
545 output_buf.form ("%7d%6s %s\n", file.number (), file.mode (), |
|
546 file.name ()); |
|
547 file_list.next (p); |
|
548 } |
|
549 |
|
550 output_buf << "\n" << ends; |
|
551 maybe_page_output (output_buf); |
|
552 |
|
553 return retval; |
|
554 } |
|
555 |
712
|
556 DEFUN ("frewind", Ffrewind, Sfrewind, 1, 1, |
529
|
557 "frewind (FILENAME or FILENUM): set file position at beginning of file") |
|
558 { |
|
559 Octave_object retval; |
|
560 |
|
561 int nargin = args.length (); |
|
562 |
712
|
563 if (nargin != 1) |
529
|
564 print_usage ("frewind"); |
|
565 else |
|
566 retval = frewind_internal (args); |
|
567 |
|
568 return retval; |
|
569 } |
|
570 |
497
|
571 Octave_object |
|
572 frewind_internal (const Octave_object& args) |
1
|
573 { |
497
|
574 Octave_object retval; |
1
|
575 |
712
|
576 Pix p = file_io_get_file (args(0), "a+", "frewind"); |
1
|
577 |
529
|
578 if (p) |
368
|
579 { |
|
580 file_info file = file_list (p); |
|
581 rewind (file.fptr ()); |
|
582 } |
1
|
583 |
|
584 return retval; |
|
585 } |
|
586 |
712
|
587 DEFUN ("fseek", Ffseek, Sfseek, 3, 1, |
529
|
588 "fseek (FILENAME or FILENUM, OFFSET [, ORIGIN])\n\ |
|
589 \n\ |
|
590 set file position for reading or writing") |
|
591 { |
|
592 Octave_object retval; |
|
593 |
|
594 int nargin = args.length (); |
|
595 |
712
|
596 if (nargin != 2 && nargin != 3) |
529
|
597 print_usage ("fseek"); |
|
598 else |
|
599 retval = fseek_internal (args); |
|
600 |
|
601 return retval; |
|
602 } |
|
603 |
497
|
604 Octave_object |
506
|
605 fseek_internal (const Octave_object& args) |
1
|
606 { |
497
|
607 Octave_object retval; |
1
|
608 |
506
|
609 int nargin = args.length (); |
|
610 |
712
|
611 Pix p = file_io_get_file (args(0), "a+", "fseek"); |
1
|
612 |
529
|
613 if (! p) |
368
|
614 return retval; |
1
|
615 |
|
616 long origin = SEEK_SET; |
636
|
617 |
712
|
618 double doff = args(1).double_value (); |
636
|
619 |
|
620 if (error_state) |
|
621 return retval; |
|
622 |
|
623 long offset = NINT (doff); |
|
624 |
|
625 if ((double) offset != doff) |
1
|
626 { |
636
|
627 error ("fseek: offset not an integer value"); |
|
628 return retval; |
|
629 } |
|
630 |
712
|
631 if (nargin == 3) |
636
|
632 { |
712
|
633 double dorig = args(2).double_value (); |
636
|
634 |
|
635 if (error_state) |
|
636 return retval; |
|
637 |
|
638 origin = NINT (dorig); |
|
639 |
|
640 if ((double) dorig != origin) |
1
|
641 { |
636
|
642 error ("fseek: origin not an integer value"); |
1
|
643 return retval; |
|
644 } |
|
645 |
763
|
646 if (origin == 0) |
|
647 origin = SEEK_SET; |
|
648 else if (origin == 1) |
1
|
649 origin = SEEK_CUR; |
763
|
650 else if (origin == 2) |
1
|
651 origin = SEEK_END; |
|
652 else |
|
653 { |
636
|
654 error ("fseek: invalid value for origin"); |
|
655 return retval; |
1
|
656 } |
|
657 } |
|
658 |
240
|
659 file_info file = file_list (p); |
1
|
660 int success = fseek (file.fptr (), offset, origin); |
|
661 |
|
662 if (success == 0) |
636
|
663 retval(0) = 1.0; // succeeded |
1
|
664 else |
|
665 { |
|
666 error ("fseek: file error"); |
636
|
667 retval(0) = 0.0; // failed |
1
|
668 } |
|
669 |
|
670 return retval; |
|
671 } |
|
672 |
761
|
673 // Tell current position of file. |
|
674 |
712
|
675 DEFUN ("ftell", Fftell, Sftell, 1, 1, |
529
|
676 "POSITION = ftell (FILENAME or FILENUM): returns the current file position") |
|
677 { |
|
678 Octave_object retval; |
|
679 |
|
680 int nargin = args.length (); |
|
681 |
712
|
682 if (nargin != 1) |
529
|
683 print_usage ("ftell"); |
|
684 else |
|
685 retval = ftell_internal (args); |
|
686 |
|
687 return retval; |
|
688 } |
|
689 |
497
|
690 Octave_object |
|
691 ftell_internal (const Octave_object& args) |
1
|
692 { |
497
|
693 Octave_object retval; |
1
|
694 |
712
|
695 Pix p = file_io_get_file (args(0), "a+", "ftell"); |
1
|
696 |
529
|
697 if (p) |
368
|
698 { |
|
699 file_info file = file_list (p); |
|
700 long offset = ftell (file.fptr ()); |
636
|
701 |
|
702 retval(0) = (double) offset; |
1
|
703 |
368
|
704 if (offset == -1L) |
|
705 error ("ftell: write error"); |
|
706 } |
1
|
707 |
|
708 return retval; |
|
709 } |
|
710 |
|
711 void |
164
|
712 close_files (void) |
1
|
713 { |
|
714 Pix p = file_list.first (); |
|
715 |
|
716 for (int i = 0; i < file_count; i++) |
|
717 { |
240
|
718 file_info file = file_list (p); |
1
|
719 if (i > 2) // do not close stdin, stdout, stderr! |
|
720 { |
|
721 int success = fclose (file.fptr ()); |
|
722 if (success != 0) |
|
723 error ("closing %s", file.name ()); |
|
724 } |
|
725 file_list.del (p); |
|
726 } |
|
727 } |
|
728 |
|
729 static int |
497
|
730 process_printf_format (const char *s, const Octave_object& args, |
506
|
731 ostrstream& sb, const char *type) |
1
|
732 { |
|
733 ostrstream fmt; |
|
734 |
506
|
735 int nargin = args.length (); |
|
736 |
1
|
737 fmt << "%"; // do_printf() already blew past this one... |
|
738 |
|
739 int chars_from_fmt_str = 0; |
|
740 |
|
741 again: |
|
742 switch (*s) |
|
743 { |
|
744 case '+': case '-': case ' ': case '0': case '#': |
|
745 chars_from_fmt_str++; |
|
746 fmt << *s++; |
|
747 goto again; |
|
748 |
|
749 case '\0': |
|
750 goto invalid_format; |
|
751 |
|
752 default: |
|
753 break; |
|
754 } |
|
755 |
|
756 if (*s == '*') |
|
757 { |
712
|
758 if (fmt_arg_count > nargin) |
1
|
759 { |
218
|
760 error ("%s: not enough arguments", type); |
1
|
761 return -1; |
|
762 } |
|
763 |
636
|
764 double tmp_len = args(fmt_arg_count++).double_value (); |
|
765 |
|
766 if (error_state) |
1
|
767 { |
218
|
768 error ("%s: `*' must be replaced by an integer", type); |
1
|
769 return -1; |
|
770 } |
|
771 |
636
|
772 fmt << NINT (tmp_len); |
1
|
773 s++; |
|
774 chars_from_fmt_str++; |
|
775 } |
|
776 else |
|
777 { |
|
778 while (*s != '\0' && isdigit (*s)) |
|
779 { |
|
780 chars_from_fmt_str++; |
|
781 fmt << *s++; |
|
782 } |
|
783 } |
|
784 |
|
785 if (*s == '\0') |
|
786 goto invalid_format; |
|
787 |
|
788 if (*s == '.') |
|
789 { |
|
790 chars_from_fmt_str++; |
|
791 fmt << *s++; |
|
792 } |
|
793 |
|
794 if (*s == '*') |
|
795 { |
|
796 if (*(s-1) == '*') |
|
797 goto invalid_format; |
|
798 |
712
|
799 if (fmt_arg_count > nargin) |
1
|
800 { |
218
|
801 error ("%s: not enough arguments", type); |
1
|
802 return -1; |
|
803 } |
|
804 |
636
|
805 double tmp_len = args(fmt_arg_count++).double_value (); |
|
806 |
|
807 if (error_state) |
1
|
808 { |
218
|
809 error ("%s: `*' must be replaced by an integer", type); |
1
|
810 return -1; |
|
811 } |
|
812 |
636
|
813 fmt << NINT (tmp_len); |
1
|
814 s++; |
|
815 chars_from_fmt_str++; |
|
816 } |
|
817 else |
|
818 { |
|
819 while (*s != '\0' && isdigit (*s)) |
|
820 { |
|
821 chars_from_fmt_str++; |
|
822 fmt << *s++; |
|
823 } |
|
824 } |
|
825 |
|
826 if (*s == '\0') |
|
827 goto invalid_format; |
|
828 |
|
829 if (*s != '\0' && (*s == 'h' || *s == 'l' || *s == 'L')) |
|
830 { |
|
831 chars_from_fmt_str++; |
|
832 fmt << *s++; |
|
833 } |
|
834 |
|
835 if (*s == '\0') |
|
836 goto invalid_format; |
|
837 |
712
|
838 if (fmt_arg_count > nargin) |
1
|
839 { |
218
|
840 error ("%s: not enough arguments", type); |
1
|
841 return -1; |
|
842 } |
|
843 |
|
844 switch (*s) |
|
845 { |
|
846 case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': |
636
|
847 { |
|
848 double d = args(fmt_arg_count++).double_value (); |
1
|
849 |
636
|
850 int val = NINT (d); |
|
851 |
|
852 if (error_state || (double) val != d) |
|
853 goto invalid_conversion; |
|
854 else |
|
855 { |
|
856 chars_from_fmt_str++; |
|
857 fmt << *s << ends; |
|
858 char *tmp_fmt = fmt.str (); |
|
859 sb.form (tmp_fmt, val); |
|
860 delete [] tmp_fmt; |
|
861 return chars_from_fmt_str; |
|
862 } |
|
863 } |
1
|
864 |
|
865 case 'e': case 'E': case 'f': case 'g': case 'G': |
636
|
866 { |
|
867 double val = args(fmt_arg_count++).double_value (); |
1
|
868 |
636
|
869 if (error_state) |
|
870 goto invalid_conversion; |
|
871 else |
|
872 { |
|
873 chars_from_fmt_str++; |
|
874 fmt << *s << ends; |
|
875 char *tmp_fmt = fmt.str (); |
|
876 sb.form (tmp_fmt, val); |
|
877 delete [] tmp_fmt; |
|
878 return chars_from_fmt_str; |
|
879 } |
|
880 } |
1
|
881 |
|
882 case 's': |
636
|
883 { |
|
884 char *val = args(fmt_arg_count++).string_value (); |
1
|
885 |
636
|
886 if (error_state) |
|
887 goto invalid_conversion; |
|
888 else |
|
889 { |
|
890 chars_from_fmt_str++; |
|
891 fmt << *s << ends; |
|
892 char *tmp_fmt = fmt.str (); |
|
893 sb.form (tmp_fmt, val); |
|
894 delete [] tmp_fmt; |
|
895 return chars_from_fmt_str; |
|
896 } |
|
897 } |
1
|
898 |
|
899 case 'c': |
636
|
900 { |
|
901 char *val = args(fmt_arg_count++).string_value (); |
1
|
902 |
636
|
903 if (error_state || strlen (val) != 1) |
|
904 goto invalid_conversion; |
|
905 else |
|
906 { |
|
907 chars_from_fmt_str++; |
|
908 fmt << *s << ends; |
|
909 char *tmp_fmt = fmt.str (); |
|
910 sb.form (tmp_fmt, *val); |
|
911 delete [] tmp_fmt; |
|
912 return chars_from_fmt_str; |
|
913 } |
|
914 } |
1
|
915 |
|
916 default: |
|
917 goto invalid_format; |
|
918 } |
|
919 |
|
920 invalid_conversion: |
218
|
921 error ("%s: invalid conversion", type); |
1
|
922 return -1; |
|
923 |
|
924 invalid_format: |
218
|
925 error ("%s: invalid format", type); |
1
|
926 return -1; |
|
927 } |
|
928 |
761
|
929 // Formatted printing to a file. |
1
|
930 |
529
|
931 DEFUN ("fprintf", Ffprintf, Sfprintf, -1, 1, |
|
932 "fprintf (FILENAME or FILENUM, FORMAT, ...)") |
|
933 { |
|
934 Octave_object retval; |
|
935 |
|
936 int nargin = args.length (); |
|
937 |
712
|
938 if (nargin < 2) |
529
|
939 print_usage ("fprintf"); |
|
940 else |
|
941 retval = do_printf ("fprintf", args, nargout); |
|
942 |
|
943 return retval; |
|
944 } |
|
945 |
761
|
946 // Formatted printing. |
|
947 |
529
|
948 DEFUN ("printf", Fprintf, Sprintf, -1, 1, |
|
949 "printf (FORMAT, ...)") |
|
950 { |
|
951 Octave_object retval; |
|
952 |
|
953 int nargin = args.length (); |
|
954 |
712
|
955 if (nargin < 1) |
529
|
956 print_usage ("printf"); |
|
957 else |
|
958 retval = do_printf ("printf", args, nargout); |
|
959 |
|
960 return retval; |
|
961 } |
|
962 |
761
|
963 // Formatted printing to a string. |
|
964 |
529
|
965 DEFUN ("sprintf", Fsprintf, Ssprintf, -1, 1, |
|
966 "s = sprintf (FORMAT, ...)") |
|
967 { |
|
968 Octave_object retval; |
|
969 |
|
970 int nargin = args.length (); |
|
971 |
712
|
972 if (nargin < 1) |
529
|
973 print_usage ("sprintf"); |
|
974 else |
|
975 retval = do_printf ("sprintf", args, nargout); |
|
976 |
|
977 return retval; |
|
978 } |
|
979 |
497
|
980 Octave_object |
506
|
981 do_printf (const char *type, const Octave_object& args, int nargout) |
1
|
982 { |
497
|
983 Octave_object retval; |
712
|
984 fmt_arg_count = 0; |
1
|
985 char *fmt; |
240
|
986 file_info file; |
1
|
987 |
|
988 if (strcmp (type, "fprintf") == 0) |
|
989 { |
712
|
990 Pix p = file_io_get_file (args(0), "a+", type); |
368
|
991 |
529
|
992 if (! p) |
368
|
993 return retval; |
1
|
994 |
|
995 file = file_list (p); |
368
|
996 |
1
|
997 if (file.mode () == "r") |
|
998 { |
|
999 error ("%s: file is read only", type); |
|
1000 return retval; |
|
1001 } |
368
|
1002 |
712
|
1003 fmt = args(1).string_value (); |
368
|
1004 |
636
|
1005 if (error_state) |
|
1006 { |
|
1007 error ("%s: format must be a string", type); |
|
1008 return retval; |
|
1009 } |
|
1010 |
|
1011 fmt_arg_count += 2; |
1
|
1012 } |
|
1013 else |
|
1014 { |
712
|
1015 fmt = args(0).string_value (); |
636
|
1016 |
|
1017 if (error_state) |
|
1018 { |
|
1019 error ("%s: invalid format string", type); |
|
1020 return retval; |
|
1021 } |
|
1022 |
|
1023 fmt_arg_count++; |
1
|
1024 } |
|
1025 |
|
1026 // Scan fmt for % escapes and print out the arguments. |
|
1027 |
|
1028 ostrstream output_buf; |
|
1029 |
|
1030 char *ptr = fmt; |
|
1031 |
|
1032 for (;;) |
|
1033 { |
|
1034 char c; |
|
1035 while ((c = *ptr++) != '\0' && c != '%') |
|
1036 output_buf << c; |
|
1037 |
|
1038 if (c == '\0') |
|
1039 break; |
|
1040 |
|
1041 if (*ptr == '%') |
|
1042 { |
|
1043 ptr++; |
|
1044 output_buf << c; |
|
1045 continue; |
|
1046 } |
|
1047 |
|
1048 // We must be looking at a format specifier. Extract it or fail. |
|
1049 |
|
1050 |
506
|
1051 int status = process_printf_format (ptr, args, output_buf, type); |
1
|
1052 |
|
1053 if (status < 0) |
|
1054 return retval; |
|
1055 |
|
1056 ptr += status; |
|
1057 } |
|
1058 |
|
1059 output_buf << ends; |
|
1060 if (strcmp (type, "printf") == 0 |
|
1061 || (strcmp (type, "fprintf") == 0 && file.number () == 1)) |
|
1062 { |
|
1063 maybe_page_output (output_buf); |
|
1064 } |
|
1065 else if (strcmp (type, "fprintf") == 0) |
|
1066 { |
|
1067 char *msg = output_buf.str (); |
|
1068 int success = fputs (msg, file.fptr ()); |
|
1069 if (success == EOF) |
218
|
1070 warning ("%s: unknown failure writing to file", type); |
1
|
1071 delete [] msg; |
|
1072 } |
|
1073 else if (strcmp (type, "sprintf") == 0) |
|
1074 { |
|
1075 char *msg = output_buf.str (); |
636
|
1076 retval(0) = msg; |
1
|
1077 delete [] msg; |
|
1078 } |
|
1079 |
|
1080 return retval; |
|
1081 } |
|
1082 |
|
1083 static int |
368
|
1084 process_scanf_format (const char *s, ostrstream& fmt, |
|
1085 const char *type, int nargout, FILE* fptr, |
497
|
1086 Octave_object& values) |
1
|
1087 { |
|
1088 fmt << "%"; |
|
1089 |
|
1090 int chars_from_fmt_str = 0; |
|
1091 int store_value = 1; |
628
|
1092 int string_width = 0; |
1
|
1093 int success = 1; |
|
1094 |
|
1095 if (*s == '*') |
|
1096 { |
|
1097 store_value = 0; |
|
1098 s++; |
|
1099 chars_from_fmt_str++; |
|
1100 } |
|
1101 |
|
1102 if (isdigit (*s)) |
|
1103 { |
|
1104 ostrstream str_number; |
|
1105 while (*s != '\0' && isdigit (*s)) |
|
1106 { |
|
1107 chars_from_fmt_str++; |
|
1108 str_number << *s; |
|
1109 fmt << *s++; |
|
1110 } |
|
1111 str_number << ends; |
|
1112 char *number = str_number.str (); |
|
1113 string_width = atoi (number); |
|
1114 delete [] number; |
|
1115 } |
|
1116 |
|
1117 if (*s == '\0') |
|
1118 goto invalid_format; |
|
1119 |
|
1120 if (*s != '\0' && (*s == 'h' || *s == 'l' || *s == 'L')) |
|
1121 { |
|
1122 chars_from_fmt_str++; |
|
1123 s++; |
|
1124 } |
|
1125 |
|
1126 if (*s == '\0') |
|
1127 goto invalid_format; |
|
1128 |
368
|
1129 // Even if we don't have a place to store them, attempt to convert |
|
1130 // everything specified by the format string. |
1
|
1131 |
712
|
1132 if (fmt_arg_count > (nargout ? nargout : 1)) |
368
|
1133 store_value = 0; |
1
|
1134 |
|
1135 switch (*s) |
|
1136 { |
|
1137 case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': |
|
1138 { |
|
1139 chars_from_fmt_str++; |
|
1140 fmt << *s << ends; |
|
1141 int temp; |
|
1142 char *str = fmt.str (); |
|
1143 success = fscanf (fptr, str, &temp); |
|
1144 delete [] str; |
|
1145 if (success > 0 && store_value) |
636
|
1146 values(fmt_arg_count++) = (double) temp; |
1
|
1147 } |
|
1148 break; |
628
|
1149 |
1
|
1150 case 'e': case 'E': case 'f': case 'g': case 'G': |
|
1151 { |
|
1152 chars_from_fmt_str++; |
|
1153 fmt << 'l' << *s << ends; |
|
1154 double temp; |
|
1155 char *str = fmt.str (); |
|
1156 success = fscanf (fptr, str, &temp); |
|
1157 delete [] str; |
|
1158 if (success > 0 && store_value) |
636
|
1159 values(fmt_arg_count++) = temp; |
1
|
1160 } |
|
1161 break; |
628
|
1162 |
1
|
1163 case 's': |
|
1164 { |
|
1165 if (string_width < 1) |
|
1166 { |
628
|
1167 // XXX FIXME XXX -- The code below is miscompiled on the Alpha with |
|
1168 // gcc 2.6.0, so that string_width is never incremented, even though |
|
1169 // reading the data works correctly. One fix is to use a fixed-size |
|
1170 // buffer... |
|
1171 // string_width = 8192; |
|
1172 |
1
|
1173 string_width = 0; |
|
1174 long original_position = ftell (fptr); |
628
|
1175 |
1
|
1176 int c; |
|
1177 |
636
|
1178 while ((c = getc (fptr)) != EOF && isspace (c)) |
1
|
1179 ; // Don't count leading whitespace. |
|
1180 |
|
1181 if (c != EOF) |
|
1182 string_width++; |
|
1183 |
|
1184 for (;;) |
|
1185 { |
|
1186 c = getc (fptr); |
636
|
1187 if (c != EOF && ! isspace (c)) |
1
|
1188 string_width++; |
|
1189 else |
|
1190 break; |
|
1191 } |
|
1192 |
|
1193 fseek (fptr, original_position, SEEK_SET); |
|
1194 } |
|
1195 chars_from_fmt_str++; |
636
|
1196 char temp [string_width+1]; |
1
|
1197 fmt << *s << ends; |
|
1198 char *str = fmt.str (); |
|
1199 success = fscanf (fptr, str, temp); |
|
1200 delete [] str; |
636
|
1201 temp[string_width] = '\0'; |
628
|
1202 if (success > 0 && store_value) |
636
|
1203 values(fmt_arg_count++) = temp; |
1
|
1204 } |
|
1205 break; |
628
|
1206 |
1
|
1207 case 'c': |
|
1208 { |
|
1209 if (string_width < 1) |
|
1210 string_width = 1; |
|
1211 chars_from_fmt_str++; |
636
|
1212 char temp [string_width+1]; |
1
|
1213 memset (temp, '\0', string_width+1); |
|
1214 fmt << *s << ends; |
|
1215 char *str = fmt.str (); |
|
1216 success = fscanf (fptr, str, temp); |
|
1217 delete [] str; |
|
1218 temp[string_width] = '\0'; |
|
1219 if (success > 0 && store_value) |
636
|
1220 values(fmt_arg_count++) = temp; |
1
|
1221 } |
|
1222 break; |
628
|
1223 |
1
|
1224 default: |
|
1225 goto invalid_format; |
|
1226 } |
|
1227 |
368
|
1228 if (success > 0) |
1
|
1229 return chars_from_fmt_str; |
368
|
1230 else if (success == 0) |
218
|
1231 warning ("%s: invalid conversion", type); |
1
|
1232 else if (success == EOF) |
|
1233 { |
|
1234 if (strcmp (type, "fscanf") == 0) |
218
|
1235 warning ("%s: end of file reached before final conversion", type); |
1
|
1236 else if (strcmp (type, "sscanf") == 0) |
218
|
1237 warning ("%s: end of string reached before final conversion", type); |
1
|
1238 else if (strcmp (type, "scanf") == 0) |
218
|
1239 warning ("%s: end of input reached before final conversion", type); |
1
|
1240 } |
|
1241 else |
|
1242 { |
|
1243 invalid_format: |
218
|
1244 warning ("%s: invalid format", type); |
1
|
1245 } |
|
1246 |
|
1247 return -1; |
|
1248 } |
|
1249 |
761
|
1250 // Formatted reading from a file. |
|
1251 |
712
|
1252 DEFUN ("fscanf", Ffscanf, Sfscanf, 2, -1, |
529
|
1253 "[A, B, C, ...] = fscanf (FILENAME or FILENUM, FORMAT)") |
|
1254 { |
|
1255 Octave_object retval; |
|
1256 |
|
1257 int nargin = args.length (); |
|
1258 |
712
|
1259 if (nargin != 1 && nargin != 2) |
529
|
1260 print_usage ("fscanf"); |
|
1261 else |
|
1262 retval = do_scanf ("fscanf", args, nargout); |
|
1263 |
|
1264 return retval; |
|
1265 } |
|
1266 |
761
|
1267 // Formatted reading. |
|
1268 |
712
|
1269 DEFUN ("scanf", Fscanf, Sscanf, 1, -1, |
529
|
1270 "[A, B, C, ...] = scanf (FORMAT)") |
|
1271 { |
|
1272 Octave_object retval; |
|
1273 |
|
1274 int nargin = args.length (); |
|
1275 |
712
|
1276 if (nargin != 1) |
529
|
1277 print_usage ("scanf"); |
|
1278 else |
|
1279 retval = do_scanf ("scanf", args, nargout); |
|
1280 |
|
1281 return retval; |
|
1282 } |
|
1283 |
761
|
1284 // Formatted reading from a string. |
|
1285 |
712
|
1286 DEFUN ("sscanf", Fsscanf, Ssscanf, 2, -1, |
529
|
1287 "[A, B, C, ...] = sscanf (STRING, FORMAT)") |
|
1288 { |
|
1289 Octave_object retval; |
|
1290 |
|
1291 int nargin = args.length (); |
|
1292 |
712
|
1293 if (nargin != 2) |
529
|
1294 print_usage ("sscanf"); |
|
1295 else |
|
1296 retval = do_scanf ("sscanf", args, nargout); |
|
1297 |
|
1298 return retval; |
|
1299 } |
|
1300 |
497
|
1301 Octave_object |
506
|
1302 do_scanf (const char *type, const Octave_object& args, int nargout) |
1
|
1303 { |
497
|
1304 Octave_object retval; |
529
|
1305 char *scanf_fmt = 0; |
|
1306 char *tmp_file = 0; |
1
|
1307 int tmp_file_open = 0; |
529
|
1308 FILE *fptr = 0; |
240
|
1309 file_info file; |
1
|
1310 |
|
1311 fmt_arg_count = 0; |
|
1312 |
|
1313 if (strcmp (type, "scanf") != 0) |
|
1314 { |
712
|
1315 scanf_fmt = args(1).string_value (); |
636
|
1316 |
|
1317 if (error_state) |
1
|
1318 { |
|
1319 error ("%s: format must be a string", type); |
|
1320 return retval; |
|
1321 } |
|
1322 } |
|
1323 |
|
1324 int doing_fscanf = (strcmp (type, "fscanf") == 0); |
|
1325 |
|
1326 if (doing_fscanf) |
|
1327 { |
712
|
1328 Pix p = file_io_get_file (args(0), "r", type); |
368
|
1329 |
529
|
1330 if (! p) |
368
|
1331 return retval; |
1
|
1332 |
|
1333 file = file_list (p); |
|
1334 |
|
1335 if (strcmp (file.mode (), "w") == 0 || strcmp (file.mode (), "a") == 0) |
|
1336 { |
|
1337 error ("%s: this file is opened for writing only", type); |
|
1338 return retval; |
|
1339 } |
|
1340 |
|
1341 fptr = file.fptr (); |
|
1342 } |
|
1343 |
712
|
1344 if ((! fptr && args(0).is_string ()) |
368
|
1345 || (doing_fscanf && file.number () == 0)) |
1
|
1346 { |
|
1347 char *string; |
|
1348 |
|
1349 if (strcmp (type, "scanf") == 0) |
712
|
1350 scanf_fmt = args(0).string_value (); |
1
|
1351 |
|
1352 if (strcmp (type, "scanf") == 0 |
|
1353 || (doing_fscanf && file.number () == 0)) |
|
1354 { |
|
1355 string = gnu_readline (""); |
|
1356 if (string && *string) |
|
1357 maybe_save_history (string); |
|
1358 } |
|
1359 else |
712
|
1360 string = args(0).string_value (); |
1
|
1361 |
637
|
1362 tmp_file = octave_tmp_file_name (); |
1
|
1363 |
|
1364 fptr = fopen (tmp_file, "w+"); |
529
|
1365 if (! fptr) |
1
|
1366 { |
|
1367 error ("%s: error opening temporary file", type); |
|
1368 return retval; |
|
1369 } |
|
1370 tmp_file_open = 1; |
|
1371 unlink (tmp_file); |
|
1372 |
529
|
1373 if (! string) |
1
|
1374 panic_impossible (); |
|
1375 |
|
1376 int success = fputs (string, fptr); |
|
1377 fflush (fptr); |
|
1378 rewind (fptr); |
|
1379 |
|
1380 if (success < 0) |
|
1381 { |
|
1382 error ("%s: trouble writing temporary file", type); |
|
1383 fclose (fptr); |
|
1384 return retval; |
|
1385 } |
|
1386 } |
|
1387 else if (! doing_fscanf) |
|
1388 { |
|
1389 error ("%s: first argument must be a string", type); |
|
1390 return retval; |
|
1391 } |
|
1392 |
|
1393 // Scan scanf_fmt for % escapes and assign the arguments. |
|
1394 |
636
|
1395 retval.resize (nargout); |
1
|
1396 |
|
1397 char *ptr = scanf_fmt; |
|
1398 |
|
1399 for (;;) |
|
1400 { |
|
1401 ostrstream fmt; |
|
1402 char c; |
|
1403 while ((c = *ptr++) != '\0' && c != '%') |
|
1404 fmt << c; |
|
1405 |
|
1406 if (c == '\0') |
|
1407 break; |
|
1408 |
|
1409 if (*ptr == '%') |
|
1410 { |
|
1411 ptr++; |
|
1412 fmt << c; |
|
1413 continue; |
|
1414 } |
|
1415 |
|
1416 // We must be looking at a format specifier. Extract it or fail. |
|
1417 |
368
|
1418 int status = process_scanf_format (ptr, fmt, type, nargout, |
|
1419 fptr, retval); |
1
|
1420 |
|
1421 if (status < 0) |
|
1422 { |
|
1423 if (fmt_arg_count == 0) |
497
|
1424 retval.resize (0); |
1
|
1425 break; |
|
1426 } |
|
1427 |
|
1428 ptr += status; |
|
1429 } |
|
1430 |
|
1431 if (tmp_file_open) |
|
1432 fclose (fptr); |
|
1433 |
|
1434 return retval; |
|
1435 } |
|
1436 |
761
|
1437 // Find out how many elements are left to read. |
|
1438 |
444
|
1439 static long |
471
|
1440 num_items_remaining (FILE *fptr, char *type) |
444
|
1441 { |
471
|
1442 size_t size; |
|
1443 |
|
1444 if (strcasecmp (type, "uchar") == 0) |
|
1445 size = sizeof (u_char); |
|
1446 else if (strcasecmp (type, "char") == 0) |
|
1447 size = sizeof (char); |
|
1448 else if (strcasecmp (type, "short") == 0) |
|
1449 size = sizeof (short); |
|
1450 else if (strcasecmp (type, "ushort") == 0) |
|
1451 size = sizeof (u_short); |
|
1452 else if (strcasecmp (type, "int") == 0) |
|
1453 size = sizeof (int); |
|
1454 else if (strcasecmp (type, "uint") == 0) |
|
1455 size = sizeof (u_int); |
|
1456 else if (strcasecmp (type, "long") == 0) |
|
1457 size = sizeof (long); |
|
1458 else if (strcasecmp (type, "ulong") == 0) |
|
1459 size = sizeof (u_long); |
|
1460 else if (strcasecmp (type, "float") == 0) |
|
1461 size = sizeof (float); |
|
1462 else if (strcasecmp (type, "double") == 0) |
|
1463 size = sizeof (double); |
|
1464 else |
|
1465 return 0; |
|
1466 |
444
|
1467 long curr_pos = ftell (fptr); |
|
1468 |
|
1469 fseek (fptr, 0, SEEK_END); |
|
1470 long end_of_file = ftell (fptr); |
|
1471 |
471
|
1472 fseek (fptr, curr_pos, SEEK_SET); |
444
|
1473 |
|
1474 long len = end_of_file - curr_pos; |
|
1475 |
471
|
1476 return len / size; |
444
|
1477 } |
|
1478 |
712
|
1479 DEFUN ("fread", Ffread, Sfread, 3, 2, |
529
|
1480 "[DATA, COUNT] = fread (FILENUM, SIZE, PRECISION)\n\ |
|
1481 \n\ |
|
1482 Reads data in binary form of type PRECISION from a file.\n\ |
|
1483 \n\ |
|
1484 FILENUM : file number from fopen\n\ |
|
1485 SIZE : size specification for the Data matrix\n\ |
|
1486 PRECISION : type of data to read, valid types are\n\ |
|
1487 \n\ |
|
1488 'char', 'schar', 'short', 'int', 'long', 'float'\n\ |
|
1489 'double', 'uchar', 'ushort', 'uint', 'ulong'\n\ |
|
1490 \n\ |
|
1491 DATA : matrix in which the data is stored\n\ |
|
1492 COUNT : number of elements read") |
|
1493 { |
|
1494 Octave_object retval; |
|
1495 |
|
1496 int nargin = args.length (); |
|
1497 |
712
|
1498 if (nargin < 1 || nargin > 3) |
529
|
1499 print_usage ("fread"); |
|
1500 else |
|
1501 retval = fread_internal (args, nargout); |
|
1502 |
|
1503 return retval; |
|
1504 } |
|
1505 |
761
|
1506 // Read binary data from a file. |
|
1507 // |
|
1508 // [data, count] = fread (fid, size, 'precision') |
|
1509 // |
|
1510 // fid : the file id from fopen |
|
1511 // size : the size of the matrix or vector or scaler to read |
|
1512 // |
|
1513 // n : reads n elements of a column vector |
|
1514 // inf : reads to the end of file (default) |
|
1515 // [m, n] : reads enough elements to fill the matrix |
|
1516 // the number of columns can be inf |
|
1517 // |
|
1518 // precision : type of the element. Can be: |
|
1519 // |
|
1520 // char, uchar, schar, short, ushort, int, uint, |
|
1521 // long, ulong, float, double |
|
1522 // |
|
1523 // Default is uchar. |
|
1524 // |
|
1525 // data : output data |
|
1526 // count : number of elements read |
|
1527 |
497
|
1528 Octave_object |
506
|
1529 fread_internal (const Octave_object& args, int nargout) |
444
|
1530 { |
497
|
1531 Octave_object retval; |
444
|
1532 |
506
|
1533 int nargin = args.length (); |
|
1534 |
712
|
1535 Pix p = file_io_get_file (args(0), "r", "fread"); |
444
|
1536 |
529
|
1537 if (! p) |
444
|
1538 return retval; |
|
1539 |
|
1540 // Get type and number of bytes per element to read. |
|
1541 char *prec = "uchar"; |
712
|
1542 if (nargin > 2) |
444
|
1543 { |
712
|
1544 prec = args(2).string_value (); |
636
|
1545 |
|
1546 if (error_state) |
444
|
1547 { |
|
1548 error ("fread: precision must be a specified as a string"); |
|
1549 return retval; |
|
1550 } |
|
1551 } |
|
1552 |
471
|
1553 // Get file info. |
444
|
1554 |
|
1555 file_info file = file_list (p); |
471
|
1556 |
|
1557 FILE *fptr = file.fptr (); |
444
|
1558 |
|
1559 // Set up matrix to read into. If specified in arguments use that |
|
1560 // number, otherwise read everyting left in file. |
|
1561 |
471
|
1562 double dnr = 0.0; |
|
1563 double dnc = 0.0; |
|
1564 int nr; |
|
1565 int nc; |
444
|
1566 |
712
|
1567 if (nargin > 1) |
444
|
1568 { |
712
|
1569 if (args(1).is_scalar_type ()) |
444
|
1570 { |
712
|
1571 dnr = args(1).double_value (); |
636
|
1572 |
|
1573 if (error_state) |
|
1574 return retval; |
|
1575 |
471
|
1576 dnc = 1.0; |
444
|
1577 } |
636
|
1578 else |
444
|
1579 { |
712
|
1580 ColumnVector tmp = args(1).vector_value (); |
444
|
1581 |
636
|
1582 if (error_state || tmp.length () != 2) |
471
|
1583 { |
|
1584 error ("fread: invalid size specification\n"); |
|
1585 return retval; |
|
1586 } |
636
|
1587 |
|
1588 dnr = tmp.elem (0); |
|
1589 dnc = tmp.elem (1); |
471
|
1590 } |
|
1591 |
|
1592 if ((xisinf (dnr)) && (xisinf (dnc))) |
444
|
1593 { |
471
|
1594 error ("fread: number of rows and columns cannot both be infinite"); |
444
|
1595 return retval; |
|
1596 } |
|
1597 |
471
|
1598 if (xisinf (dnr)) |
|
1599 { |
|
1600 nc = NINT (dnc); |
|
1601 int n = num_items_remaining (fptr, prec); |
|
1602 nr = n / nc; |
|
1603 if (n > nr * nc) |
|
1604 nr++; |
|
1605 } |
|
1606 else if (xisinf (dnc)) |
|
1607 { |
|
1608 nr = NINT (dnr); |
|
1609 int n = num_items_remaining (fptr, prec); |
|
1610 nc = n / nr; |
|
1611 if (n > nc * nr) |
|
1612 nc++; |
|
1613 } |
|
1614 else |
|
1615 { |
|
1616 nr = NINT (dnr); |
|
1617 nc = NINT (dnc); |
|
1618 } |
444
|
1619 } |
|
1620 else |
|
1621 { |
|
1622 // No size parameter, read what's left of the file. |
471
|
1623 nc = 1; |
|
1624 int n = num_items_remaining (fptr, prec); |
|
1625 nr = n / nc; |
|
1626 if (n > nr * nc) |
|
1627 nr++; |
444
|
1628 } |
|
1629 |
|
1630 Matrix m (nr, nc, octave_NaN); |
|
1631 |
|
1632 // Read data. |
|
1633 |
471
|
1634 int count = m.read (fptr, prec); |
444
|
1635 |
|
1636 if (nargout > 1) |
636
|
1637 retval(1) = (double) count; |
444
|
1638 |
636
|
1639 retval(0) = m; |
444
|
1640 |
|
1641 return retval; |
|
1642 } |
|
1643 |
712
|
1644 DEFUN ("fwrite", Ffwrite, Sfwrite, 3, 1, |
529
|
1645 "COUNT = fwrite (FILENUM, DATA, PRECISION)\n\ |
|
1646 \n\ |
|
1647 Writes data to a file in binary form of size PRECISION\n\ |
|
1648 \n\ |
|
1649 FILENUM : file number from fopen\n\ |
|
1650 DATA : matrix of elements to be written\n\ |
|
1651 PRECISION : type of data to read, valid types are\n\ |
|
1652 \n\ |
|
1653 'char', 'schar', 'short', 'int', 'long', 'float'\n\ |
|
1654 'double', 'uchar', 'ushort', 'uint', 'ulong'\n\ |
|
1655 \n\ |
|
1656 COUNT : number of elements written") |
|
1657 { |
|
1658 Octave_object retval; |
|
1659 |
|
1660 int nargin = args.length (); |
|
1661 |
712
|
1662 if (nargin < 2 || nargin > 3) |
529
|
1663 print_usage ("fwrite"); |
|
1664 else |
|
1665 retval = fwrite_internal (args, nargout); |
|
1666 |
|
1667 return retval; |
|
1668 } |
|
1669 |
761
|
1670 // Write binary data to a file. |
|
1671 // |
|
1672 // count = fwrite (fid, data, 'precision') |
|
1673 // |
|
1674 // fid : file id from fopen |
|
1675 // Data : data to be written |
|
1676 // precision : type of output element. Can be: |
|
1677 // |
|
1678 // char, uchar, schar, short, ushort, int, uint, |
|
1679 // long, float, double |
|
1680 // |
|
1681 // Default is uchar. |
|
1682 // |
|
1683 // count : the number of elements written |
|
1684 |
497
|
1685 Octave_object |
506
|
1686 fwrite_internal (const Octave_object& args, int nargout) |
444
|
1687 { |
497
|
1688 Octave_object retval; |
444
|
1689 |
506
|
1690 int nargin = args.length (); |
|
1691 |
761
|
1692 Pix p = file_io_get_file (args(0), "a+", "fwrite"); |
444
|
1693 |
529
|
1694 if (! p) |
444
|
1695 return retval; |
|
1696 |
|
1697 // Get type and number of bytes per element to read. |
|
1698 char *prec = "uchar"; |
712
|
1699 if (nargin > 2) |
444
|
1700 { |
761
|
1701 prec = args(2).string_value (); |
636
|
1702 |
|
1703 if (error_state) |
444
|
1704 { |
|
1705 error ("fwrite: precision must be a specified as a string"); |
|
1706 return retval; |
|
1707 } |
|
1708 } |
|
1709 |
|
1710 file_info file = file_list (p); |
|
1711 |
761
|
1712 Matrix m = args(1).matrix_value (); |
444
|
1713 |
636
|
1714 if (! error_state) |
|
1715 { |
|
1716 int count = m.write (file.fptr (), prec); |
444
|
1717 |
636
|
1718 retval(0) = (double) count; |
|
1719 } |
444
|
1720 |
|
1721 return retval; |
|
1722 } |
|
1723 |
712
|
1724 DEFUN ("feof", Ffeof, Sfeof, 1, 1, |
529
|
1725 "ERROR = feof (FILENAME or FILENUM)\n\ |
|
1726 \n\ |
|
1727 Returns a non zero value for an end of file condition for the\n\ |
|
1728 file specified by FILENAME or FILENUM from fopen") |
|
1729 { |
|
1730 Octave_object retval; |
|
1731 |
|
1732 int nargin = args.length (); |
|
1733 |
712
|
1734 if (nargin != 1) |
529
|
1735 print_usage ("feof"); |
|
1736 else |
|
1737 retval = feof_internal (args, nargout); |
|
1738 |
|
1739 return retval; |
|
1740 } |
|
1741 |
761
|
1742 // Check for an EOF condition on a file opened by fopen. |
|
1743 // |
|
1744 // eof = feof (fid) |
|
1745 // |
|
1746 // fid : file id from fopen |
|
1747 // eof : non zero for an end of file condition |
|
1748 |
497
|
1749 Octave_object |
506
|
1750 feof_internal (const Octave_object& args, int nargout) |
444
|
1751 { |
497
|
1752 Octave_object retval; |
444
|
1753 |
|
1754 // Get file info. |
497
|
1755 Pix p = return_valid_file (args(1)); |
444
|
1756 |
529
|
1757 if (! p) |
444
|
1758 return retval; |
|
1759 |
|
1760 file_info file = file_list (p); |
|
1761 |
529
|
1762 retval(0) = (double) feof (file.fptr ()); |
|
1763 |
|
1764 return retval; |
|
1765 } |
|
1766 |
712
|
1767 DEFUN ("ferror", Fferror, Sferror, 1, 1, |
529
|
1768 "ERROR = ferror (FILENAME or FILENUM)\n\ |
|
1769 \n\ |
|
1770 Returns a non zero value for an error condition on the\n\ |
|
1771 file specified by FILENAME or FILENUM from fopen") |
|
1772 { |
|
1773 Octave_object retval; |
|
1774 |
|
1775 int nargin = args.length (); |
|
1776 |
712
|
1777 if (nargin != 1) |
529
|
1778 print_usage ("ferror"); |
|
1779 else |
|
1780 retval = ferror_internal (args, nargout); |
444
|
1781 |
|
1782 return retval; |
|
1783 } |
|
1784 |
761
|
1785 // Check for an error condition on a file opened by fopen. |
|
1786 // |
|
1787 // [message, errnum] = ferror (fid) |
|
1788 // |
|
1789 // fid : file id from fopen |
|
1790 // message : system error message |
|
1791 // errnum : error number |
|
1792 |
497
|
1793 Octave_object |
506
|
1794 ferror_internal (const Octave_object& args, int nargout) |
444
|
1795 { |
497
|
1796 Octave_object retval; |
444
|
1797 |
|
1798 // Get file info. |
712
|
1799 Pix p = return_valid_file (args(0)); |
444
|
1800 |
529
|
1801 if (! p) |
444
|
1802 return retval; |
|
1803 |
|
1804 file_info file = file_list (p); |
|
1805 |
|
1806 int ierr = ferror (file.fptr ()); |
|
1807 |
|
1808 if (nargout > 1) |
636
|
1809 retval(1) = (double) ierr; |
444
|
1810 |
636
|
1811 retval(0) = strsave (strerror (ierr)); |
444
|
1812 |
|
1813 return retval; |
|
1814 } |
|
1815 |
|
1816 /* |
1
|
1817 ;;; Local Variables: *** |
|
1818 ;;; mode: C++ *** |
|
1819 ;;; page-delimiter: "^/\\*" *** |
|
1820 ;;; End: *** |
|
1821 */ |