aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWaldemar Celes <celes@tecgraf.puc-rio.br>1993-12-17 16:41:19 -0200
committerWaldemar Celes <celes@tecgraf.puc-rio.br>1993-12-17 16:41:19 -0200
commit1923c7d620ba392ee2ca7ba9dc4b2df7839d0050 (patch)
tree2838f0f35f77b856badd5211219701a2f1f6c171
parentb405fb0ad740cc4ec21923989be220c64301569f (diff)
downloadlua-1923c7d620ba392ee2ca7ba9dc4b2df7839d0050.tar.gz
lua-1923c7d620ba392ee2ca7ba9dc4b2df7839d0050.tar.bz2
lua-1923c7d620ba392ee2ca7ba9dc4b2df7839d0050.zip
Input/output library to LUA
-rw-r--r--iolib.c71
1 files changed, 66 insertions, 5 deletions
diff --git a/iolib.c b/iolib.c
index 174dd501..9bc68ecb 100644
--- a/iolib.c
+++ b/iolib.c
@@ -1,16 +1,15 @@
1/* 1/*
2** iolib.c 2** iolib.c
3** Input/output library to LUA 3** Input/output library to LUA
4**
5** Waldemar Celes Filho
6** TeCGraf - PUC-Rio
7** 19 May 93
8*/ 4*/
9 5
6char *rcs_iolib="$Id: $";
7
10#include <stdlib.h> 8#include <stdlib.h>
11#include <string.h> 9#include <string.h>
12#include <stdio.h> 10#include <stdio.h>
13#include <ctype.h> 11#include <ctype.h>
12#include <sys/stat.h>
14#ifdef __GNUC__ 13#ifdef __GNUC__
15#include <floatingpoint.h> 14#include <floatingpoint.h>
16#endif 15#endif
@@ -110,6 +109,58 @@ static void io_writeto (void)
110 109
111 110
112/* 111/*
112** Open a file to write appended.
113** LUA interface:
114** status = appendto (filename)
115** where:
116** status = 2 -> success (already exist)
117** status = 1 -> success (new file)
118** status = 0 -> error
119*/
120static void io_appendto (void)
121{
122 lua_Object o = lua_getparam (1);
123 if (o == NULL) /* restore standart output */
124 {
125 if (out != stdout)
126 {
127 fclose (out);
128 out = stdout;
129 }
130 lua_pushnumber (1);
131 }
132 else
133 {
134 if (!lua_isstring (o))
135 {
136 lua_error ("incorrect argument to function 'appendto`");
137 lua_pushnumber (0);
138 }
139 else
140 {
141 int r;
142 FILE *fp;
143 struct stat st;
144 if (stat(lua_getstring(o), &st) == -1) r = 1;
145 else r = 2;
146 fp = fopen (lua_getstring(o),"a");
147 if (fp == NULL)
148 {
149 lua_pushnumber (0);
150 }
151 else
152 {
153 if (out != stdout) fclose (out);
154 out = fp;
155 lua_pushnumber (r);
156 }
157 }
158 }
159}
160
161
162
163/*
113** Read a variable. On error put nil on stack. 164** Read a variable. On error put nil on stack.
114** LUA interface: 165** LUA interface:
115** variable = read ([format]) 166** variable = read ([format])
@@ -183,7 +234,16 @@ static void io_read (void)
183 char f[80]; 234 char f[80];
184 char s[256]; 235 char s[256];
185 sprintf (f, "%%%ds", m); 236 sprintf (f, "%%%ds", m);
186 fscanf (in, f, s); 237 if (fgets (s, m, in) == NULL)
238 {
239 lua_pushnil();
240 return;
241 }
242 else
243 {
244 if (s[strlen(s)-1] == '\n')
245 s[strlen(s)-1] = 0;
246 }
187 switch (tolower(t)) 247 switch (tolower(t))
188 { 248 {
189 case 'i': 249 case 'i':
@@ -394,6 +454,7 @@ void iolib_open (void)
394{ 454{
395 lua_register ("readfrom", io_readfrom); 455 lua_register ("readfrom", io_readfrom);
396 lua_register ("writeto", io_writeto); 456 lua_register ("writeto", io_writeto);
457 lua_register ("appendto", io_appendto);
397 lua_register ("read", io_read); 458 lua_register ("read", io_read);
398 lua_register ("write", io_write); 459 lua_register ("write", io_write);
399 lua_register ("execute", io_execute); 460 lua_register ("execute", io_execute);