summaryrefslogtreecommitdiff
path: root/navit/maptool/buffer.c
blob: 2907b06703a8a837cdf0d0ca0b29176aa1a09918 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
 * Navit, a modular navigation system.
 * Copyright (C) 2005-2011 Navit Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */
#include "navit_lfs.h"
#include <stdlib.h>
#include "maptool.h"
#include "debug.h"

void
save_buffer(char *filename, struct buffer *b, long long offset)
{
	FILE *f;
	f=fopen(filename,"rb+");
	if (! f)
		f=fopen(filename,"wb+");
		
	dbg_assert(f != NULL);
	dbg_assert(fseeko(f, offset, SEEK_SET)==0);
	dbg_assert(fwrite(b->base, b->size, 1, f)==1);
	fclose(f);
}

void
load_buffer(char *filename, struct buffer *b, long long offset, long long size)
{
	FILE *f;
	long long len;
	dbg_assert(size>=0);
	dbg_assert(offset>=0);
	if (b->base)
		free(b->base);
	b->malloced=0;
	f=fopen(filename,"rb");
	fseeko(f, 0, SEEK_END);
	len=ftello(f);
	dbg_assert(len>=0);
	if (offset+size > len) {
		size=len-offset;
	}
	b->size=b->malloced=size;
	dbg_assert(b->size>0);
#if 0
	fprintf(stderr,"reading "LONGLONG_FMT" bytes from %s of "LONGLONG_FMT" bytes at "LONGLONG_FMT"\n", b->size, filename, len, offset);
#endif
	fseeko(f, offset, SEEK_SET);
	b->base=malloc(b->size);
	dbg_assert(b->base != NULL);
	fread(b->base, b->size, 1, f);
	fclose(f);
}

long long
sizeof_buffer(char *filename)
{
	long long ret;
	FILE *f=fopen(filename,"rb");
	fseeko(f, 0, SEEK_END);
	ret=ftello(f);
	fclose(f);
	return ret;
}