File indexing completed on 2023-03-17 10:38:45
0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "Alignment/CocoaUtilities/interface/ALIFileIn.h"
0010
0011 #include <cstdlib>
0012 #include <strstream>
0013
0014
0015
0016
0017 std::vector<ALIFileIn*> ALIFileIn::theInstances;
0018
0019
0020
0021
0022 ALIFileIn& ALIFileIn::getInstance(const ALIstring& filename) {
0023 for (auto vfc : theInstances) {
0024 if (vfc->name() == filename) {
0025 return *vfc;
0026 }
0027 }
0028
0029 ALIFileIn* instance = new ALIFileIn(filename);
0030 instance->theCurrentFile = -1;
0031 instance->openNewFile(filename.c_str());
0032 theInstances.push_back(instance);
0033
0034 return *instance;
0035 }
0036
0037
0038 void ALIFileIn::openNewFile(const char* filename) {
0039 theCurrentFile++;
0040 std::ifstream* fin = new std::ifstream(filename);
0041 theFiles.push_back(fin);
0042
0043
0044
0045 theLineNo.push_back(0);
0046
0047 theNames.push_back(filename);
0048
0049 #ifndef OS_SUN_4_2
0050 if (!fin->is_open()) {
0051 std::cerr << "!!!! Input file does not exist: " << filename << std::endl;
0052 exit(1);
0053 }
0054 #endif
0055 }
0056
0057
0058
0059
0060 ALIFileIn& ALIFileIn::getInstanceOpened(const ALIstring& filename) {
0061 ALIFileIn& filein = ALIFileIn::getInstance(filename);
0062 if (filein.name() != filename) {
0063 std::cerr << "Error: file not opened yet " << filename << std::endl;
0064 exit(0);
0065 } else {
0066 return filein;
0067 }
0068 }
0069
0070
0071
0072
0073
0074 ALIint ALIFileIn::getWordsInLine(std::vector<ALIstring>& wordlist) {
0075 ALIint isok = 1;
0076
0077
0078
0079
0080 ALIint wsiz = wordlist.size();
0081 ALIint ii;
0082 for (ii = 0; ii < wsiz; ii++) {
0083 wordlist.pop_back();
0084 }
0085
0086
0087 const ALIint NMAXLIN = 1000;
0088 char ltemp[NMAXLIN];
0089 for (;;) {
0090 (theLineNo[theCurrentFile])++;
0091 for (ii = 0; ii < NMAXLIN; ii++)
0092 ltemp[ii] = ' ';
0093 theFiles[theCurrentFile]->getline(ltemp, NMAXLIN);
0094
0095 ALIint ii;
0096 for (ii = 0; ii < NMAXLIN; ii++) {
0097 if (ltemp[ii] == '\0')
0098 break;
0099 }
0100 if (ii == NMAXLIN - 1) {
0101 ErrorInLine();
0102 std::cerr << "!!!! line longer than " << NMAXLIN << " characters" << std::endl
0103 << " please split it putting a '\\' at the end of line" << std::endl;
0104 exit(0);
0105 }
0106
0107
0108
0109 if (eof()) {
0110
0111 return 0;
0112 }
0113
0114
0115 std::istrstream istr_line(ltemp);
0116
0117
0118 ALIint NoWords = 0;
0119 char* tt = ltemp;
0120 ALIstring stemp(ltemp);
0121 do {
0122 if (*tt != ' ' && *(tt) != '\0') {
0123 if (tt == ltemp) {
0124 NoWords++;
0125
0126 } else if (*(tt - 1) == ' ' || *(tt - 1) == '\015' || *(tt - 1) == '\t') {
0127 NoWords++;
0128
0129 }
0130 }
0131 tt++;
0132 } while (*tt != '\0' && stemp.length() != 0);
0133 ALIstring stempt(ltemp);
0134 if (stempt.length() == 0)
0135 NoWords = 0;
0136
0137
0138
0139 for (ii = 0; ii < NoWords; ii++) {
0140 ALIstring stemp = "";
0141 istr_line >> stemp;
0142 if (stemp.length() == 0)
0143 break;
0144 ALIint comment = stemp.find(ALIstring("//"));
0145
0146 if (comment == 0) {
0147 break;
0148 } else if (comment > 0) {
0149 stemp = stemp.substr(0, comment);
0150 wordlist.push_back(stemp);
0151 break;
0152
0153 }
0154 wordlist.push_back(stemp);
0155 }
0156
0157
0158
0159
0160
0161
0162
0163
0164 if (!wordlist.empty()) {
0165 if ((*(wordlist.end() - 1)) == "\\") {
0166 wordlist.pop_back();
0167 } else {
0168 break;
0169 }
0170 }
0171 }
0172
0173
0174
0175
0176
0177
0178
0179 if (wordlist[0] == "#include") {
0180 if (wordlist.size() != 2) {
0181 ErrorInLine();
0182 std::cerr << "'#include' should have as second argument the filename " << std::endl;
0183 exit(0);
0184 }
0185
0186 openNewFile(wordlist[1].c_str());
0187 isok = getWordsInLine(wordlist);
0188 }
0189
0190 return isok;
0191 }
0192
0193
0194
0195
0196 void ALIFileIn::ErrorInLine() {
0197 std::cerr << "!! EXITING: ERROR IN LINE No " << theLineNo[theCurrentFile] << " file: " << theNames[theCurrentFile]
0198 << " : ";
0199 }
0200
0201 ALIbool ALIFileIn::eof() {
0202 ALIbool isok = theFiles[theCurrentFile]->eof();
0203 if (isok) {
0204
0205 theCurrentFile--;
0206 if (theCurrentFile != -1)
0207 close();
0208 }
0209
0210
0211 if (theCurrentFile != -1) {
0212 return false;
0213 } else {
0214 return isok;
0215 }
0216 }
0217
0218 void ALIFileIn::close() {
0219
0220
0221
0222
0223
0224
0225 theFiles[theCurrentFile + 1]->close();
0226 theFiles.pop_back();
0227
0228 }