comparison libinterp/corefcn/file-io.cc @ 20616:fd0efcdb3718

use new string_value method to handle value extraction errors * dirfns.cc, file-io.cc, gammainc.cc, help.cc, load-path.cc, octave-link.cc, qz.cc, regexp.cc, strfns.cc, syscalls.cc, time.cc, variables.cc: Use new string_value method.
author John W. Eaton <jwe@octave.org>
date Thu, 08 Oct 2015 19:00:51 -0400
parents c41595061186
children
comparison
equal deleted inserted replaced
20615:729a85dafba8 20616:fd0efcdb3718
536 { 536 {
537 octave_stream retval; 537 octave_stream retval;
538 538
539 fid = -1; 539 fid = -1;
540 540
541 if (tc_name.is_string ()) 541 std::string name = tc_name.string_value ("%s: file name must be a string", fcn);
542 { 542 std::string mode = tc_mode.string_value ("%s: file mode must be a string", fcn);
543 std::string name = tc_name.string_value (); 543 std::string arch = tc_arch.string_value ("%s: architecture type must be a string", fcn);
544 544
545 if (tc_mode.is_string ()) 545 retval = do_stream_open (name, mode, arch, fid);
546 {
547 std::string mode = tc_mode.string_value ();
548
549 if (tc_arch.is_string ())
550 {
551 std::string arch = tc_arch.string_value ();
552
553 retval = do_stream_open (name, mode, arch, fid);
554 }
555 else
556 error ("%s: architecture type must be a string", fcn);
557 }
558 else
559 error ("%s: file mode must be a string", fcn);
560 }
561 else
562 error ("%s: file name must be a string", fcn);
563 546
564 return retval; 547 return retval;
565 } 548 }
566 549
567 DEFUN (fopen, args, nargout, 550 DEFUN (fopen, args, nargout,
1345 1328
1346 Array<double> size = size_arg.vector_value (); 1329 Array<double> size = size_arg.vector_value ();
1347 1330
1348 if (! error_state) 1331 if (! error_state)
1349 { 1332 {
1350 if (prec_arg.is_string ()) 1333 std::string prec = prec_arg.string_value ("fread: PRECISION must be a string");
1351 { 1334
1352 std::string prec = prec_arg.string_value (); 1335 int block_size = 1;
1353 1336 oct_data_conv::data_type input_type;
1354 int block_size = 1; 1337 oct_data_conv::data_type output_type;
1355 oct_data_conv::data_type input_type; 1338
1356 oct_data_conv::data_type output_type; 1339 oct_data_conv::string_to_data_type (prec, block_size,
1357 1340 input_type, output_type);
1358 oct_data_conv::string_to_data_type (prec, block_size, 1341
1359 input_type, output_type); 1342 if (! error_state)
1343 {
1344 int skip = skip_arg.int_value (true);
1360 1345
1361 if (! error_state) 1346 if (! error_state)
1362 { 1347 {
1363 int skip = skip_arg.int_value (true); 1348 std::string arch = arch_arg.string_value ("fread: ARCH architecture type must be a string");
1364 1349
1365 if (! error_state) 1350 oct_mach_info::float_format flt_fmt
1366 { 1351 = oct_mach_info::string_to_float_format (arch);
1367 if (arch_arg.is_string ()) 1352
1368 { 1353 retval = os.read (size, block_size, input_type,
1369 std::string arch = arch_arg.string_value (); 1354 output_type, skip, flt_fmt, count);
1370
1371 oct_mach_info::float_format flt_fmt
1372 = oct_mach_info::string_to_float_format (arch);
1373
1374 retval = os.read (size, block_size, input_type,
1375 output_type, skip, flt_fmt, count);
1376 }
1377 else
1378 error ("fread: ARCH architecture type must be a string");
1379 }
1380 else
1381 error ("fread: SKIP must be an integer");
1382 } 1355 }
1383 else 1356 else
1384 error ("fread: invalid PRECISION specified"); 1357 error ("fread: SKIP must be an integer");
1385 } 1358 }
1386 else 1359 else
1387 error ("fread: PRECISION must be a string"); 1360 error ("fread: invalid PRECISION specified");
1388 } 1361 }
1389 else 1362 else
1390 error ("fread: invalid SIZE specified"); 1363 error ("fread: invalid SIZE specified");
1391 1364
1392 return retval; 1365 return retval;
1616 const octave_value& prec_arg, const octave_value& skip_arg, 1589 const octave_value& prec_arg, const octave_value& skip_arg,
1617 const octave_value& arch_arg) 1590 const octave_value& arch_arg)
1618 { 1591 {
1619 int retval = -1; 1592 int retval = -1;
1620 1593
1621 if (prec_arg.is_string ()) 1594 std::string prec = prec_arg.string_value ("fwrite: PRECISION must be a string");
1622 { 1595
1623 std::string prec = prec_arg.string_value (); 1596 int block_size = 1;
1624 1597 oct_data_conv::data_type output_type;
1625 int block_size = 1; 1598
1626 oct_data_conv::data_type output_type; 1599 oct_data_conv::string_to_data_type (prec, block_size, output_type);
1627 1600
1628 oct_data_conv::string_to_data_type (prec, block_size, output_type); 1601 if (! error_state)
1602 {
1603 int skip = skip_arg.int_value (true);
1629 1604
1630 if (! error_state) 1605 if (! error_state)
1631 { 1606 {
1632 int skip = skip_arg.int_value (true); 1607 std::string arch = arch_arg.string_value ("fwrite: ARCH architecture type must be a string");
1633 1608
1634 if (! error_state) 1609 oct_mach_info::float_format flt_fmt
1635 { 1610 = oct_mach_info::string_to_float_format (arch);
1636 if (arch_arg.is_string ()) 1611
1637 { 1612 retval = os.write (data, block_size, output_type,
1638 std::string arch = arch_arg.string_value (); 1613 skip, flt_fmt);
1639
1640 oct_mach_info::float_format flt_fmt
1641 = oct_mach_info::string_to_float_format (arch);
1642
1643 retval = os.write (data, block_size, output_type,
1644 skip, flt_fmt);
1645 }
1646 else
1647 error ("fwrite: ARCH architecture type must be a string");
1648 }
1649 else
1650 error ("fwrite: SKIP must be an integer");
1651 } 1614 }
1652 else 1615 else
1653 error ("fwrite: invalid PRECISION specified"); 1616 error ("fwrite: SKIP must be an integer");
1654 } 1617 }
1655 else 1618 else
1656 error ("fwrite: PRECISION must be a string"); 1619 error ("fwrite: invalid PRECISION specified");
1657 1620
1658 return retval; 1621 return retval;
1659 } 1622 }
1660 1623
1661 DEFUN (fwrite, args, , 1624 DEFUN (fwrite, args, ,
1841 1804
1842 int nargin = args.length (); 1805 int nargin = args.length ();
1843 1806
1844 if (nargin == 2) 1807 if (nargin == 2)
1845 { 1808 {
1846 if (args(0).is_string ()) 1809 std::string name = args(0).string_value ("popen: COMMAND must be a string");
1847 { 1810 std::string mode = args(1).string_value ("popen: MODE must be a string");
1848 std::string name = args(0).string_value (); 1811
1849 1812 if (mode == "r")
1850 if (args(1).is_string ()) 1813 {
1851 { 1814 octave_stream ips = octave_iprocstream::create (name);
1852 std::string mode = args(1).string_value (); 1815
1853 1816 retval = octave_stream_list::insert (ips);
1854 if (mode == "r") 1817 }
1855 { 1818 else if (mode == "w")
1856 octave_stream ips = octave_iprocstream::create (name); 1819 {
1857 1820 octave_stream ops = octave_oprocstream::create (name);
1858 retval = octave_stream_list::insert (ips); 1821
1859 } 1822 retval = octave_stream_list::insert (ops);
1860 else if (mode == "w")
1861 {
1862 octave_stream ops = octave_oprocstream::create (name);
1863
1864 retval = octave_stream_list::insert (ops);
1865 }
1866 else
1867 error ("popen: invalid MODE specified");
1868 }
1869 else
1870 error ("popen: MODE must be a string");
1871 } 1823 }
1872 else 1824 else
1873 error ("popen: COMMAND must be a string"); 1825 error ("popen: invalid MODE specified");
1874 } 1826 }
1875 else 1827 else
1876 print_usage (); 1828 print_usage ();
1877 1829
1878 return retval; 1830 return retval;
1924 int len = args.length (); 1876 int len = args.length ();
1925 1877
1926 if (len < 3) 1878 if (len < 3)
1927 { 1879 {
1928 std::string dir; 1880 std::string dir;
1881
1929 if (len > 0) 1882 if (len > 0)
1930 { 1883 dir = args(0).string_value ("tempname: DIR must be a string");
1931 if (args(0).is_string ())
1932 dir = args(0).string_value ();
1933 else
1934 error ("DIR must be a string");
1935 }
1936 1884
1937 std::string pfx ("oct-"); 1885 std::string pfx ("oct-");
1886
1938 if (len > 1) 1887 if (len > 1)
1939 { 1888 pfx = args(1).string_value ("tempname: PREFIX must be a string");
1940 if (args(1).is_string ())
1941 pfx = args(1).string_value ();
1942 else
1943 error ("PREFIX must be a string");
1944 }
1945 1889
1946 retval = octave_tempnam (dir, pfx); 1890 retval = octave_tempnam (dir, pfx);
1947 } 1891 }
1948 else 1892 else
1949 print_usage (); 1893 print_usage ();
2082 2026
2083 int nargin = args.length (); 2027 int nargin = args.length ();
2084 2028
2085 if (nargin == 1 || nargin == 2) 2029 if (nargin == 1 || nargin == 2)
2086 { 2030 {
2087 if (args(0).is_string ()) 2031 std::string tmpl8 = args(0).string_value ("mkstemp: TEMPLATE argument must be a string");
2088 { 2032
2089 std::string tmpl8 = args(0).string_value (); 2033 OCTAVE_LOCAL_BUFFER (char, tmp, tmpl8.size () + 1);
2090 2034 strcpy (tmp, tmpl8.c_str ());
2091 OCTAVE_LOCAL_BUFFER (char, tmp, tmpl8.size () + 1); 2035
2092 strcpy (tmp, tmpl8.c_str ()); 2036 int fd = gnulib::mkostemp (tmp, O_BINARY);
2093 2037
2094 int fd = gnulib::mkostemp (tmp, O_BINARY); 2038 if (fd < 0)
2095 2039 {
2096 if (fd < 0) 2040 retval(2) = gnulib::strerror (errno);
2041 retval(0) = fd;
2042 }
2043 else
2044 {
2045 const char *fopen_mode = "w+b";
2046
2047 FILE *fid = fdopen (fd, fopen_mode);
2048
2049 if (fid)
2097 { 2050 {
2098 retval(2) = gnulib::strerror (errno); 2051 std::string nm = tmp;
2099 retval(0) = fd; 2052
2053 std::ios::openmode md = fopen_mode_to_ios_mode (fopen_mode);
2054
2055 octave_stream s = octave_stdiostream::create (nm, fid, md);
2056
2057 if (s)
2058 {
2059 retval(1) = nm;
2060 retval(0) = octave_stream_list::insert (s);
2061
2062 if (nargin == 2 && args(1).is_true ())
2063 mark_for_deletion (nm);
2064 }
2065 else
2066 error ("mkstemp: failed to create octave_stdiostream object");
2100 } 2067 }
2101 else 2068 else
2102 { 2069 {
2103 const char *fopen_mode = "w+b"; 2070 retval(2) = gnulib::strerror (errno);
2104 2071 retval(0) = -1;
2105 FILE *fid = fdopen (fd, fopen_mode);
2106
2107 if (fid)
2108 {
2109 std::string nm = tmp;
2110
2111 std::ios::openmode md = fopen_mode_to_ios_mode (fopen_mode);
2112
2113 octave_stream s = octave_stdiostream::create (nm, fid, md);
2114
2115 if (s)
2116 {
2117 retval(1) = nm;
2118 retval(0) = octave_stream_list::insert (s);
2119
2120 if (nargin == 2 && args(1).is_true ())
2121 mark_for_deletion (nm);
2122 }
2123 else
2124 error ("mkstemp: failed to create octave_stdiostream object");
2125 }
2126 else
2127 {
2128 retval(2) = gnulib::strerror (errno);
2129 retval(0) = -1;
2130 }
2131 } 2072 }
2132 } 2073 }
2133 else
2134 error ("mkstemp: TEMPLATE argument must be a string");
2135 } 2074 }
2136 else 2075 else
2137 print_usage (); 2076 print_usage ();
2138 2077
2139 return retval; 2078 return retval;