comparison src/file-io.cc @ 1181:b2b7c5264ac2

[project @ 1995-03-28 23:51:50 by jwe]
author jwe
date Tue, 28 Mar 1995 23:56:28 +0000
parents 75fc98220389
children 92609e161b29
comparison
equal deleted inserted replaced
1180:0a9c94ee69e8 1181:b2b7c5264ac2
250 } 250 }
251 251
252 return p; 252 return p;
253 } 253 }
254 254
255 static Octave_object
256 fclose_internal (const Octave_object& args)
257 {
258 Octave_object retval;
259
260 Pix p = return_valid_file (args(0));
261
262 if (! p)
263 return retval;
264
265 file_info file = file_list (p);
266
267 if (file.number () < 3)
268 {
269 warning ("fclose: can't close stdin, stdout, or stderr!");
270 return retval;
271 }
272
273 int success = fclose (file.fptr ());
274 file_list.del (p);
275
276 if (success == 0)
277 retval(0) = 1.0; // succeeded
278 else
279 {
280 error ("fclose: error on closing file");
281 retval(0) = 0.0; // failed
282 }
283
284 return retval;
285 }
286
255 DEFUN ("fclose", Ffclose, Sfclose, 1, 1, 287 DEFUN ("fclose", Ffclose, Sfclose, 1, 1,
256 "fclose (FILENAME or FILENUM): close a file") 288 "fclose (FILENAME or FILENUM): close a file")
257 { 289 {
258 Octave_object retval; 290 Octave_object retval;
259 291
265 retval = fclose_internal (args); 297 retval = fclose_internal (args);
266 298
267 return retval; 299 return retval;
268 } 300 }
269 301
270 Octave_object 302 static Octave_object
271 fclose_internal (const Octave_object& args) 303 fflush_internal (const Octave_object& args)
272 { 304 {
273 Octave_object retval; 305 Octave_object retval;
274 306
275 Pix p = return_valid_file (args(0)); 307 Pix p = return_valid_file (args(0));
276 308
277 if (! p) 309 if (! p)
278 return retval; 310 return retval;
279 311
280 file_info file = file_list (p); 312 file_info file = file_list (p);
281 313
282 if (file.number () < 3) 314 if (strcmp (file.mode (), "r") == 0)
283 { 315 {
284 warning ("fclose: can't close stdin, stdout, or stderr!"); 316 warning ("can't flush an input stream");
285 return retval; 317 return retval;
286 } 318 }
287 319
288 int success = fclose (file.fptr ()); 320 int success = 0;
289 file_list.del (p); 321
322 if (file.number () == 1)
323 flush_output_to_pager ();
324 else
325 success = fflush (file.fptr ());
290 326
291 if (success == 0) 327 if (success == 0)
292 retval(0) = 1.0; // succeeded 328 retval(0) = 1.0; // succeeded
293 else 329 else
294 { 330 {
295 error ("fclose: error on closing file"); 331 error ("fflush: write error");
296 retval(0) = 0.0; // failed 332 retval(0) = 0.0; // failed
297 } 333 }
298 334
299 return retval; 335 return retval;
300 } 336 }
312 retval = fflush_internal (args); 348 retval = fflush_internal (args);
313 349
314 return retval; 350 return retval;
315 } 351 }
316 352
317 Octave_object 353 static int
318 fflush_internal (const Octave_object& args) 354 valid_mode (const char *mode)
319 { 355 {
320 Octave_object retval; 356 if (mode)
321 357 {
322 Pix p = return_valid_file (args(0)); 358 char m = mode[0];
323 359 if (m == 'r' || m == 'w' || m == 'a')
360 {
361 m = mode[1];
362 return (m == '\0' || (m == '+' && mode[2] == '\0'));
363 }
364 }
365 return 0;
366 }
367
368 static Octave_object
369 fgets_internal (const Octave_object& args, int nargout)
370 {
371 Octave_object retval;
372
373 Pix p = file_io_get_file (args(0), "r", "fgets");
374
324 if (! p) 375 if (! p)
325 return retval; 376 return retval;
326 377
378
379 double dlen = args(1).double_value ();
380
381 if (error_state)
382 return retval;
383
384 if (xisnan (dlen))
385 {
386 error ("fgets: NaN invalid as length");
387 return retval;
388 }
389
390 int length = NINT (dlen);
391
392 if ((double) length != dlen)
393 {
394 error ("fgets: length not an integer value");
395 return retval;
396 }
397
327 file_info file = file_list (p); 398 file_info file = file_list (p);
328 399
329 if (strcmp (file.mode (), "r") == 0) 400 char string [length+1];
330 { 401 char *success = fgets (string, length+1, file.fptr ());
331 warning ("can't flush an input stream"); 402
403 if (! success)
404 {
405 retval(0) = -1.0;
332 return retval; 406 return retval;
333 } 407 }
334 408
335 int success = 0; 409 if (nargout == 2)
336 410 retval(1) = (double) strlen (string);
337 if (file.number () == 1) 411
338 flush_output_to_pager (); 412 retval(0) = string;
339 else 413
340 success = fflush (file.fptr ()); 414 return retval;
341
342 if (success == 0)
343 retval(0) = 1.0; // succeeded
344 else
345 {
346 error ("fflush: write error");
347 retval(0) = 0.0; // failed
348 }
349
350 return retval;
351 }
352
353 static int
354 valid_mode (const char *mode)
355 {
356 if (mode)
357 {
358 char m = mode[0];
359 if (m == 'r' || m == 'w' || m == 'a')
360 {
361 m = mode[1];
362 return (m == '\0' || (m == '+' && mode[2] == '\0'));
363 }
364 }
365 return 0;
366 } 415 }
367 416
368 DEFUN ("fgets", Ffgets, Sfgets, 2, 2, 417 DEFUN ("fgets", Ffgets, Sfgets, 2, 2,
369 "[STRING, LENGTH] = fgets (FILENAME or FILENUM, LENGTH)\n\ 418 "[STRING, LENGTH] = fgets (FILENAME or FILENUM, LENGTH)\n\
370 \n\ 419 \n\
380 retval = fgets_internal (args, nargout); 429 retval = fgets_internal (args, nargout);
381 430
382 return retval; 431 return retval;
383 } 432 }
384 433
385 Octave_object 434 static Octave_object
386 fgets_internal (const Octave_object& args, int nargout) 435 fopen_internal (const Octave_object& args)
387 { 436 {
388 Octave_object retval; 437 Octave_object retval;
389 438 Pix p;
390 Pix p = file_io_get_file (args(0), "r", "fgets"); 439
391 440 if (! args(0).is_string ())
392 if (! p) 441 {
393 return retval; 442 error ("fopen: file name must be a string");
394
395
396 double dlen = args(1).double_value ();
397
398 if (error_state)
399 return retval;
400
401 if (xisnan (dlen))
402 {
403 error ("fgets: NaN invalid as length");
404 return retval; 443 return retval;
405 } 444 }
406 445
407 int length = NINT (dlen); 446 p = return_valid_file (args(0));
408 447
409 if ((double) length != dlen) 448 if (p)
410 { 449 {
411 error ("fgets: length not an integer value"); 450 file_info file = file_list (p);
451
452 retval(0) = (double) file.number ();
453
412 return retval; 454 return retval;
413 } 455 }
414 456
415 file_info file = file_list (p); 457 if (! args(1).is_string ())
416 458 {
417 char string [length+1]; 459 error ("fopen: file mode must be a string");
418 char *success = fgets (string, length+1, file.fptr ());
419
420 if (! success)
421 {
422 retval(0) = -1.0;
423 return retval; 460 return retval;
424 } 461 }
425 462
426 if (nargout == 2) 463 char *name = args(0).string_value ();
427 retval(1) = (double) strlen (string); 464 char *mode = args(1).string_value ();
428 465
429 retval(0) = string; 466 if (! valid_mode (mode))
467 {
468 error ("fopen: invalid mode");
469 return retval;
470 }
471
472 struct stat buffer;
473 if (stat (name, &buffer) == 0 && (buffer.st_mode & S_IFDIR) == S_IFDIR)
474 {
475 error ("fopen: can't open directory");
476 return retval;
477 }
478
479 FILE *file_ptr = fopen (name, mode);
480
481 if (! file_ptr)
482 {
483 error ("fopen: unable to open file `%s'", name);
484 return retval;
485 }
486
487 int file_number = file_list.length () + 1;
488
489 file_info file (file_number, name, file_ptr, mode);
490 file_list.append (file);
491
492 retval(0) = (double) file_number;
430 493
431 return retval; 494 return retval;
432 } 495 }
433 496
434 DEFUN ("fopen", Ffopen, Sfopen, 2, 1, 497 DEFUN ("fopen", Ffopen, Sfopen, 2, 1,
454 retval = fopen_internal (args); 517 retval = fopen_internal (args);
455 518
456 return retval; 519 return retval;
457 } 520 }
458 521
459 Octave_object 522 static Octave_object
460 fopen_internal (const Octave_object& args)
461 {
462 Octave_object retval;
463 Pix p;
464
465 if (! args(0).is_string ())
466 {
467 error ("fopen: file name must be a string");
468 return retval;
469 }
470
471 p = return_valid_file (args(0));
472
473 if (p)
474 {
475 file_info file = file_list (p);
476
477 retval(0) = (double) file.number ();
478
479 return retval;
480 }
481
482 if (! args(1).is_string ())
483 {
484 error ("fopen: file mode must be a string");
485 return retval;
486 }
487
488 char *name = args(0).string_value ();
489 char *mode = args(1).string_value ();
490
491 if (! valid_mode (mode))
492 {
493 error ("fopen: invalid mode");
494 return retval;
495 }
496
497 struct stat buffer;
498 if (stat (name, &buffer) == 0 && (buffer.st_mode & S_IFDIR) == S_IFDIR)
499 {
500 error ("fopen: can't open directory");
501 return retval;
502 }
503
504 FILE *file_ptr = fopen (name, mode);
505
506 if (! file_ptr)
507 {
508 error ("fopen: unable to open file `%s'", name);
509 return retval;
510 }
511
512 int file_number = file_list.length () + 1;
513
514 file_info file (file_number, name, file_ptr, mode);
515 file_list.append (file);
516
517 retval(0) = (double) file_number;
518
519 return retval;
520 }
521
522 DEFUN ("freport", Ffreport, Sfreport, 0, 1,
523 "freport (): list open files and their status")
524 {
525 Octave_object retval;
526
527 int nargin = args.length ();
528
529 if (nargin > 0)
530 warning ("freport: ignoring extra arguments");
531
532 retval = freport_internal ();
533
534 return retval;
535 }
536
537 Octave_object
538 freport_internal (void) 523 freport_internal (void)
539 { 524 {
540 Octave_object retval; 525 Octave_object retval;
541 Pix p = file_list.first (); 526 Pix p = file_list.first ();
542 527
557 maybe_page_output (output_buf); 542 maybe_page_output (output_buf);
558 543
559 return retval; 544 return retval;
560 } 545 }
561 546
547 DEFUN ("freport", Ffreport, Sfreport, 0, 1,
548 "freport (): list open files and their status")
549 {
550 Octave_object retval;
551
552 int nargin = args.length ();
553
554 if (nargin > 0)
555 warning ("freport: ignoring extra arguments");
556
557 retval = freport_internal ();
558
559 return retval;
560 }
561
562 static Octave_object
563 frewind_internal (const Octave_object& args)
564 {
565 Octave_object retval;
566
567 Pix p = file_io_get_file (args(0), "a+", "frewind");
568
569 if (p)
570 {
571 file_info file = file_list (p);
572 rewind (file.fptr ());
573 }
574
575 return retval;
576 }
577
562 DEFUN ("frewind", Ffrewind, Sfrewind, 1, 1, 578 DEFUN ("frewind", Ffrewind, Sfrewind, 1, 1,
563 "frewind (FILENAME or FILENUM): set file position at beginning of file") 579 "frewind (FILENAME or FILENUM): set file position at beginning of file")
564 { 580 {
565 Octave_object retval; 581 Octave_object retval;
566 582
572 retval = frewind_internal (args); 588 retval = frewind_internal (args);
573 589
574 return retval; 590 return retval;
575 } 591 }
576 592
577 Octave_object 593 static Octave_object
578 frewind_internal (const Octave_object& args)
579 {
580 Octave_object retval;
581
582 Pix p = file_io_get_file (args(0), "a+", "frewind");
583
584 if (p)
585 {
586 file_info file = file_list (p);
587 rewind (file.fptr ());
588 }
589
590 return retval;
591 }
592
593 DEFUN ("fseek", Ffseek, Sfseek, 3, 1,
594 "fseek (FILENAME or FILENUM, OFFSET [, ORIGIN])\n\
595 \n\
596 set file position for reading or writing")
597 {
598 Octave_object retval;
599
600 int nargin = args.length ();
601
602 if (nargin != 2 && nargin != 3)
603 print_usage ("fseek");
604 else
605 retval = fseek_internal (args);
606
607 return retval;
608 }
609
610 Octave_object
611 fseek_internal (const Octave_object& args) 594 fseek_internal (const Octave_object& args)
612 { 595 {
613 Octave_object retval; 596 Octave_object retval;
614 597
615 int nargin = args.length (); 598 int nargin = args.length ();
686 } 669 }
687 670
688 return retval; 671 return retval;
689 } 672 }
690 673
674 DEFUN ("fseek", Ffseek, Sfseek, 3, 1,
675 "fseek (FILENAME or FILENUM, OFFSET [, ORIGIN])\n\
676 \n\
677 set file position for reading or writing")
678 {
679 Octave_object retval;
680
681 int nargin = args.length ();
682
683 if (nargin != 2 && nargin != 3)
684 print_usage ("fseek");
685 else
686 retval = fseek_internal (args);
687
688 return retval;
689 }
690
691 // Tell current position of file. 691 // Tell current position of file.
692
693 static Octave_object
694 ftell_internal (const Octave_object& args)
695 {
696 Octave_object retval;
697
698 Pix p = file_io_get_file (args(0), "a+", "ftell");
699
700 if (p)
701 {
702 file_info file = file_list (p);
703 long offset = ftell (file.fptr ());
704
705 retval(0) = (double) offset;
706
707 if (offset == -1L)
708 error ("ftell: write error");
709 }
710
711 return retval;
712 }
692 713
693 DEFUN ("ftell", Fftell, Sftell, 1, 1, 714 DEFUN ("ftell", Fftell, Sftell, 1, 1,
694 "POSITION = ftell (FILENAME or FILENUM): returns the current file position") 715 "POSITION = ftell (FILENAME or FILENUM): returns the current file position")
695 { 716 {
696 Octave_object retval; 717 Octave_object retval;
699 720
700 if (nargin != 1) 721 if (nargin != 1)
701 print_usage ("ftell"); 722 print_usage ("ftell");
702 else 723 else
703 retval = ftell_internal (args); 724 retval = ftell_internal (args);
704
705 return retval;
706 }
707
708 Octave_object
709 ftell_internal (const Octave_object& args)
710 {
711 Octave_object retval;
712
713 Pix p = file_io_get_file (args(0), "a+", "ftell");
714
715 if (p)
716 {
717 file_info file = file_list (p);
718 long offset = ftell (file.fptr ());
719
720 retval(0) = (double) offset;
721
722 if (offset == -1L)
723 error ("ftell: write error");
724 }
725 725
726 return retval; 726 return retval;
727 } 727 }
728 728
729 void 729 void
958 return -1; 958 return -1;
959 } 959 }
960 960
961 // Formatted printing to a file. 961 // Formatted printing to a file.
962 962
963 DEFUN ("fprintf", Ffprintf, Sfprintf, -1, 1, 963 static Octave_object
964 "fprintf (FILENAME or FILENUM, FORMAT, ...)")
965 {
966 Octave_object retval;
967
968 int nargin = args.length ();
969
970 if (nargin < 2)
971 print_usage ("fprintf");
972 else
973 retval = do_printf ("fprintf", args, nargout);
974
975 return retval;
976 }
977
978 // Formatted printing.
979
980 DEFUN ("printf", Fprintf, Sprintf, -1, 1,
981 "printf (FORMAT, ...)")
982 {
983 Octave_object retval;
984
985 int nargin = args.length ();
986
987 if (nargin < 1)
988 print_usage ("printf");
989 else
990 retval = do_printf ("printf", args, nargout);
991
992 return retval;
993 }
994
995 // Formatted printing to a string.
996
997 DEFUN ("sprintf", Fsprintf, Ssprintf, -1, 1,
998 "s = sprintf (FORMAT, ...)")
999 {
1000 Octave_object retval;
1001
1002 int nargin = args.length ();
1003
1004 if (nargin < 1)
1005 print_usage ("sprintf");
1006 else
1007 retval = do_printf ("sprintf", args, nargout);
1008
1009 return retval;
1010 }
1011
1012 Octave_object
1013 do_printf (const char *type, const Octave_object& args, int nargout) 964 do_printf (const char *type, const Octave_object& args, int nargout)
1014 { 965 {
1015 Octave_object retval; 966 Octave_object retval;
1016 fmt_arg_count = 0; 967 fmt_arg_count = 0;
1017 char *fmt; 968 char *fmt;
1076 output_buf << c; 1027 output_buf << c;
1077 continue; 1028 continue;
1078 } 1029 }
1079 1030
1080 // We must be looking at a format specifier. Extract it or fail. 1031 // We must be looking at a format specifier. Extract it or fail.
1081
1082 1032
1083 int status = process_printf_format (ptr, args, output_buf, type); 1033 int status = process_printf_format (ptr, args, output_buf, type);
1084 1034
1085 if (status < 0) 1035 if (status < 0)
1086 return retval; 1036 return retval;
1106 { 1056 {
1107 char *msg = output_buf.str (); 1057 char *msg = output_buf.str ();
1108 retval(0) = msg; 1058 retval(0) = msg;
1109 delete [] msg; 1059 delete [] msg;
1110 } 1060 }
1061
1062 return retval;
1063 }
1064
1065 DEFUN ("fprintf", Ffprintf, Sfprintf, -1, 1,
1066 "fprintf (FILENAME or FILENUM, FORMAT, ...)")
1067 {
1068 Octave_object retval;
1069
1070 int nargin = args.length ();
1071
1072 if (nargin < 2)
1073 print_usage ("fprintf");
1074 else
1075 retval = do_printf ("fprintf", args, nargout);
1076
1077 return retval;
1078 }
1079
1080 // Formatted printing.
1081
1082 DEFUN ("printf", Fprintf, Sprintf, -1, 1,
1083 "printf (FORMAT, ...)")
1084 {
1085 Octave_object retval;
1086
1087 int nargin = args.length ();
1088
1089 if (nargin < 1)
1090 print_usage ("printf");
1091 else
1092 retval = do_printf ("printf", args, nargout);
1093
1094 return retval;
1095 }
1096
1097 // Formatted printing to a string.
1098
1099 DEFUN ("sprintf", Fsprintf, Ssprintf, -1, 1,
1100 "s = sprintf (FORMAT, ...)")
1101 {
1102 Octave_object retval;
1103
1104 int nargin = args.length ();
1105
1106 if (nargin < 1)
1107 print_usage ("sprintf");
1108 else
1109 retval = do_printf ("sprintf", args, nargout);
1111 1110
1112 return retval; 1111 return retval;
1113 } 1112 }
1114 1113
1115 static int 1114 static int
1279 return -1; 1278 return -1;
1280 } 1279 }
1281 1280
1282 // Formatted reading from a file. 1281 // Formatted reading from a file.
1283 1282
1284 DEFUN ("fscanf", Ffscanf, Sfscanf, 2, -1, 1283 static Octave_object
1285 "[A, B, C, ...] = fscanf (FILENAME or FILENUM, FORMAT)")
1286 {
1287 Octave_object retval;
1288
1289 int nargin = args.length ();
1290
1291 if (nargin != 1 && nargin != 2)
1292 print_usage ("fscanf");
1293 else
1294 retval = do_scanf ("fscanf", args, nargout);
1295
1296 return retval;
1297 }
1298
1299 // Formatted reading.
1300
1301 DEFUN ("scanf", Fscanf, Sscanf, 1, -1,
1302 "[A, B, C, ...] = scanf (FORMAT)")
1303 {
1304 Octave_object retval;
1305
1306 int nargin = args.length ();
1307
1308 if (nargin != 1)
1309 print_usage ("scanf");
1310 else
1311 retval = do_scanf ("scanf", args, nargout);
1312
1313 return retval;
1314 }
1315
1316 // Formatted reading from a string.
1317
1318 DEFUN ("sscanf", Fsscanf, Ssscanf, 2, -1,
1319 "[A, B, C, ...] = sscanf (STRING, FORMAT)")
1320 {
1321 Octave_object retval;
1322
1323 int nargin = args.length ();
1324
1325 if (nargin != 2)
1326 print_usage ("sscanf");
1327 else
1328 retval = do_scanf ("sscanf", args, nargout);
1329
1330 return retval;
1331 }
1332
1333 Octave_object
1334 do_scanf (const char *type, const Octave_object& args, int nargout) 1284 do_scanf (const char *type, const Octave_object& args, int nargout)
1335 { 1285 {
1336 Octave_object retval; 1286 Octave_object retval;
1337 char *scanf_fmt = 0; 1287 char *scanf_fmt = 0;
1338 char *tmp_file = 0; 1288 char *tmp_file = 0;
1470 ptr += status; 1420 ptr += status;
1471 } 1421 }
1472 1422
1473 if (tmp_file_open) 1423 if (tmp_file_open)
1474 fclose (fptr); 1424 fclose (fptr);
1425
1426 return retval;
1427 }
1428
1429 DEFUN ("fscanf", Ffscanf, Sfscanf, 2, -1,
1430 "[A, B, C, ...] = fscanf (FILENAME or FILENUM, FORMAT)")
1431 {
1432 Octave_object retval;
1433
1434 int nargin = args.length ();
1435
1436 if (nargin != 1 && nargin != 2)
1437 print_usage ("fscanf");
1438 else
1439 retval = do_scanf ("fscanf", args, nargout);
1440
1441 return retval;
1442 }
1443
1444 // Formatted reading.
1445
1446 DEFUN ("scanf", Fscanf, Sscanf, 1, -1,
1447 "[A, B, C, ...] = scanf (FORMAT)")
1448 {
1449 Octave_object retval;
1450
1451 int nargin = args.length ();
1452
1453 if (nargin != 1)
1454 print_usage ("scanf");
1455 else
1456 retval = do_scanf ("scanf", args, nargout);
1457
1458 return retval;
1459 }
1460
1461 // Formatted reading from a string.
1462
1463 DEFUN ("sscanf", Fsscanf, Ssscanf, 2, -1,
1464 "[A, B, C, ...] = sscanf (STRING, FORMAT)")
1465 {
1466 Octave_object retval;
1467
1468 int nargin = args.length ();
1469
1470 if (nargin != 2)
1471 print_usage ("sscanf");
1472 else
1473 retval = do_scanf ("sscanf", args, nargout);
1475 1474
1476 return retval; 1475 return retval;
1477 } 1476 }
1478 1477
1479 // Find out how many elements are left to read. 1478 // Find out how many elements are left to read.
1514 fseek (fptr, curr_pos, SEEK_SET); 1513 fseek (fptr, curr_pos, SEEK_SET);
1515 1514
1516 long len = end_of_file - curr_pos; 1515 long len = end_of_file - curr_pos;
1517 1516
1518 return len / size; 1517 return len / size;
1519 }
1520
1521 DEFUN ("fread", Ffread, Sfread, 3, 2,
1522 "[DATA, COUNT] = fread (FILENUM, SIZE, PRECISION)\n\
1523 \n\
1524 Reads data in binary form of type PRECISION from a file.\n\
1525 \n\
1526 FILENUM : file number from fopen\n\
1527 SIZE : size specification for the Data matrix\n\
1528 PRECISION : type of data to read, valid types are\n\
1529 \n\
1530 'char', 'schar', 'short', 'int', 'long', 'float'\n\
1531 'double', 'uchar', 'ushort', 'uint', 'ulong'\n\
1532 \n\
1533 DATA : matrix in which the data is stored\n\
1534 COUNT : number of elements read")
1535 {
1536 Octave_object retval;
1537
1538 int nargin = args.length ();
1539
1540 if (nargin < 1 || nargin > 3)
1541 print_usage ("fread");
1542 else
1543 retval = fread_internal (args, nargout);
1544
1545 return retval;
1546 } 1518 }
1547 1519
1548 // Read binary data from a file. 1520 // Read binary data from a file.
1549 // 1521 //
1550 // [data, count] = fread (fid, size, 'precision') 1522 // [data, count] = fread (fid, size, 'precision')
1565 // Default is uchar. 1537 // Default is uchar.
1566 // 1538 //
1567 // data : output data 1539 // data : output data
1568 // count : number of elements read 1540 // count : number of elements read
1569 1541
1570 Octave_object 1542 static Octave_object
1571 fread_internal (const Octave_object& args, int nargout) 1543 fread_internal (const Octave_object& args, int nargout)
1572 { 1544 {
1573 Octave_object retval; 1545 Octave_object retval;
1574 1546
1575 int nargin = args.length (); 1547 int nargin = args.length ();
1710 retval(0) = m; 1682 retval(0) = m;
1711 1683
1712 return retval; 1684 return retval;
1713 } 1685 }
1714 1686
1715 DEFUN ("fwrite", Ffwrite, Sfwrite, 3, 1, 1687 DEFUN ("fread", Ffread, Sfread, 3, 2,
1716 "COUNT = fwrite (FILENUM, DATA, PRECISION)\n\ 1688 "[DATA, COUNT] = fread (FILENUM, SIZE, PRECISION)\n\
1717 \n\ 1689 \n\
1718 Writes data to a file in binary form of size PRECISION\n\ 1690 Reads data in binary form of type PRECISION from a file.\n\
1719 \n\ 1691 \n\
1720 FILENUM : file number from fopen\n\ 1692 FILENUM : file number from fopen\n\
1721 DATA : matrix of elements to be written\n\ 1693 SIZE : size specification for the Data matrix\n\
1722 PRECISION : type of data to read, valid types are\n\ 1694 PRECISION : type of data to read, valid types are\n\
1723 \n\ 1695 \n\
1724 'char', 'schar', 'short', 'int', 'long', 'float'\n\ 1696 'char', 'schar', 'short', 'int', 'long', 'float'\n\
1725 'double', 'uchar', 'ushort', 'uint', 'ulong'\n\ 1697 'double', 'uchar', 'ushort', 'uint', 'ulong'\n\
1726 \n\ 1698 \n\
1727 COUNT : number of elements written") 1699 DATA : matrix in which the data is stored\n\
1728 { 1700 COUNT : number of elements read")
1729 Octave_object retval; 1701 {
1730 1702 Octave_object retval;
1731 int nargin = args.length (); 1703
1732 1704 int nargin = args.length ();
1733 if (nargin < 2 || nargin > 3) 1705
1734 print_usage ("fwrite"); 1706 if (nargin < 1 || nargin > 3)
1735 else 1707 print_usage ("fread");
1736 retval = fwrite_internal (args, nargout); 1708 else
1709 retval = fread_internal (args, nargout);
1737 1710
1738 return retval; 1711 return retval;
1739 } 1712 }
1740 1713
1741 // Write binary data to a file. 1714 // Write binary data to a file.
1751 // 1724 //
1752 // Default is uchar. 1725 // Default is uchar.
1753 // 1726 //
1754 // count : the number of elements written 1727 // count : the number of elements written
1755 1728
1756 Octave_object 1729 static Octave_object
1757 fwrite_internal (const Octave_object& args, int nargout) 1730 fwrite_internal (const Octave_object& args, int nargout)
1758 { 1731 {
1759 Octave_object retval; 1732 Octave_object retval;
1760 1733
1761 int nargin = args.length (); 1734 int nargin = args.length ();
1790 } 1763 }
1791 1764
1792 return retval; 1765 return retval;
1793 } 1766 }
1794 1767
1795 DEFUN ("feof", Ffeof, Sfeof, 1, 1, 1768 DEFUN ("fwrite", Ffwrite, Sfwrite, 3, 1,
1796 "ERROR = feof (FILENAME or FILENUM)\n\ 1769 "COUNT = fwrite (FILENUM, DATA, PRECISION)\n\
1797 \n\ 1770 \n\
1798 Returns a non zero value for an end of file condition for the\n\ 1771 Writes data to a file in binary form of size PRECISION\n\
1799 file specified by FILENAME or FILENUM from fopen") 1772 \n\
1800 { 1773 FILENUM : file number from fopen\n\
1801 Octave_object retval; 1774 DATA : matrix of elements to be written\n\
1802 1775 PRECISION : type of data to read, valid types are\n\
1803 int nargin = args.length (); 1776 \n\
1804 1777 'char', 'schar', 'short', 'int', 'long', 'float'\n\
1805 if (nargin != 1) 1778 'double', 'uchar', 'ushort', 'uint', 'ulong'\n\
1806 print_usage ("feof"); 1779 \n\
1807 else 1780 COUNT : number of elements written")
1808 retval = feof_internal (args, nargout); 1781 {
1782 Octave_object retval;
1783
1784 int nargin = args.length ();
1785
1786 if (nargin < 2 || nargin > 3)
1787 print_usage ("fwrite");
1788 else
1789 retval = fwrite_internal (args, nargout);
1809 1790
1810 return retval; 1791 return retval;
1811 } 1792 }
1812 1793
1813 // Check for an EOF condition on a file opened by fopen. 1794 // Check for an EOF condition on a file opened by fopen.
1815 // eof = feof (fid) 1796 // eof = feof (fid)
1816 // 1797 //
1817 // fid : file id from fopen 1798 // fid : file id from fopen
1818 // eof : non zero for an end of file condition 1799 // eof : non zero for an end of file condition
1819 1800
1820 Octave_object 1801 static Octave_object
1821 feof_internal (const Octave_object& args, int nargout) 1802 feof_internal (const Octave_object& args, int nargout)
1822 { 1803 {
1823 Octave_object retval; 1804 Octave_object retval;
1824 1805
1825 // Get file info. 1806 // Get file info.
1833 retval(0) = (double) feof (file.fptr ()); 1814 retval(0) = (double) feof (file.fptr ());
1834 1815
1835 return retval; 1816 return retval;
1836 } 1817 }
1837 1818
1838 DEFUN ("ferror", Fferror, Sferror, 1, 1, 1819 DEFUN ("feof", Ffeof, Sfeof, 1, 1,
1839 "ERROR = ferror (FILENAME or FILENUM)\n\ 1820 "ERROR = feof (FILENAME or FILENUM)\n\
1840 \n\ 1821 \n\
1841 Returns a non zero value for an error condition on the\n\ 1822 Returns a non zero value for an end of file condition for the\n\
1842 file specified by FILENAME or FILENUM from fopen") 1823 file specified by FILENAME or FILENUM from fopen")
1843 { 1824 {
1844 Octave_object retval; 1825 Octave_object retval;
1845 1826
1846 int nargin = args.length (); 1827 int nargin = args.length ();
1847 1828
1848 if (nargin != 1) 1829 if (nargin != 1)
1849 print_usage ("ferror"); 1830 print_usage ("feof");
1850 else 1831 else
1851 retval = ferror_internal (args, nargout); 1832 retval = feof_internal (args, nargout);
1852 1833
1853 return retval; 1834 return retval;
1854 } 1835 }
1855 1836
1856 // Check for an error condition on a file opened by fopen. 1837 // Check for an error condition on a file opened by fopen.
1859 // 1840 //
1860 // fid : file id from fopen 1841 // fid : file id from fopen
1861 // message : system error message 1842 // message : system error message
1862 // errnum : error number 1843 // errnum : error number
1863 1844
1864 Octave_object 1845 static Octave_object
1865 ferror_internal (const Octave_object& args, int nargout) 1846 ferror_internal (const Octave_object& args, int nargout)
1866 { 1847 {
1867 Octave_object retval; 1848 Octave_object retval;
1868 1849
1869 // Get file info. 1850 // Get file info.
1878 1859
1879 if (nargout > 1) 1860 if (nargout > 1)
1880 retval(1) = (double) ierr; 1861 retval(1) = (double) ierr;
1881 1862
1882 retval(0) = strsave (strerror (ierr)); 1863 retval(0) = strsave (strerror (ierr));
1864
1865 return retval;
1866 }
1867
1868 DEFUN ("ferror", Fferror, Sferror, 1, 1,
1869 "ERROR = ferror (FILENAME or FILENUM)\n\
1870 \n\
1871 Returns a non zero value for an error condition on the\n\
1872 file specified by FILENAME or FILENUM from fopen")
1873 {
1874 Octave_object retval;
1875
1876 int nargin = args.length ();
1877
1878 if (nargin != 1)
1879 print_usage ("ferror");
1880 else
1881 retval = ferror_internal (args, nargout);
1883 1882
1884 return retval; 1883 return retval;
1885 } 1884 }
1886 1885
1887 /* 1886 /*