summaryrefslogtreecommitdiff
path: root/src/csv.c
blob: b1100953e31a8324e7dad2d8bfe3f8bb669155f5 (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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "def.h"
#include "str.h"
#include "err.h"
#include "mem.h"
#include "csv.h"

BEGIN_C
static int csvlist_makelist (csvlist_t *, long);
END_C

csvlist_t *
csvlist_open (const char *path)
{
	csvlist_t *csv_p;
	FILE *fp;
	char *buffer;
	long size;

	if (!path)
		return NULL;

	csv_p = mem_new0 (csvlist_t, 1);
	csv_p->path = str_clone ((char *) path, strlen (path));

	fp = fopen (path, "rb");
	if (!fp)
		err_system (path);

	fseek (fp, 0L, SEEK_END);
	size = ftell (fp);
	rewind (fp);

	buffer = mem_new (char, size);

	if (fread (buffer, size, 1, fp) < size)
	{
		if (ferror (fp))
			err_system ("csv_open");
	}
	fclose (fp);

	csv_p->top_p = buffer;
	if (csvlist_makelist (csv_p, size))
	{
		csvlist_close (csv_p);
		return NULL;
	}

	return csv_p;
}


void
csvlist_close (csvlist_t *csv_p)
{
	if (csv_p)
	{
		if (csv_p->path)
			mem_free (csv_p->path);

		if (csv_p->top_p)
			mem_free (csv_p->top_p);

		if (csv_p->list)
			mem_free (csv_p->list);

		mem_free (csv_p);
	}

	return;
}


int
csvlist_search_keyword (csvlist_t *csv_p, int pos, const char *keyword)
{
	char **list = csv_p->list;
	int i;

	for (i = pos; i < csv_p->lnum; i ++)
	{
		if (!strcmp (list[i], keyword))
			return i;
	}

	return -1;
}


char *
csvlist_get_word (csvlist_t *csv_p, int pos)
{
	if (pos < 0 || pos > csv_p->lnum)
		return NULL;

	return csv_p->list[pos];
}


int
csvlist_get_max (csvlist_t *csv_p)
{
	return csv_p->lnum;
}


static int
csvlist_makelist (csvlist_t *csv_p, long size)
{
	char *from_buf = csv_p->top_p;
	char *to_buf = from_buf;
	char **list;
	int list_max, list_num;
	long from_point, to_point;
	bool_t quote_in;

	list_max = 16;		/* Initial value = 16 */
	list = mem_new (char *, list_max);
	
	list_num = 1;
	list[0] = to_buf;
	to_point = 0;
	quote_in = 0;

	for (from_point = 0; from_point < size; from_point ++)
	{
		if (!quote_in
		    && from_buf[from_point] == '#')	/* In comment */
		{
			from_point += strcspn (from_buf + from_point, "\n\0");
			continue;
		}

		else if (!quote_in
			 && (from_buf[from_point] == '\n' || from_buf[from_point] == ','))
		{
			to_buf[to_point++] = '\0';

			list[list_num++] = to_buf + to_point;

			if (list_num >= list_max)
			{
				list_max *= 2;
				list = mem_renew (char *, list, list_max);
			}
		}

		else if (from_buf[from_point] == '"')
		{
			quote_in ^= 1;
			continue;
		}

		else
		{
			to_buf[to_point++] = from_buf[from_point];
		}
	}

	to_buf = mem_renew (char, to_buf, to_point);
	list = mem_renew (char *, list, list_num + 1);

	csv_p->top_p = to_buf;
	csv_p->size = to_point;
	csv_p->list = list;
	csv_p->lnum = list_num;

	return 0;
}