int cstring_concat2(cstring dest, const char* s1, const char* s2) { assert(NULL != dest); assert(NULL != s1); assert(NULL != s2); if(cstring_concat(dest, s1) && cstring_concat(dest, s2)) return 1; else return 0; }
/* * Profiling shows us that we must pre-render the menu and store * the results internally. The menu struct contains a cstring member, * rendered, which initially is NULL. Render the data to that buffer * and return a pointer to it, or NULL if an error occurs. */ int html_menu_render(html_menu m, cstring buffer) { const char *text, *link, *image, *hover_image; html_menu submenu; list_iterator i; text = html_menu_get_text(m); link = html_menu_get_link(m); image = html_menu_get_image(m); hover_image = html_menu_get_hover_image(m); (void)image; (void)hover_image; if (text != NULL) { if (link != NULL) { if (!cstring_printf(buffer, "<a href='%s'>%s</a><br>\n", link, text)) return 0; } else if (!cstring_concat(buffer, text)) return 0; } if (m->items != NULL) { for (i = list_first(m->items); !list_end(i); i = list_next(i)) { submenu = list_get(i); if (!html_menu_render(submenu, buffer)) return 0; } } return 1; }
cstring osd_fixDefine (cstring x) { /*@access cstring@*/ llassert (cstring_isDefined (x)); # ifdef UNIX if (strchr (x, '\'') != NULL) { /* ** If there is a single quote, check for <ident>='<string>' and ** produce <ident>=<string> */ char *eqs = strchr (x, '='); if (eqs != NULL) { if (eqs[1] == '\'') { char *endqu = strrchr (x, '\''); if (endqu != NULL) { if (*(endqu - 1) != '\\') { if (*(endqu + 1) == '\0') { cstring res; cstring def; *endqu = '\0'; def = cstring_fromChars (eqs + 2); eqs[1] = '\0'; res = cstring_concat (cstring_fromChars (x), def); return res; } } } } } } # endif return cstring_copy (x); /*@noaccess cstring@*/ }
int main(void) { cstring s, dest, *pstr; int rc; const char* start = "This is a string"; const char* end = start + strlen(start); char longstring[10000]; size_t i, nelem = 100; memset(longstring, 'A', sizeof(longstring)); longstring[sizeof(longstring) - 1] = '\0'; for(i = 0; i < nelem; i++) { s = cstring_new(); assert(s != NULL); rc = cstring_copy(s, "Hello"); assert(rc == 1); rc = cstring_compare(s, "Hello"); assert(rc == 0); rc = cstring_compare(s, "hello"); assert(rc != 0); rc = cstring_concat(s, ", world"); assert(rc == 1); rc = cstring_compare(s, "Hello, world"); assert(rc == 0); rc = cstring_concat(s, longstring); assert(rc == 1); rc = cstring_charcat(s, 'A'); assert(rc == 1); cstring_recycle(s); rc = cstring_concat(s, longstring); assert(rc == 1); rc = cstring_concat2(s, longstring, longstring); assert(rc == 1); rc = cstring_concat3(s, longstring, longstring, longstring); assert(rc == 1); /* Test strpcat */ cstring_recycle(s); rc = cstring_pcat(s, start, end); assert(rc == 1); rc = cstring_compare(s, start); assert(rc == 0); /* Test cstring_left() */ cstring_copy(s, "hello, world"); dest = cstring_left(s, 5); rc = cstring_compare(dest, "hello"); assert(rc == 0); cstring_free(dest); /* Test cstring_left() with short strings */ dest = cstring_left(s, 5000); rc = cstring_compare(dest, "hello, world"); assert(rc == 0); cstring_free(dest); /* cstring_right() */ cstring_copy(s, "hello, world"); dest = cstring_right(s, 5); rc = cstring_compare(dest, "world"); assert(rc == 0); cstring_free(dest); dest = cstring_right(s, 5000); rc = cstring_compare(dest, "hello, world"); assert(rc == 0); cstring_free(dest); /* cstring_substring */ cstring_copy(s, "hello, world"); dest = cstring_substring(s, 0, 5); rc = cstring_compare(dest, "hello"); assert(rc == 0); cstring_free(dest); dest = cstring_substring(s, 1, 5); rc = cstring_compare(dest, "ello"); assert(rc == 0); cstring_free(dest); dest = cstring_substring(s, 7, 12); rc = cstring_compare(dest, "world"); assert(rc == 0); cstring_free(dest); /* cstring_reverse */ cstring_copy(s, "hello, world"); cstring_reverse(s); rc = cstring_compare(s, "dlrow ,olleh"); assert(rc == 0); /* cstring_strip */ cstring_copy(s, " a b c d e f "); cstring_strip(s); rc = cstring_compare(s, "a b c d e f"); assert(rc == 0); cstring_upper(s); rc = cstring_compare(s, "A B C D E F"); assert(rc == 0); cstring_lower(s); rc = cstring_compare(s, "a b c d e f"); assert(rc == 0); cstring_free(s); /* cstring_split() */ rc = cstring_split(&pstr, "foo bar baz", " "); assert(rc == 3); cstring_multifree(pstr, rc); mem_free(pstr); rc = cstring_split(&pstr, " foo bar baz", " "); assert(rc == 3); cstring_multifree(pstr, rc); mem_free(pstr); rc = cstring_split(&pstr, " foo bar baz ", " "); assert(rc == 3); cstring_multifree(pstr, rc); mem_free(pstr); rc = cstring_split(&pstr, " foo ", " "); assert(rc == 1); cstring_multifree(pstr, rc); mem_free(pstr); } return 0; }