File: io.c

package info (click to toggle)
s3d 0.2.1.1-5
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 5,256 kB
  • ctags: 2,684
  • sloc: ansic: 20,585; perl: 98; makefile: 32; python: 8; sh: 4
file content (125 lines) | stat: -rw-r--r-- 3,892 bytes parent folder | download
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * io.c
 *
 * Copyright (C) 2006-2008 Simon Wunderlich <dotslash@packetmixer.de>
 *
 * This file is part of s3dosm, a gps card application for s3d.
 * See http://s3d.berlios.de/ for more updates.
 *
 * s3dosm is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * s3dosm 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 s3dosm; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */


#include "s3dosm.h"
#include <stdio.h>   /* fopen(), fread(), fclose() */
#include <stdlib.h>   /* malloc(), free() */
#include <string.h>   /* strstr() */
#include <sys/stat.h>  /* fstat() */
#include <getopt.h>   /* getopt() */
#include <errno.h>   /* errno */
#include <s3d.h>   /* s3d_usage() */

char *read_file(const char *fname, int *fsize)
{
	FILE *fp;
	char *buf = NULL;
	int filesize;
	struct stat bf;

	if ((fp = fopen(fname, "rb")) == NULL) {
		fprintf(stderr, "read_file( %s ):fopen(): %s", fname, strerror(errno));
		return(NULL);
	}
	if (fstat(fileno(fp), &bf))    {
		fprintf(stderr, "read_file( %s ):fopen(): %s", fname, strerror(errno));
		return(NULL);
	}
	filesize = bf.st_size;
	if ((buf = (char*)malloc(filesize)) == NULL)  {
		fprintf(stderr, "read_file( %s ):malloc(): %s", fname, strerror(errno));
		return(NULL);
	}
	fread(buf, filesize, 1, fp);
	fclose(fp);
	if (fsize != NULL) *fsize = filesize;
	return(buf);
}

int process_args(int argc, char **argv)
{
	int      lopt_idx = 0, i;
	int      c;
	float    minlat, minlon, maxlat, maxlon;
	char     info[1024];
	struct option long_options[] = {
		{"help", 0, NULL, 'h'
		}, {"osm", 1, NULL, 'o'}, {NULL, 0, NULL, 0}
	};
	optind = 0;
	opterr = 0;
	while (-1 != (c = getopt_long(argc, argv, "dH:?ho:", long_options, &lopt_idx))) {
		switch (c) {
		case 0:
			break;
		case 'o':
			if (4 == sscanf(optarg, "%f,%f,%f,%f", &minlat, &minlon, &maxlat, &maxlon))
				layerset_add(load_osm_web(minlat, minlon, maxlat, maxlon));
			else {
				printf("%s: bad map bounding box", optarg);
				return(-1);
			}
			break;
		case 'h':
		case '?':
			printf("\nUSAGE: %s [options] [files]\n\n", argv[0]);
			printf("options:\n");
			printf("\t--osm, -o MINLAT,MINLON,MAXLAT,MAXLON:\n");
			printf("\t\tload a map with the given bounding box\n\t\tfrom the openstreetmap server\n");
			printf("\t--help, -?, -h: this helpful text\n\n");
			printf("supported file types:\n");
			printf("\t+ Kismet .xml Logs\n");
			printf("\t+ Opemstreetmap .osm files\n\n");
			s3d_usage(); /* add s3d usage */
			return(-1);
		default:
			break;
		}
	}
	for (i = 1;i < argc;i++) {
		if (strstr(argv[i], ".osm") - argv[i] == (signed)(strlen(argv[i]) - 4)) {
			snprintf(info, 1024, "loading OSM-File: %s", argv[i]);
			load_window(info);
			layerset_add(load_osm_file(argv[i]));
		} else if (strstr(argv[i], ".xml") - argv[i] == (signed)(strlen(argv[i]) - 4)) { /* might be osm or kismet xml */
			char *file;
			int fsize;
			if (NULL == (file = read_file(argv[i], &fsize)))
				break;
			if (NULL != strstr(file, "<!DOCTYPE detection-run SYSTEM \"http://kismetwireless.net")) {
				snprintf(info, 1024, "loading Kismet-File: %s", argv[i]);
				load_window(info);
				layerset_add(parse_kismet(file, fsize));
			} else if (NULL != strstr(file, "<osm ")) {
				snprintf(info, 1024, "loading OSM-File: %s", argv[i]);
				load_window(info);
				layerset_add(parse_osm(file, fsize));
			}
			free(file);
		}
	}
	load_window_remove();
	return(0);
}