Page 1 of 1

How Come Folders Can't Have a Dot in their Name?

Posted: Tue Feb 17, 2015 8:55 pm
by davisdude
It seems non-trivial to implement:

Code: Select all

-- Replace '.' if using `require 'Path.to.lib'`
function require( inputPath ) -- or whatever it is
    local currentPath = ''
    local currentIndex = 0
    while #currentPath ~= #inputPath do
        local _, index, temp = inputPath:find( '(.-)%.', currentIndex )
        if love.filesystem.exists( currentPath .. temp ) then -- Allow for . in folder names
            currentPath = currentPath .. temp .. '/'
            currentIndex = index + 1
        end
    end
    return currentPath
end
So how come the .'s are overwritten by "/"?
Is this a Lua problem, or a LÖVE problem?

Edit: Uploaded .love example

Re: How Come Folders Can't Have a Dot in their Name?

Posted: Wed Feb 18, 2015 5:32 am
by szensk
In the example.love it's a Lua problem (require blindly replaces . with the system separator). The easiest solution, in Lua, is to use loadfile. I suspect it is not Love's fault because assert(love.filesystem.exists('Folder.with.dots/File.lua')) works perfectly fine, for instance.

Your example require function makes Love crash, on my PC. Personally, I'd just avoid `.`

Regardless of suggestions, I wrote a require function that works for the Example.love shown. I do not state its correctness. It doesn't handle errors. You'd probably want to use this as a loader rather than overriding require.

Code: Select all

require = function(path) 
  return assert(love.filesystem.load(path:gsub('%.([^%.]*)$', '/%1.lua')))()
end

Re: How Come Folders Can't Have a Dot in their Name?

Posted: Wed Feb 18, 2015 2:49 pm
by undef
This behaviour is normal in Lua:
The second searcher looks for a loader as a Lua library, using the path stored at package.path. A path is a sequence of templates separated by semicolons. For each template, the searcher will change each interrogation mark in the template by filename, which is the module name with each dot replaced by a "directory separator" (such as "/" in Unix); then it will try to open the resulting file name. So, for instance, if the Lua path is the string

"./?.lua;./?.lc;/usr/local/?/init.lua"

the search for a Lua file for module foo will try to open the files ./foo.lua, ./foo.lc, and /usr/local/foo/init.lua, in that order.
source

Re: How Come Folders Can't Have a Dot in their Name?

Posted: Wed Feb 18, 2015 3:15 pm
by davisdude
Good to know, thanks. :)