aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-02-05 19:32:19 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-02-05 19:32:19 -0200
commit5fa51fc4263522d8c5056d76aacbb77e52a3b1ea (patch)
treeac9796d0e9ccfb20b1d9e7e3a6400fefa28a1090
parent15057aa0a42477a04d718270c9bc3303b4e9b613 (diff)
downloadlua-5fa51fc4263522d8c5056d76aacbb77e52a3b1ea.tar.gz
lua-5fa51fc4263522d8c5056d76aacbb77e52a3b1ea.tar.bz2
lua-5fa51fc4263522d8c5056d76aacbb77e52a3b1ea.zip
new option "q" in function "write", to write literal strings.
-rw-r--r--iolib.c44
-rw-r--r--manual.tex27
2 files changed, 54 insertions, 17 deletions
diff --git a/iolib.c b/iolib.c
index 51ad432a..d014ec4f 100644
--- a/iolib.c
+++ b/iolib.c
@@ -3,7 +3,7 @@
3** Input/output library to LUA 3** Input/output library to LUA
4*/ 4*/
5 5
6char *rcs_iolib="$Id: iolib.c,v 1.31 1996/01/22 17:46:55 roberto Exp roberto $"; 6char *rcs_iolib="$Id: iolib.c,v 1.32 1996/01/29 16:40:09 roberto Exp roberto $";
7 7
8#include <stdio.h> 8#include <stdio.h>
9#include <ctype.h> 9#include <ctype.h>
@@ -142,15 +142,14 @@ static char getformat (char *f, int *just, int *m, int *n)
142 int t; 142 int t;
143 switch (*f++) 143 switch (*f++)
144 { 144 {
145 case 'q': case 'Q':
145 case 's': case 'S': 146 case 's': case 'S':
146 t = 's'; 147 case 'i': case 'I':
148 t = tolower(*(f-1));
147 break; 149 break;
148 case 'f': case 'F': case 'g': case 'G': case 'e': case 'E': 150 case 'f': case 'F': case 'g': case 'G': case 'e': case 'E':
149 t = 'f'; 151 t = 'f';
150 break; 152 break;
151 case 'i': case 'I':
152 t = 'i';
153 break;
154 default: 153 default:
155 t = 0; /* to avoid compiler warnings */ 154 t = 0; /* to avoid compiler warnings */
156 lua_arg_error("read/write (format)"); 155 lua_arg_error("read/write (format)");
@@ -293,6 +292,8 @@ static void io_read (void)
293 lua_pushnil(); 292 lua_pushnil();
294 break; 293 break;
295 } 294 }
295 default:
296 lua_arg_error("read (format)");
296 } 297 }
297 } 298 }
298} 299}
@@ -369,6 +370,34 @@ static int write_string (char *s, int just, int m)
369 return status; 370 return status;
370} 371}
371 372
373static int write_quoted (int just, int m)
374{
375 char *s = lua_check_string(1, "write");
376 luaI_addchar(0);
377 luaI_addchar('"');
378 while (1)
379 {
380 switch (*s)
381 {
382 case '"': case '\\':
383 luaI_addchar('\\');
384 luaI_addchar(*s);
385 break;
386 case '\n':
387 luaI_addchar('\\');
388 luaI_addchar('n');
389 break;
390 case 0:
391 goto END_WHILE;
392 default:
393 luaI_addchar(*s);
394 }
395 s++;
396 } END_WHILE:
397 luaI_addchar('"');
398 return write_string(luaI_addchar(0), just, m);
399}
400
372static int write_float (int just, int m, int n) 401static int write_float (int just, int m, int n)
373{ 402{
374 char buffer[100]; 403 char buffer[100];
@@ -413,7 +442,7 @@ static void io_write (void)
413 else /* formated */ 442 else /* formated */
414 { 443 {
415 int just, m, n; 444 int just, m, n;
416 switch (getformat (lua_check_string(2, "write"), &just, &m, &n)) 445 switch (getformat(lua_check_string(2, "write"), &just, &m, &n))
417 { 446 {
418 case 's': 447 case 's':
419 { 448 {
@@ -424,6 +453,9 @@ static void io_write (void)
424 status = 0; 453 status = 0;
425 break; 454 break;
426 } 455 }
456 case 'q':
457 status = write_quoted(just, m);
458 break;
427 case 'f': 459 case 'f':
428 status = write_float(just, m, n); 460 status = write_float(just, m, n);
429 break; 461 break;
diff --git a/manual.tex b/manual.tex
index d3aa89dc..a3b9309e 100644
--- a/manual.tex
+++ b/manual.tex
@@ -1,4 +1,4 @@
1% $Id: manual.tex,v 1.4 1996/01/30 15:24:49 roberto Exp roberto $ 1% $Id: manual.tex,v 1.5 1996/02/05 14:52:47 roberto Exp roberto $
2 2
3\documentstyle[A4,11pt,bnf]{article} 3\documentstyle[A4,11pt,bnf]{article}
4 4
@@ -11,7 +11,7 @@
11\newcommand{\Index}[1]{#1\index{#1}} 11\newcommand{\Index}[1]{#1\index{#1}}
12\newcommand{\IndexVerb}[1]{{\tt #1}\index{#1}} 12\newcommand{\IndexVerb}[1]{{\tt #1}\index{#1}}
13\newcommand{\Def}[1]{{\em #1}\index{#1}} 13\newcommand{\Def}[1]{{\em #1}\index{#1}}
14\newcommand{\Deffunc}[1]{\index{{\tt #1}}} 14\newcommand{\Deffunc}[1]{\index{#1}}
15 15
16%\makeindex 16%\makeindex
17 17
@@ -32,7 +32,7 @@ Waldemar Celes Filho
32Departamento de Inform\'atica --- PUC-Rio 32Departamento de Inform\'atica --- PUC-Rio
33} 33}
34 34
35\date{\small \verb$Date: 1996/01/30 15:24:49 $} 35\date{\small \verb$Date: 1996/02/05 14:52:47 $}
36 36
37\maketitle 37\maketitle
38 38
@@ -1268,6 +1268,7 @@ Returns the ascii code of the character \verb's[i]'.
1268If \verb'i' is absent, it is assumed to be 1. 1268If \verb'i' is absent, it is assumed to be 1.
1269 1269
1270\subsubsection*{{\tt format (formatstring, e1, e2, \ldots)}}\Deffunc{format} 1270\subsubsection*{{\tt format (formatstring, e1, e2, \ldots)}}\Deffunc{format}
1271\label{format}
1271This function returns a formated version of its variable number of arguments 1272This function returns a formated version of its variable number of arguments
1272following the description given in its first argument (which must be a string). 1273following the description given in its first argument (which must be a string).
1273The format string follows the same rules as the \verb'printf' family of 1274The format string follows the same rules as the \verb'printf' family of
@@ -1400,7 +1401,13 @@ following characters:
1400\begin{description} 1401\begin{description}
1401\item['s' or 'S'] to write strings; 1402\item['s' or 'S'] to write strings;
1402\item['f' or 'F'] to write floats; 1403\item['f' or 'F'] to write floats;
1403\item['i' or 'I'] to write integers. 1404\item['i' or 'I'] to write integers;
1405\item['q' or 'Q'] to write quoted strings.
1406This format writes the string in a form suitable to be safely read
1407back by the Lua interpreter.
1408The string is written between double quotes,
1409and all double quotes, returns and backslashes in the string
1410are correctly escaped when written.
1404\end{description} 1411\end{description}
1405These characters can be followed by 1412These characters can be followed by
1406\begin{verbatim} 1413\begin{verbatim}
@@ -1420,13 +1427,11 @@ For integers, it is the minimum number of digits.
1420This option has no meaning for strings. 1427This option has no meaning for strings.
1421\end{description} 1428\end{description}
1422 1429
1423{\em Warning:}
1424This format parameter is now obsolete;
1425formated output should use the \verb'format' function.
1426
1427When called without a format string, 1430When called without a format string,
1428this function writes numbers using the \verb'%g' format 1431this function writes numbers using the \verb'%g' format
1429and strings with \verb'%s'. 1432and strings with \verb'%s'.
1433For better format facilities,
1434the function \verb'format' should be used (\see{format}).
1430 1435
1431\subsubsection*{{\tt date ()}}\Deffunc{date} 1436\subsubsection*{{\tt date ()}}\Deffunc{date}
1432 1437
@@ -1571,7 +1576,7 @@ To store a single value with a name,
1571the following code is enough: 1576the following code is enough:
1572\begin{verbatim} 1577\begin{verbatim}
1573function store (name, value) 1578function store (name, value)
1574 write('\n' .. name .. '=') 1579 write(format('\n%s =', name))
1575 write_value(value) 1580 write_value(value)
1576end 1581end
1577\end{verbatim} 1582\end{verbatim}
@@ -1580,7 +1585,7 @@ function write_value (value)
1580 local t = type(value) 1585 local t = type(value)
1581 if t == 'nil' then write('nil') 1586 if t == 'nil' then write('nil')
1582 elseif t == 'number' then write(value) 1587 elseif t == 'number' then write(value)
1583 elseif t == 'string' then write('[[' .. value .. ']]') 1588 elseif t == 'string' then write(value, 'q')
1584 end 1589 end
1585end 1590end
1586\end{verbatim} 1591\end{verbatim}
@@ -1597,7 +1602,7 @@ function write_value (value)
1597 local t = type(value) 1602 local t = type(value)
1598 if t == 'nil' then write('nil') 1603 if t == 'nil' then write('nil')
1599 elseif t == 'number' then write(value) 1604 elseif t == 'number' then write(value)
1600 elseif t == 'string' then write('"' .. value .. '"') 1605 elseif t == 'string' then write(value, 'q')
1601 elseif t == 'table' then write_record(value) 1606 elseif t == 'table' then write_record(value)
1602 end 1607 end
1603end 1608end