78
/* This function adds '...' to the end of the string.
81
void cs_strcpy_dottt(size_t size, char *d, const char *s, size_t len)
84
cs_strcpy(size, d, s, len);
88
/* Silly, but anyway... */
89
cs_strcpy(size, d, "...");
93
memcpy(d+size-4, "...", 3);
62
97
void cs_strcpy_left(size_t size, char *to, const char *from, char ch)
174
209
cs_strcat(size, buffer, ")");
212
int cs_path_depth(const char *path)
216
if (IS_DIR_CHAR(*path))
224
static const char *find_wildcard(const char *pattern)
226
bool escaped = false;
228
if ((*pattern == '*' || *pattern == '?' ) && !escaped)
231
if (*pattern == '\\')
242
// Check if the path contains any variable components.
243
bool cs_fixed_pattern(const char *str)
245
return (find_wildcard(str) == NULL);
250
bool cs_match_patern(const char *pattern, const char *str, bool ignore_case)
252
bool escaped = false;
254
while (*pattern && *str) {
255
if ((*pattern == '*' || *pattern == '?' ) && !escaped) {
256
if (*pattern == '?') {
262
while (*pattern == '*' || *pattern == '?' ) pattern++; // eat the pattern matching characters.
264
if (!*pattern) // A * at the end of the pattern matches everything.
267
// This is where it gets complicted.
273
if (*pattern == '\\')
282
if (toupper(*pattern) != toupper(*str))
284
} else if (*pattern != *str)
292
return ((!*pattern) && (!*str));
296
bool cs_match_patern(const char *pattern, const char *str, bool ignore_case)
298
return (fnmatch(pattern, str, (ignore_case)?FNM_CASEFOLD:0) == 0);
177
302
/* This function returns "" if the path ends with a dir char */
178
303
char *cs_last_name_of_path(const char *path, int count)
418
549
return *cstr || !*w_cstr;
552
bool cs_ends_with(const char *cstr, const char *w_cstr)
554
size_t len = strlen(cstr);
555
size_t w_len = strlen(w_cstr);
556
const char *ptr = cstr + len - 1;
557
const char *w_ptr = w_cstr + w_len - 1;
565
while (w_ptr >= w_cstr) {
575
void cs_replace_string(size_t size, char *into, const char *find_str, const char *str)
579
if ((ptr = strstr(into, find_str))) {
580
size_t len = strlen(into);
581
size_t len2 = strlen(str);
582
size_t len3 = strlen(find_str);
584
if (len + len2 + len3 >= size)
587
memmove(ptr+len2, ptr+len3, len - (ptr + len3 - into));
588
memcpy(ptr, str, len2);
589
into[len + len2 - len3] = 0;
421
593
void cs_replace_string(size_t size, char *into, const char ch, const char *str)
472
int64_t cs_byte_size_to_int8(const char *ptr)
644
int64_t cs_byte_size_to_int8(const char *ptr, bool *invalid)
474
char number[101], *num_ptr;
477
652
while (*ptr && isspace(*ptr))
481
while (*ptr && isdigit(*ptr)) {
482
if (num_ptr < number+100) {
489
size = cs_str_to_int8(number, NULL);
655
if (!isdigit(*ptr) && *ptr != '.')
658
size = (int64_t) strtod(ptr, &end_ptr);
491
661
while (*ptr && isspace(*ptr))
494
664
switch (toupper(*ptr)) {
666
size *= (int64_t) 1024;
668
size *= (int64_t) 1024;
496
size *= 1024LL * 1024LL * 1024LL;
670
size *= (int64_t) 1024;
499
size *= 1024LL * 1024LL;
672
size *= (int64_t) 1024;
510
/*--------------------------------------------------------------------------------------------------*/
511
size_t cs_hex_to_bin(size_t size, void *bin, size_t len, const char *hex)
513
unsigned char *bin_ptr, *hex_ptr, c, val;
516
if (len %2) /* The hex string must be an even number of bytes. */
519
if (len > (2 *size)) {
523
bin_ptr = (unsigned char *) bin;
524
hex_ptr = (unsigned char *) hex;
527
for (; len > 0; len--, hex_ptr++) {
529
if ((c >= '0') && (c <= '9')) {
534
if ((c >= 'A') && (c <= 'F')) {
554
/*--------------------------------------------------------------------------------------------------*/
555
size_t cs_bin_to_hex(size_t size, char *hex, size_t len, const void *bin)
557
static uint16_t hex_table[256], initialized = 0;
558
uint16_t *hex_ptr = (uint16_t *)hex;
559
unsigned char *bin_ptr = (unsigned char *)bin;
562
/* init the hex table if required */
566
for ( i=0; i < 256; i++) {
567
snprintf(buf, 20,"%X", i + 256);
568
memcpy(&(hex_table[i]), buf +1, 2);
573
/*----------------------------------*/
583
for (; len != 0; len--, hex_ptr--, bin_ptr--) {
584
memcpy(hex_ptr, hex_table + *bin_ptr, 2);
587
// If there is room null terminate the hex string.
674
size *= (int64_t) 1024;
683
if (toupper(*ptr) == 'B')
686
while (*ptr && isspace(*ptr))
692
return (int64_t) size;
701
static uint32_t cs_hex_value(char ch)
703
u_char uch = (u_char) ch;
705
if (uch >= '0' && uch <= '9')
707
if (uch >= 'A' && uch <= 'F')
708
return uch - 'A' + 10;
709
if (uch >= 'a' && uch <= 'f')
710
return uch - 'a' + 10;
714
size_t cs_hex_to_bin(size_t size, void *v_bin, size_t len, const char *hex)
716
size_t tot_size = size;
719
u_char *bin = (u_char *) v_bin;
723
for (size_t i=shift; i<len+shift && size > 0; i++) {
725
val = val | cs_hex_value(*hex);
731
val = cs_hex_value(*hex) << 4;
734
return tot_size - size;
737
size_t cs_hex_to_bin(size_t size, void *bin, const char *hex)
739
return cs_hex_to_bin(size, bin, strlen(hex), hex);
742
#define HEX_DIGIT(x) ((x) <= 9 ? '0' + (x) : 'A' + ((x) - 10))
744
void cs_bin_to_hex(size_t size, char *hex, size_t len, const void *v_bin)
746
const u_char *bin = (u_char *) v_bin;
750
for (size_t i=0; i<len && size > 0; i++) {
751
*hex = HEX_DIGIT(*bin >> 4);
756
*hex = HEX_DIGIT(*bin & 0x0F);
594
764
void cs_strToUpper(char *ptr)