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