1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
/*! \file
* miscellaneous functions
*/
# include <string>
# include <string.h>
# include <stdio.h>
# include <stdlib.h>
# include "qexception.hh"
# include <iostream.h>
# include <math.h>
#ifdef NLS
# include <locale.h>
# include <libintl.h>
# define _(s) gettext (s)
#else
# define _(s) (s)
#endif
u_int32_t time2ms (char *str) {
// char a[]="7.7";
char *endptr = (char*)!0; // no-null
u_int32_t h=0,m=0,s=0,ms=0;
s = strtol(str,&endptr,10);
if (str==endptr)
throw qexception(__PRETTY_FUNCTION__,_("invalid time specification"));
str = endptr;
if (*str==':') {
str++;
m = s;
s = strtol(str,&endptr,10);
if (str==endptr)
throw qexception(__PRETTY_FUNCTION__,_("invalid time specification"));
str = endptr;
}
if (*str==':') {
str++;
h = m;
m = s;
s = strtol(str,&endptr,10);
if (str==endptr)
throw qexception(__PRETTY_FUNCTION__,_("invalid time specification"));
str = endptr;
}
// printf("%g\n",strtod(a,&endptr));
if (*str=='.') {
*--str='0';
// printf("%s\n",str);
// printf("%g %g %d\n",strtod(str,&endptr),1000*strtod(str,&endptr),(unsigned int)rint(strtod(str,&endptr)*1000LL));
ms = (u_int32_t)rint(strtod(str,&endptr)*1000LL);
// printf("%g %d\n",strtod(str,&endptr),ms);
if (str==endptr)
throw qexception(__PRETTY_FUNCTION__,_("invalid time specification"));
str=endptr;
}
if (*str)
throw qexception(__PRETTY_FUNCTION__,_("invalid time specification"));
else
return 3600000*h + 60000*m + 1000*s + ms;
}
u_int32_t time2msec (char *str) {
char *index;
char *secure;
u_int32_t milliseconds = 0;
// sure there aren't invalid chars
if (strlen(str)!=strspn(str,"0123456789:."))
throw qexception(__PRETTY_FUNCTION__,
string("invalid char in ")+string(str));
// look for milliseconds part
index = strrchr(str,'.');
if (index) {
*index=0;
milliseconds += (u_int32_t) strtod (index+1,&secure);
if (*secure!=0)
throw qexception(__PRETTY_FUNCTION__,
string("invalid char in ")+string(index+1));
}
if (!strlen(str))
return milliseconds;
// look for seconds part
index = strrchr(str,':');
if (index) {
*index=0;
milliseconds += 1000 * (u_int32_t) strtod (index+1,&secure);
if (*secure!=0)
throw qexception(__PRETTY_FUNCTION__,
string("invalid char in ")+string(index+1));
}
else {
milliseconds += 1000 * (u_int32_t) strtod (str,&secure);
if (*secure!=0)
throw qexception(__PRETTY_FUNCTION__,
string("invalid char in ")+string(index+1));
return milliseconds;
}
cerr << "time2sec: ms=" << milliseconds << endl;
// look for minutes part
index = strrchr(str,':');
if (index) {
*index=0;
milliseconds += 60 * 1000 * (u_int32_t) strtod (index+1,&secure);
if (*secure!=0)
throw qexception(__PRETTY_FUNCTION__,
string("invalid char in ")+string(index+1));
}
else {
milliseconds += 60 * 1000 * (u_int32_t) strtod (str,&secure);
if (*secure!=0)
throw qexception(__PRETTY_FUNCTION__,
string("invalid char in ")+string(index+1));
return milliseconds;
}
cerr << "time2sec: ms=" << milliseconds << endl;
// look for hours part
milliseconds += 60 * 60 * 1000 * (u_int32_t) strtod (str,&secure);
if (*secure!=0)
throw qexception(__PRETTY_FUNCTION__,
string("invalid char in ")+string(index+1));
return milliseconds;
}
string uint2string (u_int32_t ui) {
char buf[16];
sprintf(buf,"%u",ui);
return string(buf);
}
string char2string (char c) {
char buf[2];
sprintf(buf,"%c",c);
return string(buf);
}
bool strcmpn (char *str1, char *str2, u_int32_t n) {
while (n && (*str1==*str2)) {
str1++;
str2++;
n--;
}
return n==0;
}
string htmlize (string str) {
int pos;
while ((pos = str.find(" "))!=-1)
str.replace(pos,1,"%20");
return str;
}
|