1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2007, 2009, 2010, 2011, 2015 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/>. */
19 #include "data/settings.h"
24 #include "data/case.h"
25 #include "data/format.h"
26 #include "data/value.h"
27 #include "libpspp/i18n.h"
28 #include "libpspp/integer-format.h"
29 #include "libpspp/message.h"
31 #include "gl/minmax.h"
32 #include "gl/xalloc.h"
35 #define _(msgid) gettext (msgid)
39 /* Integer format used for IB and PIB input. */
40 enum integer_format input_integer_format;
42 /* Floating-point format used for RB and RBHEX input. */
43 enum float_format input_float_format;
45 /* Format of integers in output (SET WIB). */
46 enum integer_format output_integer_format;
48 /* Format of reals in output (SET WRB). */
49 enum float_format output_float_format;
55 bool route_errors_to_terminal;
56 bool route_errors_to_listing;
60 int max_messages[MSG_N_SEVERITIES];
65 struct fmt_spec default_format;
73 struct fmt_settings styles;
76 enum settings_output_devices output_routing[SETTINGS_N_OUTPUT_TYPES];
78 enum settings_value_show show_values;
79 enum settings_value_show show_variables;
82 static struct settings the_settings = {
83 .input_integer_format = INTEGER_NATIVE,
84 .input_float_format = FLOAT_NATIVE_DOUBLE,
85 .output_integer_format = INTEGER_NATIVE,
86 .output_float_format = FLOAT_NATIVE_DOUBLE,
91 .route_errors_to_terminal = true,
92 .route_errors_to_listing = true,
99 [MSG_S_WARNING] = 100,
106 .workspace = 64L * 1024 * 1024,
107 .default_format = { .type = FMT_F, .w = 8, .d = 2 },
108 .testing_mode = false,
110 .cmd_algorithm = ENHANCED,
111 .global_algorithm = ENHANCED,
113 .styles = FMT_SETTINGS_INIT,
118 #define LT (SETTINGS_DEVICE_LISTING | SETTINGS_DEVICE_TERMINAL)
119 [SETTINGS_OUTPUT_ERROR] = LT,
120 [SETTINGS_OUTPUT_NOTE] = LT,
121 [SETTINGS_OUTPUT_SYNTAX] = 0,
122 [SETTINGS_OUTPUT_RESULT] = LT
126 .show_values = SETTINGS_VALUE_SHOW_LABEL,
127 .show_variables = SETTINGS_VALUE_SHOW_LABEL,
130 /* Initializes the settings module. */
134 settings_set_decimal_char (get_system_decimal ());
137 /* Cleans up the settings module. */
141 settings_destroy (&the_settings);
145 settings_copy (struct settings *dst, const struct settings *src)
148 dst->styles = fmt_settings_copy (&src->styles);
151 /* Returns a copy of the current settings. */
155 struct settings *s = xmalloc (sizeof *s);
156 settings_copy (s, &the_settings);
160 /* Replaces the current settings by those in S. The caller retains ownership
163 settings_set (const struct settings *s)
165 settings_destroy (&the_settings);
166 settings_copy (&the_settings, s);
171 settings_destroy (struct settings *s)
175 fmt_settings_uninit (&s->styles);
176 if (s != &the_settings)
181 /* Returns the floating-point format used for RB and RBHEX
184 settings_get_input_float_format (void)
186 return the_settings.input_float_format;
189 /* Sets the floating-point format used for RB and RBHEX input to
192 settings_set_input_float_format (enum float_format format)
194 the_settings.input_float_format = format;
197 /* Returns the integer format used for IB and PIB input. */
199 settings_get_input_integer_format (void)
201 return the_settings.input_integer_format;
204 /* Sets the integer format used for IB and PIB input to
207 settings_set_input_integer_format (enum integer_format format)
209 the_settings.input_integer_format = format;
212 /* Returns the current output integer format. */
214 settings_get_output_integer_format (void)
216 return the_settings.output_integer_format;
219 /* Sets the output integer format to INTEGER_FORMAT. */
221 settings_set_output_integer_format (
222 enum integer_format integer_format)
224 the_settings.output_integer_format = integer_format;
227 /* Returns the current output float format. */
229 settings_get_output_float_format (void)
231 return the_settings.output_float_format;
234 /* Sets the output float format to FLOAT_FORMAT. */
236 settings_set_output_float_format (enum float_format float_format)
238 the_settings.output_float_format = float_format;
241 /* Screen length in lines. */
243 settings_get_viewlength (void)
245 return the_settings.viewlength;
248 /* Sets the view length. */
250 settings_set_viewlength (int viewlength_)
252 the_settings.viewlength = viewlength_;
257 settings_get_viewwidth(void)
259 return the_settings.viewwidth;
262 /* Sets the screen width. */
264 settings_set_viewwidth (int viewwidth_)
266 the_settings.viewwidth = viewwidth_;
269 /* Whether PSPP can erase and overwrite files. */
271 settings_get_safer_mode (void)
273 return the_settings.safer_mode;
276 /* Set safer mode. */
278 settings_set_safer_mode (void)
280 the_settings.safer_mode = true;
283 /* If echo is on, whether commands from include files are echoed. */
285 settings_get_include (void)
287 return the_settings.include;
290 /* Set include file echo. */
292 settings_set_include (bool include)
294 the_settings.include = include;
297 /* Sets the year that starts the epoch. */
299 settings_set_epoch (int epoch)
301 the_settings.styles.epoch = epoch;
304 /* Compress system files by default? */
306 settings_get_scompression (void)
308 return the_settings.scompress;
311 /* Set system file default compression. */
313 settings_set_scompression (bool scompress)
315 the_settings.scompress = scompress;
318 /* Whether to warn on undefined values in numeric data. */
320 settings_get_undefined (void)
322 return the_settings.undefined;
325 /* Set whether to warn on undefined values. */
327 settings_set_undefined (bool undefined)
329 the_settings.undefined = undefined;
332 /* The value that blank numeric fields are set to when read in. */
334 settings_get_blanks (void)
336 return the_settings.blanks;
339 /* Set the value that blank numeric fields are set to when read
342 settings_set_blanks (double blanks)
344 the_settings.blanks = blanks;
347 /* Returns the maximum number of messages to show of the given SEVERITY before
348 aborting. (The value for MSG_S_WARNING is interpreted as maximum number of
349 warnings and errors combined.) */
351 settings_get_max_messages (enum msg_severity severity)
353 assert (severity < MSG_N_SEVERITIES);
354 return the_settings.max_messages[severity];
357 /* Sets the maximum number of messages to show of the given SEVERITY before
358 aborting to MAX. (The value for MSG_S_WARNING is interpreted as maximum
359 number of warnings and errors combined.) In addition, in the case of
360 warnings the special value of zero indicates that no warnings are to be
364 settings_set_max_messages (enum msg_severity severity, int max)
366 assert (severity < MSG_N_SEVERITIES);
368 if (severity == MSG_S_WARNING)
373 _("MXWARNS set to zero. No further warnings will be given even when potentially problematic situations are encountered."));
374 msg_ui_disable_warnings (true);
376 else if (the_settings.max_messages [MSG_S_WARNING] == 0)
378 msg_ui_disable_warnings (false);
379 the_settings.max_messages[MSG_S_WARNING] = max;
380 msg (MW, _("Warnings re-enabled. %d warnings will be issued before aborting syntax processing."), max);
384 the_settings.max_messages[severity] = max;
387 /* Independent of get_printback, controls whether the commands
388 generated by macro invocations are displayed. */
390 settings_get_mprint (void)
392 return the_settings.mprint;
395 /* Sets whether the commands generated by macro invocations are
398 settings_set_mprint (bool mprint)
400 the_settings.mprint = mprint;
403 /* Implied limit of unbounded loop. */
405 settings_get_mxloops (void)
407 return the_settings.mxloops;
410 /* Set implied limit of unbounded loop. */
412 settings_set_mxloops (int mxloops)
414 the_settings.mxloops = mxloops;
417 /* Approximate maximum amount of memory to use for cases, in
420 settings_get_workspace (void)
422 return the_settings.workspace;
425 /* Approximate maximum number of cases to allocate in-core, given
426 that each case has the format given in PROTO. */
428 settings_get_workspace_cases (const struct caseproto *proto)
430 size_t n_cases = settings_get_workspace () / case_get_cost (proto);
431 return MAX (n_cases, 4);
434 /* Set approximate maximum amount of memory to use for cases, in
438 settings_set_workspace (size_t workspace)
440 the_settings.workspace = workspace;
443 /* Default format for variables created by transformations and by
444 DATA LIST {FREE,LIST}. */
445 const struct fmt_spec *
446 settings_get_format (void)
448 return &the_settings.default_format;
451 /* Set default format for variables created by transformations
452 and by DATA LIST {FREE,LIST}. */
454 settings_set_format (const struct fmt_spec *default_format)
456 the_settings.default_format = *default_format;
459 /* Are we in testing mode? (e.g. --testing-mode command line
462 settings_get_testing_mode (void)
464 return the_settings.testing_mode;
467 /* Set testing mode. */
469 settings_set_testing_mode (bool testing_mode)
471 the_settings.testing_mode = testing_mode;
475 settings_get_fuzzbits (void)
477 return the_settings.fuzzbits;
481 settings_set_fuzzbits (int fuzzbits)
483 the_settings.fuzzbits = fuzzbits;
486 /* Return the current algorithm setting */
488 settings_get_algorithm (void)
490 return the_settings.cmd_algorithm;
493 /* Set the algorithm option globally. */
495 settings_set_algorithm (enum behavior_mode mode)
497 the_settings.global_algorithm = the_settings.cmd_algorithm = mode;
500 /* Set the algorithm option for this command only */
502 settings_set_cmd_algorithm (enum behavior_mode mode)
504 the_settings.cmd_algorithm = mode;
507 /* Unset the algorithm option for this command */
509 unset_cmd_algorithm (void)
511 the_settings.cmd_algorithm = the_settings.global_algorithm;
514 /* Get the current syntax setting */
516 settings_get_syntax (void)
518 return the_settings.syntax;
521 /* Set the syntax option */
523 settings_set_syntax (enum behavior_mode mode)
525 the_settings.syntax = mode;
529 /* Sets custom currency specifier CC having name CC_NAME ('A' through
530 'E') to correspond to the settings in CC_STRING. */
532 settings_set_cc (const char *cc_string, enum fmt_type type)
534 struct fmt_number_style *style = fmt_number_style_from_string (cc_string);
537 msg (SE, _("%s: Custom currency string `%s' does not contain "
538 "exactly three periods or commas (or it contains both)."),
539 fmt_name (type), cc_string);
543 fmt_settings_set_cc (&the_settings.styles, type, style);
548 settings_set_decimal_char (char decimal)
550 the_settings.styles.decimal = decimal;
553 const struct fmt_settings *
554 settings_get_fmt_settings (void)
556 return &the_settings.styles;
560 settings_get_small (void)
562 return the_settings.small;
566 settings_set_small (double small)
568 the_settings.small = small;
571 /* Returns a string of the form "$#,###.##" according to FMT,
572 which must be of type FMT_DOLLAR. The caller must free the
575 settings_dollar_template (const struct fmt_spec *fmt)
577 struct string str = DS_EMPTY_INITIALIZER;
579 const struct fmt_number_style *fns ;
581 assert (fmt->type == FMT_DOLLAR);
583 fns = fmt_settings_get_style (&the_settings.styles, fmt->type);
585 ds_put_byte (&str, '$');
586 for (c = MAX (fmt->w - fmt->d - 1, 0); c > 0;)
588 ds_put_byte (&str, '#');
589 if (--c % 4 == 0 && c > 0)
591 ds_put_byte (&str, fns->grouping);
597 ds_put_byte (&str, fns->decimal);
598 ds_put_byte_multiple (&str, '#', fmt->d);
601 return ds_cstr (&str);
605 settings_set_output_routing (enum settings_output_type type,
606 enum settings_output_devices devices)
608 assert (type < SETTINGS_N_OUTPUT_TYPES);
609 the_settings.output_routing[type] = devices;
612 enum settings_output_devices
613 settings_get_output_routing (enum settings_output_type type)
615 assert (type < SETTINGS_N_OUTPUT_TYPES);
616 return the_settings.output_routing[type] | SETTINGS_DEVICE_UNFILTERED;
619 enum settings_value_show
620 settings_get_show_values (void)
622 return the_settings.show_values;
626 settings_set_show_values (enum settings_value_show s)
628 the_settings.show_values = s;
632 enum settings_value_show
633 settings_get_show_variables (void)
635 return the_settings.show_variables;
639 settings_set_show_variables (enum settings_value_show s)
641 the_settings.show_variables = s;