// COMPILE: // g++ -o ~/bin/tfs2csv2 ~/src/tfs2csv2.cpp #include #include #include using namespace std; /////////////////////////////////////////////////////// //// Declarations //// /////////////////////////////////////////////////////_____________________________________________________________________________________ stringstream sline; // Per-line string stream string temp; // Temporary string string outfile; // Output file name string csvExt(string filename); // Function to replace/add extension to .csv /////////////////////////////////////////////////////// //// Main //// /////////////////////////////////////////////////////_____________________________________________________________________________________ int main(int argc, char *argv[]) { ///// Cmd Line Args ///// if((argc != 2)&&(argc != 3)) // If infile and optionally outfile are not specified print usage and exit(1). { cout << "Usage: " << endl; cout << "\ttfs2csv [infile]" << endl; cout << "\ttfs2csv [infile] [outfile]" << endl; exit(1); } if(argc == 2) // If infile specified but no outfile, convert infile name to .csv and use as outfile { outfile = csvExt(argv[1]).c_str(); } if(argc == 3) // If infile and outfile specified, just use outfile as output { outfile = argv[2]; } ///// Open Files ///// ifstream in(argv[1]); // Open infile ofstream out(outfile.c_str()); // Open outfile ///// Conversion ///// while(in) // While in has not reached End Of File... { temp.clear(); // Ensure temp is empty - don't want to append to it getline(in,temp); // Read a line (until \n) of input file to temporary string if((temp[0]=='@')||(temp[0]=='$')) { temp.clear(); continue; // ignore lines beginning with @ or $ (eliminate blurb) } sline << temp; // Read string into stream for manipulation while(sline) // Just to make sure you don't hit the end of the line prematurely, despite the "if" later { temp.clear(); // Ensure temp is empty again sline >> temp; // Read the next word of the line into the temporary string if(temp!="*") // Ignore * before column names { out << temp; // Read the string out to the output file } if((temp!="*")&&(sline.peek() != EOF)){out << ',';} // Look one character ahead of current word - if it's the start of another word, output a comma separator else // If the next character is the end of the file... { continue; // ...break out of the while without outputting a comma (so no commas at line ends) } } sline.clear(); // Clear sline for next loop temp.clear(); // Clear temp for next loop out << endl; // Output a new line } ///// Close Files ///// in.close(); // Close input file out.close(); // Close output file } /////////////////////////////////////////////////////// //// Functions //// /////////////////////////////////////////////////////_____________________________________________________________________________________ string csvExt(string filename) // Function to automatically generate a filename from file.ext or file to file.csv { string output = filename; // Output string. Set to input string and modify (to preserve input string in case it has no .ext - see later) bool ext = false; // Boolean to set depending on whether input file is found to have an extension for(int i = output.size() - 1; i >= 0; --i) // Start at end, work backwards until '.' is found (to ignore any previous points in name) { if(output[i] == '.') // If current character is a point... { ext = true; // ...say an extension has been found... break; // ...and break from loop. } else // If not... { output.erase(i); // ...erase character. } } // Erases characters until point is found. if(ext) // If a point WAS found, we can just append 'csv' onto the end of the string. { output.append("csv"); } else // If one wasn't, we've deleted the whole string. Oops. { output = filename; // So reset it... output.append(".csv"); // ...and append '.csv'. } return output; }