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