summaryrefslogtreecommitdiff
path: root/iolib.c
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 /iolib.c
parent15057aa0a42477a04d718270c9bc3303b4e9b613 (diff)
downloadlua-5fa51fc4263522d8c5056d76aacbb77e52a3b1ea.tar.gz
lua-5fa51fc4263522d8c5056d76aacbb77e52a3b1ea.tar.bz2
lua-5fa51fc4263522d8c5056d76aacbb77e52a3b1ea.zip
new option "q" in function "write", to write literal strings.
Diffstat (limited to 'iolib.c')
-rw-r--r--iolib.c44
1 files changed, 38 insertions, 6 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;