[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 05/13] format: New functions fmt_change_width(), fmt_change_decim
From: |
Ben Pfaff |
Subject: |
[PATCH 05/13] format: New functions fmt_change_width(), fmt_change_decimals(). |
Date: |
Mon, 16 Apr 2012 20:52:11 -0700 |
These will be used in an upcoming commit for GUI-based format
adjustments, where we want to implement the user's request for
width or decimal places to the extent that we can, while
adjusting decimal places or width, respectively, as necessary.
---
src/data/format.c | 75 +++++++++++++++++++++++++++++++++++++++-------------
src/data/format.h | 3 ++
2 files changed, 59 insertions(+), 19 deletions(-)
diff --git a/src/data/format.c b/src/data/format.c
index 3c407f4..1bab775 100644
--- a/src/data/format.c
+++ b/src/data/format.c
@@ -49,6 +49,8 @@ bool is_fmt_type (enum fmt_type);
static bool valid_width (enum fmt_type, int width, bool for_input);
static int max_digits_for_bytes (int bytes);
+static void fmt_clamp_width (struct fmt_spec *, bool for_input);
+static void fmt_clamp_decimals (struct fmt_spec *, bool for_input);
static void fmt_affix_set (struct fmt_affix *, const char *);
static void fmt_affix_free (struct fmt_affix *);
@@ -500,21 +502,8 @@ fmt_resize (struct fmt_spec *fmt, int width)
void
fmt_fix (struct fmt_spec *fmt, bool for_input)
{
- unsigned int step;
- int min_w, max_w;
- int max_d;
-
/* Clamp width to those allowed by format. */
- min_w = fmt_min_width (fmt->type, for_input);
- max_w = fmt_max_width (fmt->type, for_input);
- if (fmt->w < min_w)
- fmt->w = min_w;
- else if (fmt->w > max_w)
- fmt->w = max_w;
-
- /* Round width to step. */
- step = fmt_step_width (fmt->type);
- fmt->w = ROUND_DOWN (fmt->w, step);
+ fmt_clamp_width (fmt, for_input);
/* If FMT has more decimal places than allowed, attempt to increase FMT's
width until that number of decimal places can be achieved. */
@@ -528,11 +517,7 @@ fmt_fix (struct fmt_spec *fmt, bool for_input)
}
/* Clamp decimals to those allowed by format and width. */
- max_d = fmt_max_decimals (fmt->type, fmt->w, for_input);
- if (fmt->d < 0)
- fmt->d = 0;
- else if (fmt->d > max_d)
- fmt->d = max_d;
+ fmt_clamp_decimals (fmt, for_input);
}
/* Adjusts FMT's width and decimal places to be valid for an
@@ -550,6 +535,26 @@ fmt_fix_output (struct fmt_spec *fmt)
{
fmt_fix (fmt, false);
}
+
+/* Sets FMT's width to WIDTH (or the nearest width allowed by FMT's type) and
+ reduces its decimal places as necessary (if necessary) for that width. */
+void
+fmt_change_width (struct fmt_spec *fmt, int width, bool for_input)
+{
+ fmt->w = width;
+ fmt_clamp_width (fmt, for_input);
+ fmt_clamp_decimals (fmt, for_input);
+}
+
+/* Sets FMT's decimal places to DECIMALS (or the nearest number of decimal
+ places allowed by FMT's type) and increases its width as necessary (if
+ necessary) for that number of decimal places. */
+void
+fmt_change_decimals (struct fmt_spec *fmt, int decimals, bool for_input)
+{
+ fmt->d = decimals;
+ fmt_fix (fmt, for_input);
+}
/* Describes a display format. */
struct fmt_desc
@@ -993,6 +998,38 @@ max_digits_for_bytes (int bytes)
assert (bytes > 0 && bytes <= sizeof map / sizeof *map);
return map[bytes - 1];
}
+
+/* Clamp FMT's width to the range and values allowed by FMT's type. */
+static void
+fmt_clamp_width (struct fmt_spec *fmt, bool for_input)
+{
+ unsigned int step;
+ int min_w, max_w;
+
+ min_w = fmt_min_width (fmt->type, for_input);
+ max_w = fmt_max_width (fmt->type, for_input);
+ if (fmt->w < min_w)
+ fmt->w = min_w;
+ else if (fmt->w > max_w)
+ fmt->w = max_w;
+
+ /* Round width to step. */
+ step = fmt_step_width (fmt->type);
+ fmt->w = ROUND_DOWN (fmt->w, step);
+}
+
+/* Clamp FMT's decimal places to the range allowed by FMT's type and width. */
+static void
+fmt_clamp_decimals (struct fmt_spec *fmt, bool for_input)
+{
+ int max_d;
+
+ max_d = fmt_max_decimals (fmt->type, fmt->w, for_input);
+ if (fmt->d < 0)
+ fmt->d = 0;
+ else if (fmt->d > max_d)
+ fmt->d = max_d;
+}
/* Sets AFFIX's string value to S, a UTF-8 encoded string. */
static void
diff --git a/src/data/format.h b/src/data/format.h
index 88a82ee..8057dc6 100644
--- a/src/data/format.h
+++ b/src/data/format.h
@@ -95,6 +95,9 @@ void fmt_fix (struct fmt_spec *, bool for_input);
void fmt_fix_input (struct fmt_spec *);
void fmt_fix_output (struct fmt_spec *);
+void fmt_change_width (struct fmt_spec *, int width, bool for_input);
+void fmt_change_decimals (struct fmt_spec *, int decimals, bool for_input);
+
/* Format types. */
bool is_fmt_type (enum fmt_type);
--
1.7.2.5
- Re: [PATCH 01/13] psppire-dict: g_return_if_fail for idx in psppire_dict_get_variable()., (continued)
- Re: [PATCH 01/13] psppire-dict: g_return_if_fail for idx in psppire_dict_get_variable()., Ben Pfaff, 2012/04/18
- Re: [PATCH 01/13] psppire-dict: g_return_if_fail for idx in psppire_dict_get_variable()., John Darrington, 2012/04/18
- Re: [PATCH 01/13] psppire-dict: g_return_if_fail for idx in psppire_dict_get_variable()., Ben Pfaff, 2012/04/19
- Re: [PATCH 01/13] psppire-dict: g_return_if_fail for idx in psppire_dict_get_variable()., Ben Pfaff, 2012/04/21
- Re: [PATCH 01/13] psppire-dict: g_return_if_fail for idx in psppire_dict_get_variable()., John Darrington, 2012/04/21
[PATCH 02/13] gui: Use canonical names for signals., Ben Pfaff, 2012/04/16
[PATCH 03/13] gui: Add undocumented --measure-startup option., Ben Pfaff, 2012/04/16
[PATCH 04/13] gui: Call g_thread_init() earlier., Ben Pfaff, 2012/04/16
[PATCH 05/13] format: New functions fmt_change_width(), fmt_change_decimals().,
Ben Pfaff <=
[PATCH 07/13] format: Fix typo in comment., Ben Pfaff, 2012/04/16
[PATCH 08/13] helper: New function value_to_text__()., Ben Pfaff, 2012/04/16
[PATCH 09/13] value-labels: New function val_labs_find_value()., Ben Pfaff, 2012/04/16
[PATCH 06/13] format: Introduce a new type "enum fmt_use"., Ben Pfaff, 2012/04/16
[PATCH 10/13] abt: Drop child parameters from 'reaugment' function., Ben Pfaff, 2012/04/16
[PATCH 11/13] abt: New function abt_is_empty()., Ben Pfaff, 2012/04/16
[PATCH 12/13] range-set: Rename "insert" function "set1", "delete" to "set0"., Ben Pfaff, 2012/04/16
[PATCH 13/13] range-set: New macro RANGE_SET_FOR_EACH to make iteration easier., Ben Pfaff, 2012/04/16
Re: [PATCH 00/13] second batch of psppsheet changes, John Darrington, 2012/04/18