Data chunk building logic in C++ -


well, might simple people not managed figure out strong logic far following data chunk building.

  1. i have chunk of char* or std::string data of size 1505 bytes
  2. i need cut data each max size of 500 bytes
  3. each piece of data should start particular string set

please see below data chunk (i.e. full data set of 1505 bytes) here need cut data 500 bytes of chunks (e.g. put vector element) , each element text should start [sampleset]. can possible in 1 vector element got 2 or more sampleset in other vector element 1 [sampleset].

any clues how can start off this?

[sampleset] sampletime=2014-08-13 t12:02:04 data=type:datatype|value:0.11|alert:false|lead:ii data=type:datatype|value:65|alert:false data=type:datatype|value:95|alert:false data=type:datatype|value:97.8|alert:false|channel:1 data=type:datatype|value:90|alert:false data=type:datatype|value:0.10|alert:false|lead:i data=type:datatype|value:0.20|alert:true|lead:ii  [sampleset] sampletime=2014-08-13 t12:05:09 data=type:datatype|value:82|alert:false data=type:datatype|value:98|alert:true data=type:datatype|value:97.8|alert:false|channel:1 data=type:datatype|value:97.8|alert:false|channel:1 data=type:datatype|value:97.2|alert:false|channel:2 data=type:datatype|value:31|alert:false data=type:datatype|value:120|alert:true data=type:datatype|value:95|alert:true data=type:datatype|value:90|alert:false data=type:datatype|value:0.10|alert:false|lead:i data=type:datatype|value:0.20|alert:true|lead:ii  [sampleset] sampletime=2014-08-13 t12:05:20 data=type:datatype|value:82|alert:false data=type:datatype|value:98|alert:true data=type:datatype|value:31|alert:false data=type:datatype|value:120|alert:true data=type:datatype|value:95|alert:true data=type:datatype|value:90|alert:false data=type:datatype|value:0.10|alert:false|lead:i data=type:datatype|value:0.20|alert:true|lead:ii  [sampleset] sampletime=2014-08-13 t12:15:11 data=type:datatype|value:82|alert:false data=type:datatype|value:0.10|alert:false|lead:i data=type:datatype|value:0.20|alert:true|lead:ii 

the intuitive way of doing use std::string::find find position of headers (="[sampleset]") in input string. cut input string tokens of each contains 1 sample set. use these tokens build 500-bytes-max-chunks. if sample sets of size smaller chunk size, call find often.

a better approach therefore use std::string::rfind begin searching backwards [sampleset]-header @ right of 500bytes-block-boundary. i.e. find first chunk start searching backwards header [sampleset] @ position 500. position (e.g. 467) right boundary of first chunk. find next chunk start searching backwards @ 467+500 , on. hope approach clear. here's sample implementation:

std::string input; // input data std::vector<std::string> chunks; std::string header("[sampleset]"); std::string::size_type chunksize = 500; // size including header std::string::size_type posl, posr = 0, posend = input.length()+1;  while(posr != posend)  {     posl = posr;     if (posend-posl <= chunksize)         posr = posend;     else         posr = input.rfind(header, posl+chunksize);      if (posr-posl)         chunks.push_back(input.substr(posl, posr-posl));     else     {         std::string toobigsample(input.substr(posl, input.find(header,posl+1)-posl));         std::cerr  << "error: sampleset big store in 1 chunk ("                   << toobigsample.length() << " bytes):" << std::endl << std::endl                   << toobigsample;         break;     } } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -