comparison src/file-io.cc @ 4326:1cae4472c624

[project @ 2003-02-15 23:14:47 by jwe]
author jwe
date Sat, 15 Feb 2003 23:14:47 +0000
parents 7cd865a8c815
children c29c382a5b4b
comparison
equal deleted inserted replaced
4325:f30803e587ac 4326:1cae4472c624
60 #include "oct-obj.h" 60 #include "oct-obj.h"
61 #include "oct-prcstrm.h" 61 #include "oct-prcstrm.h"
62 #include "oct-stream.h" 62 #include "oct-stream.h"
63 #include "oct-strstrm.h" 63 #include "oct-strstrm.h"
64 #include "pager.h" 64 #include "pager.h"
65 #include "pt-plot.h"
65 #include "sysdep.h" 66 #include "sysdep.h"
66 #include "utils.h" 67 #include "utils.h"
67 #include "variables.h" 68 #include "variables.h"
68 69
69 static octave_value stdin_file; 70 static octave_value stdin_file;
1382 return retval; 1383 return retval;
1383 } 1384 }
1384 1385
1385 DEFUN (popen, args, , 1386 DEFUN (popen, args, ,
1386 "-*- texinfo -*-\n\ 1387 "-*- texinfo -*-\n\
1387 @deftypefn {Built-in Function} {fid =} popen (@var{command}, @var{mode})\n\ 1388 @deftypefn {Built-in Function} {@var{fid} =} popen (@var{command}, @var{mode})\n\
1388 Start a process and create a pipe. The name of the command to run is\n\ 1389 Start a process and create a pipe. The name of the command to run is\n\
1389 given by @var{command}. The file identifier corresponding to the input\n\ 1390 given by @var{command}. The file identifier corresponding to the input\n\
1390 or output stream of the process is returned in @var{fid}. The argument\n\ 1391 or output stream of the process is returned in @var{fid}. The argument\n\
1391 @var{mode} may be\n\ 1392 @var{mode} may be\n\
1392 \n\ 1393 \n\
1484 If @var{dir} is also omitted, the default directory for temporary files\n\ 1485 If @var{dir} is also omitted, the default directory for temporary files\n\
1485 is used. If @var{dir} is provided, it must exist, otherwise the default\n\ 1486 is used. If @var{dir} is provided, it must exist, otherwise the default\n\
1486 directory for temporary files is used. Since the named file is not\n\ 1487 directory for temporary files is used. Since the named file is not\n\
1487 opened, by @code{tmpnam}, it is possible (though relatively unlikely)\n\ 1488 opened, by @code{tmpnam}, it is possible (though relatively unlikely)\n\
1488 that it will not be available by the time your program attempts to open it.\n\ 1489 that it will not be available by the time your program attempts to open it.\n\
1489 @end deftypefn") 1490 @end deftypefn\n\
1491 @seealso{tmpfile, mkstemp, and P_tmpdir}")
1490 { 1492 {
1491 octave_value retval; 1493 octave_value retval;
1492 1494
1493 int len = args.length (); 1495 int len = args.length ();
1494 1496
1514 1516
1515 return retval; 1517 return retval;
1516 } 1518 }
1517 1519
1518 DEFALIAS (octave_tmp_file_name, tmpnam); 1520 DEFALIAS (octave_tmp_file_name, tmpnam);
1521
1522 DEFUN (tmpfile, args, ,
1523 "-*- texinfo -*-\n\
1524 @deftypefn {Built-in Function} {[@var{fid}, @var{msg}] =} tmpfile ()\n\
1525 Return the file ID corresponding to a new temporary file with a unique\n\
1526 name. The file is opened in binary read/write (@code{\"w+b\"}) mode.\n\
1527 The file will be deleted automatically when it is closed or when Octave\n\
1528 exits.\n\
1529 \n\
1530 If successful, @var{fid} is a valid file ID and @var{msg} is an empty\n\
1531 string. Otherwise, @var{fid} is -1 and @var{msg} contains a\n\
1532 system-dependent error message.\n\
1533 @end deftypefn\n\
1534 @seealso{tmpnam, mkstemp, and P_tmpdir}")
1535 {
1536 octave_value_list retval;
1537
1538 retval(1) = std::string ();
1539 retval(0) = -1;
1540
1541 int nargin = args.length ();
1542
1543 if (nargin == 0)
1544 {
1545 FILE *fid = tmpfile ();
1546
1547 if (fid)
1548 {
1549 std::string nm;
1550
1551 std::ios::openmode md = fopen_mode_to_ios_mode ("w+b");
1552
1553 octave_stream s = octave_iostdiostream::create (nm, fid, md);
1554
1555 if (s)
1556 retval(0) = octave_stream_list::insert (s);
1557 else
1558 error ("tmpfile: failed to create octave_iostdiostream object");
1559
1560 }
1561 else
1562 {
1563 using namespace std;
1564 retval(1) = ::strerror (errno);
1565 retval(0) = -1;
1566 }
1567 }
1568 else
1569 print_usage ("tmpfile");
1570
1571 return retval;
1572 }
1573
1574 #define HAVE_MKSTEMP 1
1575
1576
1577 DEFUN (mkstemp, args, ,
1578 "-*- texinfo -*-\n\
1579 @deftypefn {Built-in Function} {[@var{fid}, @var{name}, @var{msg}] =} tmpfile (@var{template}, @var{delete})\n\
1580 Return the file ID corresponding to a new temporary file with a unique\n\
1581 name created from @var{template}. The last six characters of @var{template}\n\
1582 must be @code{XXXXXX} and tehse are replaced with a string that makes the\n\
1583 filename unique. The file is then created with mode read/write and\n\
1584 permissions that are system dependent (on GNU/Linux systems, the permissions\n\
1585 will be 0600 for versions of glibc 2.0.7 and later). The file is opened\n\
1586 with the @code{O_EXCL} flag.\n\
1587 \n\
1588 If the optional argument @var{delete} is supplied and is true,\n\
1589 the file will be deleted automatically when Octave exits, or when\n\
1590 the function @code{purge_tmp_files} is called.\n\
1591 \n\
1592 If successful, @var{fid} is a valid file ID, @var{name} is the name of\n\
1593 the file, and and @var{msg} is an empty string. Otherwise, @var{fid}\n\
1594 is -1, @var{name} is empty, and @var{msg} contains a system-dependent\n\
1595 error message.\n\
1596 @end deftypefn\n\
1597 @seealso{tmpfile, tmpnam, and P_tmpdir}")
1598 {
1599 octave_value_list retval;
1600
1601 retval(2) = std::string ();
1602 retval(1) = std::string ();
1603 retval(0) = -1;
1604
1605 #if defined (HAVE_MKSTEMP)
1606
1607 int nargin = args.length ();
1608
1609 if (nargin == 1 || nargin == 2)
1610 {
1611 std::string tmpl8 = args(0).string_value ();
1612
1613 if (! error_state)
1614 {
1615 std::auto_ptr<char> tmp_auto_ptr (strsave (tmpl8.c_str ()));
1616 char *tmp = tmp_auto_ptr.get ();
1617
1618 int fd = mkstemp (tmp);
1619
1620 if (fd < 0)
1621 {
1622 using namespace std;
1623 retval(1) = ::strerror (errno);
1624 retval(0) = fd;
1625 }
1626 else
1627 {
1628 const char *fopen_mode = "w+";
1629
1630 FILE *fid = fdopen (fd, fopen_mode);
1631
1632 if (fid)
1633 {
1634 std::string nm = tmp;
1635
1636 std::ios::openmode md = fopen_mode_to_ios_mode (fopen_mode);
1637
1638 octave_stream s = octave_iostdiostream::create (nm, fid, md);
1639
1640 if (s)
1641 {
1642 retval(1) = nm;
1643 retval(0) = octave_stream_list::insert (s);
1644
1645 if (nargin == 2)
1646 mark_for_deletion (nm);
1647 }
1648 else
1649 error ("mkstemp: failed to create octave_iostdiostream object");
1650 }
1651 else
1652 {
1653 using namespace std;
1654 retval(1) = ::strerror (errno);
1655 retval(0) = -1;
1656 }
1657 }
1658 }
1659 else
1660 error ("mkstemp: expecting string as first argument");
1661 }
1662 else
1663 print_usage ("mkstemp");
1664
1665 #else
1666 retval(2) = "mkstemp: not supported on this sytem";
1667 #endif
1668
1669 return retval;
1670 }
1519 1671
1520 static int 1672 static int
1521 convert (int x, int ibase, int obase) 1673 convert (int x, int ibase, int obase)
1522 { 1674 {
1523 int retval = 0; 1675 int retval = 0;