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