>From 3b6fcc8f45db83de9c8478076db6ff32df14cc7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Droz?= Date: Thu, 7 Mar 2013 17:52:06 +0100 Subject: [PATCH] ldif and vcard parsers: don't return end-of-file as an error and avoid unecessary roundtrips and warnings in writephonebook(). --- common/ldif.c | 2 +- common/vcard.c | 13 ++++++------- gnokii/gnokii-phonebook.c | 18 ++++++++++++++---- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/common/ldif.c b/common/ldif.c index 6e40313..aa82fc2 100644 --- a/common/ldif.c +++ b/common/ldif.c @@ -174,7 +174,7 @@ GNOKII_API int gn_ldif2phonebook(FILE *f, gn_phonebook_entry *entry) while (1) { if (!fgets(buf, 1024, f)) - return -1; + return 1; if (BEGINS("dn:")) break; } diff --git a/common/vcard.c b/common/vcard.c index a1887de..3485c91 100644 --- a/common/vcard.c +++ b/common/vcard.c @@ -342,13 +342,13 @@ GNOKII_API int gn_vcard2phonebook(FILE *f, gn_phonebook_entry *entry) { char buf[1024]; vcard_string str; - int retval; + int retval = -1; memset(&str, 0, sizeof(str)); while (1) { if (!fgets(buf, 1024, f)) - return -1; + return 1; if (BEGINS("BEGIN:VCARD")) break; } @@ -356,13 +356,12 @@ GNOKII_API int gn_vcard2phonebook(FILE *f, gn_phonebook_entry *entry) str_append_printf(&str, "BEGIN:VCARD\r\n"); while (fgets(buf, 1024, f)) { str_append_printf(&str, buf); - if (BEGINS("END:VCARD")) + if (BEGINS("END:VCARD")) { + retval = gn_vcardstr2phonebook(str.str, entry); + free(str.str); break; + } } - - retval = gn_vcardstr2phonebook(str.str, entry); - free(str.str); - return retval; } diff --git a/gnokii/gnokii-phonebook.c b/gnokii/gnokii-phonebook.c index ba05a23..f1c3342 100644 --- a/gnokii/gnokii-phonebook.c +++ b/gnokii/gnokii-phonebook.c @@ -280,6 +280,7 @@ gn_error writephonebook(int argc, char *argv[], gn_data *data, struct gn_statema */ char *line, oline[MAX_INPUT_LINE_LEN]; int i; + int parser_ret_val; struct option options[] = { { "overwrite", 0, NULL, 'o'}, @@ -340,13 +341,22 @@ gn_error writephonebook(int argc, char *argv[], gn_data *data, struct gn_statema entry.location = default_location; switch (type) { case 1: - if (gn_vcard2phonebook(stdin, &entry)) - error = GN_ERR_WRONGDATAFORMAT; + parser_ret_val = gn_vcard2phonebook(stdin, &entry); + if(parser_ret_val < 0) + error = GN_ERR_WRONGDATAFORMAT; + else if(parser_ret_val == 1) + goto out; // no more entry + break; + case 2: - if (gn_ldif2phonebook(stdin, &entry)) - error = GN_ERR_WRONGDATAFORMAT; + parser_ret_val = gn_ldif2phonebook(stdin, &entry); + if(parser_ret_val < 0) + error = GN_ERR_WRONGDATAFORMAT; + else if(parser_ret_val == 1) + goto out; // no more entry break; + default: if (!gn_line_get(stdin, line, MAX_INPUT_LINE_LEN)) goto out; /* it means we read an empty line, but that's not an error */ -- 1.8.1.5