1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
string TableInFile::msSeparator = " ";
TableInFile::TableInFile(const string &aFileName, vector<string> &aColumnNames, vector<string> &aColumnTypes, vector<void *> &aVectors, vector<enum OpeningMode> &aModes)
: mFileName(aFileName), mColumnNames(aColumnNames), mColumnTypes(aColumnTypes), mVectors(aVectors), mModes(aModes) {
// initialisations
fill(mPosInFile2VectorId.begin(), mPosInFile2VectorId.end(), -1); // -1 for columns to be ignored : all at init
//
if (is_file(aFileName.c_str())) { // exists ?
// Read the input file
string line, item;
mFile.open(aFileName.c_str());
int iNoComment = 0;
for(int i = 0; getline(mFile, line); i++) {
if (line[0] == '#') continue;
iNoComment++;
istringstream sline( line );
for(int j = 0; getline(sline, item); j++) {
if (iNoComment == 1) { // first line == ColumnNames
bool isFound = false; int iPos = 0; for (vector<string>::iterator ii = mColumnNames.begin(); ii != mColumnNames.end(); ii++, iPos++) if (*ii == item) {isFound = true; break;}
if (isFound) {
mPosInFile2VectorId[j] = iPos;
}
else { // this column is ignored - we don't load the data in RAM to save ressources
}
}
else if (iNoComment == 2) { // second line == Types
if (mPosInFile2VectorId[j] != -1) {
assert(item == mColumnTypes[mPosInFile2VectorId[j]]);
}
}
else if (mPosInFile2VectorId[j] != -1 && mModes[mPosInFile2VectorId[j]] != IGNORE){ // read data
if (mPosInFile2VectorId[j] == -1) continue;
if (mColumnTypes[mPosInFile2VectorId[j]] == "string") { // typeid(string).name()) {
vector<string>* p = static_cast<vector<string>*>(mVectors[mPosInFile2VectorId[j]]);
p->at(i) = item;
}
else if (mColumnTypes[mPosInFile2VectorId[j]] == "int") { // typeid(int).name()) {
vector<int>* p = static_cast<vector<int>*>(mVectors[mPosInFile2VectorId[j]]);
p->at(i) = atoi(item.c_str());
}
else if (mColumnTypes[mPosInFile2VectorId[j]] == "u_int64_t") { // typeid(u_int64_t).name()) {
vector<u_int64_t>* p = static_cast<vector<u_int64_t>*>(mVectors[mPosInFile2VectorId[j]]);
p->at(i) = strtoull(item.c_str(), NULL, 0);
}
else if (mColumnTypes[mPosInFile2VectorId[j]] == "u_int32_t") { // typeid(u_int32_t).name()) {
vector<u_int32_t>* p = static_cast<vector<u_int32_t>*>(mVectors[mPosInFile2VectorId[j]]);
p->at(i) = strtoul(item.c_str(), NULL, 0);
}
else if (mColumnTypes[mPosInFile2VectorId[j]] == "u_int16_t") { // typeid(u_int16_t).name()) {
vector<u_int16_t>* p = static_cast<vector<u_int16_t>*>(mVectors[mPosInFile2VectorId[j]]);
p->at(i) = strtoul(item.c_str(), NULL, 0);
}
else if (mColumnTypes[mPosInFile2VectorId[j]] == "u_int8_t") { // typeid(u_int8_t).name()) {
vector<u_int8_t>* p = static_cast<vector<u_int8_t>*>(mVectors[mPosInFile2VectorId[j]]);
p->at(i) = strtoul(item.c_str(), NULL, 0);
}
else if (mColumnTypes[mPosInFile2VectorId[j]] == "float") { // typeid(float).name()) {
vector<float>* p = static_cast<vector<float>*>(mVectors[mPosInFile2VectorId[j]]);
p->at(i) = atof(item.c_str()); // strtof absent !
}
else if (mColumnTypes[mPosInFile2VectorId[j]] == "double") { // typeid(double).name()) {
vector<double>* p = static_cast<vector<double>*>(mVectors[mPosInFile2VectorId[j]]);
p->at(i) = atof(item.c_str()); // strtod absent !
}
else throw std::runtime_error(string("Error : unknown type : ") + mColumnTypes[mPosInFile2VectorId[j]]);
}
}
}
mFile.close();
}
else { // new file
}
}
|