summaryrefslogtreecommitdiff
path: root/apps/plugins
diff options
context:
space:
mode:
authorWilliam Wilgus <me.theuser@yahoo.com>2018-10-13 13:35:01 -0400
committerWilliam Wilgus <me.theuser@yahoo.com>2018-10-15 18:24:10 +0200
commit07fed9053a476b4c6e5ecd23fc43ec84f4094978 (patch)
treeda366faad51087b2e5000a1d9d3659d5bd1a73a8 /apps/plugins
parent0b7a8d5afd3d751fd0f6454098bc9fd1d05ee764 (diff)
downloadrockbox-07fed9053a476b4c6e5ecd23fc43ec84f4094978.tar.gz
rockbox-07fed9053a476b4c6e5ecd23fc43ec84f4094978.zip
lua optimize current_path function
frees up around 500 bytes by using the builtin string functionality Change-Id: Icd4ec921d3fec339b8a4b7f80c9c63d51d4c101c
Diffstat (limited to 'apps/plugins')
-rw-r--r--apps/plugins/lua/loadlib.c5
-rw-r--r--apps/plugins/lua/rockaux.c22
-rw-r--r--apps/plugins/lua/rocklib.c8
-rw-r--r--apps/plugins/lua/rocklib.h2
4 files changed, 13 insertions, 24 deletions
diff --git a/apps/plugins/lua/loadlib.c b/apps/plugins/lua/loadlib.c
index 1cc7ebd7db..87873f133f 100644
--- a/apps/plugins/lua/loadlib.c
+++ b/apps/plugins/lua/loadlib.c
@@ -54,7 +54,10 @@ static const char *pushnexttemplate (lua_State *L, const char *path) {
54 54
55static const char *findfile (lua_State *L, const char *name, 55static const char *findfile (lua_State *L, const char *name,
56 const char *pname) { 56 const char *pname) {
57 const char *path, *current_path = get_current_path(L, 2); 57 get_current_path(L, 2);
58 const char *current_path = lua_tostring(L, -1);
59 const char *path;
60
58 name = luaL_gsub(L, name, ".", LUA_DIRSEP); 61 name = luaL_gsub(L, name, ".", LUA_DIRSEP);
59 lua_getfield(L, LUA_ENVIRONINDEX, pname); 62 lua_getfield(L, LUA_ENVIRONINDEX, pname);
60 path = lua_tostring(L, -1); 63 path = lua_tostring(L, -1);
diff --git a/apps/plugins/lua/rockaux.c b/apps/plugins/lua/rockaux.c
index f10a1cb021..b51364f718 100644
--- a/apps/plugins/lua/rockaux.c
+++ b/apps/plugins/lua/rockaux.c
@@ -73,9 +73,8 @@ int strcoll(const char * str1, const char * str2)
73 return rb->strcmp(str1, str2); 73 return rb->strcmp(str1, str2);
74} 74}
75 75
76const char* get_current_path(lua_State *L, int level) 76int get_current_path(lua_State *L, int level)
77{ 77{
78 static char buffer[MAX_PATH];
79 lua_Debug ar; 78 lua_Debug ar;
80 79
81 if(lua_getstack(L, level, &ar)) 80 if(lua_getstack(L, level, &ar))
@@ -84,22 +83,15 @@ const char* get_current_path(lua_State *L, int level)
84 and write it to dest. */ 83 and write it to dest. */
85 lua_getinfo(L, "S", &ar); 84 lua_getinfo(L, "S", &ar);
86 85
87 char* curfile = (char*) &ar.source[1]; 86 const char* curfile = &ar.source[1];
88 char* pos = rb->strrchr(curfile, '/'); 87 const char* pos = rb->strrchr(curfile, '/');
89 if(pos != NULL) 88 if(pos != NULL)
90 { 89 {
91 unsigned int len = (unsigned int)(pos - curfile); 90 lua_pushlstring (L, curfile, pos - curfile + 1);
92 len = len + 1 > sizeof(buffer) ? sizeof(buffer) - 1 : len; 91 return 1;
93
94 if(len > 0)
95 memcpy(buffer, curfile, len);
96
97 buffer[len] = '/';
98 buffer[len+1] = '\0';
99
100 return buffer;
101 } 92 }
102 } 93 }
103 94
104 return NULL; 95 lua_pushnil(L);
96 return 1;
105} 97}
diff --git a/apps/plugins/lua/rocklib.c b/apps/plugins/lua/rocklib.c
index 57165a5d15..ffd449e9d4 100644
--- a/apps/plugins/lua/rocklib.c
+++ b/apps/plugins/lua/rocklib.c
@@ -194,13 +194,7 @@ RB_WRAP(font_getstringsize)
194 194
195RB_WRAP(current_path) 195RB_WRAP(current_path)
196{ 196{
197 const char *current_path = get_current_path(L, 1); 197 return get_current_path(L, 1);
198 if(current_path != NULL)
199 lua_pushstring(L, current_path);
200 else
201 lua_pushnil(L);
202
203 return 1;
204} 198}
205 199
206static void fill_text_message(lua_State *L, struct text_message * message, 200static void fill_text_message(lua_State *L, struct text_message * message,
diff --git a/apps/plugins/lua/rocklib.h b/apps/plugins/lua/rocklib.h
index 25b5ae1088..b650207e67 100644
--- a/apps/plugins/lua/rocklib.h
+++ b/apps/plugins/lua/rocklib.h
@@ -46,7 +46,7 @@ struct lua_str_reg {
46}; 46};
47 47
48LUALIB_API int (luaopen_rock) (lua_State *L); 48LUALIB_API int (luaopen_rock) (lua_State *L);
49const char* get_current_path(lua_State *L, int level); 49int get_current_path(lua_State *L, int level);
50 50
51#endif /* _ROCKLIB_H_ */ 51#endif /* _ROCKLIB_H_ */
52 52