00001
00006 #include "functions.h"
00007 #include <cstdlib>
00008 #include <assert.h>
00009 #include <ctype.h>
00010 #include <errno.h>
00011 #include <string.h>
00012 #include <iostream>
00013
00014
00015 double diffTimes(timeval * t1, timeval * t2) {
00016 return ((t2->tv_usec + t2->tv_sec * 1000000) - (t1->tv_usec + t1->tv_sec * 1000000)) / 1000000.0;
00017 }
00018
00019 int extractValue(const string& s, unsigned offset, const string& confFilename, int line)
00020 throw (ConfigSemanticException) {
00021
00022 if (s.length() == offset) {
00023 throw ConfigSemanticException("No value specified", confFilename, line);
00024 }
00025
00026 int x;
00027 istringstream in(s.substr(offset));
00028 in >> x;
00029
00030 if (!in.eof()) {
00031 throw ConfigSemanticException("Wrong number format", confFilename, line);
00032 }
00033
00034
00035 return x;
00036 }
00037
00038 std::string removeBlanks(string s) {
00039
00040 bool hash = 0;
00041 bool lineStarted = 0;
00042
00043
00044 for (std::string::iterator i = s.begin(); i < s.end(); i++) {
00045 if (hash || (!lineStarted && (*i == ' ' || *i == '\t' || *i == '\r'))) {
00046 s.erase(i--);
00047 } else if (*i == '#') {
00048 hash = 1;
00049 s.erase(i--);
00050 } else {
00051 lineStarted = 1;
00052 }
00053 }
00054
00055
00056 for (std::string::iterator i = s.end() - 1; i >= s.begin(); i--) {
00057 if (*i == ' ' || *i == '\t' || *i == '\r') {
00058 s.erase(i);
00059 } else {
00060 break;
00061 }
00062 }
00063
00064 int i;
00065 while ((i = s.find(" =")) != -1) {
00066 s.replace(i, 2, "=");
00067 }
00068 while ((i = s.find("= ")) != -1) {
00069 s.replace(i, 2, "=");
00070 }
00071
00072 return s;
00073 }
00074
00075 bool safe_strtoull(const char *str, uint64_t *out) {
00076
00077 errno = 0;
00078 *out = 0;
00079 char *endptr;
00080 unsigned long long ull = strtoull(str, &endptr, 10);
00081 if (errno == ERANGE){
00082 return false;
00083 }
00084 if (isspace(*endptr) || (*endptr == '\0' && endptr != str)) {
00085 if ((long long) ull < 0 && strchr(str, '-') != NULL) {
00086 return false;
00087 }
00088 *out = ull;
00089 return true;
00090 }
00091 return false;
00092 }
00093
00094 std::string pend_spaces_2_numb(unsigned long long val, unsigned count, bool append) {
00095
00096 int digits = 0;
00097 unsigned long long tmp = val;
00098 while (tmp / 10) {
00099 digits++;
00100 tmp /= 10;
00101 }
00102 if (tmp > 0)
00103 digits++;
00104
00105 if (val == 0)
00106 digits = 1;
00107
00108 std::ostringstream o;
00109
00110 if (append) {
00111 o << val;
00112 for (unsigned k = 0; k < count - digits; k++)
00113 o << ' ';
00114 } else {
00115 for (unsigned k = 0; k < count - digits; k++)
00116 o << ' ';
00117 o << val;
00118 }
00119
00120 return o.str();
00121 }
00122
00123 template<typename T, typename L>
00124 ostream & operator<<(ostream & out, pair<T, L> & p) {
00125 return cout << "(" << p.first << ", " << p.second << ")";
00126 }
00127
00128 ostream & operator<<(ostream & out, map<int, char> & mymap) {
00129
00130 for (map<int, char>::iterator it = mymap.begin(); it != mymap.end(); it++) {
00131 if (it->second) {
00132 cout << " " << it->first << "->" << (int) it->second << " ";
00133 }
00134 }
00135
00136 return out;
00137 }
00138
00139 string configurationDirectory(char * directory) {
00140
00141 static char * confDirectory = NULL;
00142
00143 if (directory) {
00144 confDirectory = directory;
00145 }
00146
00147 if (confDirectory) {
00148 return confDirectory;
00149 } else {
00150 return string(CONFDIR);
00151 }
00152 }
00153
00154 string getPackageName() {
00155 return string(PACKAGE);
00156 }