diff options
author | Maurus Cuelenaere <mcuelenaere@gmail.com> | 2009-06-25 13:26:05 +0000 |
---|---|---|
committer | Maurus Cuelenaere <mcuelenaere@gmail.com> | 2009-06-25 13:26:05 +0000 |
commit | 3ff84e5e4ff402b550b7fa768e010a3586dded10 (patch) | |
tree | 34a6c480c7ce02063ebb22c1c1e73d41b0fb7073 /apps/plugins/lua/lauxlib.c | |
parent | 48f4512518c60456d02b3802d0bae41e6095ec21 (diff) | |
download | rockbox-3ff84e5e4ff402b550b7fa768e010a3586dded10.tar.gz rockbox-3ff84e5e4ff402b550b7fa768e010a3586dded10.zip |
Lua: add the package library
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21506 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins/lua/lauxlib.c')
-rw-r--r-- | apps/plugins/lua/lauxlib.c | 50 |
1 files changed, 1 insertions, 49 deletions
diff --git a/apps/plugins/lua/lauxlib.c b/apps/plugins/lua/lauxlib.c index 0c987cfa30..b8020b7475 100644 --- a/apps/plugins/lua/lauxlib.c +++ b/apps/plugins/lua/lauxlib.c | |||
@@ -560,62 +560,14 @@ static int errfile (lua_State *L, const char *what, int fnameindex) { | |||
560 | return LUA_ERRFILE; | 560 | return LUA_ERRFILE; |
561 | } | 561 | } |
562 | 562 | ||
563 | bool get_cur_path(lua_State *L, char* dest, size_t dest_size) | ||
564 | { | ||
565 | lua_Debug ar; | ||
566 | if(lua_getstack(L, 1, &ar)) | ||
567 | { | ||
568 | /* Try determining the base path of the current Lua chunk | ||
569 | and write it to dest. */ | ||
570 | lua_getinfo(L, "S", &ar); | ||
571 | |||
572 | char* curfile = (char*) &ar.source[1]; | ||
573 | char* pos = rb->strrchr(curfile, '/'); | ||
574 | if(pos != NULL) | ||
575 | { | ||
576 | unsigned int len = (unsigned int)(pos - curfile); | ||
577 | len = len + 1 > dest_size ? dest_size - 1 : len; | ||
578 | |||
579 | if(len > 0) | ||
580 | memcpy(dest, curfile, len); | ||
581 | |||
582 | dest[len] = '/'; | ||
583 | dest[len+1] = '\0'; | ||
584 | |||
585 | return true; | ||
586 | } | ||
587 | else | ||
588 | return false; | ||
589 | } | ||
590 | else | ||
591 | return false; | ||
592 | } | ||
593 | |||
594 | LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { | 563 | LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { |
595 | LoadF lf; | 564 | LoadF lf; |
596 | int status; | 565 | int status; |
597 | char buffer[MAX_PATH]; | ||
598 | int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ | 566 | int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ |
599 | lf.extraline = 0; | 567 | lf.extraline = 0; |
600 | lf.f = rb->open(filename, O_RDONLY); | 568 | lf.f = rb->open(filename, O_RDONLY); |
601 | lua_pushfstring(L, "@%s", filename); | 569 | lua_pushfstring(L, "@%s", filename); |
602 | if(lf.f < 0) { | 570 | if (lf.f < 0) return errfile(L, "open", fnameindex); |
603 | /* Fallback */ | ||
604 | |||
605 | if(get_cur_path(L, buffer, sizeof(buffer))) { | ||
606 | strncat(buffer, filename, sizeof(buffer) - strlen(buffer)); | ||
607 | lf.f = rb->open(buffer, O_RDONLY); | ||
608 | } | ||
609 | |||
610 | if(lf.f < 0) { | ||
611 | snprintf(buffer, sizeof(buffer), "%s/%s", VIEWERS_DIR, filename); | ||
612 | lf.f = rb->open(buffer, O_RDONLY); | ||
613 | |||
614 | if(lf.f < 0) | ||
615 | return errfile(L, "open", fnameindex); | ||
616 | } | ||
617 | } | ||
618 | |||
619 | status = lua_load(L, getF, &lf, lua_tostring(L, -1)); | 571 | status = lua_load(L, getF, &lf, lua_tostring(L, -1)); |
620 | rb->close(lf.f); | 572 | rb->close(lf.f); |
621 | lua_remove(L, fnameindex); | 573 | lua_remove(L, fnameindex); |