Util.cpp

Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include <time.h>
00003 #include "Util.h"
00004 
00005 namespace Util {  
00006 
00007   //---------------------
00008   // STRING METHODS
00009 
00010   string StringF(const char *const format,...) {
00011     va_list arglist;
00012     
00013     char mex[10000];
00014     va_start(arglist,format);
00015     vsprintf(mex,format,arglist);
00016     
00017     return mex;
00018   }
00019 
00020 
00021   void removeEmptyStrings(vector<string>& s) {
00022     //removes empty Strings like: "";
00023     for(vector<string>::iterator k = s.begin(); k != s.end();){
00024       if(*k == ""){
00025         vector<string>::iterator m = k;
00026         k++;
00027         s.erase(m);
00028       }
00029       else
00030         k++;
00031     }
00032   } 
00033 
00034   string Clean(string s) {
00035     char *r = new char[s.length()];
00036 
00037     unsigned int c = 0;
00038     for(unsigned int i=0; i<s.length(); i++ ) {
00039       if( s[i]!=' ' && s[i]!='\n' && s[i]!='\t' ) {
00040         r[c++] = s[i];
00041       }
00042     }
00043     r[c] = '\0';
00044     
00045     return r;
00046   }
00047 
00048   void clean(string& s) {
00049     for(string::iterator k = s.begin(); k != s.end();){
00050       if((*k) == ' ' || (*k)=='\n' || (*k)=='\t' ){
00051         string::iterator m = k;
00052         k++;
00053         s.erase(m);
00054       }
00055       else
00056         k++;
00057     }
00058   }
00059 
00060   void clean(vector<string>& sa) {  
00061     for(unsigned int i = 0; i < sa.size(); i++)
00062       clean(sa[i]);
00063   }
00064 
00065   string chomp(const string& s) {
00066     if( s.substr(s.length()-1,1)=="\n" )
00067       return s.substr(0,s.length()-1);
00068     return s;
00069   }
00070 
00071   string squeeze(const string& s, char tosearch) {
00072     string s2 = "";
00073     
00074     char last = 0;
00075     for(unsigned int i = 0; i < s.size(); i++){
00076       if( i==0 || s[i]!=tosearch || last!=tosearch ) 
00077         s2 += s[i];
00078       last = s[i];
00079     }
00080     return s2;    
00081   }
00082   
00083   string replace(const string& s, char tosearch, char toreplace) {
00084     string s2 = "";
00085     for(unsigned int i = 0; i < s.size(); i++){
00086       if( s[i]==tosearch)
00087         s2 += toreplace;
00088       else
00089         s2 += s[i];
00090     }
00091     return s2;
00092   }
00093   
00094   string uppercase(const string& s) {
00095     string s2 = "";
00096     for(unsigned int i = 0; i < s.size(); i++){
00097       if(isalpha(s[i]))
00098         s2 += ::toupper(s[i]);
00099       else
00100         s2 += s[i];
00101     }
00102     return s2;
00103   }
00104 
00105   string toupper(const string& s) {
00106     return uppercase(s);
00107   }
00108 
00109   string lowercase(const string& s) {
00110     string s2 = "";
00111     for(unsigned int i = 0; i < s.size(); i++){
00112       if(isalpha(s[i]))
00113         s2 += ::tolower(s[i]);
00114       else
00115         s2 += s[i];
00116     }
00117     return s2;
00118   }
00119 
00120   string tolower(const string& s) {
00121     return lowercase(s);
00122   }
00123                      
00124   vector<string> tokenize(const string& s, const string& delim, bool squeeze_delim) {
00125     vector<string> tokenized;
00126     
00127     char *str = new char[s.length()+1];
00128     strcpy(str,s.c_str());
00129     for( char *nav = str; nav!=NULL; ) {
00130       char *token = strsep(&nav,delim.c_str());
00131       if( '\0'==token[0] && squeeze_delim ) 
00132         continue;
00133       tokenized.push_back(token);
00134     }
00135     delete str;
00136     return tokenized;
00137   }
00138 
00139   string trim(string in) {
00140     unsigned int i,l;
00141     for( i=0; i<in.size() && (in.c_str()[i]==' ' || in.c_str()[i]=='\t'); i++ );
00142     for( l=in.size()-1; l>=0 && (in.c_str()[l]==' ' || in.c_str()[l]=='\t'); l-- );
00143     return in.substr(i,l-i+1);
00144   }  
00145 
00146   string Trim(string in) {
00147     return trim(in);
00148   }    
00149 
00150  
00151   //-------------------
00152   // PIPE METHODS
00153 
00154   bool readPipe(vector<string> &dest, string command) {     
00155     FILE *fp = popen(command.c_str(), "r");
00156     if( fp==NULL ) 
00157       return false;
00158 
00159     size_t read = 0;
00160     char *buffer = NULL;
00161     while( getline(&buffer,&read,fp)!=-1 )      
00162       dest.push_back(trim(chomp(buffer)));
00163       
00164     pclose(fp);
00165     return true; 
00166   }
00167 
00168   string runCommand(string command) {     
00169     FILE *fp = popen(command.c_str(), "r");
00170     if( fp==NULL ) 
00171       return false;
00172 
00173     size_t read = 0;
00174     char *buffer = NULL;
00175     stringstream ss;
00176     while( getline(&buffer,&read,fp)!=-1 ) {
00177       ss << buffer;
00178     }
00179       
00180     pclose(fp);
00181     return ss.str(); 
00182   }
00183   //-------------------
00184   // FILENAME METHODS
00185   
00186   string CheckDir(const string& folder) {
00187     if( folder.size()==0 )
00188       return "/";
00189     if( folder[folder.size()-1]=='/' )
00190       return folder;
00191     return folder+'/';
00192   }
00193   
00194   const char* basename(const char* filename) {
00195     string buf = filename;
00196     unsigned int offset = buf.find_last_of("/");
00197     if(offset != string::npos && offset < buf.size()-1)
00198       buf = buf.substr(offset+1);
00199     return buf.c_str();
00200   }
00201 
00202 
00203   //------------------
00204   // STREAM METHODS
00205   
00206   void outProgress(int progress, int period, ostream& out) {
00207     if( ((progress+1)%period)==0 ) {
00208       out << StringF( "%d",progress+1 );
00209       out.flush();
00210     }
00211     else if( ((progress+1)%period)==(period/2) ) {
00212       out << ".";
00213       out.flush();
00214     }
00215   }
00216 
00217   void OutProgress(int progress, int period, ostream& out) {
00218     outProgress(progress,period,out);
00219   }
00220 
00221 
00222   void endProgress(int end, ostream &out) {
00223     out << StringF("..%d\n",end);
00224     out.flush();
00225   }
00226 
00227   void EndProgress(int end, ostream &out) {
00228     endProgress(end,out);
00229   }
00230 
00231   void outProgressPercentage(int progress, int end, ostream& out) {
00232     double p = (double)(progress*100)/(double)end;
00233     int ip   = (int)p;
00234     bool isint = (((double)ip)==p);
00235 
00236     if( isint ) {
00237       if( (ip%10)==0 ) 
00238         out << p << "." << flush;
00239       else if( (ip%10)==5 ) 
00240         out << "." << flush;
00241     }
00242   }
00243   
00244   void endProgressPercentage(ostream& out) {
00245     out << ".100%" << flush;
00246   }
00247 
00248   
00249   istream& readTag(istream &is, const char *tag) {
00250   
00251     string read_tag;
00252     Exception::Assert(is >> read_tag, "Error while reading <%s>", tag);
00253     Exception::Assert(read_tag==tag,  "Error while reading <%s>: unexpected value <%s>", tag, read_tag.c_str()); 
00254     
00255     return is;
00256   }
00257 
00258   ostream& writeTag(ostream &os, const char *tag) {
00259   
00260     Exception::Assert(os << tag << endl, "Error while writing <%s>", tag);
00261     
00262     return os;
00263   }
00264 
00265   //------------------
00266   // MATH METHODS
00267 
00268   void initrand() {
00269     srand(time(NULL));
00270   }
00271 
00272   int irand(int min, int max) {
00273     return (min + (rand()%(max-min+1)));
00274   }
00275   
00276   double urand(double min, double max) {
00277     double p = (double)rand()/(double)RAND_MAX;
00278     return (p*(max-min)+min);
00279   }
00280 
00281   double grand(double mean, double std) {
00282     double x = 0.;    
00283     for( int i=0; i<30; i++ ) 
00284       x += urand(0.,1.);
00285     
00286     return (0.6325*x-9.487);
00287   }
00288 
00289   double square(double v) {
00290     return v*v;
00291   }
00292 
00293   short tofixed(float v) {
00294     return (short)(v*16384.);
00295   }
00296 
00297   float tofloat(short v) {
00298     return ((float)v/16384.);
00299   }
00300 
00301   bool firstCombination( vector<uint> &counters, uint n, uint k) {
00302     if( k>n )
00303       return false;
00304     
00305     if( k==0 ) {
00306       counters.clear();
00307       return true;
00308     }
00309 
00310     counters.resize(k);
00311     for( uint i=0; i<k; i++ )
00312       counters[i] = i;
00313     return true;
00314   }
00315   
00316   bool nextCombination( vector<uint> &counters, uint n, uint k) {
00317     if( k==0 )
00318       return false;
00319 
00320     // find first counter to update
00321     uint c = k-1;
00322     for(; c>0 && counters[c]>=n+c-k; c--);
00323     if( c==0 && counters[c]==n-k ) {
00324       // last combination reached
00325       return false;
00326     }
00327 
00328     // update counters
00329     for(uint v = counters[c]+1; c<k; c++, v++ )
00330       counters[c] = v;
00331     
00332     return true;
00333   }
00334   
00335   void printCombination( vector<uint> &counters, uint n, uint k ) {
00336     cout << StringF("(%d/%d) ", n, k);
00337     for( uint i=0; i<k; i++ )
00338       cout << counters[i] << " ";
00339     cout << endl;
00340   }  
00341  
00342 
00343 }

Generated on Fri Sep 7 16:29:17 2007 for SVM-Dlight by  doxygen 1.5.1