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