text stringlengths 0 357 |
|---|
/******************************************************************************* |
* String tools |
******************************************************************************/ |
char* dc_strdup(const char* s) /* strdup(NULL) is undefined, save_strdup(NULL) returns an empty string in this case */ |
{ |
char* ret = NULL; |
if (s) { |
if ((ret=strdup(s))==NULL) { |
exit(16); /* cannot allocate (little) memory, unrecoverable error */ |
} |
} |
else { |
if ((ret=(char*)calloc(1, 1))==NULL) { |
exit(17); /* cannot allocate little memory, unrecoverable error */ |
} |
} |
return ret; |
} |
char* dc_strdup_keep_null(const char* s) /* strdup(NULL) is undefined, safe_strdup_keep_null(NULL) returns NULL in this case */ |
{ |
return s? dc_strdup(s) : NULL; |
} |
int dc_atoi_null_is_0(const char* s) |
{ |
return s? atoi(s) : 0; |
} |
double dc_atof(const char* str) |
{ |
// hack around atof() that may accept only `,` as decimal point on mac |
char* test = dc_mprintf("%f", 1.2); |
test[2] = 0; |
char* str_locale = dc_strdup(str); |
dc_str_replace(&str_locale, ".", test+1); |
double f = atof(str_locale); |
free(test); |
free(str_locale); |
return f; |
} |
char* dc_ftoa(double f) |
{ |
// hack around printf(%f) that may return `,` as decimal point on mac |
char* test = dc_mprintf("%f", 1.2); |
test[2] = 0; |
char* str = dc_mprintf("%f", f); |
dc_str_replace(&str, test+1, "."); |
free(test); |
return str; |
} |
void dc_ltrim(char* buf) |
{ |
size_t len = 0; |
const unsigned char* cur = NULL; |
if (buf && *buf) { |
len = strlen(buf); |
cur = (const unsigned char*)buf; |
while (*cur && isspace(*cur)) { |
cur++; len--; |
} |
if ((const unsigned char*)buf!=cur) { |
memmove(buf, cur, len + 1); |
} |
} |
} |
void dc_rtrim(char* buf) |
{ |
size_t len = 0; |
unsigned char* cur = NULL; |
if (buf && *buf) { |
len = strlen(buf); |
cur = (unsigned char*)buf + len - 1; |
while (cur!=(unsigned char*)buf && isspace(*cur)) { |
--cur, --len; |
} |
cur[isspace(*cur) ? 0 : 1] = '\0'; |
} |
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.