comparison libinterp/corefcn/file-io.cc @ 21622:fed1111e1899

textscan: eliminate ReturnOnError="continue" mode (bug #47538) * oct-stream.cc (textscan_format_list::read_first_row, textscan::scan_one) No longer skip failed fields. (textscan::do_scan): Do not terminate on no_conversions. (textscan::read_format_once): Only "fail" if field wasn't empty. (textscan::parse_options): No longer parse "continue" option. * file-io.cc (Ftextscan): Update docstring and self-tests.
author Lachlan Andrew <lachlanbis@gmail.com>
date Sun, 10 Apr 2016 19:49:50 +1000
parents fe0a6de805e4
children 3cadca91e390
comparison
equal deleted inserted replaced
21621:fe0a6de805e4 21622:fed1111e1899
1335 The word specified by the remainder of the conversion specifier is skipped.\n\ 1335 The word specified by the remainder of the conversion specifier is skipped.\n\
1336 \n\ 1336 \n\
1337 @item literals\n\ 1337 @item literals\n\
1338 In addition the format may contain literal character strings; these will be\n\ 1338 In addition the format may contain literal character strings; these will be\n\
1339 skipped during reading. If the input string does not match this literal,\n\ 1339 skipped during reading. If the input string does not match this literal,\n\
1340 the processing terminates, unless @qcode{\"ReturnOnError\"} is set to\n\ 1340 the processing terminates.\n\
1341 @qcode{\"continue\"}.\n\
1342 @end table\n\ 1341 @end table\n\
1343 \n\ 1342 \n\
1344 Parsed words corresponding to the first specifier are returned in the first\n\ 1343 Parsed words corresponding to the first specifier are returned in the first\n\
1345 output argument and likewise for the rest of the specifiers.\n\ 1344 output argument and likewise for the rest of the specifiers.\n\
1346 \n\ 1345 \n\
1454 Treat single occurrences (surrounded by delimiters or whitespace) of the\n\ 1453 Treat single occurrences (surrounded by delimiters or whitespace) of the\n\
1455 string(s) in @var{value} as missing values.\n\ 1454 string(s) in @var{value} as missing values.\n\
1456 \n\ 1455 \n\
1457 @item @qcode{\"ReturnOnError\"}\n\ 1456 @item @qcode{\"ReturnOnError\"}\n\
1458 If set to numerical 1 or true, return normally as soon as an error is\n\ 1457 If set to numerical 1 or true, return normally as soon as an error is\n\
1459 encountered, such as trying to read a string using @qcode{%f}. If set to 0\n\ 1458 encountered, such as trying to read a string using @qcode{%f}.\n\
1460 or false, return an error and no data. If set to @qcode{\"continue\"}\n\ 1459 If set to 0 or false, return an error and no data.\n\
1461 (default), textscan attempts to continue reading beyond the location;\n\
1462 however, this may cause the parsing to get out of sync.\n\
1463 \n\ 1460 \n\
1464 @item @qcode{\"Whitespace\"}\n\ 1461 @item @qcode{\"Whitespace\"}\n\
1465 Any character in @var{value} will be interpreted as whitespace and trimmed;\n\ 1462 Any character in @var{value} will be interpreted as whitespace and trimmed;\n\
1466 The default value for whitespace is\n\ 1463 The default value for whitespace is\n\
1467 @c Note: the next line specifically has a newline which generates a space\n\ 1464 @c Note: the next line specifically has a newline which generates a space\n\
1688 %! f = tempname (); 1685 %! f = tempname ();
1689 %! fid = fopen (f, "w+"); 1686 %! fid = fopen (f, "w+");
1690 %! str = "1 2 3\n4 s 6"; 1687 %! str = "1 2 3\n4 s 6";
1691 %! fprintf (fid, str); 1688 %! fprintf (fid, str);
1692 %! fseek (fid, 0, "bof"); 1689 %! fseek (fid, 0, "bof");
1693 %! c = textscan (fid, "%f %f %f"); 1690 %! c = textscan (fid, "%f %f %f", "ReturnOnError", 1);
1694 %! fseek (fid, 0, "bof");
1695 %! d = textscan (fid, "%f %f %f", "ReturnOnError", 1);
1696 %! fseek (fid, 0, "bof"); 1691 %! fseek (fid, 0, "bof");
1697 %! fclose (fid); 1692 %! fclose (fid);
1698 %! unlink (f); 1693 %! unlink (f);
1699 %! u = textscan (str, "%f %f %f"); 1694 %! u = textscan (str, "%f %f %f", "ReturnOnError", 1);
1700 %! v = textscan (str, "%f %f %f", "ReturnOnError", 1); 1695 %! assert (c, {[1;4], [2], [3]});
1701 %! assert (c, {[1;4], [2;NaN], [3;6]}); 1696 %! assert (u, {[1;4], [2], [3]});
1702 %! assert (d, {[1;4], [2], [3]});
1703 %! assert (u, {[1;4], [2;NaN], [3;6]});
1704 %! assert (v, {[1;4], [2], [3]});
1705 1697
1706 %!test 1698 %!test
1707 %! ## Check ReturnOnError 1699 %! ## Check ReturnOnError
1708 %! f = tempname (); 1700 %! f = tempname ();
1709 %! fid = fopen (f, "w+"); 1701 %! fid = fopen (f, "w+");
1724 %! ## Check ReturnOnError 1716 %! ## Check ReturnOnError
1725 %! f = tempname (); 1717 %! f = tempname ();
1726 %! fid = fopen (f, "w+"); 1718 %! fid = fopen (f, "w+");
1727 %! fprintf (fid, "1 s 3\n4 5 6"); 1719 %! fprintf (fid, "1 s 3\n4 5 6");
1728 %! fseek (fid, 0, "bof"); 1720 %! fseek (fid, 0, "bof");
1729 %! c = textscan (fid, ""); 1721 %! c = textscan (fid, "", "ReturnOnError", 1);
1730 %! fseek (fid, 0, "bof");
1731 %! d = textscan (fid, "", "ReturnOnError", 1);
1732 %! fseek (fid, 0, "bof"); 1722 %! fseek (fid, 0, "bof");
1733 %! fclose (fid); 1723 %! fclose (fid);
1734 %! unlink (f); 1724 %! unlink (f);
1735 %! assert (c, {[1;4], [NaN;5], [3;6]}); 1725 %! assert (c, {1});
1736 %! assert (d, {1});
1737 1726
1738 %!test 1727 %!test
1739 %! ## Check ReturnOnError with empty fields 1728 %! ## Check ReturnOnError with empty fields
1740 %! c = textscan ("1,,3\n4,5,6", "", "Delimiter", ",", "ReturnOnError", 1); 1729 %! c = textscan ("1,,3\n4,5,6", "", "Delimiter", ",", "ReturnOnError", 1);
1741 %! assert (c, {[1;4], [NaN;5], [3;6]}); 1730 %! assert (c, {[1;4], [NaN;5], [3;6]});