// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <vector>

using namespace std;

string find(string s, string start, string end)
{
    return s.substr(s.find(start) + start.length(), s.find(end) - s.find(start) - start.length());
}

void multifind(string s, string start, string end, void (*f)(string))
{
    int ix = 0;
    while((ix = s.find(start, ix)) != string::npos)
    {
        int ix2 = s.find(end, ix);
        f(s.substr(ix + start.length(), ix2 - ix - start.length()));
        ix = ix2;
    }
}

static vector<int> pairfrom;
static vector<int> pairto;

void process_sequencepart(string s)
{
    cout << "exon sequnce part = " << s << endl;
}

void process_feature(string s)
{
    if(find(s, "<INSDFeature_key>", "</INSDFeature_key>") == "exon")
    {
        pairfrom.push_back(atoi(find(s, "<INSDInterval_from>", "</INSDInterval_from>").c_str()));
        pairto.push_back(atoi(find(s, "<INSDInterval_to>", "</INSDInterval_to>").c_str()));
    }
}

void process(string s)
{
    multifind(s, "<INSDFeature>", "</INSDFeature>", process_feature);
    string sequence = find(s, "<INSDSeq_sequence>", "</INSDSeq_sequence>");
    for(int i = 0; i < pairfrom.size(); i++)
    {
        string part = sequence.substr(pairfrom[i], pairto[i] - pairfrom[i] + 1);
        process_sequencepart(part);
    }
}

int main()
{
    ifstream f("INSD.xml");
    string buf = "";
    string line;
    while(getline(f,line))
    {
        buf += line;
    }
    f.close();
    process(buf);
    return 0;
}
