summaryrefslogtreecommitdiff
path: root/src/tiger.c
blob: 019e93d8cc8fb81b203ff917746d22a9777a0bb7 (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
/*
 * 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"

static GSList *state_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;
}