summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/rocklib.c
diff options
context:
space:
mode:
authorMaurus Cuelenaere <mcuelenaere@gmail.com>2009-05-22 00:06:45 +0000
committerMaurus Cuelenaere <mcuelenaere@gmail.com>2009-05-22 00:06:45 +0000
commit2cd4a94bdc4977e1ff1f26d1e911c1d826ef2f84 (patch)
tree6c8bbc9f76d9185d9dd8cf8b96b8b48456766558 /apps/plugins/lua/rocklib.c
parentbcfba0805547f5aa7ea7a48f4740b909a2284193 (diff)
downloadrockbox-2cd4a94bdc4977e1ff1f26d1e911c1d826ef2f84.tar.gz
rockbox-2cd4a94bdc4977e1ff1f26d1e911c1d826ef2f84.zip
Lua: add file handling
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21024 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins/lua/rocklib.c')
-rw-r--r--apps/plugins/lua/rocklib.c131
1 files changed, 131 insertions, 0 deletions
diff --git a/apps/plugins/lua/rocklib.c b/apps/plugins/lua/rocklib.c
index 8eaa13348e..1c6469641e 100644
--- a/apps/plugins/lua/rocklib.c
+++ b/apps/plugins/lua/rocklib.c
@@ -298,6 +298,114 @@ RB_WRAP(backlight_set_brightness)
298} 298}
299#endif 299#endif
300 300
301RB_WRAP(open)
302{
303 const char* pathname = luaL_checkstring(L, 1);
304 int flags = luaL_checkint(L, 2);
305 int result = rb->open(pathname, flags);
306 lua_pushinteger(L, result);
307 return 1;
308}
309
310RB_WRAP(close)
311{
312 int fd = luaL_checkint(L, 1);
313 int result = rb->close(fd);
314 lua_pushinteger(L, result);
315 return 1;
316}
317
318RB_WRAP(read)
319{
320 size_t len, n, result = 0;
321 luaL_Buffer b;
322
323 int fd = luaL_checkint(L, 1);
324 size_t count = luaL_checkint(L, 2);
325
326 luaL_buffinit(L, &b);
327 len = LUAL_BUFFERSIZE;
328 do
329 {
330 char *p = luaL_prepbuffer(&b);
331
332 if (len > count)
333 len = count;
334
335 n = rb->read(fd, p, len);
336
337 luaL_addsize(&b, n);
338 count -= n;
339 result += n;
340 } while (count > 0 && n == len);
341 luaL_pushresult(&b); /* close buffer */
342
343 lua_pushinteger(L, result);
344 return 2;
345}
346
347RB_WRAP(lseek)
348{
349 int fd = luaL_checkint(L, 1);
350 off_t offset = luaL_checkint(L, 2);
351 int whence = luaL_checkint(L, 3);
352 off_t result = rb->lseek(fd, offset, whence);
353 lua_pushinteger(L, result);
354 return 1;
355}
356
357RB_WRAP(creat)
358{
359 const char* pathname = luaL_checkstring(L, 1);
360 int result = rb->creat(pathname);
361 lua_pushinteger(L, result);
362 return 1;
363}
364
365RB_WRAP(write)
366{
367 size_t count;
368 int fd = luaL_checkint(L, 1);
369 void* buf = (void*)luaL_checklstring(L, 2, &count);
370 ssize_t result = rb->write(fd, buf, count);
371 lua_pushinteger(L, result);
372 return 1;
373}
374
375RB_WRAP(remove)
376{
377 const char* pathname = luaL_checkstring(L, 1);
378 int result = rb->remove(pathname);
379 lua_pushinteger(L, result);
380 return 1;
381}
382
383RB_WRAP(rename)
384{
385 const char* path = luaL_checkstring(L, 1);
386 const char* newname = luaL_checkstring(L, 2);
387 int result = rb->rename(path, newname);
388 lua_pushinteger(L, result);
389 return 1;
390}
391
392RB_WRAP(ftruncate)
393{
394 int fd = luaL_checkint(L, 1);
395 off_t length = luaL_checkint(L, 2);
396 int result = rb->ftruncate(fd, length);
397 lua_pushinteger(L, result);
398 return 1;
399}
400
401RB_WRAP(filesize)
402{
403 int fd = luaL_checkint(L, 1);
404 off_t result = rb->filesize(fd);
405 lua_pushinteger(L, result);
406 return 1;
407}
408
301#define R(NAME) {#NAME, rock_##NAME} 409#define R(NAME) {#NAME, rock_##NAME}
302static const luaL_Reg rocklib[] = 410static const luaL_Reg rocklib[] =
303{ 411{
@@ -321,6 +429,18 @@ static const luaL_Reg rocklib[] =
321 R(lcd_fillrect), 429 R(lcd_fillrect),
322#endif 430#endif
323 431
432 /* File handling */
433 R(open),
434 R(close),
435 R(read),
436 R(lseek),
437 R(creat),
438 R(write),
439 R(remove),
440 R(rename),
441 R(ftruncate),
442 R(filesize),
443
324 /* Kernel */ 444 /* Kernel */
325 R(sleep), 445 R(sleep),
326 R(yield), 446 R(yield),
@@ -361,9 +481,20 @@ LUALIB_API int luaopen_rock(lua_State *L)
361 luaL_register(L, LUA_ROCKLIBNAME, rocklib); 481 luaL_register(L, LUA_ROCKLIBNAME, rocklib);
362 482
363 RB_CONSTANT(HZ); 483 RB_CONSTANT(HZ);
484
364 RB_CONSTANT(LCD_WIDTH); 485 RB_CONSTANT(LCD_WIDTH);
365 RB_CONSTANT(LCD_HEIGHT); 486 RB_CONSTANT(LCD_HEIGHT);
366 487
488 RB_CONSTANT(O_RDONLY);
489 RB_CONSTANT(O_WRONLY);
490 RB_CONSTANT(O_RDWR);
491 RB_CONSTANT(O_CREAT);
492 RB_CONSTANT(O_APPEND);
493 RB_CONSTANT(O_TRUNC);
494 RB_CONSTANT(SEEK_SET);
495 RB_CONSTANT(SEEK_CUR);
496 RB_CONSTANT(SEEK_END);
497
367 return 1; 498 return 1;
368} 499}
369 500