Browse Source

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

binary files).
v5-2
Roberto Ierusalimschy 26 years ago
parent
commit
e4830ddce3
  1. 18
      liolib.c
  2. 16
      manual.tex

18
liolib.c

@ -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
** See Copyright Notice in lua.h
*/
@ -62,6 +62,7 @@ static void pushresult (int i)
else {
lua_pushnil();
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)
{
FILE *f = getfilebyname(name);
@ -140,7 +150,7 @@ static void io_readfrom (void)
current = lua_getuserdata(f);
else {
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) {
pushresult(0);
return;
@ -162,7 +172,7 @@ static void io_writeto (void)
current = lua_getuserdata(f);
else {
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) {
pushresult(0);
return;
@ -175,7 +185,7 @@ static void io_writeto (void)
static void io_appendto (void)
{
char *s = luaL_check_string(FIRSTARG);
FILE *fp = fopen (s, "a");
FILE *fp = fopen (s, getmode('a'));
if (fp != NULL)
setreturn(fp, FOUTPUT);
else

16
manual.tex

@ -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}
\usepackage{fullpage,bnf}
@ -41,7 +41,7 @@ Waldemar Celes
\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
@ -2615,12 +2615,14 @@ Unless otherwise stated,
all I/O functions return \nil\ on failure and
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.
When called with a file name, it opens the named file,
sets its handle as the value of \verb|_INPUT|,
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.
When called without parameters,
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.
\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.
When called with a file name,
it opens the named file,
sets its handle as the value of \verb|_OUTPUT|,
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.
Note that, if the file already exists,
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.
\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
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,
this function does not erase any previous content of the file.
If this function fails, it returns \nil,

Loading…
Cancel
Save