1string_to_decimal(3C) Standard C Library Functions string_to_decimal(3C)
2
3
4
6 string_to_decimal, file_to_decimal, func_to_decimal - parse characters
7 into decimal record
8
10 #include <floatingpoint.h>
11
12 void string_to_decimal(char **pc, int nmax,
13 int fortran_conventions, decimal_record *pd,
14 enum decimal_string_form *pform, char **pechar);
15
16
17 void func_to_decimal(char **pc, int nmax,
18 int fortran_conventions, decimal_record *pd,
19 enum decimal_string_form *pform, char **pechar,
20 int (*pget)(void), int *pnread, int (*punget)(int c));
21
22
23 #include <stdio.h>
24
25 void file_to_decimal(char **pc, int nmax,
26 int fortran_conventions, decimal_record *pd,
27 enum decimal_string_form *pform, char **pechar,
28 FILE *pf, int *pnread);
29
30
32 These functions attempt to parse a numeric token from at most nmax
33 characters read from a string **pc, a file *pf, or function (*pget).
34 They set the decimal record *pd to reflect the value of the numeric
35 token recognized and set *pform and *pechar to indicate its form.
36
37
38 The accepted forms for the numeric token consist of an initial, possi‐
39 bly empty, sequence of white-space characters, as defined by iss‐
40 pace(3C), followed by a subject sequence representing a numeric value,
41 infinity, or NaN. The subject sequence consists of an optional plus or
42 minus sign followed by one of the following:
43
44 o a non-empty sequence of decimal digits optionally containing
45 a decimal point character, then an optional exponent part
46
47 o one of INF or INFINITY, ignoring case
48
49 o one of NAN or NAN(string), ignoring case in the NAN part;
50 string can be any sequence of characters not containing ')'
51 (right parenthesis) or '\0' (null).
52
53
54 The fortran_conventions argument provides additional control over the
55 set of accepted forms. It must be one of the following values:
56
57 0 no Fortran conventions
58
59
60 1 Fortran list-directed input conventions
61
62
63 2 Fortran formatted input conventions, blanks are ignored
64
65
66 3 Fortran formatted input conventions, blanks are interpreted as
67 zeroes
68
69
70
71 When fortran_conventions is zero, the decimal point character is the
72 current locale's decimal point character, and the exponent part con‐
73 sists of the letter E or e followed by an optional sign and a non-empty
74 string of decimal digits.
75
76
77 When fortran_conventions is non-zero, the decimal point character is
78 "." (period), and the exponent part consists of either a sign or one of
79 the letters E, e, D, d, Q, or q followed by an optional sign, then a
80 non-empty string of decimal digits.
81
82
83 When fortran_conventions is 2 or 3, blanks can appear in the digit
84 strings for the integer, fraction, and exponent parts, between the
85 exponent delimiter and optional exponent sign, and after an INF, INFIN‐
86 ITY, NAN, or NAN(string). When fortran_conventions is 2, all blanks
87 are ignored. When fortran_conventions is 3, blanks in digit strings
88 are interpreted as zeros and other blanks are ignored.
89
90
91 The following table summarizes the accepted forms and shows the corre‐
92 sponding values to which *pform and pd->fpclass are set. Here digits
93 represents any string of decimal digits, "." (period) stands for the
94 decimal point character, and exponent represents the exponent part as
95 defined above. Numbers in brackets refer to the notes following the
96 table.
97
98
99
100
101 form *pform pd->fpclass
102 ───────────────────────────────────────────────────────────────────────
103 all white space [1] whitespace_form fp_zero
104 digits fixed_int_form fp_normal [2]
105 digits. fixed_intdot_form fp_normal [2]
106 .digits fixed_dotfrac_form fp_normal [2]
107 digits.digits fixed_intdotfrac_form fp_normal [2]
108 digits exponent floating_int_form fp_normal [2]
109 digits. exponent floating_intdot_form fp_normal [2]
110 .digits exponent floating_dotfrac_form fp_normal [2]
111 digits.digits exponent floating_intdotfrac_form fp_normal [2]
112 INF inf_form fp_infinity
113 INFINITY infinity_form fp_infinity
114 NAN nan_form fp_quiet
115 NAN(string) nanstring_form fp_quiet
116 none of the above invalid_form fp_signaling
117
118
119
120 Notes:
121
122 1. The whitespace_form is accepted only when fortran_conven‐
123 tions is 2 or 3 and is interpreted as zero.
124
125 2. For all numeric forms, pd->fpclass is set to fp_normal if
126 any non-zero digits appear in the integer or fraction parts,
127 and otherwise pd->fpclass is set to fp_zero.
128
129
130 If the accepted token has one of the numeric forms and represents a
131 non-zero number x, its significant digits are stored in pd->ds. Lead‐
132 ing and trailing zeroes and the radix point are omitted. pd->sign and
133 pd->exponent are set so that if m is the integer represented by pd->ds,
134
135 −1**(pd->sign) * m * 10**(pd->exponent)
136
137
138
139 approximates x to at least 511 significant digits. pd->more is set to
140 1 if this approximation is not exact (that is, the accepted token con‐
141 tains additional non-zero digits beyond those copied to pd->ds) and to
142 0 otherwise.
143
144
145 If the accepted token has the NAN(string) form, up to 511 characters
146 from the string part are copied to pd->ds.
147
148
149 pd->ds is always terminated by a null byte, and pd->ndigits is set to
150 the length of the string stored in pd->ds.
151
152
153 On entry, *pc points to the beginning of a character string buffer.
154 The string_to_decimal() function reads characters from this buffer
155 until either enough characters are read to delimit the accepted token
156 (for example, a null character marking the end of the string is found)
157 or the limit of nmax characters is reached. The file_to_decimal() func‐
158 tion reads characters from the file *pf and stores them in the buffer.
159 The func_to_decimal() function reads characters one at a time by call‐
160 ing the function (*pget)() and stores them in the buffer; (*pget)()
161 must return integer values in the range −1 to 255, where −1 is inter‐
162 preted as EOF and 0, ..., 255 are interpreted as unsigned char values.
163 Both file_to_decimal() and func_to_decimal() read characters until
164 either enough characters are read to delimit the accepted token, EOF is
165 encountered, or the limit of nmax characters is reached. These func‐
166 tions, therefore, typically read one or more additional characters
167 beyond the end of the accepted token and attempt to push back any
168 excess characters read. Provided that the punget argument is not NULL,
169 func_to_decimal() pushes back characters one at a time by calling
170 (*punget)(c), where c is an integer in the range 0 to 255 corresponding
171 to a value previously read via (*pget)(). After pushing back as many
172 excess characters as possible, file_to_decimal() and func_to_decimal()
173 store a null byte in the buffer following the last character read and
174 not pushed back and set *pnread to the number of characters stored in
175 the buffer prior to this null byte. Since these functions can read up
176 to nmax characters, the buffer must be large enough to hold nmax + 1.
177
178
179 On exit, *pc points to the next character in the buffer past the last
180 one that was accepted as part of the numeric token. If no valid token
181 is found, *pc is unchanged. If file_to_decimal() and func_to_decimal()
182 successfully push back all unused characters, *pc points to the null
183 byte stored in the buffer following the last character read and not
184 pushed back.
185
186
187 If the accepted token contains an exponent part, *pechar is set to
188 point to the position in the buffer where the first character of the
189 exponent field is stored. If the accepted token does not contain an
190 exponent part, *pechar is set to NULL.
191
193 If the _IOWRT flag is set in *pf, file_to_decimal() reads characters
194 directly from the file buffer until a null character is found. (The
195 _IOWRT flag should only be set when file_to_decimal() is called from
196 sscanf(3C).) Otherwise, file_to_decimal() uses getc_unlocked(3C), so
197 it is not MT-safe unless the caller holds the stream lock.
198
200 See attributes(5) for descriptions of the following attributes:
201
202
203
204
205 ┌─────────────────────────────┬─────────────────────────────┐
206 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
207 ├─────────────────────────────┼─────────────────────────────┤
208 │MT-Level │MT-Safe with exceptions │
209 └─────────────────────────────┴─────────────────────────────┘
210
212 ctype(3C), decimal_to_floating(3C), getc_unlocked(3C), isspace(3C),
213 localeconv(3C), scanf(3C), setlocale(3C), strtod(3C), ungetc(3C),
214 attributes(5)
215
216
217
218SunOS 5.11 1 Oct 2003 string_to_decimal(3C)