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