Is there a way to avoid warning in emulator if system.pathForFile fails?

I want to test for the existence of a file *WITHOUT* generating a warning in the emulator …

Back over half a century ago, Burroughs ALGOL was smart enough to distinguish between

an “OPEN” with and without an error handling clause.

I.e., I’d like to be able to do:

   fpath, errstr = system.pathForFile (“foo”)

similar to how I can already do:

   fhandle, errst = io.open (“foo”)

Currently, I’m getting:

    WARNING: Cannot create path for resource file ‘page_52.png’. File does not exist.

from system.pathForFile

Hmm … I’m beginning to suspect that although this question is interesting, it’s not the question

I really want to ask (I probably really want to ask "how do I check if a .png file exists without

generating a warning if it doesn’t" :slight_smile: .

(Note: on Android)

thanks,

Stan

The documentation describe well how to test for a file in the first example.

https://docs.coronalabs.com/api/library/system/pathForFile.html

however, your code assume default directory, not sure if thats the best approach on android.

also, paths to files need the file type included in the file name.

Maybe try this? (I may have return order backwards)

https://docs.coronalabs.com/api/library/global/pcall.html
 

function pathForFile( value ) return system.pathForFile( value ) end local err, path = pcall( pathForFile, "foo" )

Hi…

Uh, @anaqim, no … perhaps you misread my problem?

The documentation *for the routine I *AM* already calling* (system.pathForFile) says:

   "If the base directory is system.ResourceDirectory and the generated path points to a non-existent file, 

   nil is returned and a warning message is displayed in the Corona Simulator Console."

I.e., that’s not a solution path at the moment … barring an enhancement like I suggested, which would (BTW) be

100% backward compatible!

@roaminggamer … unfortunately, system.pathForFile doesn’t return a pair of items, just a single result.

What you suggest is what I said in the OP that I’d like to be able to do :slight_smile:

Re: my suggestion (to have system.pathForValue display a warning only if the caller didn’t ask for an error string) …

Note that I don’t know if Lua (or Corona) has the ability for a function (like system.pathForValue) to say:

      – about to generate a warning to console …

     oh, hey … is the caller expecting an error string (as the second functional result)?

     if no then

        display the warning.

It could do it if there was a mechanism like, say, 

    self.num_return_values_expected      – would be two if caller said:   a,b = me (…).   Would be 0 if caller said:   me ()

Stan (been there, implemented that kind of logic in compilers/interpreters before :slight_smile: Sieler

  1. I don’t think system.pathForFile throws a warning.  That sounds wrong. Maybe only on Mac?
     

  2. I just tried this in a game with an images folder and no file called test2.txt:

    local function pathForFile( filename, baseDir ) baseDir = baseDir or system.DocumentsDirectory local function work( val1, val2 ) return system.pathForFile( val1, val2 ) end return pcall( work, filename, baseDir) end print( pathForFile( “main.lua”, system.ResourceDirectory ) ) print( pathForFile( “images”, system.ResourceDirectory ) ) print( pathForFile( “test2.txt”, system.ResourceDirectory ) )

The response was:

18:09:44.859 true X:\Work\00\_CurentProjects\Corona\T\_TurnItIntoMoney\WIP\SlideTray\main.lua 18:09:44.859 true X:\Work\00\_CurentProjects\Corona\T\_TurnItIntoMoney\WIP\SlideTray\images 18:09:44.859 true nil

No warnings in console.
 
 
3. system.pathForFile doesn’t actually check for the existence of anything.  It is a helper to make paths.
 
I think this is more what you need:

function io.exists( fileName, base ) local lfs = require "lfs" local base = base or system.DocumentsDirectory if( base ) then fileName = system.pathForFile( fileName, base ) end if not fileName then return false end local attr = lfs.attributes( fileName ) return (attr and (attr.mode == "file" or attr.mode == "directory") ) end print( io.exists( "main.lua", system.ResourceDirectory ) ) print( io.exists( "images", system.ResourceDirectory ) ) print( io.exists( "test2.txt", system.ResourceDirectory ) )

Same game prints (on Windows 10):

18:20:29.402 true 18:20:29.402 true 18:20:29.402 false

Same game prints (on Android):

I/Corona ( 5097): false I/Corona ( 5097): false I/Corona ( 5097): false

All false of course, because you cannot check for scripts or directories on Android in system.ResourceDirectory

PS - Just to be clear, we are talking about the Corona Simulator right? Or are you running on a Android emulator like Nox, or Android Studio, Blue Stacks, …

Followup…

I just ran the pathForFile() example (#2) above on my Mac (Sierra; Corona 2017.3160) and…

the simulator throws a warning.

I guess this is just a Mac thing.  i.e. Windows Simulator does not throw a warning, but Mac does.  

Sorry boss.

I work in a windows environment only so I wouldnt catch this one then.

Never had any issues with setting paths and checking if file exists or not, without errors being thrown.

Like RG said, setting the path does not check or validate the path, it just a definition.

Maybe i dont get entirely what you want to do but you wrote that 

this one does not work : fpath, errstr = system.pathForFile (“foo”)

but this one does : fhandle, errst = io.open (“foo”)

So while the second one tries to open(validate) a file, using the return/error variables work fine, the first one doesnt do anything so even if you can set the error variable, what do you expect it to return?

Its like saying:

local result,error=“Take a walk on the moon”

No logic in that, to me at least.

But, RG is an experienced coder, whereas I am not, so sometimes i’m in deep waters, for which I ask to be forgiven  :slight_smile:

I think the primary issue here is that Stan doesn’t want to dig through a deluge of Warnings if he can avoid them and that is a bummer, because I don’t know of a fix.

@roaminggamer 

Hi,

Partially that, partially my predilection for treating warnings as errors.   

I.e., I try to make my code compile with no warnings (e.g., my C code), and run with no warnings/errors.

(In two compilers I control, I added an option to say “treat all warnings as errors” :slight_smile:

@anaqim … I was trying to say by “work” that:

   1. io.open is nicely designed in that it returns a pair of values … the caller can obtain a meaningful error message if it fails

      (and, of course, failure is checked by seeing if the first return value is nil)

   2. system.pathForFile lacks an ability to return an error/warning message … I’m suggesting that it be added, and

      noted that such an addition would be completely backward  compatible (i.e., it would not affect existing code at all)

I now know that checking for a png file is “wrong” anyway, at least on Android, because any .png files I have in the .apk

file do not show up as real files (according to the documentation, and confirmed through testing).  Annoyingly, they do

show up as real files under the simulator (it would be nice if the simulator said "whoa, you’re trying to simulate an

Android, so I’ll simulate the file system oddities for you" :slight_smile:

I also now know that even if one has code like:   

fhandle,errtext = io.open (...)

one will still get a warning on the simulator console if the file does not exist.

To me, that’s a bug (or a design flaw, perhaps, subtly different).

But, as I pointed out, it may be caused by a (current and fixable) lack of a feature in the Lua language.

I don’t mind getting a warning if I did:  fhandle = io.open (…) 

but it is annoying to get one if I did: fhandle,errmsg = io.open (…)

Why?  The latter shows that the programmer is aware of the error reporting mechanism, and

is presumably going to handle an error in some manner.

I.e., warnings / errors sent to the console should be sent only when something happens that’s outside the

normal range of Lua and/or the SDK.  (E.g., simulator running out of memory during an ‘open’ request.)

thanks,

Stan

@roaminggamer … just noticed your long reply … will try that out, thanks!

The documentation describe well how to test for a file in the first example.

https://docs.coronalabs.com/api/library/system/pathForFile.html

however, your code assume default directory, not sure if thats the best approach on android.

also, paths to files need the file type included in the file name.

Maybe try this? (I may have return order backwards)

https://docs.coronalabs.com/api/library/global/pcall.html
 

function pathForFile( value ) return system.pathForFile( value ) end local err, path = pcall( pathForFile, "foo" )

Hi…

Uh, @anaqim, no … perhaps you misread my problem?

The documentation *for the routine I *AM* already calling* (system.pathForFile) says:

   "If the base directory is system.ResourceDirectory and the generated path points to a non-existent file, 

   nil is returned and a warning message is displayed in the Corona Simulator Console."

I.e., that’s not a solution path at the moment … barring an enhancement like I suggested, which would (BTW) be

100% backward compatible!

@roaminggamer … unfortunately, system.pathForFile doesn’t return a pair of items, just a single result.

What you suggest is what I said in the OP that I’d like to be able to do :slight_smile:

Re: my suggestion (to have system.pathForValue display a warning only if the caller didn’t ask for an error string) …

Note that I don’t know if Lua (or Corona) has the ability for a function (like system.pathForValue) to say:

      – about to generate a warning to console …

     oh, hey … is the caller expecting an error string (as the second functional result)?

     if no then

        display the warning.

It could do it if there was a mechanism like, say, 

    self.num_return_values_expected      – would be two if caller said:   a,b = me (…).   Would be 0 if caller said:   me ()

Stan (been there, implemented that kind of logic in compilers/interpreters before :slight_smile: Sieler

  1. I don’t think system.pathForFile throws a warning.  That sounds wrong. Maybe only on Mac?
     

  2. I just tried this in a game with an images folder and no file called test2.txt:

    local function pathForFile( filename, baseDir ) baseDir = baseDir or system.DocumentsDirectory local function work( val1, val2 ) return system.pathForFile( val1, val2 ) end return pcall( work, filename, baseDir) end print( pathForFile( “main.lua”, system.ResourceDirectory ) ) print( pathForFile( “images”, system.ResourceDirectory ) ) print( pathForFile( “test2.txt”, system.ResourceDirectory ) )

The response was:

18:09:44.859 true X:\Work\00\_CurentProjects\Corona\T\_TurnItIntoMoney\WIP\SlideTray\main.lua 18:09:44.859 true X:\Work\00\_CurentProjects\Corona\T\_TurnItIntoMoney\WIP\SlideTray\images 18:09:44.859 true nil

No warnings in console.
 
 
3. system.pathForFile doesn’t actually check for the existence of anything.  It is a helper to make paths.
 
I think this is more what you need:

function io.exists( fileName, base ) local lfs = require "lfs" local base = base or system.DocumentsDirectory if( base ) then fileName = system.pathForFile( fileName, base ) end if not fileName then return false end local attr = lfs.attributes( fileName ) return (attr and (attr.mode == "file" or attr.mode == "directory") ) end print( io.exists( "main.lua", system.ResourceDirectory ) ) print( io.exists( "images", system.ResourceDirectory ) ) print( io.exists( "test2.txt", system.ResourceDirectory ) )

Same game prints (on Windows 10):

18:20:29.402 true 18:20:29.402 true 18:20:29.402 false

Same game prints (on Android):

I/Corona ( 5097): false I/Corona ( 5097): false I/Corona ( 5097): false

All false of course, because you cannot check for scripts or directories on Android in system.ResourceDirectory

PS - Just to be clear, we are talking about the Corona Simulator right? Or are you running on a Android emulator like Nox, or Android Studio, Blue Stacks, …

Followup…

I just ran the pathForFile() example (#2) above on my Mac (Sierra; Corona 2017.3160) and…

the simulator throws a warning.

I guess this is just a Mac thing.  i.e. Windows Simulator does not throw a warning, but Mac does.  

Sorry boss.

I work in a windows environment only so I wouldnt catch this one then.

Never had any issues with setting paths and checking if file exists or not, without errors being thrown.

Like RG said, setting the path does not check or validate the path, it just a definition.

Maybe i dont get entirely what you want to do but you wrote that 

this one does not work : fpath, errstr = system.pathForFile (“foo”)

but this one does : fhandle, errst = io.open (“foo”)

So while the second one tries to open(validate) a file, using the return/error variables work fine, the first one doesnt do anything so even if you can set the error variable, what do you expect it to return?

Its like saying:

local result,error=“Take a walk on the moon”

No logic in that, to me at least.

But, RG is an experienced coder, whereas I am not, so sometimes i’m in deep waters, for which I ask to be forgiven  :slight_smile:

I think the primary issue here is that Stan doesn’t want to dig through a deluge of Warnings if he can avoid them and that is a bummer, because I don’t know of a fix.

@roaminggamer 

Hi,

Partially that, partially my predilection for treating warnings as errors.   

I.e., I try to make my code compile with no warnings (e.g., my C code), and run with no warnings/errors.

(In two compilers I control, I added an option to say “treat all warnings as errors” :slight_smile:

@anaqim … I was trying to say by “work” that:

   1. io.open is nicely designed in that it returns a pair of values … the caller can obtain a meaningful error message if it fails

      (and, of course, failure is checked by seeing if the first return value is nil)

   2. system.pathForFile lacks an ability to return an error/warning message … I’m suggesting that it be added, and

      noted that such an addition would be completely backward  compatible (i.e., it would not affect existing code at all)

I now know that checking for a png file is “wrong” anyway, at least on Android, because any .png files I have in the .apk

file do not show up as real files (according to the documentation, and confirmed through testing).  Annoyingly, they do

show up as real files under the simulator (it would be nice if the simulator said "whoa, you’re trying to simulate an

Android, so I’ll simulate the file system oddities for you" :slight_smile:

I also now know that even if one has code like:   

fhandle,errtext = io.open (...)

one will still get a warning on the simulator console if the file does not exist.

To me, that’s a bug (or a design flaw, perhaps, subtly different).

But, as I pointed out, it may be caused by a (current and fixable) lack of a feature in the Lua language.

I don’t mind getting a warning if I did:  fhandle = io.open (…) 

but it is annoying to get one if I did: fhandle,errmsg = io.open (…)

Why?  The latter shows that the programmer is aware of the error reporting mechanism, and

is presumably going to handle an error in some manner.

I.e., warnings / errors sent to the console should be sent only when something happens that’s outside the

normal range of Lua and/or the SDK.  (E.g., simulator running out of memory during an ‘open’ request.)

thanks,

Stan