1
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
3
* PrimeBase Media Stream for MySQL
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
* Original author: Paul McCullagh (H&G2JCtL)
20
* Continued development: Barry Leslie
25
* Simple utility functions.
37
#include "CSStrUtil.h"
41
void cs_strcpy(size_t size, char *to, const char *from, size_t len)
47
memcpy(to, from, len);
52
void cs_strcpy(size_t size, char *to, const char *from)
56
while (*from && size--)
62
void cs_strcpy_left(size_t size, char *to, const char *from, char ch)
66
while (*from && size-- && *from != ch)
72
void cs_strcpy_right(size_t size, char *to, const char *from, char ch)
76
while (*from && *from != ch)
80
while (*from && size-- && *from != ch)
86
void cs_strcat(size_t size, char *to, const char *from)
88
while (*to && size--) to++;
89
cs_strcpy(size, to, from);
92
void cs_strcat(size_t size, char *to, char ch)
94
while (*to && size--) to++;
101
void cs_strcat(char **to, const char *to_cat)
103
size_t len = strlen(*to) + strlen(to_cat) + 1;
105
cs_realloc((void **) to, len);
109
void cs_strcat(size_t size, char *to, int i)
113
snprintf(buffer, 20, "%d", i);
114
cs_strcat(size, to, buffer);
117
void cs_strcat(size_t size, char *to, uint32_t i)
121
snprintf(buffer, 20, "%"PRIu32"", i);
122
cs_strcat(size, to, buffer);
125
void cs_strcat(size_t size, char *to, uint64_t i)
129
snprintf(buffer, 40, "%"PRIu64"", i);
130
cs_strcat(size, to, buffer);
133
void cs_strcat_left(size_t size, char *to, const char *from, char ch)
135
while (*to && size--) to++;
136
cs_strcpy_left(size, to, from, ch);
139
void cs_strcat_right(size_t size, char *to, const char *from, char ch)
141
while (*to && size--) to++;
142
cs_strcpy_right(size, to, from, ch);
145
void cs_strcat_hex(size_t size, char *to, uint64_t i)
149
snprintf(buffer, 80, "%"PRIx64"", i);
150
cs_strcat(size, to, buffer);
153
void cs_format_context(size_t size, char *buffer, const char *func, const char *file, int line)
158
cs_strcpy(size, buffer, func);
159
// If the "pretty" function includes parameters, remove them:
160
if ((ptr = strchr(buffer, '(')))
162
cs_strcat(size, buffer, "(");
167
cs_strcat(size, buffer, cs_last_name_of_path(file));
169
cs_strcat(size, buffer, ":");
170
cs_strcat(size, buffer, line);
174
cs_strcat(size, buffer, ")");
177
/* This function returns "" if the path ends with a dir char */
178
char *cs_last_name_of_path(const char *path, int count)
183
length = strlen(path);
185
return((char *) path);
186
ptr = path + length - 1;
187
while (ptr != path) {
188
if (IS_DIR_CHAR(*ptr)) {
195
if (IS_DIR_CHAR(*ptr)) ptr++;
196
return((char *) ptr);
199
char *cs_last_name_of_path(const char *path)
201
return cs_last_name_of_path(path, 1);
204
/* This function returns the last name component, even if the path ends with a dir char */
205
char *cs_last_directory_of_path(char *path)
210
length = strlen(path);
213
ptr = path + length - 1;
214
if (IS_DIR_CHAR(*ptr))
216
while (ptr != path && !IS_DIR_CHAR(*ptr)) ptr--;
217
if (IS_DIR_CHAR(*ptr)) ptr++;
221
const char *cs_find_extension(const char *file_name)
225
for (ptr = file_name + strlen(file_name) - 1; ptr >= file_name; ptr--) {
226
if (IS_DIR_CHAR(*ptr))
234
void cs_remove_extension(char *file_name)
236
char *ptr = (char *) cs_find_extension(file_name);
242
bool cs_is_extension(const char *file_name, const char *ext)
246
if ((ptr = cs_find_extension(file_name)))
247
return strcmp(ptr, ext) == 0;
252
* Optionally remove a trailing directory delimiter (If the directory name consists of one
253
* character, the directory delimiter is not removed).
255
bool cs_remove_dir_char(char *dir_name)
259
length = strlen(dir_name);
261
if (IS_DIR_CHAR(dir_name[length - 1])) {
262
dir_name[length - 1] = '\0';
269
void cs_remove_last_name_of_path(char *path)
273
if ((ptr = cs_last_name_of_path(path)))
277
void cs_remove_last_directory_of_path(char *path)
281
if ((ptr = cs_last_directory_of_path(path)))
285
bool cs_add_dir_char(size_t max, char *path)
287
size_t slen = strlen(path);
293
/* If no path is given we will be at the current working directory, under UNIX we must
294
* NOT add a directory delimiter character:
299
if (!IS_DIR_CHAR(path[slen - 1])) {
300
path[slen] = CS_DIR_CHAR;
301
path[slen + 1] = '\0';
307
bool cs_is_absolute(const char *path)
309
return IS_DIR_CHAR(*path);
312
void cs_add_name_to_path(size_t max, char *path, const char *name)
314
char *end_ptr = path + max - 1;
316
cs_add_dir_char(max, path);
317
path = path + strlen(path);
319
if (IS_DIR_CHAR(*name))
321
while (*name && !IS_DIR_CHAR(*name) && path < end_ptr)
326
const char *cs_next_name_of_path(const char *path)
328
if (IS_DIR_CHAR(*path))
330
while (*path && !IS_DIR_CHAR(*path))
332
if (IS_DIR_CHAR(*path))
337
void cs_adjust_absolute_path(size_t max, char *path, const char *rel_path)
340
if (*rel_path == '.') {
341
if (*(rel_path + 1) == '.') {
342
if (!*(rel_path + 2) || IS_DIR_CHAR(*(rel_path + 2))) {
343
/* ..: move up one: */
344
cs_remove_last_directory_of_path(path);
349
if (!*(rel_path + 1) || IS_DIR_CHAR(*(rel_path + 1)))
355
/* Change to this directory: */
356
cs_add_name_to_path(max, path, rel_path);
358
rel_path = cs_next_name_of_path(rel_path);
362
void cs_make_absolute_path(size_t max, char *path, const char *rel_path, const char *cwd)
364
if (cs_is_absolute(rel_path))
365
cs_strcpy(max, path, rel_path);
367
/* Path is relative to the current directory */
368
cs_strcpy(max, path, cwd);
369
cs_adjust_absolute_path(max, path, rel_path);
371
cs_remove_dir_char(path);
374
char *cs_strdup(const char *in_str)
381
str = (char *) cs_malloc(strlen(in_str) + 1);
386
char *cs_strdup(int i)
391
snprintf(buffer, 20, "%d", i);
392
str = (char *) cs_malloc(strlen(buffer) + 1);
397
char *cs_strdup(const char *in_str, size_t len)
404
str = (char *) cs_malloc(len + 1);
405
memcpy(str, in_str, len);
410
bool cs_starts_with(const char *cstr, const char *w_cstr)
412
while (*cstr && *w_cstr) {
413
if (*cstr != *w_cstr)
418
return *cstr || !*w_cstr;
421
void cs_replace_string(size_t size, char *into, const char ch, const char *str)
425
if ((ptr = strchr(into, ch))) {
426
int len = strlen(into);
427
int len2 = strlen(str);
429
if (len + len2 > size)
432
memmove(ptr+1, ptr+len2, len - (ptr - into + 1));
433
memcpy(ptr, str, len2);
434
into[len + len2 - 1] = 0;
438
uint64_t cs_str_to_word8(const char *ptr, bool *overflow)
444
while (*ptr == '0') ptr++;
446
value = (uint64_t) 0;
448
sscanf(ptr, "%"PRIu64"", &value);
449
if (!value && overflow)
455
int64_t cs_str_to_int8(const char *ptr, bool *overflow)
461
while (*ptr == '0') ptr++;
465
sscanf(ptr, "%"PRId64"", &value);
466
if (!value && overflow)
472
int64_t cs_byte_size_to_int8(const char *ptr)
474
char number[101], *num_ptr;
477
while (*ptr && isspace(*ptr))
481
while (*ptr && isdigit(*ptr)) {
482
if (num_ptr < number+100) {
489
size = cs_str_to_int8(number, NULL);
491
while (*ptr && isspace(*ptr))
494
switch (toupper(*ptr)) {
496
size *= 1024LL * 1024LL * 1024LL;
499
size *= 1024LL * 1024LL;
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.
594
void cs_strToUpper(char *ptr)
597
*ptr = toupper(*ptr);
602
void cs_strToLower(char *ptr)
605
*ptr = tolower(*ptr);