Open a File as a String in Physics FS

Started by
8 comments, last by Sik_the_hedgehog 10 years, 8 months ago

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.

SAMULIKO: My blog

[twitter]samurliko[/twitter]

BitBucket

GitHub

Itch.io

YouTube

Advertisement

PhysFS_read.

SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
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.

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;
 }

}

SAMULIKO: My blog

[twitter]samurliko[/twitter]

BitBucket

GitHub

Itch.io

YouTube

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.


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;
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

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.

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.

SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

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.

SAMULIKO: My blog

[twitter]samurliko[/twitter]

BitBucket

GitHub

Itch.io

YouTube

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.

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

This topic is closed to new replies.

Advertisement