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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
PatrickJAllan
Prole
Posts: 1
Joined: Fri Jun 12, 2020 12:59 pm

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

Post 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
User avatar
pgimeno
Party member
Posts: 3641
Joined: Sun Oct 18, 2015 2:58 pm

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

Post 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.
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

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

Post 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
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

Post 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)
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 5 guests