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);
249
bool cs_match_patern(const char *pattern, const char *str, bool ignore_case)
251
bool escaped = false;
253
while (*pattern && *str) {
254
if ((*pattern == '*' || *pattern == '?' ) && !escaped) {
255
if (*pattern == '?') {
261
while (*pattern == '*' || *pattern == '?' ) pattern++; // eat the pattern matching characters.
263
if (!*pattern) // A * at the end of the pattern matches everything.
266
// This is where it gets complicted.
272
if (*pattern == '\\')
281
if (toupper(*pattern) != toupper(*str))
283
} else if (*pattern != *str)
291
return ((!*pattern) && (!*str));
294
bool cs_match_patern(const char *pattern, const char *str, bool ignore_case)
296
return (fnmatch(pattern, str, (ignore_case)?FNM_CASEFOLD:0) == 0);
177
300
/* This function returns "" if the path ends with a dir char */
178
301
char *cs_last_name_of_path(const char *path, int count)
472
int64_t cs_byte_size_to_int8(const char *ptr)
624
int64_t cs_byte_size_to_int8(const char *ptr, bool *invalid)
474
char number[101], *num_ptr;
477
632
while (*ptr && isspace(*ptr))
481
while (*ptr && isdigit(*ptr)) {
482
if (num_ptr < number+100) {
489
size = cs_str_to_int8(number, NULL);
635
if (!isdigit(*ptr) && *ptr != '.')
638
size = (int64_t) strtod(ptr, &end_ptr);
491
641
while (*ptr && isspace(*ptr))
494
644
switch (toupper(*ptr)) {
646
size *= (int64_t) 1024;
648
size *= (int64_t) 1024;
496
size *= 1024LL * 1024LL * 1024LL;
650
size *= (int64_t) 1024;
499
size *= 1024LL * 1024LL;
652
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.
654
size *= (int64_t) 1024;
663
if (toupper(*ptr) == 'B')
666
while (*ptr && isspace(*ptr))
672
return (int64_t) size;
681
static uint32_t cs_hex_value(char ch)
683
u_char uch = (u_char) ch;
685
if (uch >= '0' && uch <= '9')
687
if (uch >= 'A' && uch <= 'F')
688
return uch - 'A' + 10;
689
if (uch >= 'a' && uch <= 'f')
690
return uch - 'a' + 10;
694
size_t cs_hex_to_bin(size_t size, void *v_bin, size_t len, const char *hex)
696
size_t tot_size = size;
699
u_char *bin = (u_char *) v_bin;
703
for (size_t i=shift; i<len+shift && size > 0; i++) {
705
val = val | cs_hex_value(*hex);
711
val = cs_hex_value(*hex) << 4;
714
return tot_size - size;
717
size_t cs_hex_to_bin(size_t size, void *bin, const char *hex)
719
return cs_hex_to_bin(size, bin, strlen(hex), hex);
722
#define HEX_DIGIT(x) ((x) <= 9 ? '0' + (x) : 'A' + ((x) - 10))
724
void cs_bin_to_hex(size_t size, char *hex, size_t len, const void *v_bin)
726
const u_char *bin = (u_char *) v_bin;
730
for (size_t i=0; i<len && size > 0; i++) {
731
*hex = HEX_DIGIT(*bin >> 4);
736
*hex = HEX_DIGIT(*bin & 0x0F);
594
744
void cs_strToUpper(char *ptr)