Sign in to follow this  
samgj

Open a File as a String in Physics FS

Recommended Posts

samgj    400

I want to have my program open a Python script as a string so it can be used by Py_RunSimpleString().

bool runscript(const char* pyname)
{
 //Loads and runs a specified script

 if(PHYSFS_exists(pyname)) //if the script exists
 {
  pyname = PHYSFS_openRead(pyname); //opens the script

  PyRun_SimpleString( pyname ); //run the requested script. This is a temporary version, the final version will have low level embedding

  return true;
 }

 else //if unable to open script
 {
  return false;
 }

}

But PHYSFS_openRead() only returns data of type PhyFS_file. I can't find a Physics FS function that opens a file as a string. So, how would I get the file contents as a string.

Share this post


Link to post
Share on other sites
SiCrane    11839
If you're trying to load scripts from a zip archive you've got a potentially much simpler approach: Python has the ability to read scripts from zip files built-in. Just add the zip file to sys.path and import will automatically find scripts in the zip file.

Share this post


Link to post
Share on other sites
samgj    400

I changed it now and it compiles. I didn't want to use sys.path because I want to be able to open LZMA2 files because they are more compressed than ZIP. I also want it to support mods and load directories in the same way because that would make development easier.

 

Here's the new code

bool runscript(const char* pyname)
{
 //Loads and runs a specified script

 PHYSFS_File* pyfile = PHYSFS_openRead(pyname);
 char scriptbuffer[ PHYSFS_fileLength(pyfile) ]; //Create the string the script will be stored in

 if(PHYSFS_exists(pyname)) //if the script exists
 {
  PHYSFS_read( pyfile, scriptbuffer, strlen(scriptbuffer), strlen(scriptbuffer)); //opens the script

  PyRun_SimpleString( scriptbuffer ); //run the requested script. This is a temporary version, the final version will have low level embedding

  return true;
 }

 else //if unable to open script
 {
  return false;
 }

}

Share this post


Link to post
Share on other sites
SiCrane    11839

Using strlen() on a character buffer that you haven't explicitly initialized is a bad idea. strlen() works by looking for the first null. If you haven't initialized your buffer, it may not find a null there and try to read past your buffer, or it might find a null early and report your buffer is much smaller than it actually is.

Share this post


Link to post
Share on other sites
Promit    13246

Using strlen() on a character buffer that you haven't explicitly initialized is a bad idea.

Bad idea? BAD IDEA?! That doesn't really capture the situation here. The code makes no sense as written. It's not so much a bad idea as it is badly broken.

 

First, you can't set an array's size like that. You have to do a dynamic allocation. Second, you don't need to call strlen at all, since you know exactly what size you started with. Third, you're asking if a file exists after you've already attempted to open it, which is nonsense. If you're opening it, just check if you got a file back. Fourth, you've misunderstood the parameters to PHYSFS_read. 

So you actually need something like:

PHYSFS_File* pyfile = PHYSFS_openRead(pyname);
if(pyfile != NULL)
{
  PHYSFS_sint64 fileSize = PHYSFS_fileLength(pyfile);
  char* scriptbuffer = new char[fileSize];
  PHYSFS_read( pyfile, scriptbuffer, fileSize, 1);
  PHYSFS_close(pyfile);

  PyRun_SimpleString( scriptbuffer );
  delete[] scriptbuffer;
  return true;
}
return false;
Edited by Promit

Share this post


Link to post
Share on other sites
SiCrane    11839

First, you can't set an array's size like that.

That actually depends on the programming language. It's legal C99, which introduced variable length arrays.

Share this post


Link to post
Share on other sites
Promit    13246

 

First, you can't set an array's size like that.

That actually depends on the programming language. It's legal C99, which introduced variable length arrays.

 

It's a bad assumption that he's using C99 in the first place, and even if he were it simply shifts things over to the "bad idea" side.

Share this post


Link to post
Share on other sites
samgj    400

 

So you actually need something like:

Thanks, the with various versions of the function I tried when a script was runa script, I either got no output or, Python gave me an error like this:

  File "<string>", line 5
    Yr6S\s?bx?
     ^
SyntaxError: invalid syntax

I now have this:

bool runscript(const char* pyname)
{
 //Loads and runs a specified script

 PHYSFS_File* pyfile = PHYSFS_openRead(pyname);

 if(pyfile != NULL)
 {
  PHYSFS_sint64 filesize = PHYSFS_fileLength(pyfile);
  char* scriptbuffer = new char[ filesize ]; //Create the string the script will be stored in

  PHYSFS_read( pyfile, scriptbuffer, 1, filesize ); //opens the script

  PyRun_SimpleString( scriptbuffer ); //run the requested script. This is a temporary version, the final version will have low level embedding

  delete[] scriptbuffer;

  return true;
 }

 else //if unable to open script
 {
  return false;
 }

}

This new version works. Promit, you had the last two arguments of PHYSFS_read() wrong. It should be size in bytes of each object then number of objects to read.

Share this post


Link to post
Share on other sites

When one of the last two parameters is 1 the result is pretty much the same except by the return value (i.e. it'll return the same number as the last argument if it managed to read it all). Which reminds me, you seriously should do some error checking there, don't forget that reading can fail too.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this