diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1998-11-20 13:41:43 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1998-11-20 13:41:43 -0200 |
commit | e4830ddce3d477f51ecf1da447dd8e361d74a1a0 (patch) | |
tree | 8caff56d2056e11e8cb9a2af7722d3fb8e45e688 | |
parent | 758e330d6e5ef83c1fc3bfd58c3d626d5968d12c (diff) | |
download | lua-e4830ddce3d477f51ecf1da447dd8e361d74a1a0.tar.gz lua-e4830ddce3d477f51ecf1da447dd8e361d74a1a0.tar.bz2 lua-e4830ddce3d477f51ecf1da447dd8e361d74a1a0.zip |
new option "mode" in "readfrom", "writeto" and "appendto" (for
binary files).
-rw-r--r-- | liolib.c | 18 | ||||
-rw-r--r-- | manual.tex | 16 |
2 files changed, 25 insertions, 9 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: liolib.c,v 1.24 1998/08/30 20:25:24 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 1.25 1998/09/07 18:59:59 roberto Exp roberto $ |
3 | ** Standard I/O (and system) library | 3 | ** Standard I/O (and system) library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -62,6 +62,7 @@ static void pushresult (int i) | |||
62 | else { | 62 | else { |
63 | lua_pushnil(); | 63 | lua_pushnil(); |
64 | lua_pushstring(strerror(errno)); | 64 | lua_pushstring(strerror(errno)); |
65 | lua_pushnumber(errno); | ||
65 | } | 66 | } |
66 | } | 67 | } |
67 | 68 | ||
@@ -102,6 +103,15 @@ static FILE *getfileparam (char *name, int *arg) { | |||
102 | } | 103 | } |
103 | 104 | ||
104 | 105 | ||
106 | static char *getmode (char mode) { | ||
107 | static char m[3]; | ||
108 | m[0] = mode; | ||
109 | m[1] = (*luaL_opt_string(FIRSTARG+1, "text") == 'b') ? 'b' : '\0'; | ||
110 | m[2] = '\0'; | ||
111 | return m; | ||
112 | } | ||
113 | |||
114 | |||
105 | static void closefile (char *name) | 115 | static void closefile (char *name) |
106 | { | 116 | { |
107 | FILE *f = getfilebyname(name); | 117 | FILE *f = getfilebyname(name); |
@@ -140,7 +150,7 @@ static void io_readfrom (void) | |||
140 | current = lua_getuserdata(f); | 150 | current = lua_getuserdata(f); |
141 | else { | 151 | else { |
142 | char *s = luaL_check_string(FIRSTARG); | 152 | char *s = luaL_check_string(FIRSTARG); |
143 | current = (*s == '|') ? popen(s+1, "r") : fopen(s, "r"); | 153 | current = (*s == '|') ? popen(s+1, "r") : fopen(s, getmode('r')); |
144 | if (current == NULL) { | 154 | if (current == NULL) { |
145 | pushresult(0); | 155 | pushresult(0); |
146 | return; | 156 | return; |
@@ -162,7 +172,7 @@ static void io_writeto (void) | |||
162 | current = lua_getuserdata(f); | 172 | current = lua_getuserdata(f); |
163 | else { | 173 | else { |
164 | char *s = luaL_check_string(FIRSTARG); | 174 | char *s = luaL_check_string(FIRSTARG); |
165 | current = (*s == '|') ? popen(s+1,"w") : fopen(s,"w"); | 175 | current = (*s == '|') ? popen(s+1,"w") : fopen(s,getmode('w')); |
166 | if (current == NULL) { | 176 | if (current == NULL) { |
167 | pushresult(0); | 177 | pushresult(0); |
168 | return; | 178 | return; |
@@ -175,7 +185,7 @@ static void io_writeto (void) | |||
175 | static void io_appendto (void) | 185 | static void io_appendto (void) |
176 | { | 186 | { |
177 | char *s = luaL_check_string(FIRSTARG); | 187 | char *s = luaL_check_string(FIRSTARG); |
178 | FILE *fp = fopen (s, "a"); | 188 | FILE *fp = fopen (s, getmode('a')); |
179 | if (fp != NULL) | 189 | if (fp != NULL) |
180 | setreturn(fp, FOUTPUT); | 190 | setreturn(fp, FOUTPUT); |
181 | else | 191 | else |
@@ -1,4 +1,4 @@ | |||
1 | % $Id: manual.tex,v 1.19 1998/08/24 20:14:56 roberto Exp roberto $ | 1 | % $Id: manual.tex,v 1.20 1998/11/13 16:48:48 roberto Exp roberto $ |
2 | 2 | ||
3 | \documentclass[11pt]{article} | 3 | \documentclass[11pt]{article} |
4 | \usepackage{fullpage,bnf} | 4 | \usepackage{fullpage,bnf} |
@@ -41,7 +41,7 @@ Waldemar Celes | |||
41 | \tecgraf\ --- Computer Science Department --- PUC-Rio | 41 | \tecgraf\ --- Computer Science Department --- PUC-Rio |
42 | } | 42 | } |
43 | 43 | ||
44 | %\date{\small \verb$Date: 1998/08/24 20:14:56 $} | 44 | %\date{\small \verb$Date: 1998/11/13 16:48:48 $} |
45 | 45 | ||
46 | \maketitle | 46 | \maketitle |
47 | 47 | ||
@@ -2615,12 +2615,14 @@ Unless otherwise stated, | |||
2615 | all I/O functions return \nil\ on failure and | 2615 | all I/O functions return \nil\ on failure and |
2616 | some value different from \nil\ on success. | 2616 | some value different from \nil\ on success. |
2617 | 2617 | ||
2618 | \subsubsection*{\ff \T{readfrom (filename)}}\Deffunc{readfrom} | 2618 | \subsubsection*{\ff \T{readfrom (filename [, mode])}}\Deffunc{readfrom} |
2619 | 2619 | ||
2620 | This function may be called in two ways. | 2620 | This function may be called in two ways. |
2621 | When called with a file name, it opens the named file, | 2621 | When called with a file name, it opens the named file, |
2622 | sets its handle as the value of \verb|_INPUT|, | 2622 | sets its handle as the value of \verb|_INPUT|, |
2623 | and returns this value. | 2623 | and returns this value. |
2624 | An optional \verb|mode| argument with the string \verb|"binary"| | ||
2625 | opens file in binary mode (where this applies). | ||
2624 | It does not close the current input file. | 2626 | It does not close the current input file. |
2625 | When called without parameters, | 2627 | When called without parameters, |
2626 | it closes the \verb|_INPUT| file, | 2628 | it closes the \verb|_INPUT| file, |
@@ -2639,13 +2641,15 @@ the number of files that can be open at the same time is | |||
2639 | usually limited and depends on the system. | 2641 | usually limited and depends on the system. |
2640 | \end{quotation} | 2642 | \end{quotation} |
2641 | 2643 | ||
2642 | \subsubsection*{\ff \T{writeto (filename)}}\Deffunc{writeto} | 2644 | \subsubsection*{\ff \T{writeto (filename [, mode])}}\Deffunc{writeto} |
2643 | 2645 | ||
2644 | This function may be called in two ways. | 2646 | This function may be called in two ways. |
2645 | When called with a file name, | 2647 | When called with a file name, |
2646 | it opens the named file, | 2648 | it opens the named file, |
2647 | sets its handle as the value of \verb|_OUTPUT|, | 2649 | sets its handle as the value of \verb|_OUTPUT|, |
2648 | and returns this value. | 2650 | and returns this value. |
2651 | An optional \verb|mode| argument with the string \verb|"binary"| | ||
2652 | opens file in binary mode (where this applies). | ||
2649 | It does not close the current output file. | 2653 | It does not close the current output file. |
2650 | Note that, if the file already exists, | 2654 | Note that, if the file already exists, |
2651 | then it will be \emph{completely erased} with this operation. | 2655 | then it will be \emph{completely erased} with this operation. |
@@ -2667,10 +2671,12 @@ the number of files that can be open at the same time is | |||
2667 | usually limited and depends on the system. | 2671 | usually limited and depends on the system. |
2668 | \end{quotation} | 2672 | \end{quotation} |
2669 | 2673 | ||
2670 | \subsubsection*{\ff \T{appendto (filename)}}\Deffunc{appendto} | 2674 | \subsubsection*{\ff \T{appendto (filename [, mode])}}\Deffunc{appendto} |
2671 | 2675 | ||
2672 | Opens a file named \verb|filename| and sets it as the | 2676 | Opens a file named \verb|filename| and sets it as the |
2673 | value of \verb|_OUTPUT|. | 2677 | value of \verb|_OUTPUT|. |
2678 | An optional \verb|mode| argument with the string \verb|"binary"| | ||
2679 | opens file in binary mode (where this applies). | ||
2674 | Unlike the \verb|writeto| operation, | 2680 | Unlike the \verb|writeto| operation, |
2675 | this function does not erase any previous content of the file. | 2681 | this function does not erase any previous content of the file. |
2676 | If this function fails, it returns \nil, | 2682 | If this function fails, it returns \nil, |