Jump to content

  • Log In with Google      Sign In   
  • Create Account

Open a File as a String in Physics FS


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 samgj   Members   -  Reputation: 399

Like
0Likes
Like

Posted 25 July 2013 - 11:21 AM

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.


MLNM-ONE: open source video game engine

Unincorporated Media's Website: my company's website


Sponsor:

#2 Promit   Moderators   -  Reputation: 7620

Like
0Likes
Like

Posted 25 July 2013 - 11:33 AM

PhysFS_read.



#3 SiCrane   Moderators   -  Reputation: 9669

Like
0Likes
Like

Posted 25 July 2013 - 11:34 AM

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.

#4 samgj   Members   -  Reputation: 399

Like
0Likes
Like

Posted 26 July 2013 - 03:47 PM

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

}

MLNM-ONE: open source video game engine

Unincorporated Media's Website: my company's website


#5 SiCrane   Moderators   -  Reputation: 9669

Like
0Likes
Like

Posted 26 July 2013 - 04:03 PM

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.



#6 Promit   Moderators   -  Reputation: 7620

Like
0Likes
Like

Posted 26 July 2013 - 04:11 PM


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, 29 July 2013 - 08:04 AM.


#7 SiCrane   Moderators   -  Reputation: 9669

Like
0Likes
Like

Posted 26 July 2013 - 04:15 PM

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.

#8 Promit   Moderators   -  Reputation: 7620

Like
0Likes
Like

Posted 26 July 2013 - 04:21 PM

 

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.



#9 samgj   Members   -  Reputation: 399

Like
1Likes
Like

Posted 27 July 2013 - 08:15 AM

 

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.


MLNM-ONE: open source video game engine

Unincorporated Media's Website: my company's website


#10 Sik_the_hedgehog   Crossbones+   -  Reputation: 1833

Like
0Likes
Like

Posted 01 August 2013 - 03:52 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS