/[zanavi_public1]/navit/navit/maptool/buffer.c
ZANavi

Contents of /navit/navit/maptool/buffer.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (show annotations) (download)
Mon Feb 4 17:41:59 2013 UTC (6 years, 7 months ago) by zoff99
File MIME type: text/plain
File size: 4509 byte(s)
new map version, lots of fixes and experimental new features
1 /**
2 * ZANavi, Zoff Android Navigation system.
3 * Copyright (C) 2011-2012 Zoff <zoff@zoff.cc>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /**
21 * Navit, a modular navigation system.
22 * Copyright (C) 2005-2011 Navit Team
23 *
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License
26 * version 2 as published by the Free Software Foundation.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the
35 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
36 * Boston, MA 02110-1301, USA.
37 */
38
39 #define _FILE_OFFSET_BITS 64
40 #define _LARGEFILE_SOURCE
41 #define _LARGEFILE64_SOURCE
42 #include <stdlib.h>
43 #include "maptool.h"
44 #include "debug.h"
45
46 void save_buffer(char *filename, struct buffer *b, long long offset)
47 {
48 FILE *f;
49 f = fopen(filename, "rb+");
50 if (!f)
51 {
52 f = fopen(filename, "wb+");
53 }
54
55 dbg_assert(f != NULL);
56
57 off_t offset_64 = (off_t) offset;
58
59 if (verbose_mode) fprintf(stderr,"save_buffer:seeking bytes in %s to "LONGLONG_FMT"\n", filename, offset_64);
60 if (verbose_mode) fprintf(stderr,"save_buffer: base "LONGLONG_FMT"\n", b->base);
61
62 int ret;
63 // ret=fseek(f, offset, SEEK_SET);
64 ret = fseeko(f, offset_64, SEEK_SET);
65 //fprintf(stderr,"ret.code of seek=%d\n", ret);
66 size_t ret2;
67 ret2 = fwrite(b->base, b->size, 1, f);
68 //fprintf(stderr,"ret.code of fwrite=%d\n", ret2);
69 fclose(f);
70 }
71
72 void load_buffer_fp(FILE *f, struct buffer *b, long long offset, long long size)
73 {
74 long long len;
75 int ret;
76
77 if (verbose_mode) fprintf(stderr,"load_buffer:offset="LONGLONG_FMT" size="LONGLONG_FMT"\n", offset, size);
78
79 // fprintf(stderr,"load_buffer 1 buffer ptr=%d\n", b->base);
80
81 int reuse_buffer = 0;
82
83 if (verbose_mode) fprintf(stderr,"load_buffer:info has b->malloced=%d\n", b->malloced);
84 if (verbose_mode) fprintf(stderr,"load_buffer:info has b->size="LONGLONG_FMT"\n", (size_t)b->size);
85 if (verbose_mode) fprintf(stderr,"load_buffer:info want size="LONGLONG_FMT"\n", size);
86 if (verbose_mode) fprintf(stderr,"load_buffer:info base="LONGLONG_FMT"\n", b->base);
87 if (b->base)
88 {
89 if (size > b->size)
90 {
91 if (verbose_mode) fprintf(stderr, "load_buffer:# freeing buffer #\n");
92 free(b->base);
93 //fprintf(stderr,"load_buffer 2 buffer ptr=%d\n", b->base);
94 b->base = NULL;
95 }
96 else
97 {
98 if (verbose_mode) fprintf(stderr, "load_buffer:@ re-using buffer @\n");
99 reuse_buffer = 1;
100 }
101 }
102 else
103 {
104 //fprintf(stderr,"* no buffer to free *\n");
105 }
106 //fprintf(stderr,"load_buffer 3 buffer ptr=%d\n", b->base);
107
108 if (!reuse_buffer)
109 {
110 b->malloced = 0;
111 }
112
113 fseek(f, 0, SEEK_END);
114 len = ftello(f); // 64bit
115 //fprintf(stderr,"filep=%p\n", f);
116 //fprintf(stderr,"load_buffer ftell="LONGLONG_FMT"\n", len);
117 if (offset + size > len)
118 {
119 size = len - offset;
120 if (verbose_mode) fprintf(stderr,"load_buffer new size="LONGLONG_FMT"\n", size);
121 ret = 1;
122 }
123
124 b->size = size;
125 b->malloced = size;
126
127 off_t offset_64 = (off_t) offset;
128 if (verbose_mode) fprintf(stderr,"load_buffer:reading "LONGLONG_FMT" bytes at "LONGLONG_FMT"\n", b->size, offset_64);
129
130 int ret3;
131 ret3 = fseeko(f, offset_64, SEEK_SET); // 64bit
132
133 if (!reuse_buffer)
134 {
135 b->base = malloc((size_t) size);
136 if (verbose_mode) fprintf(stderr, "load_buffer:ret.code of malloc="LONGLONG_FMT"\n", b->base);
137 }
138
139 dbg_assert(b->base != NULL);
140 fread(b->base, b->size, 1, f);
141 }
142
143 void load_buffer(char *filename, struct buffer *b, long long offset, long long size)
144 {
145 // wrapper for "filename" -> "file pointer"
146 FILE *f;
147
148 f = fopen(filename, "rb");
149 load_buffer_fp(f, b, offset, size);
150 fclose(f);
151 }
152

   
Visit the ZANavi Wiki