00001 #ifndef __UTIL_H
00002 #define __UTIL_H
00003
00004 #include <stdarg.h>
00005 #include <stdio.h>
00006 #include <vector>
00007 #include <string>
00008 #include <sstream>
00009 #include <fstream>
00010 #include "Exception.h"
00011
00012 #define foreach(i,v) for(typeof(v.begin()) i=v.begin(); i!=v.end(); i++)
00013 #define foreachr(i,v) for(typeof(v.rbegin()) i=v.rbegin(); i!=v.rend(); i++)
00014
00015 namespace Util {
00016
00017 template <class ValueType>
00018 ValueType maxval(ValueType v1, ValueType v2) {
00019 return ((v1>v2) ?v1 :v2);
00020 }
00021
00022 template <class ValueType>
00023 ValueType minval(ValueType v1, ValueType v2) {
00024 return ((v1<v2) ?v1 :v2);
00025 }
00026
00027
00028
00029
00030 string StringF(const char *const format,...);
00031
00032 void removeEmptyStrings(vector<string>& s);
00033
00034 string Clean(string s);
00035 void clean(string& s);
00036
00037 void clean(vector<string>& sa);
00038
00039 string chomp(const string& s);
00040
00041 string squeeze(const string& s, char tosearch = ' ');
00042
00043 string replace(const string& s, char tosearch, char toreplace);
00044
00045 string uppercase(const string& s);
00046 string toupper(const string& s);
00047
00048 string lowercase(const string& s);
00049 string tolower(const string& s);
00050
00051 vector<string> tokenize(const string& s, const string& delim = " ", bool squeeze_delim=false);
00052
00053 string trim(string in);
00054 string Trim(string in);
00055
00056 template <class ReturnType, class ValueType>
00057 ReturnType cast(const ValueType &in_value) {
00058
00059 ReturnType ret_value;
00060
00061 stringstream ss;
00062 ss << in_value;
00063 ss >> ret_value;
00064
00065 return ret_value;
00066 }
00067
00068 template <class T> bool getnext(char **nav, T& val) {
00069 if( nav!=NULL && *nav!=NULL) {
00070 val = cast<T>(strsep(nav,":"));
00071 return true;
00072 }
00073 return false;
00074 }
00075
00076 template <class T> bool getnext(vector<string>::iterator &nav, vector<string>::iterator &end, T& val) {
00077 if( nav==end )
00078 return false;
00079
00080 if( nav->length()>0 )
00081 val = cast<T>(*nav);
00082
00083 nav++;
00084 return true;
00085 }
00086
00087 template <class C>
00088 string tostring(C &container) {
00089 stringstream ss;
00090 foreach(v,container) {
00091 if( v!=container.begin() )
00092 ss << ' ';
00093 ss << (*v);
00094 }
00095 return ss.str();
00096 }
00097
00098
00099
00100
00101 bool readPipe(vector<string> &dest, string command);
00102 string runCommand(string command);
00103
00104
00105
00106
00107 string CheckDir(const string& folder);
00108
00109 const char* basename(const char* filename);
00110
00111
00112
00113
00114 template <class ReturnType>
00115 ReturnType get(istream &is) {
00116
00117 ReturnType t;
00118 Exception::Assert(is >> t, "Error while reading from stream");
00119 return t;
00120 }
00121
00122 template <class T>
00123 void readfile(vector<T> &dst, string filename) {
00124
00125
00126 dst.clear();
00127
00128
00129 ifstream ifs(filename.c_str());
00130 Exception::Assert(ifs,"Cannot open file <%s> for reading", filename.c_str());
00131
00132
00133 T dummy;
00134 while( ifs >> dummy )
00135 dst.push_back(dummy);
00136 }
00137
00138 void outProgress(int progress, int period, ostream& out = cout);
00139 void OutProgress(int progress, int period, ostream& out = cout);
00140
00141 void endProgress(int end, ostream &out = cout);
00142 void EndProgress(int end, ostream &out = cout);
00143
00144 void outProgressPercentage(int progress, int end, ostream& out = cout);
00145
00146 void endProgressPercentage(ostream& out = cout);
00147
00148 istream& readTag(istream &is, const char *tag);
00149
00150 ostream& writeTag(ostream &os, const char *tag);
00151
00152 template <class ValueType>
00153 istream& readOption(istream &is, const char *tag, ValueType &value) {
00154
00155 readTag(is,tag);
00156 Exception::Assert(is >> value, "Error while reading <%s>", tag);
00157
00158 return is;
00159 }
00160
00161 template <class ValueType>
00162 istream& readOptionArray(istream &is, const char *tag, ValueType* values, int size) {
00163
00164 readTag(is,tag);
00165 for( int i=0; i<size; i++ )
00166 Exception::Assert(is >> values[i], "Error while reading <%s>", tag);
00167
00168 return is;
00169 }
00170
00171 template <class ValueType>
00172 ostream& writeOption(ostream &os, const char *tag, ValueType &value) {
00173 Exception::Assert(os << tag << " " << value << endl, "Error while writing <%s>", tag);
00174 return os;
00175 }
00176
00177 template <class ValueType>
00178 ostream& writeOptionArray(ostream &os, const char *tag, ValueType* values, int size) {
00179 Exception::Assert(os << tag << " ", "Error while writing <%s>", tag);
00180 for( int i=0; i<size; i++ )
00181 Exception::Assert(os << values[i] << " ", "Error while writing <%s>", tag);
00182 Exception::Assert(os << endl, "Error while writing <%s>", tag);
00183
00184 return os;
00185 }
00186
00187
00188
00189
00190 template <class T>
00191 T square(T x) {
00192 return x*x;
00193 }
00194
00195 void initrand();
00196
00197 int irand(int min, int max);
00198
00199 double urand(double min, double max);
00200
00201 double grand(double mean, double std);
00202
00203 double square(double v);
00204
00205 bool firstCombination( vector<uint> &counters, uint n, uint k);
00206 bool nextCombination( vector<uint> &counters, uint n, uint k);
00207 void printCombination( vector<uint> &counters, uint n, uint k );
00208
00209 float tofloat(short val);
00210 short tofixed(float val);
00211 }
00212
00213 #endif