samedi 11 juin 2016

Reading a binary file incrementally


I recently made another question about parsing binary file, and I sort of got it to work thanks to everyone here.

Need help reading binary file to a structure

But I now face a new challenge and I am in need of help, please bare with me.

My binary file looks something like this but much much longer...

ST.........¸.°Ý.ø...0.œ...........ESZ4 1975..........IYH.testDBDBDBDBST...........°Ý.ø...................DBDBDBDBST.........P.´Ý.ø...0.œ...........ESZ4 1975..........HTC.testDBDBDBDBST.........‹‚´Ý.ø...................DBDBDBDBST.........ƒD.Þ.ø...0.œ...........ESZ4 1975..........ARM.testDBDBDBDBST.........«E.Þ.ø...................DBDBDBDB

Basically, every message starts with 'ST' and ends with 'DBDBDBDB'. The goal is to parse each message and store the message into a data structure. In addition, every message is different depending on the type, and different type of message will have additional members.

The problem I am having is, I have no idea how to iterate through this binary file... If its a regular file, I can just do while(getline(file,s)), but what about binary file?? Is there a way to say, find the first "ST" and "DBDBDBDB" and parse the middle stuff, then move on to the next set of ST and DB? Or somehow read the file incrementally keeping track of where I am?

I apologise ahead of time for posting so much code.

#pragma pack(push, 1)
struct Header
{
    uint16_t marker;
    uint8_t msg_type;
    uint64_t sequence_id;
    uint64_t timestamp;
    uint8_t msg_direction;
    uint16_t msg_len;
};
#pragma pack(pop)
struct OrderEntryMessage
{
    Header header;
    uint64_t price;
    uint32_t qty;
    char instrument[10];
    uint8_t side;
    uint64_t client_assigned_id;
    uint8_t time_in_force;
    char trader_tag[3];
    uint8_t firm_id;
    char firm[256] ; 
    char termination_string[8]; 
};

struct AcknowledgementMessage
{
    Header header;
    uint32_t order_id;
    uint64_t client_id;
    uint8_t order_status;
    uint8_t reject_code;
    char termination_string[8];
};

struct OrderFillMessage
{
    Header header;
    uint32_t order_id;
    uint64_t fill_price;
    uint32_t fill_qty;
    uint8_t no_of_contras;
    uint8_t firm_id;
    char trader_tag[3];
    uint32_t qty;
    char termination_string[8]; 
};
void TradeDecoder::createMessage()
{
    ifstream file("example_data_file.bin", std::ios::binary);

     //I want to somehow Loop here to keep looking for headers ST

    Header h;
    file.read ((char*)&h.marker, sizeof(h.marker));
    file.read ((char*)&h.msg_type, sizeof(h.msg_type));
    file.read ((char*)&h.sequence_id, sizeof(h.sequence_id));
    file.read ((char*)&h.timestamp, sizeof(h.timestamp));
    file.read ((char*)&h.msg_direction, sizeof(h.msg_direction));
    file.read ((char*)&h.msg_len, sizeof(h.msg_len));
    file.close();

    switch(h.sequence_id)
    {
        case 1: 
            createOrderEntryMessage(h); //this methods creates a OrderEntryMessage with the header
        break;
        case 2:
            createOrderAckMessage(h); //same as above
        break;
        case 3:
            createOrderFillMessage(h); //same as above
        break;
        default:
        break;
    }
}  

Much much thanks.....


Aucun commentaire:

Enregistrer un commentaire