Re: writephonebook fix + MyX-2 support

RaphaĆ«l Droz <[email protected]> Mon, 15 Apr 2013 15:07:30 +0200
Newsgroups gmane.linux.drivers.gnokii
Message-ID <20130415130729.GA5244@b1b1>
On Sun, Mar 10, 2013 at 10:35:15AM +0100, Daniele Forsi wrote:
> there are two problems:
> the first is that now error checking is too liberal and it doesn't
> detect broken files such as a file with only "BEGIN:VCARD"

indeed.
One option is using LGPL-2.1 libvformat (http://sf.net/projects/vformat)
Otherwise I rerolled the patch a bit so the above issue should be fixed.

Let me know if gmail mess-up with the attachment again.


> > Unrelated question: is there any existing plan to enhance the
> > "find-free" option of writephonebook ?
> 
> I think nobody complained yet loudly
> 
> > It's a pain to use as it starts the location counter back to 2 for each
> > entry. So in the best case `--writephonebook -f` ends up testing 85!
> > (factorial) locations in order store 85 entries.
> 
> you are right, I think that the "if (find_free)" block should was
> supposed to be outside of the "while (!feof(stdin))" loop and it
> should use default_location as a starting point

I let this modification up to your knowledgeable analysis.
But I'll give a look too to see if further tweaks are needed.

_______________________________________________
gnokii-users mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/gnokii-users
0001-ldif-and-vcard-parsers-don-t-return-end-of-file-as-a.patch (text/x-diff, 2.9 KB)
From 3b6fcc8f45db83de9c8478076db6ff32df14cc7d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rapha=C3=ABl=20Droz?= <[email protected]>
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