INPUT PROGRAM: Use a separate dataset for the input program.
[pspp] / src / data / session.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2010, 2011, 2012, 2013 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 #include <config.h>
18
19 #include "data/session.h"
20
21 #include <assert.h>
22 #include <stdlib.h>
23
24 #include "data/dataset.h"
25 #include "libpspp/assertion.h"
26 #include "libpspp/cast.h"
27 #include "libpspp/hash-functions.h"
28 #include "libpspp/i18n.h"
29 #include "libpspp/str.h"
30 #include "libpspp/hmapx.h"
31
32 #include "gl/xalloc.h"
33
34 struct session
35   {
36     struct session *parent;
37     struct hmapx datasets;
38     struct dataset *active;
39     char *syntax_encoding;      /* Default encoding for syntax files. */
40     unsigned int n_dataset_names; /* For session_generate_dataset_name(). */
41   };
42
43 static struct hmapx_node *session_lookup_dataset__ (const struct session *,
44                                                     const char *name);
45
46 struct session *
47 session_create (struct session *parent)
48 {
49   struct session *s;
50
51   s = xmalloc (sizeof *s);
52   s->parent = parent;
53   hmapx_init (&s->datasets);
54   s->active = NULL;
55   s->syntax_encoding = xstrdup (s->parent != NULL
56                                 ? s->parent->syntax_encoding : "Auto");
57   s->n_dataset_names = 0;
58   return s;
59 }
60
61 void
62 session_destroy (struct session *s)
63 {
64   if (s != NULL)
65     {
66       struct hmapx_node *node, *next;
67       struct dataset *ds;
68
69       s->active = NULL;
70       HMAPX_FOR_EACH_SAFE (ds, node, next, &s->datasets)
71         dataset_destroy (ds);
72       free (s->syntax_encoding);
73       free (s);
74     }
75 }
76
77 struct dataset *
78 session_active_dataset (struct session *s)
79 {
80   return s->active;
81 }
82
83 void
84 session_set_active_dataset (struct session *s, struct dataset *ds)
85 {
86   assert (ds == NULL || dataset_session (ds) == s);
87   s->active = ds;
88 }
89
90 void
91 session_add_dataset (struct session *s, struct dataset *ds)
92 {
93   struct dataset *old;
94
95   old = session_lookup_dataset (s, dataset_name (ds));
96   if (old == s->active)
97     s->active = ds;
98   if (old != NULL)
99     session_remove_dataset (s, old);
100
101   hmapx_insert (&s->datasets, ds,
102                 utf8_hash_case_string (dataset_name (ds), 0));
103   if (s->active == NULL)
104     s->active = ds;
105
106   dataset_set_session__ (ds, s);
107 }
108
109 void
110 session_remove_dataset (struct session *s, struct dataset *ds)
111 {
112   assert (ds != s->active);
113   hmapx_delete (&s->datasets, session_lookup_dataset__ (s, dataset_name (ds)));
114   dataset_set_session__ (ds, NULL);
115 }
116
117 struct dataset *
118 session_lookup_dataset (const struct session *s, const char *name)
119 {
120   struct hmapx_node *node = session_lookup_dataset__ (s, name);
121   return (node != NULL ? node->data
122           : s->parent != NULL ? session_lookup_dataset (s->parent, name)
123           : NULL);
124 }
125
126 struct dataset *
127 session_lookup_dataset_assert (const struct session *s, const char *name)
128 {
129   struct dataset *ds = session_lookup_dataset (s, name);
130   assert (ds != NULL);
131   return ds;
132 }
133
134 void
135 session_set_default_syntax_encoding (struct session *s, const char *encoding)
136 {
137   free (s->syntax_encoding);
138   s->syntax_encoding = xstrdup (encoding);
139 }
140
141 const char *
142 session_get_default_syntax_encoding (const struct session *s)
143 {
144   return s->syntax_encoding;
145 }
146
147 size_t
148 session_n_datasets (const struct session *s)
149 {
150   return hmapx_count (&s->datasets);
151 }
152
153 void
154 session_for_each_dataset (const struct session *s,
155                           void (*cb) (struct dataset *, void *aux),
156                           void *aux)
157 {
158   struct hmapx_node *node, *next;
159   struct dataset *ds;
160
161   HMAPX_FOR_EACH_SAFE (ds, node, next, &s->datasets)
162     cb (ds, aux);
163 }
164
165 struct dataset *
166 session_get_dataset_by_seqno (const struct session *s, unsigned int seqno)
167 {
168   struct hmapx_node *node;
169   struct dataset *ds;
170
171   HMAPX_FOR_EACH (ds, node, &s->datasets)
172     if (dataset_seqno (ds) == seqno)
173       return ds;
174   return NULL;
175 }
176
177 /* Returns an identifier that is is not currently in use as a dataset name.
178    The caller must free the returned identifier, with free(). */
179 char *
180 session_generate_dataset_name (struct session *s)
181 {
182   for (;;)
183     {
184       char *name;
185
186       s->n_dataset_names++;
187       assert(s->n_dataset_names != 0);
188
189       name = xasprintf ("DataSet%u", s->n_dataset_names);
190       if (!session_lookup_dataset (s, name))
191         return name;
192       free (name);
193     }
194 }
195 \f
196 static struct hmapx_node *
197 session_lookup_dataset__ (const struct session *s_, const char *name)
198 {
199   struct session *s = CONST_CAST (struct session *, s_);
200   struct hmapx_node *node;
201   struct dataset *ds;
202
203   HMAPX_FOR_EACH_WITH_HASH (ds, node, utf8_hash_case_string (name, 0),
204                             &s->datasets)
205     if (!utf8_strcasecmp (dataset_name (ds), name))
206       return node;
207
208   return NULL;
209 }