File: format.c

package info (click to toggle)
foreign 0.8.71-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,632 kB
  • sloc: ansic: 7,543; asm: 4; makefile: 2
file content (333 lines) | stat: -rw-r--r-- 8,656 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* PSPP - computes sample statistics.
   Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
   Written by Ben Pfaff <blp@gnu.org>.
   Modified for R foreign library by Saikat DebRoy <saikat@stat.wisc.edu>.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, a copy is available at
   http://www.r-project.org/Licenses/
*/

#include <ctype.h>
#include <stdlib.h>
#include "foreign.h"
#include "format.h"

#define DEFFMT(LABEL, NAME, N_ARGS, IMIN_W, IMAX_W, OMIN_W, OMAX_W, CAT, \
	       OUTPUT, SPSS_FMT) \
	{NAME, N_ARGS, IMIN_W, IMAX_W, OMIN_W, OMAX_W, CAT, OUTPUT, SPSS_FMT},
struct fmt_desc formats[FMT_NUMBER_OF_FORMATS + 1] =
{
#include "pspp-format-def.h"
  {"",         -1, -1,  -1, -1,   -1, 0000, -1, -1},
};

const int translate_fmt[40] =
  {
    -1, FMT_A, FMT_AHEX, FMT_COMMA, FMT_DOLLAR, FMT_F, FMT_IB,
    FMT_PIBHEX, FMT_P, FMT_PIB, FMT_PK, FMT_RB, FMT_RBHEX, -1,
    -1, FMT_Z, FMT_N, FMT_E, -1, -1, FMT_DATE, FMT_TIME,
    FMT_DATETIME, FMT_ADATE, FMT_JDATE, FMT_DTIME, FMT_WKDAY,
    FMT_MONTH, FMT_MOYR, FMT_QYR, FMT_WKYR, FMT_PCT, FMT_DOT,
    FMT_CCA, FMT_CCB, FMT_CCC, FMT_CCD, FMT_CCE, FMT_EDATE,
    FMT_SDATE,
  };

#if 0
int
parse_format_specifier_name (const char **cp, int allow_xt)
{
  struct fmt_desc *f;
  char *ep;
  int x;

  ep = ds_value (&tokstr);
  while (isalpha ((unsigned char) *ep))
    ep++;
  x = *ep;
  *ep = 0;

  for (f = formats; f->name[0]; f++)
    if (!strcmp (f->name, ds_value (&tokstr)))
      {
	int indx = f - formats;

	*ep = x;
	if (cp)
	  *cp = ep;

	if (!allow_xt && (indx == FMT_T || indx == FMT_X))
	  {
	    error ("X and T format specifiers not allowed here."));
	    return -1;
	  }
	return indx;
      }

  error ("%s is not a valid data format.", ds_value (&tokstr));
  *ep = x;
  return -1;
}
#endif
/* Converts F to its string representation (for instance, "F8.2") and
   returns a pointer to a static buffer containing that string. */
char *
fmt_to_string (const struct fmt_spec *f)
{
  static char buf[32];

  if (formats[f->type].n_args >= 2)
    sprintf (buf, "%s%d.%d", formats[f->type].name, f->w, f->d);
  else
    sprintf (buf, "%s%d", formats[f->type].name, f->w);
  return buf;
}

int
check_input_specifier (const struct fmt_spec *spec)
{
  struct fmt_desc *f;
  char *str;

  f = &formats[spec->type];
  str = fmt_to_string (spec);
  if (spec->type == FMT_X)
    return 1;
  if (f->cat & FCAT_OUTPUT_ONLY)
    {
      error(_("format %s may not be used as an input format"), f->name);
      return 0;
    }
  if (spec->w < f->Imin_w || spec->w > f->Imax_w)
    {
      error(_("input format %s specifies a bad width %d.  Format %s requires a width between %d and %d"),
	   str, spec->w, f->name, f->Imin_w, f->Imax_w);
      return 0;
    }
  if ((f->cat & FCAT_EVEN_WIDTH) && spec->w % 2)
    {
      error (_("input format %s specifies an odd width %d, but format %s requires an even width between %d and %d"),
	     str, spec->w, f->name, f->Imin_w, f->Imax_w);
      return 0;
    }
  if (f->n_args > 1 && (spec->d < 0 || spec->d > 16))
    {
      error (_("Input format %s specifies a bad number of implied decimal places %d.  Input format %s allows up to 16 implied decimal places"),
	     str, spec->d, f->name);
      return 0;
    }
  return 1;
}

