[patch] writephonebook and find-free

RaphaĆ«l Droz <[email protected]> Fri, 10 May 2013 00:04:53 +0200
Newsgroups gmane.linux.drivers.gnokii
Message-ID <20130509220451.GC27763@b1b1>
Attached is an enhanced heuristic for find-free.
It relies on using the -f flag twice with --writephonebook as this may
not be needed for every phone.

The goal is to avoid the find-free loop to start from 1 for every entry of
a vcf file, even if the first free location is 72. Using the last
successful location may often be a best bet.



regards

_______________________________________________
gnokii-users mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/gnokii-users
0001-Implement-a-non-factorial-WritePhonebook-find-free-h.patch (text/x-diff, 2.9 KB)
From 7b7ccd1d41b430d9d626fce67914f1ef46170c8d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rapha=C3=ABl=20Droz?= <[email protected]>
Date: Thu, 9 May 2013 20:20:42 +0200
Subject: [PATCH] Implement a non-factorial WritePhonebook "find-free"
 heuristic.

When the "f" flag is passed twice to --writephonebook, the loop used to
 guess a valid location starts from the last location successfully used
 instead of always starting from 1.
---
 gnokii/gnokii-phonebook.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/gnokii/gnokii-phonebook.c b/gnokii/gnokii-phonebook.c
index f1c3342..9412d75 100644
--- a/gnokii/gnokii-phonebook.c
+++ b/gnokii/gnokii-phonebook.c
@@ -271,6 +271,7 @@ gn_error writephonebook(int argc, char *argv[], gn_data *data, struct gn_statema
 	gn_error error = GN_ERR_NONE;
 	gn_memory_type default_mt = GN_MT_ME; /* Default memory_type. Changed if given in the command line */
 	int default_location = 1; /* default location. Changed if given in the command line */
+	int next_find_free = 1; /* if -f is doubled, find_free start the counter from the last successful location */
 	int find_free = 0; /* By default don't try to find a free location */
 	int confirm = 0; /* By default don't overwrite existing entries */
 	int type = 0; /* type of the output:
@@ -311,7 +312,7 @@ gn_error writephonebook(int argc, char *argv[], gn_data *data, struct gn_statema
 			type = 2;
 			break;
 		case 'f':
-			find_free = 1;
+			find_free++;
 			break;
 		case 'm':
 			default_mt = gn_str2memory_type(optarg);
@@ -329,7 +330,6 @@ gn_error writephonebook(int argc, char *argv[], gn_data *data, struct gn_statema
 		/* There are too many arguments that don't start with '-' */
 		return writephonebook_usage(stderr, -1);
 	}
-
 	line = oline;
 
 	/* Go through data from stdin. */
@@ -369,11 +369,12 @@ gn_error writephonebook(int argc, char *argv[], gn_data *data, struct gn_statema
 			goto out;
 
 		if (find_free) {
+			i = next_find_free;
 #if 0
 			error = gn_sm_functions(GN_OP_FindFreePhonebookEntry, data, state);
 			if (error == GN_ERR_NOTIMPLEMENTED) {
 #endif
-			for (i = 1; ; i++) {
+		        do {
 				gn_phonebook_entry aux;
 
 				memcpy(&aux, &entry, sizeof(gn_phonebook_entry));
@@ -388,7 +389,7 @@ gn_error writephonebook(int argc, char *argv[], gn_data *data, struct gn_statema
 					error = GN_ERR_NONE;
 					break;
 				}
-			}
+			} while(i++);
 #if 0
 			}
 #endif
@@ -443,6 +444,7 @@ gn_error writephonebook(int argc, char *argv[], gn_data *data, struct gn_statema
 			 * multiple entries, don't write them to the same
 			 * location */
 			default_location++;
+			if(find_free > 1) next_find_free = entry.location + 1;
 		} else
 			fprintf(stderr, _("Write failed (%s): memory type: %s, loc: %d, name: %s, number: %s\n"),
 				 gn_error_print(error), gn_memory_type2str(entry.memory_type), entry.location, entry.name, entry.number);
-- 
1.8.1.5