Imported from ../lua-1.0.tar.gz.
[lua.git] / iolib.c
blob174dd5018e76cd98f16868a07d9ede4739078d52
1 /*
2 ** iolib.c
3 ** Input/output library to LUA
4 **
5 ** Waldemar Celes Filho
6 ** TeCGraf - PUC-Rio
7 ** 19 May 93
8 */
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <ctype.h>
14 #ifdef __GNUC__
15 #include <floatingpoint.h>
16 #endif
18 #include "lua.h"
20 static FILE *in=stdin, *out=stdout;
23 ** Open a file to read.
24 ** LUA interface:
25 ** status = readfrom (filename)
26 ** where:
27 ** status = 1 -> success
28 ** status = 0 -> error
30 static void io_readfrom (void)
32 lua_Object o = lua_getparam (1);
33 if (o == NULL) /* restore standart input */
35 if (in != stdin)
37 fclose (in);
38 in = stdin;
40 lua_pushnumber (1);
42 else
44 if (!lua_isstring (o))
46 lua_error ("incorrect argument to function 'readfrom`");
47 lua_pushnumber (0);
49 else
51 FILE *fp = fopen (lua_getstring(o),"r");
52 if (fp == NULL)
54 lua_pushnumber (0);
56 else
58 if (in != stdin) fclose (in);
59 in = fp;
60 lua_pushnumber (1);
68 ** Open a file to write.
69 ** LUA interface:
70 ** status = writeto (filename)
71 ** where:
72 ** status = 1 -> success
73 ** status = 0 -> error
75 static void io_writeto (void)
77 lua_Object o = lua_getparam (1);
78 if (o == NULL) /* restore standart output */
80 if (out != stdout)
82 fclose (out);
83 out = stdout;
85 lua_pushnumber (1);
87 else
89 if (!lua_isstring (o))
91 lua_error ("incorrect argument to function 'writeto`");
92 lua_pushnumber (0);
94 else
96 FILE *fp = fopen (lua_getstring(o),"w");
97 if (fp == NULL)
99 lua_pushnumber (0);
101 else
103 if (out != stdout) fclose (out);
104 out = fp;
105 lua_pushnumber (1);
113 ** Read a variable. On error put nil on stack.
114 ** LUA interface:
115 ** variable = read ([format])
117 ** O formato pode ter um dos seguintes especificadores:
119 ** s ou S -> para string
120 ** f ou F, g ou G, e ou E -> para reais
121 ** i ou I -> para inteiros
123 ** Estes especificadores podem vir seguidos de numero que representa
124 ** o numero de campos a serem lidos.
126 static void io_read (void)
128 lua_Object o = lua_getparam (1);
129 if (o == NULL) /* free format */
131 int c;
132 char s[256];
133 while (isspace(c=fgetc(in)))
135 if (c == '\"')
137 if (fscanf (in, "%[^\"]\"", s) != 1)
139 lua_pushnil ();
140 return;
143 else if (c == '\'')
145 if (fscanf (in, "%[^\']\'", s) != 1)
147 lua_pushnil ();
148 return;
151 else
153 char *ptr;
154 double d;
155 ungetc (c, in);
156 if (fscanf (in, "%s", s) != 1)
158 lua_pushnil ();
159 return;
161 d = strtod (s, &ptr);
162 if (!(*ptr))
164 lua_pushnumber (d);
165 return;
168 lua_pushstring (s);
169 return;
171 else /* formatted */
173 char *e = lua_getstring(o);
174 char t;
175 int m=0;
176 while (isspace(*e)) e++;
177 t = *e++;
178 while (isdigit(*e))
179 m = m*10 + (*e++ - '0');
181 if (m > 0)
183 char f[80];
184 char s[256];
185 sprintf (f, "%%%ds", m);
186 fscanf (in, f, s);
187 switch (tolower(t))
189 case 'i':
191 long int l;
192 sscanf (s, "%ld", &l);
193 lua_pushnumber(l);
195 break;
196 case 'f': case 'g': case 'e':
198 float f;
199 sscanf (s, "%f", &f);
200 lua_pushnumber(f);
202 break;
203 default:
204 lua_pushstring(s);
205 break;
208 else
210 switch (tolower(t))
212 case 'i':
214 long int l;
215 fscanf (in, "%ld", &l);
216 lua_pushnumber(l);
218 break;
219 case 'f': case 'g': case 'e':
221 float f;
222 fscanf (in, "%f", &f);
223 lua_pushnumber(f);
225 break;
226 default:
228 char s[256];
229 fscanf (in, "%s", s);
230 lua_pushstring(s);
232 break;
240 ** Write a variable. On error put 0 on stack, otherwise put 1.
241 ** LUA interface:
242 ** status = write (variable [,format])
244 ** O formato pode ter um dos seguintes especificadores:
246 ** s ou S -> para string
247 ** f ou F, g ou G, e ou E -> para reais
248 ** i ou I -> para inteiros
250 ** Estes especificadores podem vir seguidos de:
252 ** [?][m][.n]
254 ** onde:
255 ** ? -> indica justificacao
256 ** < = esquerda
257 ** | = centro
258 ** > = direita (default)
259 ** m -> numero maximo de campos (se exceder estoura)
260 ** n -> indica precisao para
261 ** reais -> numero de casas decimais
262 ** inteiros -> numero minimo de digitos
263 ** string -> nao se aplica
265 static char *buildformat (char *e, lua_Object o)
267 static char buffer[512];
268 static char f[80];
269 char *string = &buffer[255];
270 char t, j='r';
271 int m=0, n=0, l;
272 while (isspace(*e)) e++;
273 t = *e++;
274 if (*e == '<' || *e == '|' || *e == '>') j = *e++;
275 while (isdigit(*e))
276 m = m*10 + (*e++ - '0');
277 e++; /* skip point */
278 while (isdigit(*e))
279 n = n*10 + (*e++ - '0');
281 sprintf(f,"%%");
282 if (j == '<' || j == '|') sprintf(strchr(f,0),"-");
283 if (m != 0) sprintf(strchr(f,0),"%d", m);
284 if (n != 0) sprintf(strchr(f,0),".%d", n);
285 sprintf(strchr(f,0), "%c", t);
286 switch (tolower(t))
288 case 'i': t = 'i';
289 sprintf (string, f, (long int)lua_getnumber(o));
290 break;
291 case 'f': case 'g': case 'e': t = 'f';
292 sprintf (string, f, (float)lua_getnumber(o));
293 break;
294 case 's': t = 's';
295 sprintf (string, f, lua_getstring(o));
296 break;
297 default: return "";
299 l = strlen(string);
300 if (m!=0 && l>m)
302 int i;
303 for (i=0; i<m; i++)
304 string[i] = '*';
305 string[i] = 0;
307 else if (m!=0 && j=='|')
309 int i=l-1;
310 while (isspace(string[i])) i--;
311 string -= (m-i) / 2;
312 i=0;
313 while (string[i]==0) string[i++] = ' ';
314 string[l] = 0;
316 return string;
318 static void io_write (void)
320 lua_Object o1 = lua_getparam (1);
321 lua_Object o2 = lua_getparam (2);
322 if (o1 == NULL) /* new line */
324 fprintf (out, "\n");
325 lua_pushnumber(1);
327 else if (o2 == NULL) /* free format */
329 int status=0;
330 if (lua_isnumber(o1))
331 status = fprintf (out, "%g", lua_getnumber(o1));
332 else if (lua_isstring(o1))
333 status = fprintf (out, "%s", lua_getstring(o1));
334 lua_pushnumber(status);
336 else /* formated */
338 if (!lua_isstring(o2))
340 lua_error ("incorrect format to function `write'");
341 lua_pushnumber(0);
342 return;
344 lua_pushnumber(fprintf (out, "%s", buildformat(lua_getstring(o2),o1)));
349 ** Execute a executable program using "sustem".
350 ** On error put 0 on stack, otherwise put 1.
352 void io_execute (void)
354 lua_Object o = lua_getparam (1);
355 if (o == NULL || !lua_isstring (o))
357 lua_error ("incorrect argument to function 'execute`");
358 lua_pushnumber (0);
360 else
362 system(lua_getstring(o));
363 lua_pushnumber (1);
365 return;
369 ** Remove a file.
370 ** On error put 0 on stack, otherwise put 1.
372 void io_remove (void)
374 lua_Object o = lua_getparam (1);
375 if (o == NULL || !lua_isstring (o))
377 lua_error ("incorrect argument to function 'execute`");
378 lua_pushnumber (0);
380 else
382 if (remove(lua_getstring(o)) == 0)
383 lua_pushnumber (1);
384 else
385 lua_pushnumber (0);
387 return;
391 ** Open io library
393 void iolib_open (void)
395 lua_register ("readfrom", io_readfrom);
396 lua_register ("writeto", io_writeto);
397 lua_register ("read", io_read);
398 lua_register ("write", io_write);
399 lua_register ("execute", io_execute);
400 lua_register ("remove", io_remove);