1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
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.
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.
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/>. */
25 #include <data/case.h>
26 #include <data/casereader.h>
27 #include <data/casereader-provider.h>
28 #include <data/dictionary.h>
29 #include <data/procedure.h>
30 #include <data/settings.h>
31 #include <data/short-names.h>
32 #include <data/value.h>
33 #include <data/variable.h>
34 #include <language/command.h>
35 #include <language/lexer/lexer.h>
36 #include <language/lexer/variable-parser.h>
37 #include <libpspp/array.h>
38 #include <libpspp/assertion.h>
39 #include <libpspp/message.h>
40 #include <libpspp/misc.h>
41 #include <libpspp/pool.h>
42 #include <libpspp/str.h>
49 #define _(msgid) gettext (msgid)
51 /* List of variable names. */
55 char name[SHORT_NAME_LEN + 1];
58 /* Represents a FLIP input program. */
61 struct pool *pool; /* Pool containing FLIP data. */
62 const struct variable **var; /* Variables to transpose. */
63 size_t var_cnt; /* Number of elements in `var'. */
64 int case_cnt; /* Pre-flip case count. */
66 struct variable *new_names; /* Variable containing new variable names. */
67 struct varname *new_names_head; /* First new variable. */
68 struct varname *new_names_tail; /* Last new variable. */
70 FILE *file; /* Temporary file containing data. */
71 union value *input_buf; /* Input buffer for temporary file. */
72 size_t cases_read; /* Number of cases already read. */
73 bool error; /* Error reading temporary file? */
76 static const struct casereader_class flip_casereader_class;
78 static void destroy_flip_pgm (struct flip_pgm *);
79 static bool flip_file (struct flip_pgm *);
80 static bool build_dictionary (struct dictionary *, struct flip_pgm *);
81 static bool write_flip_case (struct flip_pgm *, const struct ccase *);
83 /* Parses and executes FLIP. */
85 cmd_flip (struct lexer *lexer, struct dataset *ds)
87 struct dictionary *dict = dataset_dict (ds);
88 struct flip_pgm *flip;
89 struct casereader *input, *reader;
90 union value *output_buf;
95 if (proc_make_temporary_transformations_permanent (ds))
96 msg (SW, _("FLIP ignores TEMPORARY. "
97 "Temporary transformations will be made permanent."));
99 flip = pool_create_container (struct flip_pgm, pool);
103 flip->new_names = NULL;
104 flip->new_names_head = NULL;
105 flip->new_names_tail = NULL;
107 flip->input_buf = NULL;
108 flip->cases_read = 0;
111 lex_match (lexer, '/');
112 if (lex_match_id (lexer, "VARIABLES"))
114 lex_match (lexer, '=');
115 if (!parse_variables_const (lexer, dict, &flip->var, &flip->var_cnt,
118 lex_match (lexer, '/');
121 dict_get_vars (dict, &flip->var, &flip->var_cnt, DC_SYSTEM);
122 pool_register (flip->pool, free, flip->var);
124 lex_match (lexer, '/');
125 if (lex_match_id (lexer, "NEWNAMES"))
127 lex_match (lexer, '=');
128 flip->new_names = parse_variable (lexer, dict);
129 if (!flip->new_names)
133 flip->new_names = dict_lookup_var (dict, "CASE_LBL");
137 for (i = 0; i < flip->var_cnt; i++)
138 if (flip->var[i] == flip->new_names)
140 remove_element (flip->var, flip->var_cnt, sizeof *flip->var, i);
146 output_buf = pool_nalloc (flip->pool, flip->var_cnt, sizeof *output_buf);
148 flip->file = pool_tmpfile (flip->pool);
149 if (flip->file == NULL)
151 msg (SE, _("Could not create temporary file for FLIP."));
155 /* Write variable names as first case. */
156 for (i = 0; i < flip->var_cnt; i++)
157 buf_copy_str_rpad (output_buf[i].s, MAX_SHORT_STRING,
158 var_get_name (flip->var[i]));
159 if (fwrite (output_buf, sizeof *output_buf,
160 flip->var_cnt, flip->file) != (size_t) flip->var_cnt)
162 msg (SE, _("Error writing FLIP file: %s."), strerror (errno));
168 /* Read the active file into a flip_sink. */
169 proc_discard_output (ds);
171 input = proc_open (ds);
172 while (casereader_read (input, &c))
174 write_flip_case (flip, &c);
177 ok = casereader_destroy (input);
178 ok = proc_commit (ds) && ok;
180 /* Flip the data we read. */
181 if (!ok || !flip_file (flip))
183 proc_discard_active_file (ds);
187 /* Flip the dictionary. */
189 if (!build_dictionary (dict, flip))
191 proc_discard_active_file (ds);
195 /* Set up flipped data for reading. */
196 reader = casereader_create_sequential (NULL, dict_get_next_value_idx (dict),
198 &flip_casereader_class, flip);
199 proc_set_active_file_data (ds, reader);
200 return lex_end_of_command (lexer);
203 destroy_flip_pgm (flip);
204 return CMD_CASCADING_FAILURE;
209 destroy_flip_pgm (struct flip_pgm *flip)
212 pool_destroy (flip->pool);
215 /* Make a new variable with base name NAME, which is bowdlerized and
216 mangled until acceptable, and returns success. */
218 make_new_var (struct dictionary *dict, char name[])
222 /* Trim trailing spaces. */
223 cp = strchr (name, '\0');
224 while (cp > name && isspace ((unsigned char) cp[-1]))
227 /* Fix invalid characters. */
228 for (cp = name; *cp && cp < name + SHORT_NAME_LEN; cp++)
231 if (!lex_is_id1 (*cp) || *cp == '$')
236 if (!lex_is_idn (*cp))
240 str_uppercase (name);
242 if (dict_create_var (dict, name, 0))
245 /* Add numeric extensions until acceptable. */
247 const int len = (int) strlen (name);
248 char n[SHORT_NAME_LEN + 1];
251 for (i = 1; i < 10000000; i++)
253 int ofs = MIN (7 - intlog10 (i), len);
254 memcpy (n, name, ofs);
255 sprintf (&n[ofs], "%d", i);
257 if (dict_create_var (dict, n, 0))
262 msg (SE, _("Could not create acceptable variant for variable %s."), name);
266 /* Make a new dictionary for all the new variable names. */
268 build_dictionary (struct dictionary *dict, struct flip_pgm *flip)
270 dict_create_var_assert (dict, "CASE_LBL", 8);
272 if (flip->new_names_head == NULL)
276 if (flip->case_cnt > 99999)
278 msg (SE, _("Cannot create more than 99999 variable names."));
282 for (i = 0; i < flip->case_cnt - 1; i++)
285 char s[SHORT_NAME_LEN + 1];
287 sprintf (s, "VAR%03d", i);
288 v = dict_create_var_assert (dict, s, 0);
295 for (v = flip->new_names_head; v; v = v->next)
296 if (!make_new_var (dict, v->name))
303 /* Writes case C to the FLIP sink.
304 Returns true if successful, false if an I/O error occurred. */
306 write_flip_case (struct flip_pgm *flip, const struct ccase *c)
312 if (flip->new_names != NULL)
314 struct varname *v = pool_alloc (flip->pool, sizeof *v);
316 if (var_is_numeric (flip->new_names))
318 double f = case_num (c, flip->new_names);
321 strcpy (v->name, "VSYSMIS");
322 else if (f < INT_MIN)
323 strcpy (v->name, "VNEGINF");
324 else if (f > INT_MAX)
325 strcpy (v->name, "VPOSINF");
327 snprintf (v->name, sizeof v->name, "V%d", (int) f);
331 int width = MIN (var_get_width (flip->new_names), MAX_SHORT_STRING);
332 memcpy (v->name, case_str (c, flip->new_names), width);
336 if (flip->new_names_head == NULL)
337 flip->new_names_head = v;
339 flip->new_names_tail->next = v;
340 flip->new_names_tail = v;
343 /* Write to external file. */
344 for (i = 0; i < flip->var_cnt; i++)
346 const struct variable *v = flip->var[i];
347 double out = var_is_numeric (v) ? case_num (c, v) : SYSMIS;
348 fwrite (&out, sizeof out, 1, flip->file);
353 /* Transposes the external file into a new file. */
355 flip_file (struct flip_pgm *flip)
358 size_t case_capacity;
360 union value *input_buf, *output_buf;
361 FILE *input_file, *output_file;
363 /* Allocate memory for many cases. */
364 case_bytes = flip->var_cnt * sizeof *input_buf;
365 case_capacity = get_workspace () / case_bytes;
366 if (case_capacity > flip->case_cnt * 2)
367 case_capacity = flip->case_cnt * 2;
368 if (case_capacity < 2)
372 size_t bytes = case_bytes * case_capacity;
373 if (case_capacity > 2)
374 input_buf = malloc (bytes);
376 input_buf = xmalloc (bytes);
377 if (input_buf != NULL)
381 if (case_capacity < 2)
384 pool_register (flip->pool, free, input_buf);
386 /* Use half the allocated memory for input_buf, half for
389 output_buf = input_buf + flip->var_cnt * case_capacity;
391 input_file = flip->file;
392 if (fseek (input_file, 0, SEEK_SET) != 0)
394 msg (SE, _("Error rewinding FLIP file: %s."), strerror (errno));
398 output_file = pool_tmpfile (flip->pool);
399 if (output_file == NULL)
401 msg (SE, _("Error creating FLIP source file."));
405 for (case_idx = 0; case_idx < flip->case_cnt; )
407 unsigned long read_cases = MIN (flip->case_cnt - case_idx,
411 if (read_cases != fread (input_buf, case_bytes, read_cases, input_file))
413 if (ferror (input_file))
414 msg (SE, _("Error reading FLIP file: %s."), strerror (errno));
416 msg (SE, _("Unexpected end of file reading FLIP file."));
420 for (i = 0; i < flip->var_cnt; i++)
424 for (j = 0; j < read_cases; j++)
425 output_buf[j] = input_buf[i + j * flip->var_cnt];
427 if (fseeko (output_file,
428 sizeof *input_buf * (case_idx
429 + (off_t) i * flip->case_cnt),
432 msg (SE, _("Error seeking FLIP source file: %s."),
437 if (fwrite (output_buf, sizeof *output_buf, read_cases, output_file)
440 msg (SE, _("Error writing FLIP source file: %s."),
446 case_idx += read_cases;
449 if (pool_fclose (flip->pool, input_file) == EOF)
451 msg (SE, _("Error closing FLIP source file: %s."), strerror (errno));
454 pool_unregister (flip->pool, input_buf);
457 if (fseek (output_file, 0, SEEK_SET) != 0)
459 msg (SE, _("Error rewinding FLIP source file: %s."), strerror (errno));
462 flip->file = output_file;
467 /* Reads one case into C.
468 Returns true if successful, false at end of file or if an
469 I/O error occurred. */
471 flip_casereader_read (struct casereader *reader UNUSED, void *flip_,
474 struct flip_pgm *flip = flip_;
477 if (flip->error || flip->cases_read >= flip->var_cnt)
480 case_create (c, flip->case_cnt);
481 for (i = 0; i < flip->case_cnt; i++)
484 if (fread (&in, sizeof in, 1, flip->file) != 1)
487 if (ferror (flip->file))
488 msg (SE, _("Error reading FLIP temporary file: %s."),
490 else if (feof (flip->file))
491 msg (SE, _("Unexpected end of file reading FLIP temporary file."));
497 case_data_rw_idx (c, i)->f = in;
505 /* Destroys the source.
506 Returns true if successful read, false if an I/O occurred
507 during destruction or previously. */
509 flip_casereader_destroy (struct casereader *reader UNUSED, void *flip_)
511 struct flip_pgm *flip = flip_;
513 casereader_force_error (reader);
514 destroy_flip_pgm (flip);
517 static const struct casereader_class flip_casereader_class =
519 flip_casereader_read,
520 flip_casereader_destroy,