3 ** Input/output library to LUA
5 ** Waldemar Celes Filho
15 #include <floatingpoint.h>
20 static FILE *in
=NULL
, *out
=NULL
;
23 ** Open a file to read.
25 ** status = readfrom (filename)
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 */
44 if (!lua_isstring (o
))
46 lua_error ("incorrect argument to function 'readfrom`");
51 FILE *fp
= fopen (lua_getstring(o
),"r");
58 if (in
!= stdin
) fclose (in
);
68 ** Open a file to write.
70 ** status = writeto (filename)
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 */
89 if (!lua_isstring (o
))
91 lua_error ("incorrect argument to function 'writeto`");
96 FILE *fp
= fopen (lua_getstring(o
),"w");
103 if (out
!= stdout
) fclose (out
);
113 ** Read a variable. On error put nil on stack.
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 */
133 while (isspace(c
=fgetc(in
)))
137 if (fscanf (in
, "%[^\"]\"", s
) != 1)
145 if (fscanf (in
, "%[^\']\'", s
) != 1)
156 if (fscanf (in
, "%s", s
) != 1)
161 d
= strtod (s
, &ptr
);
173 char *e
= lua_getstring(o
);
176 while (isspace(*e
)) e
++;
179 m
= m
*10 + (*e
++ - '0');
185 sprintf (f
, "%%%ds", m
);
192 sscanf (s
, "%ld", &l
);
196 case 'f': case 'g': case 'e':
199 sscanf (s
, "%f", &f
);
215 fscanf (in
, "%ld", &l
);
219 case 'f': case 'g': case 'e':
222 fscanf (in
, "%f", &f
);
229 fscanf (in
, "%s", s
);
240 ** Write a variable. On error put 0 on stack, otherwise put 1.
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:
255 ** ? -> indica justificacao
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];
269 char *string
= &buffer
[255];
272 while (isspace(*e
)) e
++;
274 if (*e
== '<' || *e
== '|' || *e
== '>') j
= *e
++;
276 m
= m
*10 + (*e
++ - '0');
277 e
++; /* skip point */
279 n
= n
*10 + (*e
++ - '0');
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
);
289 sprintf (string
, f
, (long int)lua_getnumber(o
));
291 case 'f': case 'g': case 'e': t
= 'f';
292 sprintf (string
, f
, (float)lua_getnumber(o
));
295 sprintf (string
, f
, lua_getstring(o
));
307 else if (m
!=0 && j
=='|')
310 while (isspace(string
[i
])) i
--;
313 while (string
[i
]==0) string
[i
++] = ' ';
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 */
327 else if (o2
== NULL
) /* free format */
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
);
338 if (!lua_isstring(o2
))
340 lua_error ("incorrect format to function `write'");
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`");
362 system(lua_getstring(o
));
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`");
382 if (remove(lua_getstring(o
)) == 0)
393 void iolib_open (void)
395 in
=stdin
; out
=stdout
;
396 lua_register ("readfrom", io_readfrom
);
397 lua_register ("writeto", io_writeto
);
398 lua_register ("read", io_read
);
399 lua_register ("write", io_write
);
400 lua_register ("execute", io_execute
);
401 lua_register ("remove", io_remove
);