int
check_output_specifier (const struct fmt_spec *spec)
{
  struct fmt_desc *f;
  char *str;

  f = &formats[spec->type];
  str = fmt_to_string (spec);
  if (spec->type == FMT_X)
    return 1;
  if (spec->w < f->Omin_w || spec->w > f->Omax_w)
    {
      error (_("output format %s specifies a bad width %d.  Format %s requires a width between %d and %d"),
	   str, spec->w, f->name, f->Omin_w, f->Omax_w);
      return 0;
    }
  if (spec->d > 1
      && (spec->type == FMT_F || spec->type == FMT_COMMA
	  || spec->type == FMT_DOLLAR)
      && spec->w < f->Omin_w + 1 + spec->d)
    {
      error (_("output format %s requires minimum width %d to allow %d decimal places.  Try %s%d.%d instead of %s"),
	   f->name, f->Omin_w + 1 + spec->d, spec->d, f->name,
	   f->Omin_w + 1 + spec->d, spec->d, str);
      return 0;
    }
  if ((f->cat & FCAT_EVEN_WIDTH) && spec->w % 2)
    {
      error (_("output format %s specifies an odd width %d, but output format %s requires an even width between %d and %d"),
	     str, spec->w, f->name, f->Omin_w, f->Omax_w);
      return 0;
    }
  if (f->n_args > 1 && (spec->d < 0 || spec->d > 16))
    {
      error (_("Output format %s specifies a bad number of implied decimal places %d.  Output format %s allows a number of implied decimal places between 1 and 16"),
	  str, spec->d, f->name);
      return 0;
    }
  return 1;
}

/* If a string variable has width W, you can't display it with a
   format specifier with a required width MIN_LEN>W. */
int
check_string_specifier (const struct fmt_spec *f, int min_len)
{
  if ((f->type == FMT_A && min_len > f->w)
      || (f->type == FMT_AHEX && min_len * 2 > f->w))
    {
      error (_("cannot display a string variable of width %d with format specifier %s"),
	     min_len, fmt_to_string (f));
      return 0;
    }
  return 1;
}

void
convert_fmt_ItoO (const struct fmt_spec *input, struct fmt_spec *output)
{
  output->type = formats[input->type].output;
  output->w = input->w;
  if (output->w > formats[output->type].Omax_w)
    output->w = formats[output->type].Omax_w;
  output->d = input->d;

  switch (input->type)
    {
    case FMT_F:
    case FMT_N:
      if (output->d > 1 && output->w < 2 + output->d)
	output->w = 2 + output->d;
      break;
    case FMT_E:
      output->w = max (max (input->w, input->d+7), 10);
      output->d = max (input->d, 3);
      break;
    case FMT_COMMA:
    case FMT_DOT:
      /* nothing is necessary */
      break;
    case FMT_DOLLAR:
    case FMT_PCT:
      if (output->w < 2)
	output->w = 2;
      break;
    case FMT_PIBHEX:
      {
	static const int map[] = {4, 6, 9, 11, 14, 16, 18, 21};
	if (input->w % 2 != 0 || input->w < 2 || input->w > 16)
	    error("convert_fmt_ItoO : assert failed");
	output->w = map[input->w / 2 - 1];
	break;
      }
    case FMT_RBHEX:
      output->w = 8, output->d = 2;	/* FIXME */
      break;
    case FMT_IB:
    case FMT_PIB:
    case FMT_P:
    case FMT_PK:
    case FMT_RB:
      if (input->d < 1)
	output->w = 8, output->d = 2;
      else
	output->w = 9 + input->d;
      break;
    case FMT_CCA:
    case FMT_CCB:
    case FMT_CCC:
    case FMT_CCD:
    case FMT_CCE:
      error("convert_fmt_ItoO : invalid input->type : %d", input->type);
    case FMT_Z:
    case FMT_A:
      /* nothing is necessary */
      break;
    case FMT_AHEX:
      output->w = input->w / 2;
      break;
    case FMT_DATE:
    case FMT_EDATE:
    case FMT_SDATE:
    case FMT_ADATE:
    case FMT_JDATE:
      /* nothing is necessary */
      break;
    case FMT_QYR:
      if (output->w < 6)
	output->w = 6;
      break;
    case FMT_MOYR:
      /* nothing is necessary */
      break;
    case FMT_WKYR:
      if (output->w < 8)
	output->w = 8;
      break;
    case FMT_TIME:
    case FMT_DTIME:
    case FMT_DATETIME:
    case FMT_WKDAY:
    case FMT_MONTH:
      /* nothing is necessary */
      break;
    default:
      error("convert_fmt_ItoO : invalid input->type : %d", input->type);
    }
}

#if 0
int
parse_format_specifier (struct fmt_spec *input, int allow_xt)
{
  struct fmt_spec spec;
  struct fmt_desc *f;
  const char *cp;
  char *cp2;
  int type, w, d;

  if (token != T_ID)
    {
      error ("format specifier expected");
      return 0;
    }
  type = parse_format_specifier_name (&cp, allow_xt);
  if (type == -1)
    return 0;
  f = &formats[type];

  w = strtol (cp, &cp2, 10);
  if (cp2 == cp && type != FMT_X)
    {
      error ("Data format %s does not specify a width.",
	   ds_value (&tokstr));
      return 0;
    }

  cp = cp2;
  if (f->n_args > 1 && *cp == '.')
    {
      cp++;
      d = strtol (cp, &cp2, 10);
      cp = cp2;
    }
  else
    d = 0;

  if (*cp)
    {
      error ("Data format %s is not valid.", ds_value (&tokstr));
      return 0;
    }
  lex_get ();

  spec.type = type;
  spec.w = w;
  spec.d = d;
  *input = spec;

  return 1;
}
#endif