Skip to content

Commit

Permalink
add capability to load folder/object.pd_lua with same names
Browse files Browse the repository at this point in the history
  • Loading branch information
sebshader committed Feb 29, 2024
1 parent 36cd7fb commit 213f5df
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ function luax:initialize(sel, atoms) -- motivation: pd-list 2007-09-23
end
self._scriptname = pathname .. '/' .. basename(atoms[1]) .. ".pd_luax" -- mrpeach 20120201
local atomstail = { } -- munge for better lua<->luax compatibility
for i,_ in ipairs(atoms) do
for i,_ in ipairs(atoms) do
if i > 1 then
atomstail[i-1] = atoms[i]
end
Expand Down
49 changes: 33 additions & 16 deletions pdlua.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ static t_pdlua *pdlua_new
load_path_save = luaL_ref(__L, LUA_REGISTRYINDEX);
lua_pushstring(__L, buf);
lua_setfield(__L, -2, "_loadpath");

PDLUA_DEBUG("pdlua_new (basename load) path is %s", buf);
//pdlua_setpathname(o, buf);/* change the scriptname to include its path
pdlua_setrequirepath(__L, buf);
Expand Down Expand Up @@ -603,15 +603,15 @@ static void pdlua_stack_dump (lua_State *L)
case LUA_TSTRING: /* strings */
printf("`%s'", lua_tostring(L, i));
break;

case LUA_TBOOLEAN: /* booleans */
printf(lua_toboolean(L, i) ? "true" : "false");
break;

case LUA_TNUMBER: /* numbers */
printf("%g", lua_tonumber(L, i));
break;

default: /* other values */
printf("%s", lua_typename(L, t));
break;
Expand Down Expand Up @@ -1844,21 +1844,38 @@ static int pdlua_loader_pathwise
const char *path /**< The directory to search for the script */
)
{
char dirbuf[MAXPDSTRING];
char dirbuf[MAXPDSTRING], filename[MAXPDSTRING];
char *ptr;
const char *classname;
int fd;

if(!path)
{
/* we already tried all paths, so skip this */
return 0;
}
if ((classname = strrchr(objectname, '/')))
classname++;
else classname = objectname;
/* ag: Try loading <path>/<classname>.pd_lua (experimental).
sys_trytoopenone will correctly find the file in a subdirectory if a
path is given, and it will then return that subdir in dirbuf. */
fd = sys_trytoopenone(path, objectname, ".pd_lua",
dirbuf, &ptr, MAXPDSTRING, 1);
return pdlua_loader_wrappath(fd, objectname, dirbuf);
if ((fd = sys_trytoopenone(path, objectname, ".pd_lua",
dirbuf, &ptr, MAXPDSTRING, 1)) >= 0)
if(pdlua_loader_wrappath(fd, objectname, dirbuf))
return 1;

/* next try (objectname)/(classname).(sys_dllextent) ... */
strncpy(filename, objectname, MAXPDSTRING);
filename[MAXPDSTRING-2] = 0;
strcat(filename, "/");
strncat(filename, classname, MAXPDSTRING-strlen(filename));
filename[MAXPDSTRING-1] = 0;
if ((fd = sys_trytoopenone(path, filename, ".pd_lua",
dirbuf, &ptr, MAXPDSTRING, 1)) >= 0)
if(pdlua_loader_wrappath(fd, objectname, dirbuf))
return 1;
return 0;
}


Expand Down Expand Up @@ -2007,15 +2024,15 @@ void pdlua_setup(void)
else
{
int maj=0,min=0,bug=0;
sys_getversion(&maj,&min,&bug);
if((maj==0) && (min<47))
/* before Pd<0.47, the loaders had to iterate over each path themselves */
sys_register_loader((loader_t)pdlua_loader_legacy);
else
/* since Pd>=0.47, Pd tries the loaders for each path */
sys_register_loader((loader_t)pdlua_loader_pathwise);
sys_getversion(&maj,&min,&bug);
if((maj==0) && (min<47))
/* before Pd<0.47, the loaders had to iterate over each path themselves */
sys_register_loader((loader_t)pdlua_loader_legacy);
else
/* since Pd>=0.47, Pd tries the loaders for each path */
sys_register_loader((loader_t)pdlua_loader_pathwise);
}
close(fd);
close(fd);
}
else
{
Expand Down

0 comments on commit 213f5df

Please sign in to comment.