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
|
/*
* tiger.c
* TIGER helper functions
*
* Copyright 2007 Jeff Garrett <jeff@jgarrett.org>
*
* This program 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.
*
* 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 Library 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <glib.h>
#include "config.h"
#include "tiger.h"
#define TIGER_STATES_FILE DATADIR "/tiger-states.txt"
#define TIGER_COUNTIES_FILE DATADIR "/tiger-counties.txt"
static GSList *state_list;
static GHashTable *county_list;
GSList *tiger_get_states()
{
gchar *contents, **lines, **iter, **tokens;
struct tiger_state *st;
gint *fips_code;
if (state_list)
return state_list;
if (!g_file_get_contents(TIGER_STATES_FILE, &contents, NULL, NULL))
{
g_warning("Cannot read TIGER states file %s\n", TIGER_STATES_FILE);
return NULL;
}
/* The data file of TIGER states is tab-delimited with fields
* for the state FIPS code, the abbreviation, and the state name.
*/
lines = g_strsplit(contents, "\n", 0);
for (iter = lines; iter && **iter; iter++)
{
st = g_new0(struct tiger_state, 1);
fips_code = g_new0(gint, 1);
tokens = g_strsplit(*iter, "\t", 0);
st->fips_code = g_strdup(tokens[0]);
st->abbrev = g_strdup(tokens[1]);
st->name = g_strdup(tokens[2]);
state_list = g_slist_append(state_list, st);
g_strfreev(tokens);
}
g_strfreev(lines);
g_free(contents);
return state_list;
}
GSList *tiger_get_counties(const char *state_abbrev)
{
gchar *contents, **lines, **iter, **tokens;
if (county_list)
return g_hash_table_lookup(county_list, state_abbrev);
county_list = g_hash_table_new(g_str_hash, g_str_equal);
if (!g_file_get_contents(TIGER_COUNTIES_FILE, &contents, NULL, NULL))
{
g_warning("Cannot read TIGER counties file %s\n", TIGER_COUNTIES_FILE);
return NULL;
}
lines = g_strsplit(contents, "\n", 0);
for (iter = lines; iter && **iter; iter++)
{
struct tiger_county *ct = g_new0(struct tiger_county, 1);
tokens = g_strsplit(*iter, "\t", 0);
ct->fips_code = g_strdup(tokens[0]);
ct->state_abbrev = g_strdup(tokens[1]);
ct->name = g_strdup(tokens[2]);
GSList *list = g_hash_table_lookup(county_list, ct->state_abbrev);
list = g_slist_append(list, ct);
g_hash_table_insert(county_list, ct->state_abbrev, list);
g_strfreev(tokens);
}
g_strfreev(lines);
g_free(contents);
return g_hash_table_lookup(county_list, state_abbrev);
}
|