add functions: fc_mkdirs and str_replace
parent
88aa31df07
commit
13de41bc05
3
HISTORY
3
HISTORY
|
|
@ -1,7 +1,8 @@
|
||||||
|
|
||||||
Version 1.49 2021-03-15
|
Version 1.49 2021-03-16
|
||||||
* add macros: FC_ABS and FC_NEGATIVE
|
* add macros: FC_ABS and FC_NEGATIVE
|
||||||
* uniq_skiplist.c: add uniq_skiplist_pair struct and init function
|
* uniq_skiplist.c: add uniq_skiplist_pair struct and init function
|
||||||
|
* add functions: fc_mkdirs and str_replace
|
||||||
|
|
||||||
Version 1.48 2021-02-01
|
Version 1.48 2021-02-01
|
||||||
* fast_buffer.[hc]: add function fast_buffer_append_binary
|
* fast_buffer.[hc]: add function fast_buffer_append_binary
|
||||||
|
|
|
||||||
|
|
@ -1017,94 +1017,93 @@ int my_strtok(char *src, const char *delim, char **pCols, const int nMaxCols)
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
int str_replace(const char *s, const int src_len, const char *replaced,
|
int str_replace(const string_t *src, const string_t *old_str,
|
||||||
const char *new_str, char *dest, const int dest_size)
|
const string_t *new_str, string_t *dest, const int size)
|
||||||
{
|
{
|
||||||
const char *pStart;
|
const char *ps;
|
||||||
const char *pEnd;
|
const char *pe;
|
||||||
char *pDest;
|
|
||||||
const char *p;
|
const char *p;
|
||||||
int old_len;
|
char *pd;
|
||||||
int new_len;
|
|
||||||
int len;
|
int len;
|
||||||
int max_dest_len;
|
int max_dest_len;
|
||||||
int remain_len;
|
int remain_len;
|
||||||
|
int result;
|
||||||
|
|
||||||
if (dest_size <= 0)
|
if (size <= 0) {
|
||||||
{
|
dest->len = 0;
|
||||||
return 0;
|
return EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
max_dest_len = dest_size - 1;
|
max_dest_len = size - 1;
|
||||||
old_len = strlen(replaced);
|
if (old_str->len == 0) {
|
||||||
new_len = strlen(new_str);
|
if (src->len <= max_dest_len) {
|
||||||
if (old_len == 0)
|
dest->len = src->len;
|
||||||
{
|
result = 0;
|
||||||
len = src_len < max_dest_len ? src_len : max_dest_len;
|
} else {
|
||||||
memcpy(dest, s, len);
|
dest->len = max_dest_len;
|
||||||
dest[len] = '\0';
|
result = EOVERFLOW;
|
||||||
return len;
|
}
|
||||||
}
|
memcpy(dest->str, src->str, dest->len);
|
||||||
|
*(dest->str + dest->len) = '\0';
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
remain_len = max_dest_len;
|
remain_len = max_dest_len;
|
||||||
pDest = dest;
|
pd = dest->str;
|
||||||
pStart = s;
|
ps = src->str;
|
||||||
pEnd = s + src_len;
|
pe = src->str + src->len;
|
||||||
while (1)
|
while (1) {
|
||||||
{
|
p = strstr(ps, old_str->str);
|
||||||
p = strstr(pStart, replaced);
|
if (p == NULL) {
|
||||||
if (p == NULL)
|
|
||||||
{
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
len = p - pStart;
|
len = p - ps;
|
||||||
if (len > 0)
|
if (len > 0) {
|
||||||
{
|
if (len < remain_len) {
|
||||||
if (len < remain_len)
|
memcpy(pd, ps, len);
|
||||||
{
|
pd += len;
|
||||||
memcpy(pDest, pStart, len);
|
|
||||||
pDest += len;
|
|
||||||
remain_len -= len;
|
remain_len -= len;
|
||||||
}
|
} else {
|
||||||
else
|
memcpy(pd, ps, remain_len);
|
||||||
{
|
pd += remain_len;
|
||||||
memcpy(pDest, pStart, remain_len);
|
*pd = '\0';
|
||||||
pDest += remain_len;
|
dest->len = pd - dest->str;
|
||||||
*pDest = '\0';
|
return EOVERFLOW;
|
||||||
return pDest - dest;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (new_len < remain_len)
|
if (new_str->len < remain_len) {
|
||||||
{
|
memcpy(pd, new_str->str, new_str->len);
|
||||||
memcpy(pDest, new_str, new_len);
|
pd += new_str->len;
|
||||||
pDest += new_len;
|
remain_len -= new_str->len;
|
||||||
remain_len -= new_len;
|
} else {
|
||||||
}
|
memcpy(pd, new_str->str, remain_len);
|
||||||
else
|
pd += remain_len;
|
||||||
{
|
*pd = '\0';
|
||||||
memcpy(pDest, new_str, remain_len);
|
dest->len = pd - dest->str;
|
||||||
pDest += remain_len;
|
return EOVERFLOW;
|
||||||
*pDest = '\0';
|
}
|
||||||
return pDest - dest;
|
|
||||||
}
|
|
||||||
|
|
||||||
pStart = p + old_len;
|
ps = p + old_str->len;
|
||||||
}
|
}
|
||||||
|
|
||||||
len = pEnd - pStart;
|
len = pe - ps;
|
||||||
if (len > 0)
|
if (len > 0) {
|
||||||
{
|
if (len <= remain_len) {
|
||||||
if (len > remain_len)
|
result = 0;
|
||||||
{
|
} else {
|
||||||
len = remain_len;
|
len = remain_len;
|
||||||
}
|
result = EOVERFLOW;
|
||||||
memcpy(pDest, pStart, len);
|
}
|
||||||
pDest += len;
|
memcpy(pd, ps, len);
|
||||||
}
|
pd += len;
|
||||||
*pDest = '\0';
|
} else {
|
||||||
return pDest - dest;
|
result = 0;
|
||||||
|
}
|
||||||
|
*pd = '\0';
|
||||||
|
dest->len = pd - dest->str;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool fileExists(const char *filename)
|
bool fileExists(const char *filename)
|
||||||
|
|
@ -3109,11 +3108,11 @@ void fc_free_buffer(BufferInfo *buffer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int fc_check_mkdir_ex(const char *path, const mode_t mode, bool *create)
|
int fc_check_mkdir_ex(const char *path, const mode_t mode, bool *created)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
*create = false;
|
*created = false;
|
||||||
if (access(path, F_OK) == 0) {
|
if (access(path, F_OK) == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
@ -3138,7 +3137,49 @@ int fc_check_mkdir_ex(const char *path, const mode_t mode, bool *create)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
*create = true;
|
*created = true;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int fc_mkdirs_ex(const char *path, const mode_t mode, int *create_count)
|
||||||
|
{
|
||||||
|
#define MAX_SUBDIR_COUNT 128
|
||||||
|
int result;
|
||||||
|
int path_len;
|
||||||
|
int dir_count;
|
||||||
|
int i;
|
||||||
|
bool created;
|
||||||
|
char new_path[PATH_MAX];
|
||||||
|
char buff[PATH_MAX];
|
||||||
|
string_t fp;
|
||||||
|
char *subdirs[MAX_SUBDIR_COUNT];
|
||||||
|
|
||||||
|
*create_count = 0;
|
||||||
|
if (access(path, F_OK) == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
path_len = strlen(path);
|
||||||
|
if (path_len >= sizeof(new_path)) {
|
||||||
|
logError("file: "__FILE__", line: %d, "
|
||||||
|
"path length: %d is too large, exceeds %d",
|
||||||
|
__LINE__, path_len, (int)sizeof(new_path));
|
||||||
|
return ENAMETOOLONG;
|
||||||
|
}
|
||||||
|
|
||||||
|
FC_SET_STRING_EX(fp, buff, 0);
|
||||||
|
memcpy(new_path, path, path_len + 1);
|
||||||
|
dir_count = splitEx(new_path, '/', subdirs, MAX_SUBDIR_COUNT);
|
||||||
|
for (i=0; i<dir_count; i++) {
|
||||||
|
fp.len += sprintf(fp.str + fp.len, "%s/", subdirs[i]);
|
||||||
|
if ((result=fc_check_mkdir_ex(fp.str, mode, &created)) != 0) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
if (created) {
|
||||||
|
(*create_count)++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -64,6 +64,18 @@ char *formatDatetime(const time_t nTime, \
|
||||||
*/
|
*/
|
||||||
int getCharLen(const char *s);
|
int getCharLen(const char *s);
|
||||||
|
|
||||||
|
/** string replace
|
||||||
|
* parameters:
|
||||||
|
* src: the input string
|
||||||
|
* old_str: the old string
|
||||||
|
* new_str: the new string
|
||||||
|
* dest: the output string
|
||||||
|
* size: the size of output buffer
|
||||||
|
* return: 0 for success, != 0 for fail
|
||||||
|
*/
|
||||||
|
int str_replace(const string_t *src, const string_t *old_str,
|
||||||
|
const string_t *new_str, string_t *dest, const int size);
|
||||||
|
|
||||||
/** replace \r and \n to space
|
/** replace \r and \n to space
|
||||||
* parameters:
|
* parameters:
|
||||||
* s: the string
|
* s: the string
|
||||||
|
|
@ -991,12 +1003,20 @@ static inline int fc_get_umask()
|
||||||
return mode;
|
return mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
int fc_check_mkdir_ex(const char *path, const mode_t mode, bool *create);
|
int fc_check_mkdir_ex(const char *path, const mode_t mode, bool *created);
|
||||||
|
|
||||||
static inline int fc_check_mkdir(const char *path, const mode_t mode)
|
static inline int fc_check_mkdir(const char *path, const mode_t mode)
|
||||||
{
|
{
|
||||||
bool create;
|
bool created;
|
||||||
return fc_check_mkdir_ex(path, mode, &create);
|
return fc_check_mkdir_ex(path, mode, &created);
|
||||||
|
}
|
||||||
|
|
||||||
|
int fc_mkdirs_ex(const char *path, const mode_t mode, int *create_count);
|
||||||
|
|
||||||
|
static inline int fc_mkdirs(const char *path, const mode_t mode)
|
||||||
|
{
|
||||||
|
int create_count;
|
||||||
|
return fc_mkdirs_ex(path, mode, &create_count);
|
||||||
}
|
}
|
||||||
|
|
||||||
int fc_get_first_line(const char *filename, char *buff,
|
int fc_get_first_line(const char *filename, char *buff,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue