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