mirror of https://github.com/ecmwf/eccodes.git
Added utility string conversion function
This commit is contained in:
parent
df1c542097
commit
849d7b02eb
|
@ -1470,6 +1470,7 @@ int strcmp_nocase(const char *s1, const char *s2);
|
|||
void rtrim(char *s);
|
||||
const char *extract_filename(const char *filepath);
|
||||
char **string_split(char *inputString, const char *delimiter);
|
||||
int string_to_long(const char* input, long* output);
|
||||
|
||||
/* functions.c */
|
||||
long grib_op_eq(long a, long b);
|
||||
|
|
|
@ -91,3 +91,28 @@ char** string_split(char* inputString, const char* delimiter)
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
/* Return 0 if can convert input to an integer, 1 otherwise */
|
||||
int string_to_long(const char* input, long* output)
|
||||
{
|
||||
const int base = 10;
|
||||
char *endptr;
|
||||
errno = 0;
|
||||
long val = 0;
|
||||
|
||||
if (!input) return 1;
|
||||
|
||||
val = strtol(input, &endptr, base);
|
||||
if ( (errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) ||
|
||||
(errno != 0 && val == 0) )
|
||||
{
|
||||
//perror("strtol");
|
||||
return 1;
|
||||
}
|
||||
if (endptr == input) {
|
||||
//fprintf(stderr, "No digits were found. EXIT_FAILURE\n");
|
||||
return 1;
|
||||
}
|
||||
*output = val;
|
||||
return 0; // OK
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue