Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
754 views
in Technique[技术] by (71.8m points)

resources - Can I use ifstream in Android NDK to access Assets?

My question is pretty simple, but I am having a hard time finding any info about this online.

Is it possible to use ifstream to open a file from assets and/or resources using Android NDK?

For example, placing a test.txt file in /assets and trying the following does not work:

    char pLine[256];
    std::ifstream fin("/assets/test.txt");
    if(!fin.fail())
    {
        LOGD( "test.txt opened" );
        while( !fin.eof() )
        {
            fin.getline( pLine, 256 );
            LOGD(pLine);
        }
    }
    else
    {
        LOGD( "test.txt FAILED TO OPEN!" );
    }
    fin.close();

Nor does any variable of:

    std::ifstream fin("assets/test.txt");

    std::ifstream fin("test.txt");

Etc..., nor placing it in /res instead.

So, is it possible to use normal ifstream operators to access assets and or resource files?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It is right that std::ifstream cannot be used, but one can create an assetistream that could be used in a similar way. For example:

class asset_streambuf: public std::streambuf
{
public:
    asset_streambuf(AAssetManager* manager, const std::string& filename)
    : manager(manager)
    {
        asset = AAssetManager_open(manager, filename.c_str(), AASSET_MODE_STREAMING);
        buffer.resize(1024);

        setg(0, 0, 0);
        setp(&buffer.front(), &buffer.front() + buffer.size());
    }

    virtual ~asset_streambuf()
    {
        sync();
        AAsset_close(asset);
    }

    std::streambuf::int_type underflow() override
    {
        auto bufferPtr = &buffer.front();
        auto counter = AAsset_read(asset, bufferPtr, buffer.size());

        if(counter == 0)
            return traits_type::eof();
        if(counter < 0) //error, what to do now?
            return traits_type::eof();

        setg(bufferPtr, bufferPtr, bufferPtr + counter);

        return traits_type::to_int_type(*gptr());
    }

    std::streambuf::int_type overflow(std::streambuf::int_type value) override
    {
        return traits_type::eof();
    };

    int sync() override
    {
        std::streambuf::int_type result = overflow(traits_type::eof());

        return traits_type::eq_int_type(result, traits_type::eof()) ? -1 : 0;
    }

private:
    AAssetManager* manager;
    AAsset* asset;
    std::vector<char> buffer;
};


class assetistream: public std::istream
{
public:
    assetistream(AAssetManager* manager, const std::string& file)
    : std::istream(new asset_streambuf(manager, file))
    {
    }
    assetistream(const std::string& file)
    : std::istream(new asset_streambuf(manager, file))
    {
    }

    virtual ~assetistream()
    {
        delete rdbuf();
    }

    static void setAssetManager(AAssetManager* m)
    {
        manager = m;
    }

private:
    static AAssetManager* manager;
};

void foo(AAssetManager* manager)
{
    assetistream::setAssetManager(manager);

    assetistream as("text/tmp.txt");
    std::string s;

    std::getline(as, s);
}

Improvements are very welcome.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...