c - Alternative and probable better way to printf -
i writing string gstring using printf as:
char *string<i> /*where string<i> stands string1, string2 , on*/ g_string_append_printf (ustring, "@%s{%s,\n",string1, string0); if( strlen(string2)!=0 ||string2!=null) g_string_append_printf (ustring,"\tauthor=\"%s\",\n", string2); if( strlen(string3)!=0 ||string3!=null) g_string_append_printf (ustring,"\tyear=\"%s\",\n", string3); if( strlen(string4)!=0 ||string4!=null) g_string_append_printf (ustring, "\ttitle=\"%s\",\n", string4); glib's not important here. consider as
printf ("\tauthor=\"%s\",\n", string<i>) when works rather fine, seems not best way(i have strings string<1> string<30>) , looking better way.
points consider
any string may empty/null, checked every line before printf.
would better if complete print thing works function
any better way of implementing this?
eliminate duplication checks if string null or empty wrapping in function:
void conditionally_append_string(char* out, const char* fmt, const char* in) { /* check null before dereferencing. check if first character not null terminator instead of invoking strlen(). */ if (in && *in) { g_string_append_printf (out, fmt, in); } } conditionally_append_string(ustring, "@%s{\n", string0); conditionally_append_string(ustring, "%s,\n", string1); conditionally_append_string(ustring, "\tauthor=\"%s\",\n", string2);
Comments
Post a Comment