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 |
|
278 tree_constant * |
164
|
279 fclose_internal (const tree_constant *args) |
1
|
280 { |
|
281 tree_constant *retval = NULL_TREE_CONST; |
|
282 |
|
283 Pix p = return_valid_file (args[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 |
|
300 retval = new tree_constant[2]; |
|
301 if (success == 0) |
|
302 retval[0] = tree_constant (1.0); // succeeded |
|
303 else |
|
304 { |
|
305 error ("fclose: error on closing file"); |
|
306 retval[0] = tree_constant (0.0); // failed |
|
307 } |
|
308 |
|
309 return retval; |
|
310 } |
|
311 |
|
312 tree_constant * |
164
|
313 fflush_internal (const tree_constant *args) |
1
|
314 { |
|
315 tree_constant *retval = NULL_TREE_CONST; |
|
316 |
|
317 Pix p = return_valid_file (args[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 |
|
337 retval = new tree_constant[2]; |
|
338 if (success == 0) |
|
339 retval[0] = tree_constant (1.0); // succeeded |
|
340 else |
|
341 { |
|
342 error ("fflush: write error"); |
|
343 retval[0] = tree_constant (0.0); // failed |
|
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 |
|
364 tree_constant * |
164
|
365 fgets_internal (const tree_constant *args, int nargout) |
1
|
366 { |
|
367 tree_constant *retval = NULL_TREE_CONST; |
|
368 |
368
|
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; |
|
375 if (args[2].is_scalar_type ()) |
|
376 { |
|
377 length = (int) args[2].double_value (); |
|
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 { |
|
392 retval = new tree_constant[2]; |
|
393 retval[0] = tree_constant (-1.0); |
|
394 return retval; |
|
395 } |
|
396 |
|
397 if (nargout == 2) |
|
398 { |
|
399 retval = new tree_constant[3]; |
|
400 retval[1] = tree_constant ((double) strlen (string)); |
|
401 } |
|
402 else |
|
403 retval = new tree_constant[2]; |
|
404 |
|
405 retval[0] = tree_constant (string); |
|
406 |
|
407 return retval; |
|
408 } |
|
409 |
|
410 tree_constant * |
164
|
411 fopen_internal (const tree_constant *args) |
1
|
412 { |
|
413 tree_constant *retval = NULL_TREE_CONST; |
|
414 Pix p; |
|
415 |
|
416 if (! args[1].is_string_type ()) |
|
417 { |
|
418 error ("fopen: file name must be a string"); |
|
419 return retval; |
|
420 } |
|
421 |
|
422 p = return_valid_file (args[1]); |
|
423 |
|
424 if (p != (Pix) NULL) |
|
425 { |
240
|
426 file_info file = file_list (p); |
1
|
427 |
|
428 retval = new tree_constant[2]; |
|
429 retval[0] = tree_constant ((double) file.number ()); |
|
430 |
|
431 return retval; |
|
432 } |
|
433 |
|
434 if (! args[2].is_string_type ()) |
|
435 { |
370
|
436 error ("fopen: file mode must be a string"); |
1
|
437 return retval; |
|
438 } |
|
439 |
|
440 char *name = args[1].string_value (); |
|
441 char *mode = args[2].string_value (); |
|
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 |
|
469 retval = new tree_constant[2]; |
|
470 retval[0] = tree_constant ((double) number); |
|
471 |
|
472 return retval; |
|
473 } |
|
474 |
|
475 tree_constant * |
164
|
476 freport_internal (void) |
1
|
477 { |
|
478 tree_constant *retval = NULL_TREE_CONST; |
|
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 |
|
498 tree_constant * |
164
|
499 frewind_internal (const tree_constant *args) |
1
|
500 { |
|
501 tree_constant *retval = NULL_TREE_CONST; |
|
502 |
368
|
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 |
|
514 tree_constant * |
164
|
515 fseek_internal (const tree_constant *args, int nargin) |
1
|
516 { |
|
517 tree_constant *retval = NULL_TREE_CONST; |
|
518 |
368
|
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; |
|
526 if (args[2].is_scalar_type ()) |
|
527 { |
|
528 offset = (long) args[2].double_value (); |
|
529 if ((double) NINT (offset) != offset) |
|
530 { |
|
531 error ("fseek: offset not an integer value"); |
|
532 return retval; |
|
533 } |
|
534 } |
|
535 |
|
536 if (nargin == 4 && args[3].is_scalar_type ()) |
|
537 { |
|
538 origin = (long) args[3].double_value (); |
|
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); |
|
555 retval = new tree_constant[2]; |
|
556 |
|
557 if (success == 0) |
|
558 retval[0] = tree_constant (1.0); // succeeded |
|
559 else |
|
560 { |
|
561 error ("fseek: file error"); |
|
562 retval[0] = tree_constant (0.0); // failed |
|
563 } |
|
564 |
|
565 return retval; |
|
566 } |
|
567 |
|
568 tree_constant * |
164
|
569 ftell_internal (const tree_constant *args) |
1
|
570 { |
|
571 tree_constant *retval = NULL_TREE_CONST; |
|
572 |
368
|
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 ()); |
|
579 retval = new tree_constant[2]; |
|
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 |
164
|
608 process_printf_format (const char *s, const tree_constant *args, |
|
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 |
|
642 if (args[fmt_arg_count].const_type () |
|
643 != tree_constant_rep::scalar_constant) |
|
644 { |
218
|
645 error ("%s: `*' must be replaced by an integer", type); |
1
|
646 return -1; |
|
647 } |
|
648 |
66
|
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 |
|
682 if (args[fmt_arg_count].const_type () |
|
683 != tree_constant_rep::scalar_constant) |
|
684 { |
218
|
685 error ("%s: `*' must be replaced by an integer", type); |
1
|
686 return -1; |
|
687 } |
|
688 |
66
|
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 |
|
720 arg_type = args[fmt_arg_count].const_type (); |
|
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; |
|
732 double d = args[fmt_arg_count++].double_value (); |
|
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 (); |
|
753 sb.form (s, args[fmt_arg_count++].double_value ()); |
|
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 (); |
|
767 sb.form (s, args[fmt_arg_count++].string_value ()); |
|
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; |
|
780 char *str = args[fmt_arg_count++].string_value (); |
|
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 |
|
806 tree_constant * |
164
|
807 do_printf (const char *type, const tree_constant *args, int nargin, |
|
808 int nargout) |
1
|
809 { |
|
810 tree_constant *retval = NULL_TREE_CONST; |
|
811 fmt_arg_count = 1; |
|
812 char *fmt; |
240
|
813 file_info file; |
1
|
814 |
|
815 if (strcmp (type, "fprintf") == 0) |
|
816 { |
|
817 if (args[2].is_string_type ()) |
|
818 { |
|
819 fmt = args[2].string_value (); |
|
820 fmt_arg_count++; |
|
821 } |
|
822 else |
|
823 { |
|
824 error ("%s: format must be a string", type); |
|
825 return retval; |
|
826 } |
|
827 |
368
|
828 Pix p = file_io_get_file (args[1], "a+", type); |
|
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 |
1
|
841 fmt = args[2].string_value (); |
368
|
842 |
1
|
843 fmt_arg_count++; |
|
844 } |
|
845 else if (args[1].is_string_type ()) |
|
846 { |
|
847 fmt = args[1].string_value (); |
|
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 { |
|
906 retval = new tree_constant [2]; |
|
907 char *msg = output_buf.str (); |
|
908 retval[0] = tree_constant (msg); |
|
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, |
|
918 tree_constant *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 |
368
|
964 if (fmt_arg_count >= nargout) |
|
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) |
|
978 values[fmt_arg_count++] = tree_constant ((double) temp); |
|
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) |
|
990 values[fmt_arg_count++] = tree_constant (temp); |
|
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) |
|
1026 values[fmt_arg_count++] = tree_constant (temp); |
|
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) |
|
1042 values[fmt_arg_count++] = tree_constant (temp); |
|
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 |
|
1071 tree_constant * |
164
|
1072 do_scanf (const char *type, const tree_constant *args, int nargin, int nargout) |
1
|
1073 { |
|
1074 tree_constant *retval = NULL_TREE_CONST; |
|
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 { |
368
|
1085 if (args[2].is_string_type ()) |
1
|
1086 scanf_fmt = args[2].string_value (); |
|
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 { |
368
|
1098 Pix p = file_io_get_file (args[1], "r", type); |
|
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 |
368
|
1114 if ((fptr == (FILE *) NULL && args[1].is_string_type ()) |
|
1115 || (doing_fscanf && file.number () == 0)) |
1
|
1116 { |
|
1117 char *string; |
|
1118 |
|
1119 if (strcmp (type, "scanf") == 0) |
|
1120 scanf_fmt = args[1].string_value (); |
|
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 |
|
1130 string = args[1].string_value (); |
|
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 |
|
1165 retval = new tree_constant[nargout+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) |
|
1194 { |
|
1195 delete [] retval; |
|
1196 retval = NULL_TREE_CONST; |
|
1197 } |
|
1198 break; |
|
1199 } |
|
1200 |
|
1201 ptr += status; |
|
1202 } |
|
1203 |
|
1204 if (tmp_file_open) |
|
1205 fclose (fptr); |
|
1206 |
|
1207 return retval; |
|
1208 } |
|
1209 |
|
1210 /* |
444
|
1211 * Find out how many elements are left. |
|
1212 * |
|
1213 * size is the size of the elements |
|
1214 * nr is the number of rows or columns in the matrix |
|
1215 */ |
|
1216 static long |
|
1217 get_whats_left (FILE *fptr, int size, int nn) |
|
1218 { |
|
1219 long curr_pos = ftell (fptr); |
|
1220 |
|
1221 fseek (fptr, 0, SEEK_END); |
|
1222 long end_of_file = ftell (fptr); |
|
1223 |
|
1224 fseek (fptr, end_of_file, SEEK_SET); |
|
1225 |
|
1226 long len = end_of_file - curr_pos; |
|
1227 |
|
1228 long num_items = len / size / nn; |
|
1229 |
|
1230 if (len > num_items * size * nn) |
|
1231 num_items++; |
|
1232 |
|
1233 return num_items; |
|
1234 } |
|
1235 |
|
1236 static void |
|
1237 get_size_conv (const char *preci, int& size, Matrix::conversion& conv, |
|
1238 const char *warn) |
|
1239 { |
|
1240 // Get type and number of bytes per element to read. |
|
1241 |
|
1242 char *prec = strdup (preci); |
|
1243 char *ip = prec; |
|
1244 |
|
1245 while (*ip > 0) |
|
1246 { |
|
1247 tolower (*ip); |
|
1248 ip++; |
|
1249 } |
|
1250 |
|
1251 if (strcmp (prec, "uchar") == 0) |
|
1252 { |
|
1253 size = 1; |
|
1254 conv = Matrix::CNV_UCHAR; |
|
1255 } |
|
1256 else if (strcmp (prec, "char") == 0) |
|
1257 { |
|
1258 size = 1; |
|
1259 conv = Matrix::CNV_CHAR; |
|
1260 } |
|
1261 else if (strcmp (prec, "schar") == 0) |
|
1262 { |
|
1263 size = 1; |
|
1264 conv = Matrix::CNV_CHAR; |
|
1265 // Some systems may need this?? |
|
1266 // size = 1; |
|
1267 // conv = CNV_SCHAR; |
|
1268 } |
|
1269 else if (strcmp (prec, "short") == 0) |
|
1270 { |
|
1271 size = 2; |
|
1272 conv = Matrix::CNV_SHORT; |
|
1273 } |
|
1274 else if (strcmp (prec, "ushort") == 0) |
|
1275 { |
|
1276 size = 2; |
|
1277 conv = Matrix::CNV_USHORT; |
|
1278 } |
|
1279 else if (strcmp (prec, "int") == 0) |
|
1280 { |
|
1281 size = 4; |
|
1282 conv = Matrix::CNV_INT; |
|
1283 } |
|
1284 else if (strcmp (prec, "uint") == 0) |
|
1285 { |
|
1286 size = 4; |
|
1287 conv = Matrix::CNV_UINT; |
|
1288 } |
|
1289 else if (strcmp (prec, "long") == 0) |
|
1290 { |
|
1291 size = 4; |
|
1292 conv = Matrix::CNV_LONG; |
|
1293 } |
|
1294 else if (strcmp (prec, "ulong") == 0) |
|
1295 { |
|
1296 size = 4; |
|
1297 conv = Matrix::CNV_ULONG; |
|
1298 } |
|
1299 else if (strcmp (prec, "float") == 0) |
|
1300 { |
|
1301 size = 4; |
|
1302 conv = Matrix::CNV_FLOAT; |
|
1303 } |
|
1304 else if (strcmp (prec, "double") == 0) |
|
1305 { |
|
1306 size = 8; |
|
1307 conv = Matrix::CNV_DOUBLE; |
|
1308 } |
|
1309 else |
|
1310 { |
|
1311 error ("%s: precision: \'%s\' unknown", warn, prec); |
|
1312 size = -1; |
|
1313 conv = Matrix::CNV_UNKNOWN; |
|
1314 } |
|
1315 |
|
1316 delete [] prec; |
|
1317 |
|
1318 return; |
|
1319 } |
|
1320 |
|
1321 /* |
|
1322 * Read binary data from a file. |
|
1323 * |
|
1324 * [data, count] = fread (fid, size, 'precision') |
|
1325 * |
|
1326 * fid : the file id from fopen |
|
1327 * size : the size of the matrix or vector or scaler to read |
|
1328 * |
|
1329 * n : reads n elements of a column vector |
|
1330 * inf : reads to the end of file (default) |
|
1331 * [m, n] : reads enough elements to fill the matrix |
|
1332 * the number of columns can be inf |
|
1333 * |
|
1334 * precision : type of the element. Can be: |
|
1335 * |
|
1336 * char, uchar, schar, short, ushort, int, uint, |
|
1337 * long, ulong, float, double |
|
1338 * |
|
1339 * Default is uchar. |
|
1340 * |
|
1341 * data : output data |
|
1342 * count : number of elements read |
|
1343 */ |
|
1344 tree_constant * |
|
1345 fread_internal (const tree_constant *args, int nargin, int nargout) |
|
1346 { |
|
1347 tree_constant *retval = NULL_TREE_CONST; |
|
1348 |
|
1349 Pix p = file_io_get_file (args[1], "r", "fread"); |
|
1350 |
|
1351 if (p == (Pix) NULL) |
|
1352 return retval; |
|
1353 |
|
1354 // Get type and number of bytes per element to read. |
|
1355 char *prec = "uchar"; |
|
1356 if (nargin > 3) |
|
1357 { |
|
1358 if (args[3].is_string_type ()) |
|
1359 prec = args[3].string_value (); |
|
1360 else |
|
1361 { |
|
1362 error ("fread: precision must be a specified as a string"); |
|
1363 return retval; |
|
1364 } |
|
1365 } |
|
1366 |
|
1367 int size; |
|
1368 Matrix::conversion conv; |
|
1369 get_size_conv (prec, size, conv, "fread"); |
|
1370 if (size < 0) |
|
1371 return retval; |
|
1372 |
|
1373 // Get file info. |
|
1374 file_info file = file_list (p); |
|
1375 FILE * fptr = file.fptr (); |
|
1376 |
|
1377 // Set up matrix to read into. If specified in arguments use that |
|
1378 // number, otherwise read everyting left in file. |
|
1379 |
|
1380 double dnr = 1.0; |
|
1381 double dnc = 1.0; |
|
1382 int nr = 1; |
|
1383 int nc = 1; |
|
1384 |
|
1385 if (nargin > 2) |
|
1386 { |
|
1387 // tree_constant tmpa = args[2].make_numeric (); // ?? |
|
1388 |
|
1389 if (args[2].is_scalar_type ()) |
|
1390 { |
|
1391 tree_constant tmpa = args[2].make_numeric (); |
|
1392 |
|
1393 dnr = 1.0; |
|
1394 dnc = tmpa.double_value (); |
|
1395 } |
|
1396 else if (args[2].is_matrix_type ()) |
|
1397 { |
|
1398 // tree_constant tmpa = args[2].make_numeric (); // ?? |
|
1399 Matrix tmpm = args[2].to_matrix (); |
|
1400 nr = tmpm.rows (); |
|
1401 nc = tmpm.columns (); |
|
1402 |
|
1403 if(nr != 1 || nc > 2) |
|
1404 { |
|
1405 error ("fread: Illegal size specification\n"); |
|
1406 print_usage ("fread"); |
|
1407 return retval; |
|
1408 } |
|
1409 dnr = tmpm.elem (0, 0); |
|
1410 dnc = tmpm.elem (0, 1); |
|
1411 } |
|
1412 |
|
1413 if ((xisinf (dnr)) && (xisinf (dnc))) |
|
1414 { |
|
1415 error ("fread: number of rows and columns cannot both be infinite\n"); |
|
1416 return retval; |
|
1417 } |
|
1418 |
|
1419 if (xisinf (dnr)) |
|
1420 { |
|
1421 nc = NINT (dnc); |
|
1422 nr = get_whats_left (fptr, size, nc); |
|
1423 } |
|
1424 else if (xisinf (dnc)) |
|
1425 { |
|
1426 nr = NINT (dnr); |
|
1427 nc = get_whats_left (fptr, size, nr); |
|
1428 } |
|
1429 else |
|
1430 { |
|
1431 nr = NINT (dnr); |
|
1432 nc = NINT (dnc); |
|
1433 } |
|
1434 } |
|
1435 else |
|
1436 { |
|
1437 // No size parameter, read what's left of the file. |
|
1438 nr = 1; |
|
1439 nc = get_whats_left (fptr, size, nr); |
|
1440 } |
|
1441 |
|
1442 Matrix m (nr, nc, octave_NaN); |
|
1443 |
|
1444 // Read data. |
|
1445 |
|
1446 int count = m.read (fptr, size, conv); |
|
1447 |
|
1448 if (nargout > 1) |
|
1449 { |
|
1450 retval = new tree_constant[3]; |
|
1451 retval[1] = tree_constant ((double) count); |
|
1452 } |
|
1453 else |
|
1454 retval = new tree_constant[2]; |
|
1455 |
|
1456 retval[0] = tree_constant (m); |
|
1457 |
|
1458 return retval; |
|
1459 } |
|
1460 |
|
1461 /* |
|
1462 * Write binary data to a file. |
|
1463 * |
|
1464 * count = fwrite (fid, data, 'precision') |
|
1465 * |
|
1466 * fid : file id from fopen |
|
1467 * Data : data to be written |
|
1468 * precision : type of output element. Can be: |
|
1469 * |
|
1470 * char, uchar, schar, short, ushort, int, uint, |
|
1471 * long, float, double |
|
1472 * |
|
1473 * Default is uchar. |
|
1474 * |
|
1475 * count : the number of elements written |
|
1476 */ |
|
1477 tree_constant * |
|
1478 fwrite_internal (const tree_constant *args, int nargin, int nargout) |
|
1479 { |
|
1480 tree_constant *retval = NULL_TREE_CONST; |
|
1481 |
|
1482 Pix p = file_io_get_file (args[1], "a+", "fwrite"); |
|
1483 |
|
1484 if (p == (Pix) NULL) |
|
1485 return retval; |
|
1486 |
|
1487 // Get type and number of bytes per element to read. |
|
1488 char *prec = "uchar"; |
|
1489 if (nargin > 3) |
|
1490 { |
|
1491 if (args[3].is_string_type ()) |
|
1492 prec = args[3].string_value (); |
|
1493 else |
|
1494 { |
|
1495 error ("fwrite: precision must be a specified as a string"); |
|
1496 return retval; |
|
1497 } |
|
1498 } |
|
1499 |
|
1500 int size; |
|
1501 Matrix::conversion conv; |
|
1502 get_size_conv(prec, size, conv, "fwrite"); |
|
1503 if (size < 0) |
|
1504 return retval; |
|
1505 |
|
1506 // Get file info. |
|
1507 file_info file = file_list (p); |
|
1508 |
|
1509 // Write the matrix data. |
|
1510 tree_constant tmpa = args[2].make_numeric (); |
|
1511 |
|
1512 Matrix tmpm = tmpa.to_matrix (); |
|
1513 |
|
1514 int count = tmpm.write (file.fptr(), size, conv); |
|
1515 |
|
1516 retval = new tree_constant[2]; |
|
1517 retval[0] = tree_constant ((double) count); |
|
1518 |
|
1519 return retval; |
|
1520 } |
|
1521 |
|
1522 /* |
|
1523 * Check for an EOF condition on a file opened by fopen. |
|
1524 * |
|
1525 * eof = feof (fid) |
|
1526 * |
|
1527 * fid : file id from fopen |
|
1528 * eof : non zero for an end of file condition |
|
1529 */ |
|
1530 tree_constant * |
|
1531 feof_internal (const tree_constant *args, int nargin, int nargout) |
|
1532 { |
|
1533 tree_constant *retval = NULL_TREE_CONST; |
|
1534 |
|
1535 // Get file info. |
|
1536 Pix p = return_valid_file (args[1]); |
|
1537 |
|
1538 if (p == (Pix) NULL) |
|
1539 return retval; |
|
1540 |
|
1541 file_info file = file_list (p); |
|
1542 |
|
1543 retval = new tree_constant[2]; |
|
1544 retval[0] = tree_constant (feof (file.fptr ())); |
|
1545 |
|
1546 return retval; |
|
1547 } |
|
1548 |
|
1549 /* |
|
1550 * Check for an error condition on a file opened by fopen. |
|
1551 * |
|
1552 * [message, errnum] = ferror (fid) |
|
1553 * |
|
1554 * fid : file id from fopen |
|
1555 * message : system error message |
|
1556 * errnum : error number |
|
1557 */ |
|
1558 tree_constant * |
|
1559 ferror_internal (const tree_constant *args, int nargin, int nargout) |
|
1560 { |
|
1561 tree_constant *retval = NULL_TREE_CONST; |
|
1562 |
|
1563 // Get file info. |
|
1564 Pix p = return_valid_file (args[1]); |
|
1565 |
|
1566 if (p == (Pix) NULL) |
|
1567 return retval; |
|
1568 |
|
1569 file_info file = file_list (p); |
|
1570 |
|
1571 int ierr = ferror (file.fptr ()); |
|
1572 |
|
1573 if (nargout > 1) |
|
1574 { |
|
1575 retval = new tree_constant[3]; |
|
1576 retval[1] = tree_constant ((double) ierr); |
|
1577 } |
|
1578 else |
|
1579 retval = new tree_constant[2]; |
|
1580 |
|
1581 retval[0] = tree_constant (strsave (strerror (ierr))); |
|
1582 |
|
1583 return retval; |
|
1584 } |
|
1585 |
|
1586 /* |
1
|
1587 ;;; Local Variables: *** |
|
1588 ;;; mode: C++ *** |
|
1589 ;;; page-delimiter: "^/\\*" *** |
|
1590 ;;; End: *** |
|
1591 */ |