33deac0bb69c97d2277586752c17d8236790674d
[pspp] / src / language / lexer / subcommand-list.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004, 2011 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17
18 #include <config.h>
19 #include "language/lexer/subcommand-list.h"
20 #include <stdlib.h>
21 #include "language/lexer/lexer.h"
22 #include "gl/xalloc.h"
23
24 #include "gettext.h"
25 #define _(msgid) gettext (msgid)
26
27 /* I call these objects `lists' but they are in fact simple dynamic arrays */
28
29 #define CHUNKSIZE 16
30
31 /* Create a  list */
32 void
33 subc_list_double_create(subc_list_double *l)
34 {
35   l->data = xnmalloc (CHUNKSIZE, sizeof *l->data);
36   l->sz = CHUNKSIZE;
37   l->n_data = 0;
38 }
39
40 void
41 subc_list_int_create(subc_list_int *l)
42 {
43   l->data = xnmalloc (CHUNKSIZE, sizeof *l->data);
44   l->sz = CHUNKSIZE;
45   l->n_data = 0;
46 }
47
48 /* Push a value onto the list */
49 void
50 subc_list_double_push(subc_list_double *l, double d)
51 {
52   l->data[l->n_data++] = d;
53
54   if (l->n_data >= l->sz)
55     {
56       l->sz += CHUNKSIZE;
57       l->data = xnrealloc (l->data, l->sz, sizeof *l->data);
58     }
59
60 }
61
62 void
63 subc_list_int_push(subc_list_int *l, int d)
64 {
65   l->data[l->n_data++] = d;
66
67   if (l->n_data >= l->sz)
68     {
69       l->sz += CHUNKSIZE;
70       l->data = xnrealloc (l->data, l->sz, sizeof *l->data);
71     }
72
73 }
74
75 /* Return the number of items in the list */
76 int
77 subc_list_double_count(const subc_list_double *l)
78 {
79   return l->n_data;
80 }
81
82 int
83 subc_list_int_count(const subc_list_int *l)
84 {
85   return l->n_data;
86 }
87
88
89 /* Index into the list (array) */
90 double
91 subc_list_double_at(const subc_list_double *l, int idx)
92 {
93   return l->data[idx];
94 }
95
96 int
97 subc_list_int_at(const subc_list_int *l, int idx)
98 {
99   return l->data[idx];
100 }
101
102 /* Free up the list */
103 void
104 subc_list_double_destroy(subc_list_double *l)
105 {
106   free(l->data);
107 }
108
109 void
110 subc_list_int_destroy(subc_list_int *l)
111 {
112   free(l->data);
113 }
114
115 void
116 subc_list_error (struct lexer *lexer, const char *sbc, int max_list)
117 {
118   lex_error (lexer, _("No more than %d %s subcommands allowed."),
119              max_list, sbc);
120 }