summaryrefslogtreecommitdiff
path: root/navit
diff options
context:
space:
mode:
Diffstat (limited to 'navit')
-rw-r--r--navit/bookmarks.c117
-rw-r--r--navit/bookmarks.h2
-rw-r--r--navit/item_def.h1
-rw-r--r--navit/navit.c8
4 files changed, 61 insertions, 67 deletions
diff --git a/navit/bookmarks.c b/navit/bookmarks.c
index be442d9c0..c05d24901 100644
--- a/navit/bookmarks.c
+++ b/navit/bookmarks.c
@@ -29,6 +29,7 @@
#include "map.h"
#include "command.h"
#include "bookmarks.h"
+#include "navit.h"
#include "navit_nls.h"
struct bookmarks {
@@ -603,84 +604,76 @@ bookmarks_rename_bookmark(struct bookmarks *this_, const char *oldName, const ch
return FALSE;
}
+static int
+bookmarks_shrink(char *bookmarks, int offset)
+{
+ char buffer[4096];
+ int count,ioffset=offset,ooffset=0;
+ FILE *f;
+ if (!offset)
+ return 1;
+ f = fopen(bookmarks, "r+");
+ if (!f)
+ return 0;
+ for (;;) {
+ fseek(f, ioffset, SEEK_SET);
+ count=fread(buffer, 1, sizeof(buffer), f);
+ if (!count)
+ break;
+ fseek(f, ooffset, SEEK_SET);
+ if (fwrite(buffer, count, 1, f) != 1)
+ return 0;
+ ioffset+=count;
+ ooffset+=count;
+ }
+ fflush(f);
+ ftruncate(fileno(f),ooffset);
+#ifdef HAVE_FSYNC
+ fsync(fileno(f));
+#endif
+ fclose(f);
+ return 1;
+}
+
/**
* @param limit Limits the number of entries in the "backlog". Set to 0 for "infinite"
*/
void
-bookmarks_append_coord(struct bookmarks *this_, char *file, struct pcoord *c, const char *type, const char *description, GHashTable *h, int limit)
+bookmarks_append_coord(struct bookmarks *this_, char *file, struct pcoord *c, int count, const char *type, const char *description, GHashTable *h, int limit)
{
FILE *f;
- int offset=0;
- char *buffer;
- int ch,prev,lines=0;
- int numc,readc;
- int fd;
const char *prostr;
- f=fopen(file, "r");
- if (!f)
- goto new_file;
- if (limit != 0) {
- prev = '\n';
- while ((ch = fgetc(f)) != EOF) {
- if ((ch == '\n') && (prev != '\n')) {
- lines++;
+ if (limit != 0 && (f=fopen(file, "r"))) {
+ int offsets[limit];
+ int offset_pos=0;
+ int offset;
+ char buffer[4096];
+ memset(offsets, 0, sizeof(offsets));
+ for (;;) {
+ offset=ftell(f);
+ if (!fgets(buffer, sizeof(buffer), f))
+ break;
+ if (strstr(buffer,"type=")) {
+ offsets[offset_pos]=offset;
+ offset_pos=(offset_pos+1)%limit;
}
- prev = ch;
- }
-
- if (prev != '\n') { // Last line did not end with a newline
- lines++;
}
-
fclose(f);
- f = fopen(file, "r+");
- fd = fileno(f);
- while (lines >= limit) { // We have to "scroll up"
- rewind(f);
- numc = 0; // Counts how many bytes we have in our line to scroll up
- while ((ch = fgetc(f)) != EOF) {
- numc++;
- if (ch == '\n') {
- break;
- }
- }
-
- buffer=g_malloc(numc);
- offset = numc; // Offset holds where we currently are
-
- do {
- fseek(f,offset,SEEK_SET);
- readc = fread(buffer,1,numc,f);
-
- fseek(f,-(numc+readc),SEEK_CUR);
- fwrite(buffer,1,readc,f);
-
- offset += readc;
- } while (readc == numc);
-
- g_free(buffer);
- fflush(f);
- ftruncate(fd,(offset-numc));
-#ifdef HAVE_FSYNC
- fsync(fd);
-#endif
-
- lines--;
- }
+ bookmarks_shrink(file, offsets[offset_pos]);
}
- fclose(f);
-
-new_file:
f=fopen(file, "a");
if (f) {
if (c) {
- prostr = projection_to_name(c->pro,NULL);
- fprintf(f,"%s%s%s0x%x %s0x%x type=%s label=\"%s\"\n",
+ int i;
+ fprintf(f,"type=%s label=\"%s\"\n", type, description);
+ for (i = 0 ; i < count ; i++) {
+ prostr = projection_to_name(c[i].pro,NULL);
+ fprintf(f,"%s%s%s0x%x %s0x%x\n",
prostr, *prostr ? ":" : "",
- c->x >= 0 ? "":"-", c->x >= 0 ? c->x : -c->x,
- c->y >= 0 ? "":"-", c->y >= 0 ? c->y : -c->y,
- type, description);
+ c[i].x >= 0 ? "":"-", c[i].x >= 0 ? c[i].x : -c[i].x,
+ c[i].y >= 0 ? "":"-", c[i].y >= 0 ? c[i].y : -c[i].y);
+ }
} else
fprintf(f,"\n");
}
diff --git a/navit/bookmarks.h b/navit/bookmarks.h
index 792d2fe64..cee28e7db 100644
--- a/navit/bookmarks.h
+++ b/navit/bookmarks.h
@@ -53,7 +53,7 @@ char* bookmarks_get_destination_file(gboolean create);
void bookmarks_set_center_from_file(struct bookmarks *this_, char *file);
char* bookmarks_get_center_file(gboolean create);
void bookmarks_write_center_to_file(struct bookmarks *this_, char *file);
-void bookmarks_append_coord(struct bookmarks *this_, char *file, struct pcoord *c, const char *type, const char *description, GHashTable *h, int limit);
+void bookmarks_append_coord(struct bookmarks *this_, char *file, struct pcoord *c, int count, const char *type, const char *description, GHashTable *h, int limit);
/* end of prototypes */
#ifdef __cplusplus
diff --git a/navit/item_def.h b/navit/item_def.h
index d4b5c6776..cdf286fe9 100644
--- a/navit/item_def.h
+++ b/navit/item_def.h
@@ -475,6 +475,7 @@ ITEM(forest_way_2)
ITEM(forest_way_3)
ITEM(forest_way_4)
ITEM(former_itinerary)
+ITEM(former_itinerary_part)
/* Area */
ITEM2(0xc0000000,area)
ITEM2(0xc0000001,area_unspecified)
diff --git a/navit/navit.c b/navit/navit.c
index ba4411a53..5952cbcf7 100644
--- a/navit/navit.c
+++ b/navit/navit.c
@@ -848,7 +848,7 @@ navit_set_destination(struct navit *this_, struct pcoord *c, const char *descrip
} else
this_->destination_valid=0;
char *destination_file = bookmarks_get_destination_file(TRUE);
- bookmarks_append_coord(this_->bookmarks, destination_file, c, "former_destination", description, NULL, this_->recentdest_count);
+ bookmarks_append_coord(this_->bookmarks, destination_file, c, 1, "former_destination", description, NULL, this_->recentdest_count);
g_free(destination_file);
callback_list_call_attr_0(this_->attr_cbl, attr_destination);
if (this_->route) {
@@ -899,8 +899,8 @@ navit_add_former_destinations_from_file(struct navit *this_)
{
char *destination_file = bookmarks_get_destination_file(FALSE);
struct attr parent={attr_navit, .u.navit=this_};
- struct attr type={attr_type, {"textfile"}}, data={attr_data, {destination_file}};
- struct attr *attrs[]={&type, &data, NULL};
+ struct attr type={attr_type, {"textfile"}}, data={attr_data, {destination_file}}, flags={attr_flags, {1}};
+ struct attr *attrs[]={&type, &data, &flags, NULL};
struct map_rect *mr;
struct item *item;
int i,valid=0,count=0;
@@ -913,7 +913,7 @@ navit_add_former_destinations_from_file(struct navit *this_)
return;
mr=map_rect_new(this_->former_destination, NULL);
while ((item=map_rect_get_item(mr))) {
- if ((item->type == type_former_destination || item->type == type_former_itinerary) && (count=item_coord_get(item, c, 16)))
+ if ((item->type == type_former_destination || item->type == type_former_itinerary || item->type == type_former_itinerary_part) && (count=item_coord_get(item, c, 16)))
valid=1;
}
map_rect_destroy(mr);