#include <iostream>
#include <fstream>
#include <stdexcept>
#include <sstream>

const char* magic = "void output_direction_tin";

using namespace std;

int main(int argc, char** argv) {
    if (argc < 2) {
        cout << "Remember argument.\n";
    }
    const string file = argv[1];

    cout << "Opening "<<  file << endl;
    ifstream in(file.c_str());
    cout << "Done.\n";

    const istream::off_type block_size = 1024*1024*100;
    unsigned char* block = new unsigned char[block_size];
    if (block==0) {
        cout << "Could not allocate block size\n";
        throw runtime_error("Could not allocate");
    }
    istream::off_type read;

    istream::off_type total_read = 0;

    if (!in) {
        cout << "Could not open" << file << ".\n";
        throw runtime_error("Could not open " + file);
    }

    istream::off_type last_megs = 0;
    istream::off_type blocks_read = 0;
    cout << "Starting read.." << endl;
    while (in) {
        in.read((char *)block, block_size);
        read = in.gcount();
        size_t n=strlen(magic);
        for (istream::off_type i = 0; i < read-n; ++i) {
            if (strncmp(magic,(char *)block+i,n)==0)
            {
                istream::off_type off = blocks_read*block_size+i;
                cout << "Match: " << off << endl;
            }
        }
        total_read += read;
        cout << total_read/(1024*1024) << " megs" << endl;
        ++blocks_read;
    }
 return 0;
}