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.
| 10994 | // the result to *value and returns true; otherwise leaves *value |
| 10995 | // unchanged and returns false. |
| 10996 | bool ParseInt32(const Message& src_text, const char* str, Int32* value) { |
| 10997 | // Parses the environment variable as a decimal integer. |
| 10998 | char* end = nullptr; |
| 10999 | const long long_value = strtol(str, &end, 10); // NOLINT |
| 11000 | |
| 11001 | // Has strtol() consumed all characters in the string? |
| 11002 | if (*end != '\0') { |
| 11003 | // No - an invalid character was encountered. |
| 11004 | Message msg; |
| 11005 | msg << "WARNING: " << src_text |
| 11006 | << " is expected to be a 32-bit integer, but actually" |
| 11007 | << " has value \"" << str << "\".\n"; |
| 11008 | printf("%s", msg.GetString().c_str()); |
| 11009 | fflush(stdout); |
| 11010 | return false; |
| 11011 | } |
| 11012 | |
| 11013 | // Is the parsed value in the range of an Int32? |
| 11014 | const Int32 result = static_cast<Int32>(long_value); |
| 11015 | if (long_value == LONG_MAX || long_value == LONG_MIN || |
| 11016 | // The parsed value overflows as a long. (strtol() returns |
| 11017 | // LONG_MAX or LONG_MIN when the input overflows.) |
| 11018 | result != long_value |
| 11019 | // The parsed value overflows as an Int32. |
| 11020 | ) { |
| 11021 | Message msg; |
| 11022 | msg << "WARNING: " << src_text |
| 11023 | << " is expected to be a 32-bit integer, but actually" |
| 11024 | << " has value " << str << ", which overflows.\n"; |
| 11025 | printf("%s", msg.GetString().c_str()); |
| 11026 | fflush(stdout); |
| 11027 | return false; |
| 11028 | } |
| 11029 | |
| 11030 | *value = result; |
| 11031 | return true; |
| 11032 | } |
| 11033 | |
| 11034 | // Reads and returns the Boolean environment variable corresponding to |
| 11035 | // the given flag; if it's not set, returns default_value. |
no test coverage detected