DSQSS  1.1
io.h
説明を見る。
00001 
00002 #ifndef IO_H
00003 #define IO_H
00004 
00005 //######################################################################
00006 
00007 #include <stdlib.h>
00008 #include <stdio.h>
00009 #include <string.h>
00010 #include <iostream>
00011 #include <fstream>
00012 #include <sstream>
00013 #include <cstdlib>
00014 
00015 #include "array.h"
00016 
00017 #define BLEN 256
00018 
00019 //######################################################################
00020 
00021 using namespace std;
00022 
00023 string EOL = "_E_O_L_";
00024 
00025 //======================================================================
00026 
00027 inline void reform_end_of_line(string& line) {
00028   int n = line.size();
00029   const char* a = line.c_str();
00030   if ( a[n-1] == 13 ) { // 13 stands for ^M
00031     line.replace(n-1,1,1,'\n');
00032   }
00033 }
00034 
00035 //======================================================================
00036 
00037 inline int line_split(char* line, string* w) {
00038   string s(line);
00039   istringstream ist(s);
00040   int nw = 0;
00041   while ( ist >> w[nw++] );
00042   nw--;
00043   return nw;
00044 }
00045 
00046 //======================================================================
00047 
00048 inline void get_line(FILE* F, char* line) {
00049   char buff[256];
00050   strcpy(buff,"");
00051   strcpy(line,"");
00052   fgets(buff,100,F);
00053   if (buff[0]==0 || buff[0]==13 || buff[0]==10) return;
00054   char* pch=strchr(buff,'#');
00055   if (pch != NULL) {
00056     int len=pch-buff;
00057     if (len > 0) {
00058       strncat(line,buff,len);
00059       strcat(line,"\n");
00060     }
00061   } else {
00062     strcat(line,buff);
00063   }
00064 }
00065 
00066 //======================================================================
00067 
00068 inline void get_nbl(FILE* F, char* line) { // get the next non-blank line
00069   strcpy(line,"");
00070   while( ! strcmp(line,"") ) {
00071     if ( feof(F) ) break;
00072     get_line(F,line);
00073 //  printf("%s\n",line);
00074   }
00075 }
00076 
00077 //======================================================================
00078 
00079 inline int break_into_words(char* line, char* delim, char** word) { 
00080   char* last = line + strlen(line) - 1;
00081 //printf( "line = '%s'\n", line);
00082 //printf( "delimiter = '%s'\n", delim);
00083   char* w = line;
00084   int n = 0;
00085   while ( w != last ) {
00086     while ( w == strpbrk( w, delim) ) w++;
00087     char* p = strpbrk( w, delim);
00088     if ( p == 0 ) p = last;
00089 //printf("\nw= %s", w);
00090 //printf("p= %d, (*p) = '%c' (%d)\n", p, *p, *p);
00091     strncpy(word[n], w, p-w);
00092     strcat(word[n], "\0");
00093 //printf("word[%d] = '%s'\n", n, word[n]);
00094     n++;
00095     w = p;
00096   }    
00097   return n;
00098 }
00099 
00100 //######################################################################
00101 
00102 class FileReader {
00103 
00104 private:
00105 
00106   char NAME[BLEN];
00107   char LINE[BLEN];
00108   int IL;
00109   int NW;
00110   string WORD[BLEN];
00111   ifstream INS;
00112   streampos top;
00113   streampos mark;
00114 
00115 public:
00116 
00117   void open(const char* name) {
00118     strcpy(NAME,name);
00119     INS.open(NAME);
00120     top = INS.tellg();
00121     if (!INS) { printf("FILEIO::FILEIO> Error.\n"); exit(0); };
00122     IL = 0;
00123   };
00124 
00125   FileReader() {};
00126 
00127   FileReader(char* name) {
00128     open(name);
00129   };
00130 
00131   void rewind() {
00132     INS.clear();
00133     INS.seekg(top);
00134   };
00135 
00136   void set_mark() {
00137     mark = INS.tellg();
00138   };
00139 
00140   void goto_mark() {
00141     INS.clear();
00142     INS.seekg(mark);
00143   };
00144 
00145   bool read() {
00146     bool b = INS.getline( LINE, BLEN);
00147     if ( b ) { IL++; }
00148     return b;
00149   };
00150 
00151   char* line() { return LINE; };
00152 
00153   int split() {
00154     NW = line_split( LINE, WORD);
00155     return NW;
00156   };
00157 
00158   string& word(int i) {
00159     if ( i<0 && i>=NW ) {
00160       printf("FileReader::word> Error.\n"); 
00161       exit(0);
00162     }
00163     return WORD[i]; 
00164   };
00165 
00166   int as_int(int i) {
00167     return atoi(WORD[i].c_str());
00168   };
00169 
00170   double as_double(int i) {
00171     return (double)atof(WORD[i].c_str());
00172   };
00173 
00174   void show() {
00175     cout << LINE << endl;
00176   };
00177 
00178   void dump() {
00179     cout << NAME << "[" << IL << "]> ";
00180     for (int i=0; i<NW; i++) {
00181       cout << " " << WORD[i];
00182     }
00183     cout << endl;
00184   };
00185 
00186   string get(char* key);
00187 
00188   int makeIndex( 
00189     const char* scope, const char* field, const char* k0, Array<int>& index );
00190 
00191   void getWordList(int& NW, string*& W);
00192 
00193 };
00194 
00195 //======================================================================
00196 
00197 inline void FileReader::getWordList(int& NW, string*& W) {
00198 
00199   NW = 0;
00200   rewind();
00201 
00202   while(read()) NW += split(); 
00203   W = new string[NW+1];
00204   int iw = 0;
00205   rewind();
00206   while(read()) {
00207     int nw = split();
00208     for (int i=0; i<nw; i++) {
00209       W[iw] = word(i);
00210       iw++;
00211     }
00212   }
00213   W[NW] = EOL;
00214 
00215 }
00216 
00217 //======================================================================
00218 
00219 inline string FileReader::get(char* key) {
00220   string ans;
00221   char key_open[BLEN];
00222   char key_close[BLEN];
00223   strcpy( key_open, "<");
00224   strcat( key_open, key);
00225   strcat( key_open, ">");
00226   strcpy( key_close, "</");
00227   strcat( key_close, key);
00228   strcat( key_close, ">");
00229   printf(" %s %s\n", key_open, key_close);
00230   rewind();
00231   bool active = false;
00232   while(read()) {
00233     char* l = line();
00234     //    int nw = split();
00235     string keyt = word(0);
00236     if (keyt == key_close) active = false;
00237     if (active) {
00238       string s = l;
00239       reform_end_of_line(s);
00240       ans += s;
00241     }
00242     if (keyt == key_open) active = true;
00243   }
00244   return ans;
00245 }
00246 
00247 //======================================================================
00248 
00249 inline int FileReader::makeIndex( const char* scope, const char* field, const char* k0, Array<int>& index ) {
00250 
00251 //  printf("FileReader::makeIndex> start.\n");
00252 
00253   string key(k0);
00254   string close = "</";
00255   close += scope;
00256   close += ">";
00257   string activate = "<";
00258   activate += field;
00259   activate += ">";
00260   string inactivate = "</";
00261   inactivate += field;
00262   inactivate += ">";
00263 
00264 //  printf(" close= %s\n", close.c_str());
00265 //  printf(" activate= %s\n", activate.c_str());
00266 //  printf(" inactivate= %s\n", inactivate.c_str());
00267 
00268   int n=0;
00269   set_mark();
00270   bool active = false;
00271   while( read() ) {
00272 //show();
00273     int nw = split();
00274     if ( nw == 0 ) continue;
00275     string k = word(0);
00276     if ( k == close ) break;
00277     if ( k == inactivate ) active = false;
00278     if ( active ) {
00279       if ( k == key ) {
00280 //printf(" %s:%s:%s> %s\n", scope, field, k0, LINE);
00281         n++;
00282       }
00283     }
00284     if ( k == activate ) active = true;
00285   }
00286 
00287   index.init(1,n);
00288 
00289   int i=0;
00290   goto_mark();
00291   active = false;
00292   while(read()) {
00293     int nw = split();
00294     if ( nw == 0 ) continue;
00295     string k = word(0);
00296     if ( k == close ) break;
00297     if ( k == inactivate ) active = false;
00298     if (active) {
00299       if ( k == key ) {
00300         index[i] = as_int(1);
00301         i++;
00302       }
00303     }
00304     if ( k == activate ) active = true;
00305   }
00306 
00307   goto_mark();
00308 
00309 //  printf("FileReader::makeIndex> end.\n");
00310 
00311   return n;
00312 }
00313 
00314 #endif
 全て クラス ネームスペース ファイル 関数 変数 型定義 列挙型の値 フレンド マクロ定義