Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for seekg and tellg functions #14

Merged
merged 1 commit into from
Aug 1, 2022

Conversation

TAR-ALEX
Copy link
Contributor

These code change include added support for seekg and tellg functions. These functions can be useful for determining the size of the uncompressed output. For my particular use case I need to store a few positions in a file and come back to them later on a second pass. The new seek function is capable of seeking forward. If a backwards seek is triggered and it is already buffered it will just jump to that position, but when the position is not in the buffer it will reset the stream and seek from the zero position. A relative seek from the end of the stream is not possible and will throw an exception.

Here is the code that I used for testing (I also changed the buffer size to 100):

#include <iostream>
#include <fstream>
#include <bxzstr.hpp>

using namespace std;

int main(){
    bxz::ifstream in("./src.tar.gz");
    char c;
    in.seekg(2460);
    cout << in.tellg() << endl;
    in.seekg(2458);
    cout << in.tellg() << endl;
    int pos = 0;
    while(in.get(c)){
        if(c == '#' && pos == 0) pos = in.tellg()-1; 
        cout << in.tellg() << endl;
    }
    cout << endl;

    in.clear();
    in.seekg(pos);
    while(in.get(c)){
        cout << c;
    }
    cout << endl;
    
    return 0;
}

Here is the file I used as well:
src.tar.gz

@tmaklin
Copy link
Owner

tmaklin commented Aug 1, 2022

This is great, thanks a lot @TAR-ALEX! I'll merge it right away.

@tmaklin tmaklin added this to the v1.2.0 milestone Aug 1, 2022
@tmaklin tmaklin merged commit 5074452 into tmaklin:master Aug 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants