text stringlengths 0 357 |
|---|
unsigned int l_indx = 0; |
while (*p1) { |
if (*p1=='\n') { |
carray_add(lines, (void*)strndup(line_start, line_chars), &l_indx); |
p1++; |
line_start = p1; |
line_chars = 0; |
} |
else { |
p1++; |
line_chars++; |
} |
} |
carray_add(lines, (void*)strndup(line_start, line_chars), &l_indx); |
return lines; /* should be freed using dc_free_splitted_lines() */ |
} |
void dc_free_splitted_lines(carray* lines) |
{ |
if (lines) { |
int i, cnt = carray_count(lines); |
for (i = 0; i < cnt; i++) { |
free(carray_get(lines, i)); |
} |
carray_free(lines); |
} |
} |
char* dc_insert_breaks(const char* in, int break_every, const char* break_chars) |
{ |
/* insert a space every n characters, the return must be free()'d. |
this is useful to allow lines being wrapped according to RFC 5322 (adds linebreaks before spaces) */ |
if (in==NULL || break_every<=0 || break_chars==NULL) { |
return dc_strdup(in); |
} |
int out_len = strlen(in); |
int chars_added = 0; |
int break_chars_len = strlen(break_chars); |
out_len += (out_len/break_every+1)*break_chars_len + 1/*nullbyte*/; |
char* out = malloc(out_len); |
if (out==NULL) { return NULL; } |
const char* i = in; |
char* o = out; |
while (*i) { |
*o++ = *i++; |
chars_added++; |
if (chars_added==break_every && *i) { |
strcpy(o, break_chars); |
o+=break_chars_len; |
chars_added = 0; |
} |
} |
*o = 0; |
return out; |
} |
// Join clist element to a string. |
char* dc_str_from_clist(const clist* list, const char* delimiter) |
{ |
dc_strbuilder_t str; |
dc_strbuilder_init(&str, 256); |
if (list) { |
for (clistiter* cur = clist_begin(list); cur!=NULL ; cur=clist_next(cur)) { |
const char* rfc724_mid = clist_content(cur); |
if (rfc724_mid) { |
if (str.buf[0] && delimiter) { |
dc_strbuilder_cat(&str, delimiter); |
} |
dc_strbuilder_cat(&str, rfc724_mid); |
} |
} |
} |
return str.buf; |
} |
// Split a string by a character. |
// If the string is empty or NULL, an empty list is returned. |
// The returned clist must be freed using clist_free_content()+clist_free() |
// or given eg. to libetpan objects. |
clist* dc_str_to_clist(const char* str, const char* delimiter) |
{ |
clist* list = clist_new(); |
if (list==NULL) { |
exit(54); |
} |
if (str && delimiter && strlen(delimiter)>=1) { |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.