diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-02-05 19:32:19 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-02-05 19:32:19 -0200 |
commit | 5fa51fc4263522d8c5056d76aacbb77e52a3b1ea (patch) | |
tree | ac9796d0e9ccfb20b1d9e7e3a6400fefa28a1090 | |
parent | 15057aa0a42477a04d718270c9bc3303b4e9b613 (diff) | |
download | lua-5fa51fc4263522d8c5056d76aacbb77e52a3b1ea.tar.gz lua-5fa51fc4263522d8c5056d76aacbb77e52a3b1ea.tar.bz2 lua-5fa51fc4263522d8c5056d76aacbb77e52a3b1ea.zip |
new option "q" in function "write", to write literal strings.
-rw-r--r-- | iolib.c | 44 | ||||
-rw-r--r-- | manual.tex | 27 |
2 files changed, 54 insertions, 17 deletions
@@ -3,7 +3,7 @@ | |||
3 | ** Input/output library to LUA | 3 | ** Input/output library to LUA |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_iolib="$Id: iolib.c,v 1.31 1996/01/22 17:46:55 roberto Exp roberto $"; | 6 | char *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 | ||
373 | static 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 | |||
372 | static int write_float (int just, int m, int n) | 401 | static 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; |
@@ -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 | |||
32 | Departamento de Inform\'atica --- PUC-Rio | 32 | Departamento 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]'. | |||
1268 | If \verb'i' is absent, it is assumed to be 1. | 1268 | If \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} | ||
1271 | This function returns a formated version of its variable number of arguments | 1272 | This function returns a formated version of its variable number of arguments |
1272 | following the description given in its first argument (which must be a string). | 1273 | following the description given in its first argument (which must be a string). |
1273 | The format string follows the same rules as the \verb'printf' family of | 1274 | The 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. | ||
1406 | This format writes the string in a form suitable to be safely read | ||
1407 | back by the Lua interpreter. | ||
1408 | The string is written between double quotes, | ||
1409 | and all double quotes, returns and backslashes in the string | ||
1410 | are correctly escaped when written. | ||
1404 | \end{description} | 1411 | \end{description} |
1405 | These characters can be followed by | 1412 | These characters can be followed by |
1406 | \begin{verbatim} | 1413 | \begin{verbatim} |
@@ -1420,13 +1427,11 @@ For integers, it is the minimum number of digits. | |||
1420 | This option has no meaning for strings. | 1427 | This option has no meaning for strings. |
1421 | \end{description} | 1428 | \end{description} |
1422 | 1429 | ||
1423 | {\em Warning:} | ||
1424 | This format parameter is now obsolete; | ||
1425 | formated output should use the \verb'format' function. | ||
1426 | |||
1427 | When called without a format string, | 1430 | When called without a format string, |
1428 | this function writes numbers using the \verb'%g' format | 1431 | this function writes numbers using the \verb'%g' format |
1429 | and strings with \verb'%s'. | 1432 | and strings with \verb'%s'. |
1433 | For better format facilities, | ||
1434 | the 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, | |||
1571 | the following code is enough: | 1576 | the following code is enough: |
1572 | \begin{verbatim} | 1577 | \begin{verbatim} |
1573 | function store (name, value) | 1578 | function store (name, value) |
1574 | write('\n' .. name .. '=') | 1579 | write(format('\n%s =', name)) |
1575 | write_value(value) | 1580 | write_value(value) |
1576 | end | 1581 | end |
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 |
1585 | end | 1590 | end |
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 |
1603 | end | 1608 | end |