/////////////////////////////////////////////////////////////////////////////// // // // This file is part of ModelBlocks. Copyright 2009, ModelBlocks developers. // // // // ModelBlocks is free software: you can redistribute it and/or modify // // it under the terms of the GNU General Public License as published by // // the Free Software Foundation, either version 3 of the License, or // // (at your option) any later version. // // // // ModelBlocks is distributed in the hope that it will be useful, // // but WITHOUT ANY WARRANTY; without even the implied warranty of // // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // // GNU General Public License for more details. // // // // You should have received a copy of the GNU General Public License // // along with ModelBlocks. If not, see . // // // // ModelBlocks developers designate this particular file as subject to // // the "Moses" exception as provided by ModelBlocks developers in // // the LICENSE file that accompanies this code. // // // /////////////////////////////////////////////////////////////////////////////// #ifndef _NL_STRING__ #define _NL_STRING__ #include "nl-array.h" #include #include #include #include #include #include using namespace std; //////////////////////////////////////////////////////////////////////////////// // OBSOLETE! class StringInput { private: char* psIn; static char* PS_EOS; public: static const StringInput SI_EOS; static Array strTemp; StringInput ( ) : psIn(NULL) { } StringInput ( char* ps ) : psIn(ps) { } ////StringInput ( String& s ) : psIn(s.c_array()) { } char& operator[] ( int i ) { assert(psIn); return psIn[i]; } const char operator[] ( int i ) const { assert(psIn); return psIn[i]; } StringInput& operator++ ( ) { assert(psIn); psIn++; return *this; } StringInput operator+ ( int i ) { assert(psIn); return StringInput(psIn+i); } const StringInput operator+ ( int i ) const { assert(psIn); return StringInput(psIn+i); } bool operator== ( const StringInput& si ) const { return psIn==si.psIn; } bool operator!= ( const StringInput& si ) const { return psIn!=si.psIn; } //operator bool() { return psIn!=NULL; } char* c_str() { assert(psIn); return psIn; } friend ostream& operator<< ( ostream& os, const StringInput& si ) { return os<> ( StringInput psIn, const char* psDlm ) { if (StringInput(NULL)==psIn) return psIn; int i; for (i=0; psIn[i]!='\0' && psDlm[i]!='\0'; i++) if(psIn[i]!=psDlm[i]) return StringInput(NULL); //psIn; return (psDlm[i]!='\0') ? StringInput(NULL) : (psIn[i]!='\0') ? psIn+i : SI_EOS; } friend pair operator>> ( StringInput ps, int& n ) { return pair(ps,&n); } friend StringInput operator>> ( pair delimbuff, const char* psDlm ) { if (StringInput(NULL)==delimbuff.first) return delimbuff.first; ///int i; for(i=0;psIn[i]!='\0';i++) if(psIn[i]==psDlm[i]) return psIn; return psIn+i; int j=0; StringInput psIn = delimbuff.first; if(psDlm[0]=='\0') { *delimbuff.second=atoi(psIn.c_str()); return psIn+strlen(psIn.c_str()); } for(int i=0;psIn[i]!='\0';i++) { if(psIn[i]==psDlm[j]) j++; else j=0; strTemp[i]=psIn[i]; if(j==int(strlen(psDlm))) { strTemp[i+1-j]='\0'; *delimbuff.second=atoi(strTemp.c_array()); return psIn+i+1;} } return NULL; //psIn; } friend pair operator>> ( StringInput ps, unsigned int& n ) { return pair(ps,&n); } friend StringInput operator>> ( pair delimbuff, const char* psDlm ) { if (StringInput(NULL)==delimbuff.first) return delimbuff.first; ///int i; for(i=0;psIn[i]!='\0';i++) if(psIn[i]==psDlm[i]) return psIn; return psIn+i; int j=0; StringInput psIn = delimbuff.first; if(psDlm[0]=='\0') { *delimbuff.second=atoi(psIn.c_str()); return psIn+strlen(psIn.c_str()); } for(int i=0;psIn[i]!='\0';i++) { if(psIn[i]==psDlm[j]) j++; else j=0; strTemp[i]=psIn[i]; if(j==int(strlen(psDlm))) { strTemp[i+1-j]='\0'; *delimbuff.second=atoi(strTemp.c_array()); return psIn+i+1;} } return NULL; //psIn; } friend pair operator>> ( StringInput ps, double& d ) { return pair(ps,&d); } friend StringInput operator>> ( pair delimbuff, const char* psDlm ) { if (StringInput(NULL)==delimbuff.first) return delimbuff.first; ///int i; for(i=0;psIn[i]!='\0';i++) if(psIn[i]==psDlm[i]) return psIn; return psIn+i; int j=0; StringInput psIn = delimbuff.first; if(psDlm[0]=='\0') { *delimbuff.second=atof(psIn.c_str()); return psIn+strlen(psIn.c_str()); } for(int i=0;psIn[i]!='\0';i++) { if(psIn[i]==psDlm[j]) j++; else j=0; strTemp[i]=psIn[i]; if(j==int(strlen(psDlm))) { strTemp[i+1-j]='\0'; *delimbuff.second=atof(strTemp.c_array()); return psIn+i+1;} } return NULL; //psIn; } }; char* StringInput::PS_EOS = strdup(""); const StringInput StringInput::SI_EOS = StringInput::PS_EOS; Array StringInput::strTemp ( 100 ); /* template pair operator>> ( StringInput si, T& t ) { return pair(si,&t); } */ /* template class DelimiterBuffer { //private: public: char* psIn; T& tVar; public: DelimiterBuffer ( char* ps, T& t ) : psIn(ps), tVar(t) { } //char* operator>> ( const char* psDlm ) { // int j=0; // for(int i=0;psIn[i]!='\0';i++) { // if(psIn[i]==psDlm[j]) j++; // if(j==strlen(psDlm)) { psIn[i+1-j]='\0'; psIn>>tVar; return psIn+i;} // } // return psIn; //} }; */ //////////////////////////////////////////////////////////////////////////////// class String : public Array { private: static Array strTemp; public: // Constructor / destructor methods... String ( ) : Array(1) { operator[](0)='\0'; } String ( const String& str ) : Array(str) { } explicit String ( unsigned int i ) : Array(i) { operator[](0)='\0'; } explicit String ( const char* ps ) : Array(strlen(ps)) { for(unsigned int i=0; ps[i]!='\0'; i++) Array::add()=ps[i]; Array::add()='\0'; } // Specification methods... String& ensureCapacity ( unsigned int i ) { return static_cast ( Array::ensureCapacity(i) ); } String& clear ( ) { Array::clear(); operator[](0)='\0'; return *this; } char& add ( ) { Array::add()='\0'; return set(size()-2); } friend String& operator<< ( String& str, double d ) { str.addReserve(20); uint32_t i=str.size(); uint32_t j=sprintf(str.c_array()+i-1,"%g",d); assert(j<20); str[i+j-1]='\0'; return str; } //str[i+j]=str[i+j]; return str; } friend String& operator<< ( String& str, const string& s ) { return str<& split ( Array& aps, const char* psDlm ) { aps.clear(); char* psT=NULL; for(int i=0;true;i++) { char* z=strtok_r( (0==i)?c_array():NULL, psDlm, &psT ); if (!z) break; aps[i]=z; } return aps; } // Input / output methods... friend ostream& operator<< ( ostream& os, const String& str ) { return os< operator>> ( const StringInput ps, String& s ) { return pair(ps,&s); } friend StringInput operator>> ( pair delimbuff, const char* psDlm ) { if (StringInput(NULL)==delimbuff.first) return delimbuff.first; ////assert(*delimbuff.second String::strTemp ( 100 ); #endif //_NL_STRING__