00001
00002
00003
00004
00005
00006
00007
00008 #include <string>
00009 #include <iostream>
00010 #include <fstream>
00011
00012 using namespace std;
00013
00014 #define LF_CHAR 10
00015 #define CR_CHAR 13
00016
00017 void crlf2lf(char *fileName, bool doVerbose = true)
00018 {
00019 ifstream fin;
00020 unsigned hits = 0;
00021
00022 fin.open(fileName);
00023 if (fin.is_open()) {
00024 ofstream fout;
00025 streambuf *pIn;
00026 char* buffer = new char[1000000];
00027 char* ptr = buffer;
00028 int len = 0;
00029
00030 pIn = fin.rdbuf();
00031
00032 while (pIn->sgetc() != EOF) {
00033 char chr = pIn->sbumpc();
00034 if (chr != CR_CHAR) {
00035 *(ptr++) = chr;
00036 len++;
00037 }
00038 else
00039 hits++;
00040 }
00041 fin.close();
00042
00043 if (hits) {
00044 ptr = buffer;
00045 fout.open(fileName);
00046 for (;len>0;len--) fout << *(ptr++);
00047 fout.close();
00048 }
00049 delete [] buffer;
00050 }
00051 else {
00052 cerr << "Warning: Could not find file " << fileName << endl;
00053 }
00054 if (doVerbose && hits) {
00055 clog << fileName << ": "<< hits << " CRs removed"<< endl;
00056 }
00057 }
00058
00059 int main(int argc, char** argv)
00060 {
00061 if (argc > 1) {
00062 for (int i=1; i<argc; i++) crlf2lf(argv[i]);
00063 }
00064 else {
00065 cerr << "Removes the CR chars from an arbitrary file" << endl;
00066 cerr << "Usage: crlf2cr file1 ..." << endl;
00067 }
00068 return 0;
00069 }