Browse Source

new option "q" in function "write", to write literal strings.

Roberto Ierusalimschy 29 years ago
parent
commit
5fa51fc426
2 changed files with 54 additions and 17 deletions
  1. 38 6
      iolib.c
  2. 16 11
      manual.tex

+ 38 - 6
iolib.c

@@ -3,7 +3,7 @@
 ** Input/output library to LUA
 */
 
-char *rcs_iolib="$Id: iolib.c,v 1.31 1996/01/22 17:46:55 roberto Exp roberto $";
+char *rcs_iolib="$Id: iolib.c,v 1.32 1996/01/29 16:40:09 roberto Exp roberto $";
 
 #include <stdio.h>
 #include <ctype.h>
@@ -142,15 +142,14 @@ static char getformat (char *f, int *just, int *m, int *n)
   int t;
   switch (*f++)
   {
+    case 'q': case 'Q':
     case 's': case 'S':
-      t = 's';
+    case 'i': case 'I':
+      t = tolower(*(f-1));
       break;
     case 'f': case 'F': case 'g': case 'G': case 'e': case 'E':
       t = 'f';
       break;
-    case 'i': case 'I':
-      t = 'i';
-      break;
     default:
       t = 0;  /* to avoid compiler warnings */
       lua_arg_error("read/write (format)");
@@ -293,6 +292,8 @@ static void io_read (void)
           lua_pushnil();
         break;
       }
+      default:
+        lua_arg_error("read (format)");
     }
   }
 }
@@ -369,6 +370,34 @@ static int write_string (char *s, int just, int m)
   return status;
 }
 
+static int write_quoted (int just, int m)
+{
+  char *s = lua_check_string(1, "write");
+  luaI_addchar(0);
+  luaI_addchar('"');
+  while (1)
+  {
+    switch (*s)
+    {
+      case '"':  case '\\':
+        luaI_addchar('\\');
+        luaI_addchar(*s);
+        break;
+      case '\n':
+        luaI_addchar('\\');
+        luaI_addchar('n');
+        break;
+      case 0:
+        goto END_WHILE;
+      default:
+        luaI_addchar(*s);
+    }
+    s++;
+  } END_WHILE:
+  luaI_addchar('"');
+  return write_string(luaI_addchar(0), just, m);
+}
+
 static int write_float (int just, int m, int n)
 {
   char buffer[100];
@@ -413,7 +442,7 @@ static void io_write (void)
   else					/* formated */
   {
     int just, m, n;
-    switch (getformat (lua_check_string(2, "write"), &just, &m, &n))
+    switch (getformat(lua_check_string(2, "write"), &just, &m, &n))
     {
       case 's':
       {
@@ -424,6 +453,9 @@ static void io_write (void)
           status = 0;
         break;
       }
+      case 'q':
+        status = write_quoted(just, m);
+        break;
       case 'f':
         status = write_float(just, m, n);
         break;

+ 16 - 11
manual.tex

@@ -1,4 +1,4 @@
-% $Id: manual.tex,v 1.4 1996/01/30 15:24:49 roberto Exp roberto $
+% $Id: manual.tex,v 1.5 1996/02/05 14:52:47 roberto Exp roberto $
 
 \documentstyle[A4,11pt,bnf]{article}
 
@@ -11,7 +11,7 @@
 \newcommand{\Index}[1]{#1\index{#1}}
 \newcommand{\IndexVerb}[1]{{\tt #1}\index{#1}}
 \newcommand{\Def}[1]{{\em #1}\index{#1}}
-\newcommand{\Deffunc}[1]{\index{{\tt #1}}}
+\newcommand{\Deffunc}[1]{\index{#1}}
 
 %\makeindex
 
@@ -32,7 +32,7 @@ Waldemar Celes Filho
 Departamento de Inform\'atica --- PUC-Rio
 }
 
-\date{\small \verb$Date: 1996/01/30 15:24:49 $}
+\date{\small \verb$Date: 1996/02/05 14:52:47 $}
 
 \maketitle
 
@@ -1268,6 +1268,7 @@ Returns the ascii code of the character \verb's[i]'.
 If \verb'i' is absent, it is assumed to be 1.
 
 \subsubsection*{{\tt format (formatstring, e1, e2, \ldots)}}\Deffunc{format}
+\label{format}
 This function returns a formated version of its variable number of arguments
 following the description given in its first argument (which must be a string). 
 The format string follows the same rules as the \verb'printf' family of
@@ -1400,7 +1401,13 @@ following characters:
 \begin{description}
 \item['s' or 'S'] to write strings;
 \item['f' or 'F'] to write floats;
-\item['i' or 'I'] to write integers.
+\item['i' or 'I'] to write integers;
+\item['q' or 'Q'] to write quoted strings.
+This format writes the string in a form suitable to be safely read
+back by the Lua interpreter.
+The string is written between double quotes,
+and all double quotes, returns and backslashes in the string
+are correctly escaped when written.
 \end{description}
 These characters can be followed by
 \begin{verbatim}
@@ -1420,13 +1427,11 @@ For integers, it is the minimum number of digits.
 This option has no meaning for strings.
 \end{description}
 
-{\em Warning:}
-This format parameter is now obsolete;
-formated output should use the \verb'format' function.
-
 When called without a format string,
 this function writes numbers using the \verb'%g' format
 and strings with \verb'%s'.
+For better format facilities,
+the function \verb'format' should be used (\see{format}).
 
 \subsubsection*{{\tt date ()}}\Deffunc{date}
 
@@ -1571,7 +1576,7 @@ To store a single value with a name,
 the following code is enough:
 \begin{verbatim}
 function store (name, value)
-  write('\n' .. name .. '=')
+  write(format('\n%s =', name))
   write_value(value)
 end
 \end{verbatim}
@@ -1580,7 +1585,7 @@ function write_value (value)
   local t = type(value)
       if t == 'nil'    then write('nil')
   elseif t == 'number' then write(value)
-  elseif t == 'string' then write('[[' .. value .. ']]')
+  elseif t == 'string' then write(value, 'q')
   end
 end
 \end{verbatim}
@@ -1597,7 +1602,7 @@ function write_value (value)
   local t = type(value)
       if t == 'nil'    then write('nil')
   elseif t == 'number' then write(value)
-  elseif t == 'string' then write('"' .. value .. '"')
+  elseif t == 'string' then write(value, 'q')
   elseif t == 'table'  then write_record(value)
   end
 end