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/value.h>
32 #include <data/variable.h>
33 #include <language/command.h>
34 #include <language/lexer/lexer.h>
35 #include <language/lexer/variable-parser.h>
36 #include <libpspp/array.h>
37 #include <libpspp/assertion.h>
38 #include <libpspp/message.h>
39 #include <libpspp/misc.h>
40 #include <libpspp/pool.h>
41 #include <libpspp/str.h>
48 #define _(msgid) gettext (msgid)
50 /* List of variable names. */
54 char name[SHORT_NAME_LEN + 1];
57 /* Represents a FLIP input program. */
60 struct pool *pool; /* Pool containing FLIP data. */
61 const struct variable **var; /* Variables to transpose. */
62 size_t var_cnt; /* Number of elements in `var'. */
63 int case_cnt; /* Pre-flip case count. */
65 struct variable *new_names; /* Variable containing new variable names. */
66 struct varname *new_names_head; /* First new variable. */
67 struct varname *new_names_tail; /* Last new variable. */
69 FILE *file; /* Temporary file containing data. */
70 union value *input_buf; /* Input buffer for temporary file. */
71 size_t cases_read; /* Number of cases already read. */
72 bool error; /* Error reading temporary file? */
75 static const struct casereader_class flip_casereader_class;
77 static void destroy_flip_pgm (struct flip_pgm *);
78 static bool flip_file (struct flip_pgm *);
79 static bool build_dictionary (struct dictionary *, struct flip_pgm *);
80 static bool write_flip_case (struct flip_pgm *, const struct ccase *);
82 /* Parses and executes FLIP. */
84 cmd_flip (struct lexer *lexer, struct dataset *ds)
86 struct dictionary *dict = dataset_dict (ds);
87 struct flip_pgm *flip;
88 struct casereader *input, *reader;
89 union value *output_buf;
94 if (proc_make_temporary_transformations_permanent (ds))
95 msg (SW, _("FLIP ignores TEMPORARY. "
96 "Temporary transformations will be made permanent."));
98 flip = pool_create_container (struct flip_pgm, pool);
102 flip->new_names = NULL;
103 flip->new_names_head = NULL;
104 flip->new_names_tail = NULL;
106 flip->input_buf = NULL;
107 flip->cases_read = 0;
110 lex_match (lexer, '/');
111 if (lex_match_id (lexer, "VARIABLES"))
113 lex_match (lexer, '=');
114 if (!parse_variables_const (lexer, dict, &flip->var, &flip->var_cnt,
117 lex_match (lexer, '/');
120 dict_get_vars (dict, &flip->var, &flip->var_cnt, 1u << DC_SYSTEM);
121 pool_register (flip->pool, free, flip->var);
123 lex_match (lexer, '/');
124 if (lex_match_id (lexer, "NEWNAMES"))
126 lex_match (lexer, '=');
127 flip->new_names = parse_variable (lexer, dict);
128 if (!flip->new_names)
132 flip->new_names = dict_lookup_var (dict, "CASE_LBL");
136 for (i = 0; i < flip->var_cnt; i++)
137 if (flip->var[i] == flip->new_names)
139 remove_element (flip->var, flip->var_cnt, sizeof *flip->var, i);
145 output_buf = pool_nalloc (flip->pool, flip->var_cnt, sizeof *output_buf);
147 flip->file = pool_tmpfile (flip->pool);
148 if (flip->file == NULL)
150 msg (SE, _("Could not create temporary file for FLIP."));
154 /* Write variable names as first case. */
155 for (i = 0; i < flip->var_cnt; i++)
156 buf_copy_str_rpad (output_buf[i].s, MAX_SHORT_STRING,
157 var_get_name (flip->var[i]));
158 if (fwrite (output_buf, sizeof *output_buf,
159 flip->var_cnt, flip->file) != (size_t) flip->var_cnt)
161 msg (SE, _("Error writing FLIP file: %s."), strerror (errno));
167 /* Read the active file into a flip_sink. */
168 proc_discard_output (ds);
170 input = proc_open (ds);
171 while (casereader_read (input, &c))
173 write_flip_case (flip, &c);
176 ok = casereader_destroy (input);
177 ok = proc_commit (ds) && ok;
179 /* Flip the data we read. */
180 if (!ok || !flip_file (flip))
182 proc_discard_active_file (ds);
186 /* Flip the dictionary. */
188 if (!build_dictionary (dict, flip))
190 proc_discard_active_file (ds);
194 /* Set up flipped data for reading. */
195 reader = casereader_create_sequential (NULL, dict_get_next_value_idx (dict),
197 &flip_casereader_class, flip);
198 proc_set_active_file_data (ds, reader);
199 return lex_end_of_command (lexer);
202 destroy_flip_pgm (flip);
203 return CMD_CASCADING_FAILURE;
208 destroy_flip_pgm (struct flip_pgm *flip)
211 pool_destroy (flip->pool);
214 /* Make a new variable with base name NAME, which is bowdlerized and
215 mangled until acceptable, and returns success. */
217 make_new_var (struct dictionary *dict, char name[])
221 /* Trim trailing spaces. */
222 cp = strchr (name, '\0');
223 while (cp > name && isspace ((unsigned char) cp[-1]))
226 /* Fix invalid characters. */
227 for (cp = name; *cp && cp < name + SHORT_NAME_LEN; cp++)
230 if (!lex_is_id1 (*cp) || *cp == '$')
235 if (!lex_is_idn (*cp))
239 str_uppercase (name);
241 if (dict_create_var (dict, name, 0))
244 /* Add numeric extensions until acceptable. */
246 const int len = (int) strlen (name);
247 char n[SHORT_NAME_LEN + 1];
250 for (i = 1; i < 10000000; i++)
252 int ofs = MIN (7 - intlog10 (i), len);
253 memcpy (n, name, ofs);
254 sprintf (&n[ofs], "%d", i);
256 if (dict_create_var (dict, n, 0))
261 msg (SE, _("Could not create acceptable variant for variable %s."), name);
265 /* Make a new dictionary for all the new variable names. */
267 build_dictionary (struct dictionary *dict, struct flip_pgm *flip)
269 dict_create_var_assert (dict, "CASE_LBL", 8);
271 if (flip->new_names_head == NULL)
275 if (flip->case_cnt > 99999)
277 msg (SE, _("Cannot create more than 99999 variable names."));
281 for (i = 0; i < flip->case_cnt - 1; i++)
284 char s[SHORT_NAME_LEN + 1];
286 sprintf (s, "VAR%03d", i);
287 v = dict_create_var_assert (dict, s, 0);
294 for (v = flip->new_names_head; v; v = v->next)
295 if (!make_new_var (dict, v->name))
302 /* Writes case C to the FLIP sink.
303 Returns true if successful, false if an I/O error occurred. */
305 write_flip_case (struct flip_pgm *flip, const struct ccase *c)
311 if (flip->new_names != NULL)
313 struct varname *v = pool_alloc (flip->pool, sizeof *v);
315 if (var_is_numeric (flip->new_names))
317 double f = case_num (c, flip->new_names);
320 strcpy (v->name, "VSYSMIS");
321 else if (f < INT_MIN)
322 strcpy (v->name, "VNEGINF");
323 else if (f > INT_MAX)
324 strcpy (v->name, "VPOSINF");
326 snprintf (v->name, sizeof v->name, "V%d", (int) f);
330 int width = MIN (var_get_width (flip->new_names), MAX_SHORT_STRING);
331 memcpy (v->name, case_str (c, flip->new_names), width);
335 if (flip->new_names_head == NULL)
336 flip->new_names_head = v;
338 flip->new_names_tail->next = v;
339 flip->new_names_tail = v;
342 /* Write to external file. */
343 for (i = 0; i < flip->var_cnt; i++)
345 const struct variable *v = flip->var[i];
346 double out = var_is_numeric (v) ? case_num (c, v) : SYSMIS;
347 fwrite (&out, sizeof out, 1, flip->file);
352 /* Transposes the external file into a new file. */
354 flip_file (struct flip_pgm *flip)
357 size_t case_capacity;
359 union value *input_buf, *output_buf;
360 FILE *input_file, *output_file;
362 /* Allocate memory for many cases. */
363 case_bytes = flip->var_cnt * sizeof *input_buf;
364 case_capacity = get_workspace () / case_bytes;
365 if (case_capacity > flip->case_cnt * 2)
366 case_capacity = flip->case_cnt * 2;
367 if (case_capacity < 2)
371 size_t bytes = case_bytes * case_capacity;
372 if (case_capacity > 2)
373 input_buf = malloc (bytes);
375 input_buf = xmalloc (bytes);
376 if (input_buf != NULL)
380 if (case_capacity < 2)
383 pool_register (flip->pool, free, input_buf);
385 /* Use half the allocated memory for input_buf, half for
388 output_buf = input_buf + flip->var_cnt * case_capacity;
390 input_file = flip->file;
391 if (fseek (input_file, 0, SEEK_SET) != 0)
393 msg (SE, _("Error rewinding FLIP file: %s."), strerror (errno));
397 output_file = pool_tmpfile (flip->pool);
398 if (output_file == NULL)
400 msg (SE, _("Error creating FLIP source file."));
404 for (case_idx = 0; case_idx < flip->case_cnt; )
406 unsigned long read_cases = MIN (flip->case_cnt - case_idx,
410 if (read_cases != fread (input_buf, case_bytes, read_cases, input_file))
412 if (ferror (input_file))
413 msg (SE, _("Error reading FLIP file: %s."), strerror (errno));
415 msg (SE, _("Unexpected end of file reading FLIP file."));
419 for (i = 0; i < flip->var_cnt; i++)
423 for (j = 0; j < read_cases; j++)
424 output_buf[j] = input_buf[i + j * flip->var_cnt];
431 #define off_t long int
434 if (fseeko (output_file,
435 sizeof *input_buf * (case_idx
436 + (off_t) i * flip->case_cnt),
439 msg (SE, _("Error seeking FLIP source file: %s."),
444 if (fwrite (output_buf, sizeof *output_buf, read_cases, output_file)
447 msg (SE, _("Error writing FLIP source file: %s."),
453 case_idx += read_cases;
456 if (pool_fclose (flip->pool, input_file) == EOF)
458 msg (SE, _("Error closing FLIP source file: %s."), strerror (errno));
461 pool_unregister (flip->pool, input_buf);
464 if (fseek (output_file, 0, SEEK_SET) != 0)
466 msg (SE, _("Error rewinding FLIP source file: %s."), strerror (errno));
469 flip->file = output_file;
474 /* Reads one case into C.
475 Returns true if successful, false at end of file or if an
476 I/O error occurred. */
478 flip_casereader_read (struct casereader *reader UNUSED, void *flip_,
481 struct flip_pgm *flip = flip_;
484 if (flip->error || flip->cases_read >= flip->var_cnt)
487 case_create (c, flip->case_cnt);
488 for (i = 0; i < flip->case_cnt; i++)
491 if (fread (&in, sizeof in, 1, flip->file) != 1)
494 if (ferror (flip->file))
495 msg (SE, _("Error reading FLIP temporary file: %s."),
497 else if (feof (flip->file))
498 msg (SE, _("Unexpected end of file reading FLIP temporary file."));
504 case_data_rw_idx (c, i)->f = in;
512 /* Destroys the source.
513 Returns true if successful read, false if an I/O occurred
514 during destruction or previously. */
516 flip_casereader_destroy (struct casereader *reader UNUSED, void *flip_)
518 struct flip_pgm *flip = flip_;
520 casereader_force_error (reader);
521 destroy_flip_pgm (flip);
524 static const struct casereader_class flip_casereader_class =
526 flip_casereader_read,
527 flip_casereader_destroy,