Parses 'str' for a 32-bit signed integer. If successful, writes the result to *value and returns true; otherwise leaves *value unchanged and returns false.
| 8464 | // the result to *value and returns true; otherwise leaves *value |
| 8465 | // unchanged and returns false. |
| 8466 | bool ParseInt32(const Message& src_text, const char* str, Int32* value) { |
| 8467 | // Parses the environment variable as a decimal integer. |
| 8468 | char* end = NULL; |
| 8469 | const long long_value = strtol(str, &end, 10); // NOLINT |
| 8470 | |
| 8471 | // Has strtol() consumed all characters in the string? |
| 8472 | if (*end != '\0') { |
| 8473 | // No - an invalid character was encountered. |
| 8474 | Message msg; |
| 8475 | msg << "WARNING: " << src_text |
| 8476 | << " is expected to be a 32-bit integer, but actually" |
| 8477 | << " has value \"" << str << "\".\n"; |
| 8478 | printf("%s", msg.GetString().c_str()); |
| 8479 | fflush(stdout); |
| 8480 | return false; |
| 8481 | } |
| 8482 | |
| 8483 | // Is the parsed value in the range of an Int32? |
| 8484 | const Int32 result = static_cast<Int32>(long_value); |
| 8485 | if (long_value == LONG_MAX || long_value == LONG_MIN || |
| 8486 | // The parsed value overflows as a long. (strtol() returns |
| 8487 | // LONG_MAX or LONG_MIN when the input overflows.) |
| 8488 | result != long_value |
| 8489 | // The parsed value overflows as an Int32. |
| 8490 | ) { |
| 8491 | Message msg; |
| 8492 | msg << "WARNING: " << src_text |
| 8493 | << " is expected to be a 32-bit integer, but actually" |
| 8494 | << " has value " << str << ", which overflows.\n"; |
| 8495 | printf("%s", msg.GetString().c_str()); |
| 8496 | fflush(stdout); |
| 8497 | return false; |
| 8498 | } |
| 8499 | |
| 8500 | *value = result; |
| 8501 | return true; |
| 8502 | } |
| 8503 | |
| 8504 | // Reads and returns the Boolean environment variable corresponding to |
| 8505 | // the given flag; if it's not set, returns default_value. |
no outgoing calls
no test coverage detected