1

I have a problem. I have lots of code that uses ifstreams in this manner:

ex:

bool AudioManager::_loadSounds( const std::string& path, const std::string& appendPath )
{
    //open the file
    std::ifstream ifs( path.c_str() );

    //ensure it is open
    if(!ifs.is_open())
    {
        return false;
    }

    std::string line;

    //read each sound
    while( getline( ifs, line ) )
    {

...

The problem is I need to make an application-wide change to use PhysFS. All data will stay structured the same directory wise except it will be compartmentalized into zip files.

Is there a simple way to make PhysFS apply to ifstreams so that I do not need to modify all these classes?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
jmasterx
  • 52,639
  • 96
  • 311
  • 557
  • Two suggestions, not related to question: 1) `fstream`s do have constructor taking `std::string` - why do you pass a `.c_str()` to it? 2) `fstream`s do have `operator bool()` defined; instead of `if (!ifs.is_open())`, use just `if (ifs)`. – Griwes Jun 06 '12 at 21:54
  • 1
    @Griwes: The constructor that takes `std::string` is only in C++11. – Jesse Good Jun 06 '12 at 21:56
  • @JesseGood, uh, forgot to mention that. Anyway, is there any reason not to compile with `-std=c++11`/`0x` nowadays? – Griwes Jun 06 '12 at 21:58
  • @Griwes The code was written before c++11 was a standard. – jmasterx Jun 06 '12 at 21:59
  • 1
    I also find is_open clearer than !ifs because in my mind !ifs implies ifs is a pointer. – jmasterx Jun 06 '12 at 22:00

2 Answers2

2

There is not a simple way to do it. No matter what, you're going to have to not use fstreams. However, you can continue to use streams by simply writing a std::streambuf-derived class that pulls its data from PhysFS. This isn't a trivial thing, since streambuf has various bits of complexity to it. But it is certainly doable.

You can take any istream-derived class and shove a different streambuf into it.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
0

PhysFS is a C library, and has no concept of C++ types. You will either need to find a C++ wrapper or write your own if you want to treat PhysFS handles as stream objects.

cdhowie
  • 158,093
  • 24
  • 286
  • 300