Page 1 of 1

using gsub() on a filename and it does not open

Posted: Fri Jun 12, 2020 1:05 pm
by PatrickJAllan
In my program I have an edit background feature, with the ability for a user to insert a png from their computer as the background, I've added the ability to paste text in, however because windows uses "\" for paths and love2d requires "/" for paths I have had to use gsub() to change between them. However when this is done the file path fails to load, whereas if i manually type the file path with / instead of \ it works. I'm unsure if this is an issue with gsub() or the code used to open the file:

if key == "return" then
print(InsertImageText)
InsertImageText = (InsertImageText):gsub("\\", "/")
print(InsertImageText)
local file = io.open(InsertImageText , "rb")
if file then
local contents = file:read("*all")
local data = love.filesystem.newFileData( contents, "img.png", "background.png" )
local imgdata = love.image.newImageData( data )

file:close()

BackgroundImage = love.graphics.newImage( imgdata )
else
Error("Failed to find specified file")
end
end

even running an if statement to check if the covnerted string is the same as one i could manually type says they are equal, despite the code being unable to open the file path

Re: using gsub() on a filename and it does not open

Posted: Fri Jun 12, 2020 3:11 pm
by pgimeno
Hello, welcome to the forums!

Please check if any of this clarifies anything:

1. Windows accepts both \ and / as path separators (but note that COMMAND.COM doesn't accept /).
2. Löve uses PhysFS for file management; PhysFS only accepts / as separator.
3. PhysFS is case sensitive when it accesses files inside ZIP files (like .love files) and uses a system-dependent case sensitivity with files directly on disk.

Due to that:
a) Always use / as path separator. Exception: for 'require' use dots instead of slashes.
b) Always match the case used to refer to a file in the program with the case in the actual filename.

If you're already doing that, you'll have to give more information about what's your setup, what you're doing and what error you're getting.

Re: using gsub() on a filename and it does not open

Posted: Fri Jun 12, 2020 6:56 pm
by Andlac028
In love.filesystem.newFileData you are specifying 3rd argument but it has no meaning since its syntax is (filepath) or (content, name).
And please use [­code]your code here[­/code] tags when pasting code

Re: using gsub() on a filename and it does not open

Posted: Fri Jun 12, 2020 7:19 pm
by zorg
You also don't need to first create a filedata, then an imagedata, then an image.
If you don't want to edit the contents of it, just load the file directly with love.graphics.newImage(path)