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