decrease the amount of sys_writes used and add a useful error message

also pretty up console messages by adding an empty line
This commit is contained in:
Will Song
2015-11-23 20:25:55 -06:00
parent fa6a26c6fd
commit aac8bfe98f

View File

@@ -797,15 +797,19 @@ cmd_script(ProfWin *window, const char *const command, gchar **args)
return TRUE; return TRUE;
} }
/* escape a string into csv and write it to the file descriptor */
static void static void
writecsv(int fd, const char *const str){ writecsv(int fd, const char *const str){
if(str){ if(!str) return;
for(int i = 0; i < strlen(str); i++){ size_t len = strlen(str);
if(str[i] != '"') write(fd, str + i, 1); char *s = malloc((2 * len + 1) * sizeof(char));
/* two quotes ("") escapes a single quote (") */ char *c = s;
else write(fd, "\"\"", 2); for(int i = 0; i < strlen(str); i++){
} if(str[i] != '"') *c++ = str[i];
else { *c++ = '"'; *c++ = '"'; len++; }
} }
write(fd, s, len);
free(s);
} }
gboolean gboolean
@@ -815,9 +819,16 @@ cmd_export(ProfWin *window, const char *const command, gchar **args)
/* temporary, we SHOULD pass everything to an escape function (to escape out quotes) /* temporary, we SHOULD pass everything to an escape function (to escape out quotes)
* and then use fputs */ * and then use fputs */
int fd = open(args[0], O_WRONLY | O_CREAT, 00600); int fd = open(args[0], O_WRONLY | O_CREAT, 00600);
GSList *list = roster_get_contacts(ROSTER_ORD_NAME, TRUE); GSList *list = NULL;
if(fd == -1){
cons_show("error: cannot open %s: %s", args[0], strerror(errno));
cons_show("");
return TRUE;
}
write(fd, "jid,name\n", strlen("jid,name\n")); write(fd, "jid,name\n", strlen("jid,name\n"));
list = roster_get_contacts(ROSTER_ORD_NAME, TRUE);
if(list){ if(list){
GSList *curr = list; GSList *curr = list;
while(curr){ while(curr){
@@ -835,8 +846,11 @@ cmd_export(ProfWin *window, const char *const command, gchar **args)
/* loop */ /* loop */
curr = g_slist_next(curr); curr = g_slist_next(curr);
} }
cons_show("Contacts exported successfully");
cons_show("");
} else { } else {
cons_show("No contacts in roster."); cons_show("No contacts in roster.");
cons_show("");
} }
g_slist_free(list); g_slist_free(list);