So we basically want to read a text file consisting of some different segments to our program:
the structure in the program is a cache with: pair data> >
the structure in the file is (were key is used as both a key and a delimiter between segments)
key
headerKey : headerValue
headerKey : headerValue
......................
headerKey : headerValue
key
data
data
...
data
key
We have been trying to read this using the following, but it doesnt read the date format (RFC1123). we only get the dates in headerValues as "08 Gmt" or similar "XX gmt". What is wrong in our reading algorithm, below is that we are using : as a delimiter but it appears in the date format in different meaning, i.e. segmenting the time:
try{
// Create stream
ifstream ifs(this->cacheFile.c_str(), ios::binary);
// Read file to cache if stream is good
if(ifs.good()){
while (! ifs.eof() ){
map<string,string> headerPairs;
string tmp;
string key;
string data;
getline(ifs, tmp);
while(tmp.empty()){
getline(ifs, tmp);
cout << "Empty line..." << "\n";
if(ifs.eof()){
cout << "End of File.."<< "\n";
break;
}
}
//After empty lines get "Key"
key = tmp;
getline(ifs, tmp);
//Get segment of header pairs
while(tmp != key){
StringTokenizer headerPair(tmp, ":", StringTokenizer::TOK_TRIM);
//StringTokenizer::Iterator it = headerPair.begin();
std::cout << *(headerPair.begin()) <<": " << *(headerPair.end()-1)<< std::endl;
string headerKey = *(headerPair.begin());
string headerValue = *(headerPair.end()-1);
headerPairs.insert(make_pair(headerKey, headerValue));
getline(ifs, tmp);
}
cout << "Added " << headerPairs.size() << " header pairs from cache" << "\n";
//tmp equals Key
while(tmp!=key){
getline(ifs, tmp);
cout << "Searching for header->data delimiter" << "\n";
}
cout << "Found header->data delimiter" << "\n";
//Get segment of data!
getline(ifs, tmp);
while(tmp != key){
data+=tmp;
getline(ifs, tmp);
}
cout << "DATA: " << data << "\n";
cout << "Ending delimiter:" << tmp << "\n";
this->add(key,make_pair(headerPairs, data));
cout << "Added: " << key << " to memory-cache" << endl;
}
ifs.close();
}
}
catch (Exception &ex){
cerr << ex.displayText() << endl;
}
Please suggest a better way of getting the date string:
DateTime now : Mon, 29 Apr 2013 08:15:57 GMT
DateRetrieved from file: 57 GMT
In short: The problem is that we are using a : as a delimiter for the headers, i would like suggestions for another delimiter sign that is failsafe, i.e. it wont be found in the HTTP 1.0 or 1.1 Headers.