This source file includes following definitions.
- readtokens0_init
- readtokens0_free
- save_token
- readtokens0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #include <config.h>
21
22 #include <stdlib.h>
23
24 #include "readtokens0.h"
25
26 #define obstack_chunk_alloc malloc
27 #define obstack_chunk_free free
28
29 void
30 readtokens0_init (struct Tokens *t)
31 {
32 t->n_tok = 0;
33 t->tok = NULL;
34 t->tok_len = NULL;
35 obstack_init (&t->o_data);
36 obstack_init (&t->o_tok);
37 obstack_init (&t->o_tok_len);
38 }
39
40 void
41 readtokens0_free (struct Tokens *t)
42 {
43 obstack_free (&t->o_data, NULL);
44 obstack_free (&t->o_tok, NULL);
45 obstack_free (&t->o_tok_len, NULL);
46 }
47
48
49
50 static void
51 save_token (struct Tokens *t)
52 {
53
54 size_t len = obstack_object_size (&t->o_data) - 1;
55 char const *s = obstack_finish (&t->o_data);
56 obstack_ptr_grow (&t->o_tok, s);
57 obstack_grow (&t->o_tok_len, &len, sizeof len);
58 t->n_tok++;
59 }
60
61
62
63
64
65 bool
66 readtokens0 (FILE *in, struct Tokens *t)
67 {
68
69 while (1)
70 {
71 int c = fgetc (in);
72 if (c == EOF)
73 {
74 size_t len = obstack_object_size (&t->o_data);
75
76
77
78 if (len)
79 {
80 obstack_1grow (&t->o_data, '\0');
81 save_token (t);
82 }
83
84 break;
85 }
86
87 obstack_1grow (&t->o_data, c);
88 if (c == '\0')
89 save_token (t);
90 }
91
92
93
94 obstack_ptr_grow (&t->o_tok, NULL);
95
96 t->tok = obstack_finish (&t->o_tok);
97 t->tok_len = obstack_finish (&t->o_tok_len);
98 return ! ferror (in);
99 }