diff options
Diffstat (limited to 'apps/plugins/lua/rocklib.c')
-rw-r--r-- | apps/plugins/lua/rocklib.c | 131 |
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 | ||
301 | RB_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 | |||
310 | RB_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 | |||
318 | RB_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 | |||
347 | RB_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 | |||
357 | RB_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 | |||
365 | RB_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 | |||
375 | RB_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 | |||
383 | RB_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 | |||
392 | RB_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 | |||
401 | RB_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} |
302 | static const luaL_Reg rocklib[] = | 410 | static 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 | ||