Mercurial > hg > octave-nkf
comparison src/file-io.cc @ 1339:e1dbc5585afd
[project @ 1995-08-25 03:58:02 by jwe]
author | jwe |
---|---|
date | Fri, 25 Aug 1995 03:58:02 +0000 |
parents | 3d235e3c13c0 |
children | 94bedeb289e5 |
comparison
equal
deleted
inserted
replaced
1338:3d235e3c13c0 | 1339:e1dbc5585afd |
---|---|
374 } | 374 } |
375 return 0; | 375 return 0; |
376 } | 376 } |
377 | 377 |
378 static Octave_object | 378 static Octave_object |
379 fgets_internal (const Octave_object& args, int nargin, int nargout) | 379 fgets_internal (const Octave_object& args, int nargin, int nargout, |
380 int strip_final_newline = 0) | |
380 { | 381 { |
381 Octave_object retval; | 382 Octave_object retval; |
382 | 383 |
383 Pix p = file_io_get_file (args(0), "r", "fgets"); | 384 Pix p = file_io_get_file (args(0), "r", "fgets"); |
384 | 385 |
385 if (! p) | 386 if (! p) |
386 return retval; | 387 return retval; |
387 | 388 |
388 file_info file = file_list (p); | 389 int length = 0; |
389 | |
390 FILE *fileptr = file.fptr (); | |
391 | |
392 char *string = 0; | |
393 char *success = 0; | |
394 | 390 |
395 if (nargin == 2) | 391 if (nargin == 2) |
396 { | 392 { |
397 double dlen = args(1).double_value (); | 393 double dlen = args(1).double_value (); |
398 | 394 |
403 { | 399 { |
404 error ("fgets: NaN invalid as length"); | 400 error ("fgets: NaN invalid as length"); |
405 return retval; | 401 return retval; |
406 } | 402 } |
407 | 403 |
408 int length = NINT (dlen); | 404 length = NINT (dlen); |
409 | 405 |
410 if ((double) length != dlen) | 406 if ((double) length != dlen) |
411 { | 407 { |
412 error ("fgets: length not an integer value"); | 408 error ("fgets: length not an integer value"); |
413 return retval; | 409 return retval; |
414 } | 410 } |
415 | 411 |
416 char *string = new char[length+1]; | 412 if (length < 0) |
417 char *success = fgets (string, length+1, fileptr); | 413 { |
418 } | 414 error ("fgets: length must be a nonnegative integer"); |
419 else | 415 return retval; |
420 { | 416 } |
421 ostrstream buf; | 417 } |
422 int c; | 418 |
423 while ((c = fgetc (fileptr))) | 419 file_info file = file_list (p); |
424 { | 420 FILE *fileptr = file.fptr (); |
425 buf << (char) c; | 421 |
422 ostrstream buf; | |
423 int c; | |
424 int count = 0; | |
425 int newline_stripped = 0; | |
426 | |
427 if (nargin == 1 || length > 0) | |
428 { | |
429 while ((c = fgetc (fileptr)) != EOF) | |
430 { | |
431 count++; | |
426 if (c == '\n') | 432 if (c == '\n') |
427 { | 433 { |
428 buf << ends; | 434 if (! strip_final_newline) |
429 string = buf.str (); | 435 buf << (char) c; |
436 else | |
437 newline_stripped = 1; | |
438 | |
430 break; | 439 break; |
431 } | 440 } |
432 } | 441 else |
433 | 442 buf << (char) c; |
434 if (string && strlen (string) > 0) | 443 |
435 success = string; | 444 if (nargin == 2 && count == length) |
436 } | 445 break; |
437 | 446 } |
438 if (success) | 447 } |
448 | |
449 buf << ends; | |
450 char *string = buf.str (); | |
451 | |
452 if (count) | |
439 { | 453 { |
440 if (nargout == 2) | 454 if (nargout == 2) |
441 retval(1) = (double) strlen (string); | 455 retval(1) = (double) (count - newline_stripped); |
442 | 456 |
443 retval(0) = string; | 457 retval(0) = string; |
444 } | 458 } |
445 else | 459 else |
446 retval(0) = -1.0; | 460 retval(0) = -1.0; |
448 delete [] string; | 462 delete [] string; |
449 | 463 |
450 return retval; | 464 return retval; |
451 } | 465 } |
452 | 466 |
467 DEFUN ("fgetl", Ffgetl, Sfgetl, 2, 2, | |
468 "[STRING, LENGTH] = fgetl (FILENAME or FILENUM [, LENGTH])\n\ | |
469 \n\ | |
470 read a string from a file") | |
471 { | |
472 Octave_object retval; | |
473 | |
474 int nargin = args.length (); | |
475 | |
476 if (nargin == 1 || nargin == 2) | |
477 retval = fgets_internal (args, nargin, nargout, 1); | |
478 else | |
479 print_usage ("fgetl"); | |
480 | |
481 return retval; | |
482 } | |
483 | |
453 DEFUN ("fgets", Ffgets, Sfgets, 2, 2, | 484 DEFUN ("fgets", Ffgets, Sfgets, 2, 2, |
454 "[STRING, LENGTH] = fgets (FILENAME or FILENUM, LENGTH)\n\ | 485 "[STRING, LENGTH] = fgets (FILENAME or FILENUM [, LENGTH])\n\ |
455 \n\ | 486 \n\ |
456 read a string from a file") | 487 read a string from a file") |
457 { | 488 { |
458 Octave_object retval; | 489 Octave_object retval; |
459 | 490 |