| 1 |
|
|---|
| 2 |
|
|---|
| 3 | /********************************************************************\
|
|---|
| 4 |
|
|---|
| 5 | Name: Utilities.cc
|
|---|
| 6 |
|
|---|
| 7 | Created by: Sebastian Commichau, July 2008
|
|---|
| 8 | commichau@phys.ethz.ch
|
|---|
| 9 |
|
|---|
| 10 | Content: Provides numerous utilities
|
|---|
| 11 |
|
|---|
| 12 | \********************************************************************/
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 | #include "Utilities.h"
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 | // Parse user input
|
|---|
| 19 | void ParseInput(char* Pc, int* NParam, char Param[][MAX_COM_SIZE])
|
|---|
| 20 | {
|
|---|
| 21 | int i;
|
|---|
| 22 | *NParam = 0;
|
|---|
| 23 |
|
|---|
| 24 | for (i = 0; i<10; i++)
|
|---|
| 25 | Param[i][MAX_COM_SIZE] = 0;
|
|---|
| 26 |
|
|---|
| 27 | while (*Pc == ' ') // Ignore leading blanks
|
|---|
| 28 | Pc++;
|
|---|
| 29 |
|
|---|
| 30 | do {
|
|---|
| 31 | if (*Pc == '"') {
|
|---|
| 32 | Pc++;
|
|---|
| 33 | for (i = 0; *Pc && *Pc != '"'; i++)
|
|---|
| 34 | Param[*NParam][i] = *Pc++;
|
|---|
| 35 | if (*Pc)
|
|---|
| 36 | Pc++;
|
|---|
| 37 | } else if (*Pc == '\'') {
|
|---|
| 38 | Pc++;
|
|---|
| 39 | for (i = 0; *Pc && *Pc != '\''; i++)
|
|---|
| 40 | Param[*NParam][i] = *Pc++;
|
|---|
| 41 | if (*Pc)
|
|---|
| 42 | Pc++;
|
|---|
| 43 | } else if (*Pc == '`') {
|
|---|
| 44 | Pc++;
|
|---|
| 45 | for (i = 0; *Pc && *Pc != '`'; i++)
|
|---|
| 46 | Param[*NParam][i] = *Pc++;
|
|---|
| 47 | if (*Pc)
|
|---|
| 48 | Pc++;
|
|---|
| 49 | } else
|
|---|
| 50 | for (i = 0; *Pc && *Pc != ' '; i++)
|
|---|
| 51 | Param[*NParam][i] = *Pc++;
|
|---|
| 52 | Param[*NParam][i] = 0;
|
|---|
| 53 | while (*Pc == ' ' || *Pc == '\r' || *Pc == '\n')
|
|---|
| 54 | Pc++;
|
|---|
| 55 | (*NParam)++;
|
|---|
| 56 | } while (*Pc);
|
|---|
| 57 |
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 | // Check if two strings match
|
|---|
| 62 | int Match(char *str, char *cmd)
|
|---|
| 63 | {
|
|---|
| 64 | int i;
|
|---|
| 65 |
|
|---|
| 66 | if(str[0] == '\r' || str[0] == '\n')
|
|---|
| 67 | return 0;
|
|---|
| 68 |
|
|---|
| 69 | for(i = 0; i < (int) strlen(str); i++)
|
|---|
| 70 | {
|
|---|
| 71 | if(toupper(str[i]) != toupper(cmd[i]) && str[i] != '\r' && str[i] != '\n')
|
|---|
| 72 | return 0;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | return 1;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 | // Remove newline from string
|
|---|
| 80 | char* Chop(char str[]) {
|
|---|
| 81 |
|
|---|
| 82 | char *ptr;
|
|---|
| 83 |
|
|---|
| 84 | if ((ptr = strchr(str, '\n')) != NULL)
|
|---|
| 85 | *ptr = '\0';
|
|---|
| 86 |
|
|---|
| 87 | return str;
|
|---|
| 88 |
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 | int MakeDir(char *Dir, char *RunDate) {
|
|---|
| 93 |
|
|---|
| 94 | char str[MAX_COM_SIZE];
|
|---|
| 95 |
|
|---|
| 96 | sprintf(str,"%s/%s",Dir,RunDate);
|
|---|
| 97 |
|
|---|
| 98 | struct stat buf;
|
|---|
| 99 |
|
|---|
| 100 | if (stat(str, &buf))
|
|---|
| 101 | return mkdir(str, 0711);
|
|---|
| 102 |
|
|---|
| 103 | return 0;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 | void GetUTCSecondsOfDay(double* t) {
|
|---|
| 109 |
|
|---|
| 110 | struct tm * timeinfo;
|
|---|
| 111 | time_t rawtime;
|
|---|
| 112 |
|
|---|
| 113 | struct timezone tz;
|
|---|
| 114 | struct timeval actual_time; // Actual time
|
|---|
| 115 |
|
|---|
| 116 | gettimeofday(&actual_time, &tz);
|
|---|
| 117 |
|
|---|
| 118 | time(&rawtime);
|
|---|
| 119 |
|
|---|
| 120 | timeinfo = gmtime(&rawtime); // Get UTC (or GMT timezone).
|
|---|
| 121 |
|
|---|
| 122 | *t = (timeinfo->tm_hour*3600 + timeinfo->tm_min*60 + timeinfo->tm_sec) + actual_time.tv_usec/1000000.;
|
|---|
| 123 |
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 | void GetLocalSecondsOfDay(double* t) {
|
|---|
| 128 |
|
|---|
| 129 | struct tm * timeinfo;
|
|---|
| 130 | time_t rawtime;
|
|---|
| 131 |
|
|---|
| 132 | struct timezone tz;
|
|---|
| 133 | struct timeval actual_time; // Actual time
|
|---|
| 134 |
|
|---|
| 135 | gettimeofday(&actual_time, &tz);
|
|---|
| 136 |
|
|---|
| 137 | time(&rawtime);
|
|---|
| 138 |
|
|---|
| 139 | timeinfo = localtime(&rawtime); // Get local time
|
|---|
| 140 |
|
|---|
| 141 | *t = (timeinfo->tm_hour*3600 + timeinfo->tm_min*60 + timeinfo->tm_sec) + actual_time.tv_usec/1000000.;
|
|---|
| 142 |
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 | void GetLocalDay(int* t) {
|
|---|
| 147 |
|
|---|
| 148 | struct tm * timeinfo;
|
|---|
| 149 | time_t rawtime;
|
|---|
| 150 |
|
|---|
| 151 | time(&rawtime);
|
|---|
| 152 |
|
|---|
| 153 | timeinfo = localtime(&rawtime); // Get local time
|
|---|
| 154 |
|
|---|
| 155 | *t = (timeinfo->tm_yday);
|
|---|
| 156 |
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 | double GetDiffTime(time_t* StartT) {
|
|---|
| 161 |
|
|---|
| 162 | time_t ActualT;
|
|---|
| 163 |
|
|---|
| 164 | time (&ActualT);
|
|---|
| 165 |
|
|---|
| 166 | return difftime (ActualT,*StartT);
|
|---|
| 167 |
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 | long int GetMicroSeconds() {
|
|---|
| 172 |
|
|---|
| 173 | struct tm * timeinfo;
|
|---|
| 174 | time_t rawtime;
|
|---|
| 175 |
|
|---|
| 176 | struct timezone tz;
|
|---|
| 177 | struct timeval actual_time; // Actual time
|
|---|
| 178 |
|
|---|
| 179 | gettimeofday(&actual_time, &tz);
|
|---|
| 180 |
|
|---|
| 181 | time(&rawtime);
|
|---|
| 182 |
|
|---|
| 183 | timeinfo = gmtime(&rawtime); // Get UTC (or GMT timezone).
|
|---|
| 184 |
|
|---|
| 185 | return (timeinfo->tm_hour*3600 + timeinfo->tm_min*60 + timeinfo->tm_sec)*1000000 + actual_time.tv_usec;
|
|---|
| 186 |
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 | /* Prints the binary representation of a 1 byte-integer to stdout */
|
|---|
| 191 | void PrintByteBin(unsigned char i) {
|
|---|
| 192 |
|
|---|
| 193 | for (int k=7;k>=0;k--)
|
|---|
| 194 | if ((i & (1 << k)) !=0) {
|
|---|
| 195 | if ((k)%8)
|
|---|
| 196 | printf("1");
|
|---|
| 197 | else
|
|---|
| 198 | printf("1 ");
|
|---|
| 199 | }
|
|---|
| 200 | else {
|
|---|
| 201 | if ((k)%8)
|
|---|
| 202 | printf("0");
|
|---|
| 203 | else
|
|---|
| 204 | printf("0 ");
|
|---|
| 205 | }
|
|---|
| 206 | printf("\n");
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 | /* Prints the binary representation of a 1-byte integer to str */
|
|---|
| 211 | void sPrintByteBin(unsigned char i, char* str) {
|
|---|
| 212 |
|
|---|
| 213 | bzero(str,sizeof(str));
|
|---|
| 214 |
|
|---|
| 215 | for (int k=7;k>=0;k--)
|
|---|
| 216 | if ((i & (1 << k)) !=0) {
|
|---|
| 217 | if ((k)%8)
|
|---|
| 218 | strcat(str,"1");
|
|---|
| 219 | else
|
|---|
| 220 | strcat(str,"1 ");
|
|---|
| 221 | }
|
|---|
| 222 | else {
|
|---|
| 223 | if ((k)%8)
|
|---|
| 224 | strcat(str,"0");
|
|---|
| 225 | else
|
|---|
| 226 | strcat(str,"0 ");
|
|---|
| 227 | }
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 | /* Prints the binary representation of a 2-byte integer to stdout */
|
|---|
| 232 | void PrintWordBin(unsigned short i) {
|
|---|
| 233 |
|
|---|
| 234 | for (int k=15;k>=0;k--)
|
|---|
| 235 | if ((i & (1 << k)) !=0) {
|
|---|
| 236 | if ((k)%8)
|
|---|
| 237 | printf("1");
|
|---|
| 238 | else
|
|---|
| 239 | printf("1 ");
|
|---|
| 240 | }
|
|---|
| 241 | else {
|
|---|
| 242 | if ((k)%8)
|
|---|
| 243 | printf("0");
|
|---|
| 244 | else
|
|---|
| 245 | printf("0 ");
|
|---|
| 246 | }
|
|---|
| 247 | printf("\n");
|
|---|
| 248 |
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 | /* Prints the binary representation of a 2-byte integer to str */
|
|---|
| 253 | void sPrintWordBin(unsigned short i, char* str) {
|
|---|
| 254 |
|
|---|
| 255 | bzero(str,sizeof(str));
|
|---|
| 256 |
|
|---|
| 257 | for (int k=15;k>=0;k--)
|
|---|
| 258 | if ((i & (1 << k)) !=0) {
|
|---|
| 259 | if ((k)%8)
|
|---|
| 260 | strcat(str,"1");
|
|---|
| 261 | else
|
|---|
| 262 | strcat(str,"1 ");
|
|---|
| 263 | }
|
|---|
| 264 | else {
|
|---|
| 265 | if ((k)%8)
|
|---|
| 266 | strcat(str,"0");
|
|---|
| 267 | else
|
|---|
| 268 | strcat(str,"0 ");
|
|---|
| 269 | }
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 | /*
|
|---|
| 274 | Converts a hexadecimal string to integer - returns
|
|---|
| 275 | -1 - Conversion was abnormally terminated by
|
|---|
| 276 | occurrence of an illegal character
|
|---|
| 277 | 0 - Conversion was successful
|
|---|
| 278 | 1 - String is empty
|
|---|
| 279 | 2 - String has more than 2 characters
|
|---|
| 280 | */
|
|---|
| 281 | int sPrintHex2Dec(const char* xs, unsigned int* result) {
|
|---|
| 282 |
|
|---|
| 283 | size_t szlen = strlen(xs);
|
|---|
| 284 | int xv, fact;
|
|---|
| 285 |
|
|---|
| 286 | if (szlen>0) {
|
|---|
| 287 | // Converting more than 32 bit hexadecimal value?
|
|---|
| 288 | if (szlen>4) return 4;
|
|---|
| 289 |
|
|---|
| 290 | // Begin conversion here
|
|---|
| 291 | *result = 0;
|
|---|
| 292 | fact = 1;
|
|---|
| 293 |
|
|---|
| 294 | // Run until no more character to convert
|
|---|
| 295 | for (int i=szlen-1; i>=0; i--) {
|
|---|
| 296 | if (isxdigit(*(xs+i))) {
|
|---|
| 297 | if (*(xs+i)>=97) {
|
|---|
| 298 | xv = ( *(xs+i) - 97) + 10;
|
|---|
| 299 | }
|
|---|
| 300 | else if ( *(xs+i) >= 65) {
|
|---|
| 301 | xv = (*(xs+i) - 65) + 10;
|
|---|
| 302 | }
|
|---|
| 303 | else {
|
|---|
| 304 | xv = *(xs+i) - 48;
|
|---|
| 305 | }
|
|---|
| 306 | *result += (xv * fact);
|
|---|
| 307 | fact *= 16;
|
|---|
| 308 | }
|
|---|
| 309 | else {
|
|---|
| 310 | // Conversion was abnormally terminated
|
|---|
| 311 | // by non hexadecimal digit, hence
|
|---|
| 312 | // returning only the converted with
|
|---|
| 313 | // an error value 0 (illegal hex character)
|
|---|
| 314 | return -1;
|
|---|
| 315 | }
|
|---|
| 316 | }
|
|---|
| 317 | return 0;
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | // Nothing to convert - string is empty
|
|---|
| 321 | return 1;
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 |
|
|---|
| 325 | /* An integer is interpreted as binary number and converted to an integer */
|
|---|
| 326 | int Bin2Dec(int bin)
|
|---|
| 327 | {
|
|---|
| 328 | int power = 0;
|
|---|
| 329 | int decimal = 0;
|
|---|
| 330 | int num;
|
|---|
| 331 |
|
|---|
| 332 | while (bin > 0) {
|
|---|
| 333 |
|
|---|
| 334 | num = bin % 10;
|
|---|
| 335 | decimal = decimal + num * (int) pow(num * 2, power++);
|
|---|
| 336 | bin = bin / 10;
|
|---|
| 337 | }
|
|---|
| 338 |
|
|---|
| 339 | return decimal;
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 |
|
|---|
| 343 | /*
|
|---|
| 344 | Converts a binary string to integer - returns
|
|---|
| 345 | -1 - Conversion was abnormally terminated by
|
|---|
| 346 | occurrence of an illegal character
|
|---|
| 347 | 0 - Conversion was successful
|
|---|
| 348 | 1 - String is empty
|
|---|
| 349 | 8 - String has more than 8 characters
|
|---|
| 350 | */
|
|---|
| 351 | int sPrintBin2Dec(const char* bs, unsigned int* result) {
|
|---|
| 352 |
|
|---|
| 353 | size_t szlen = strlen(bs);
|
|---|
| 354 |
|
|---|
| 355 | *result = 0;
|
|---|
| 356 |
|
|---|
| 357 | int p=0;
|
|---|
| 358 |
|
|---|
| 359 | if (szlen>0) {
|
|---|
| 360 |
|
|---|
| 361 | // Converting more than 32 bit value?
|
|---|
| 362 | if (szlen>32) return 32;
|
|---|
| 363 |
|
|---|
| 364 | // Begin conversion here
|
|---|
| 365 | for (int i=szlen-1; i>=0; i--) {
|
|---|
| 366 |
|
|---|
| 367 | if (*(bs+i)=='1' || *(bs+i)=='0') {
|
|---|
| 368 |
|
|---|
| 369 | if (*(bs+i)=='1')
|
|---|
| 370 | *result += (int)pow(2.,(double)p);
|
|---|
| 371 |
|
|---|
| 372 | p++;
|
|---|
| 373 | }
|
|---|
| 374 | else return -1;
|
|---|
| 375 | }
|
|---|
| 376 | return 0;
|
|---|
| 377 |
|
|---|
| 378 | }
|
|---|
| 379 | // Nothing to convert - string is empty
|
|---|
| 380 | return 1;
|
|---|
| 381 | }
|
|---|
| 382 |
|
|---|
| 383 |
|
|---|
| 384 |
|
|---|
| 385 | /* Converts a binary string to a decimal number, returns decimal value */
|
|---|
| 386 | int Bin2Dec(const char *bs) {
|
|---|
| 387 |
|
|---|
| 388 | int b, /*m,*/ n;
|
|---|
| 389 | int len, sum = 0;
|
|---|
| 390 |
|
|---|
| 391 | len = strlen(bs) - 1;
|
|---|
| 392 |
|
|---|
| 393 | for (int k = 0; k <= len; k++) {
|
|---|
| 394 |
|
|---|
| 395 | b = 1;
|
|---|
| 396 |
|
|---|
| 397 | n = (bs[k] - '0'); // Char to numeric value
|
|---|
| 398 |
|
|---|
| 399 | if ((n > 1) || (n < 0))
|
|---|
| 400 | return 0;
|
|---|
| 401 |
|
|---|
| 402 |
|
|---|
| 403 | b = b << (len-k);
|
|---|
| 404 |
|
|---|
| 405 | //for (b = 1, m = len; m > k; m--)
|
|---|
| 406 | //b *= 2; // 1 2 4 8 16 32 64 ... place-values, reversed here
|
|---|
| 407 |
|
|---|
| 408 | // Sum it up
|
|---|
| 409 | sum = sum + n * b;
|
|---|
| 410 |
|
|---|
| 411 | //printf("%d*%d + ",n,b);
|
|---|
| 412 | }
|
|---|
| 413 | return sum;
|
|---|
| 414 | }
|
|---|
| 415 |
|
|---|
| 416 |
|
|---|
| 417 | int IsNoDigit(const char* str) {
|
|---|
| 418 |
|
|---|
| 419 | size_t szlen = strlen(str);
|
|---|
| 420 |
|
|---|
| 421 | if (szlen>0) {
|
|---|
| 422 |
|
|---|
| 423 | for (int i=szlen-1; i>=0; i--) {
|
|---|
| 424 | if (isalpha((int)*(str+i)))
|
|---|
| 425 | return 0;
|
|---|
| 426 | }
|
|---|
| 427 | // Okay, no alphabetic character found
|
|---|
| 428 | return 1;
|
|---|
| 429 | }
|
|---|
| 430 | // String is empty
|
|---|
| 431 | return -1;
|
|---|
| 432 | }
|
|---|
| 433 |
|
|---|
| 434 |
|
|---|
| 435 | void SignalHandler(int Signal) {
|
|---|
| 436 | return;
|
|---|
| 437 | }
|
|---|