diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-09-11 14:38:42 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-09-11 14:38:42 -0300 |
commit | 787a78f83e0484c9e9698189982e2f309808fae8 (patch) | |
tree | 0682eddf4ea5a49bf5078bac937a36f90057df57 /lauxlib.c | |
parent | 70c8a310925d6c41c3ef4f7feeae604a4c9a3a95 (diff) | |
download | lua-787a78f83e0484c9e9698189982e2f309808fae8.tar.gz lua-787a78f83e0484c9e9698189982e2f309808fae8.tar.bz2 lua-787a78f83e0484c9e9698189982e2f309808fae8.zip |
new scheme for buffers
Diffstat (limited to 'lauxlib.c')
-rw-r--r-- | lauxlib.c | 97 |
1 files changed, 93 insertions, 4 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.32 2000/08/29 14:33:31 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.33 2000/08/29 20:43:28 roberto Exp roberto $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -140,8 +140,97 @@ void luaL_chunkid (char *out, const char *source, int len) { | |||
140 | } | 140 | } |
141 | 141 | ||
142 | 142 | ||
143 | void luaL_filesource (char *out, const char *filename, int len) { | 143 | /* |
144 | if (filename == NULL) filename = "(stdin)"; | 144 | ** {====================================================== |
145 | sprintf(out, "@%.*s", len-2, filename); /* -2 for '@' and '\0' */ | 145 | ** Generic Buffer manipulation |
146 | ** ======================================================= | ||
147 | */ | ||
148 | |||
149 | |||
150 | #define buffempty(B) ((B)->p == (B)->buffer) | ||
151 | #define bufflen(B) ((B)->p - (B)->buffer) | ||
152 | #define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B))) | ||
153 | |||
154 | #define LIMIT (LUA_MINSTACK/2) | ||
155 | |||
156 | |||
157 | static int emptybuffer (luaL_Buffer *B) { | ||
158 | size_t l = bufflen(B); | ||
159 | if (l == 0) return 0; /* put nothing on stack */ | ||
160 | else { | ||
161 | lua_pushlstring(B->L, B->buffer, l); | ||
162 | B->p = B->buffer; | ||
163 | B->level++; | ||
164 | return 1; | ||
165 | } | ||
166 | } | ||
167 | |||
168 | |||
169 | static void adjuststack (luaL_Buffer *B) { | ||
170 | if (B->level > 1) { | ||
171 | lua_State *L = B->L; | ||
172 | int toget = 1; /* number of levels to concat */ | ||
173 | size_t toplen = lua_strlen(L, -1); | ||
174 | do { | ||
175 | size_t l = lua_strlen(L, -(toget+1)); | ||
176 | if (B->level - toget + 1 >= LIMIT || toplen > l) { | ||
177 | toplen += l; | ||
178 | toget++; | ||
179 | } | ||
180 | else break; | ||
181 | } while (toget < B->level); | ||
182 | if (toget >= 2) { | ||
183 | lua_concat(L, toget); | ||
184 | B->level = B->level - toget + 1; | ||
185 | } | ||
186 | } | ||
187 | } | ||
188 | |||
189 | |||
190 | char *luaL_prepbuffer (luaL_Buffer *B) { | ||
191 | if (emptybuffer(B)) | ||
192 | adjuststack(B); | ||
193 | return B->buffer; | ||
194 | } | ||
195 | |||
196 | |||
197 | void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { | ||
198 | while (l--) | ||
199 | luaL_putchar(B, *s++); | ||
200 | } | ||
201 | |||
202 | |||
203 | void luaL_pushresult (luaL_Buffer *B) { | ||
204 | emptybuffer(B); | ||
205 | if (B->level == 0) | ||
206 | lua_pushlstring(B->L, NULL, 0); | ||
207 | else if (B->level > 1) | ||
208 | lua_concat(B->L, B->level); | ||
209 | B->level = 1; | ||
210 | } | ||
211 | |||
212 | |||
213 | void luaL_addvalue (luaL_Buffer *B) { | ||
214 | lua_State *L = B->L; | ||
215 | size_t vl = lua_strlen(L, -1); | ||
216 | if (vl <= bufffree(B)) { /* fit into buffer? */ | ||
217 | memcpy(B->p, lua_tostring(L, -1), vl); /* put it there */ | ||
218 | B->p += vl; | ||
219 | lua_pop(L, 1); /* remove from stack */ | ||
220 | } | ||
221 | else { | ||
222 | if (emptybuffer(B)) | ||
223 | lua_insert(L, -2); /* put buffer before new value */ | ||
224 | B->level++; /* add new value into B stack */ | ||
225 | adjuststack(B); | ||
226 | } | ||
227 | } | ||
228 | |||
229 | |||
230 | void luaL_buffinit (lua_State *L, luaL_Buffer *B) { | ||
231 | B->L = L; | ||
232 | B->p = B->buffer; | ||
233 | B->level = 0; | ||
146 | } | 234 | } |
147 | 235 | ||
236 | /* }====================================================== */ | ||