char* processDurationString(char* str, int* seconds) Take a duration string which is composed of digits followed by a unit specifier w - week d - day h - hour m - min s - sec Trailing digits without a specifier are assumed to be seconds Returns nullptr on success and a static error string on failure
| 324 | // error string on failure |
| 325 | // |
| 326 | const char * |
| 327 | processDurationString(char *str, int *seconds) |
| 328 | { |
| 329 | char *s = str; |
| 330 | char *current = str; |
| 331 | char unit; |
| 332 | int tmp; |
| 333 | int multiplier; |
| 334 | int result = 0; |
| 335 | int len; |
| 336 | |
| 337 | if (str == nullptr) { |
| 338 | return "Missing time"; |
| 339 | } |
| 340 | |
| 341 | len = strlen(str); |
| 342 | for (int i = 0; i < len; i++) { |
| 343 | if (!ParseRules::is_digit(*current)) { |
| 344 | // Make sure there is a time to process |
| 345 | if (current == s) { |
| 346 | return "Malformed time"; |
| 347 | } |
| 348 | |
| 349 | unit = *current; |
| 350 | |
| 351 | switch (unit) { |
| 352 | case 'w': |
| 353 | multiplier = 7 * 24 * 60 * 60; |
| 354 | break; |
| 355 | case 'd': |
| 356 | multiplier = 24 * 60 * 60; |
| 357 | break; |
| 358 | case 'h': |
| 359 | multiplier = 60 * 60; |
| 360 | break; |
| 361 | case 'm': |
| 362 | multiplier = 60; |
| 363 | break; |
| 364 | case 's': |
| 365 | multiplier = 1; |
| 366 | break; |
| 367 | case '-': |
| 368 | return "Negative time not permitted"; |
| 369 | default: |
| 370 | return "Invalid time unit specified"; |
| 371 | } |
| 372 | |
| 373 | *current = '\0'; |
| 374 | |
| 375 | // coverity[secure_coding] |
| 376 | if (sscanf(s, "%d", &tmp) != 1) { |
| 377 | // Really should not happen since everything |
| 378 | // in the string is digit |
| 379 | ink_assert(0); |
| 380 | return "Malformed time"; |
| 381 | } |
| 382 | |
| 383 | result += (multiplier * tmp); |