new option "mode" in "readfrom", "writeto" and "appendto" (for

binary files).
This commit is contained in:
Roberto Ierusalimschy 1998-11-20 13:41:43 -02:00
parent 758e330d6e
commit e4830ddce3
2 changed files with 25 additions and 9 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: liolib.c,v 1.24 1998/08/30 20:25:24 roberto Exp roberto $ ** $Id: liolib.c,v 1.25 1998/09/07 18:59:59 roberto Exp roberto $
** Standard I/O (and system) library ** Standard I/O (and system) library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -62,6 +62,7 @@ static void pushresult (int i)
else { else {
lua_pushnil(); lua_pushnil();
lua_pushstring(strerror(errno)); lua_pushstring(strerror(errno));
lua_pushnumber(errno);
} }
} }
@ -102,6 +103,15 @@ static FILE *getfileparam (char *name, int *arg) {
} }
static char *getmode (char mode) {
static char m[3];
m[0] = mode;
m[1] = (*luaL_opt_string(FIRSTARG+1, "text") == 'b') ? 'b' : '\0';
m[2] = '\0';
return m;
}
static void closefile (char *name) static void closefile (char *name)
{ {
FILE *f = getfilebyname(name); FILE *f = getfilebyname(name);
@ -140,7 +150,7 @@ static void io_readfrom (void)
current = lua_getuserdata(f); current = lua_getuserdata(f);
else { else {
char *s = luaL_check_string(FIRSTARG); char *s = luaL_check_string(FIRSTARG);
current = (*s == '|') ? popen(s+1, "r") : fopen(s, "r"); current = (*s == '|') ? popen(s+1, "r") : fopen(s, getmode('r'));
if (current == NULL) { if (current == NULL) {
pushresult(0); pushresult(0);
return; return;
@ -162,7 +172,7 @@ static void io_writeto (void)
current = lua_getuserdata(f); current = lua_getuserdata(f);
else { else {
char *s = luaL_check_string(FIRSTARG); char *s = luaL_check_string(FIRSTARG);
current = (*s == '|') ? popen(s+1,"w") : fopen(s,"w"); current = (*s == '|') ? popen(s+1,"w") : fopen(s,getmode('w'));
if (current == NULL) { if (current == NULL) {
pushresult(0); pushresult(0);
return; return;
@ -175,7 +185,7 @@ static void io_writeto (void)
static void io_appendto (void) static void io_appendto (void)
{ {
char *s = luaL_check_string(FIRSTARG); char *s = luaL_check_string(FIRSTARG);
FILE *fp = fopen (s, "a"); FILE *fp = fopen (s, getmode('a'));
if (fp != NULL) if (fp != NULL)
setreturn(fp, FOUTPUT); setreturn(fp, FOUTPUT);
else else

View File

@ -1,4 +1,4 @@
% $Id: manual.tex,v 1.19 1998/08/24 20:14:56 roberto Exp roberto $ % $Id: manual.tex,v 1.20 1998/11/13 16:48:48 roberto Exp roberto $
\documentclass[11pt]{article} \documentclass[11pt]{article}
\usepackage{fullpage,bnf} \usepackage{fullpage,bnf}
@ -41,7 +41,7 @@ Waldemar Celes
\tecgraf\ --- Computer Science Department --- PUC-Rio \tecgraf\ --- Computer Science Department --- PUC-Rio
} }
%\date{\small \verb$Date: 1998/08/24 20:14:56 $} %\date{\small \verb$Date: 1998/11/13 16:48:48 $}
\maketitle \maketitle
@ -2615,12 +2615,14 @@ Unless otherwise stated,
all I/O functions return \nil\ on failure and all I/O functions return \nil\ on failure and
some value different from \nil\ on success. some value different from \nil\ on success.
\subsubsection*{\ff \T{readfrom (filename)}}\Deffunc{readfrom} \subsubsection*{\ff \T{readfrom (filename [, mode])}}\Deffunc{readfrom}
This function may be called in two ways. This function may be called in two ways.
When called with a file name, it opens the named file, When called with a file name, it opens the named file,
sets its handle as the value of \verb|_INPUT|, sets its handle as the value of \verb|_INPUT|,
and returns this value. and returns this value.
An optional \verb|mode| argument with the string \verb|"binary"|
opens file in binary mode (where this applies).
It does not close the current input file. It does not close the current input file.
When called without parameters, When called without parameters,
it closes the \verb|_INPUT| file, it closes the \verb|_INPUT| file,
@ -2639,13 +2641,15 @@ the number of files that can be open at the same time is
usually limited and depends on the system. usually limited and depends on the system.
\end{quotation} \end{quotation}
\subsubsection*{\ff \T{writeto (filename)}}\Deffunc{writeto} \subsubsection*{\ff \T{writeto (filename [, mode])}}\Deffunc{writeto}
This function may be called in two ways. This function may be called in two ways.
When called with a file name, When called with a file name,
it opens the named file, it opens the named file,
sets its handle as the value of \verb|_OUTPUT|, sets its handle as the value of \verb|_OUTPUT|,
and returns this value. and returns this value.
An optional \verb|mode| argument with the string \verb|"binary"|
opens file in binary mode (where this applies).
It does not close the current output file. It does not close the current output file.
Note that, if the file already exists, Note that, if the file already exists,
then it will be \emph{completely erased} with this operation. 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
usually limited and depends on the system. usually limited and depends on the system.
\end{quotation} \end{quotation}
\subsubsection*{\ff \T{appendto (filename)}}\Deffunc{appendto} \subsubsection*{\ff \T{appendto (filename [, mode])}}\Deffunc{appendto}
Opens a file named \verb|filename| and sets it as the Opens a file named \verb|filename| and sets it as the
value of \verb|_OUTPUT|. value of \verb|_OUTPUT|.
An optional \verb|mode| argument with the string \verb|"binary"|
opens file in binary mode (where this applies).
Unlike the \verb|writeto| operation, Unlike the \verb|writeto| operation,
this function does not erase any previous content of the file. this function does not erase any previous content of the file.
If this function fails, it returns \nil, If this function fails, it returns \nil,