File: time-core-strpf.c

package info (click to toggle)
dateutils 0.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,924 kB
  • sloc: ansic: 22,097; makefile: 1,666; yacc: 198; sh: 168; lex: 108
file content (278 lines) | stat: -rw-r--r-- 6,209 bytes parent folder | download | duplicates (2)
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
/*** time-core-strpf.c -- parser and formatter funs for time-core
 *
 * Copyright (C) 2011-2018 Sebastian Freundt
 *
 * Author:  Sebastian Freundt <freundt@ga-group.nl>
 *
 * This file is part of dateutils.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the author nor the names of any contributors
 *    may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 **/
/* implementation part of time-core-strpf.h */
#if !defined INCLUDED_time_core_strpf_c_
#define INCLUDED_time_core_strpf_c_

#if defined HAVE_CONFIG_H
# include "config.h"
#endif	/* HAVE_CONFIG_H */
#include "strops.h"
#include "token.h"
#include "time-core.h"
#include "time-core-strpf.h"

#if !defined DEFUN
# define DEFUN
#endif	/* !DEFUN */

#if defined __INTEL_COMPILER
/* we MUST return a char* */
# pragma warning (disable:2203)
#elif defined __GNUC__
# pragma GCC diagnostic ignored "-Wcast-qual"
#endif	/* __INTEL_COMPILER */

DEFUN int
__strpt_card(struct strpt_s *d, const char *str, struct dt_spec_s s, char **ep)
{
	const char *sp = str;

	switch (s.spfl) {
	default:
	case DT_SPFL_UNK:
		goto fucked;
	case DT_SPFL_N_TSTD:
		if ((d->h = strtoi_lim(sp, &sp, 0, 23)) < 0 ||
		    *sp++ != ':') {
			goto fucked;
		} else if ((d->m = strtoi_lim(sp, &sp, 0, 59)) < 0 ||
			   *sp++ != ':') {
			goto fucked;
		} else if ((d->s = strtoi_lim(sp, &sp, 0, 60)) < 0) {
			goto fucked;
		}
		break;
	case DT_SPFL_N_HOUR:
		if (!s.sc12) {
			d->h = strtoi_lim(sp, &sp, 0, 23);
		} else {
			d->h = strtoi_lim(sp, &sp, 1, 12);
		}
		if (d->h < 0) {
			goto fucked;
		}
		break;
	case DT_SPFL_N_MIN:
		if ((d->m = strtoi_lim(sp, &sp, 0, 59)) < 0) {
			goto fucked;
		}
		break;
	case DT_SPFL_N_SEC:
		if ((d->s = strtoi_lim(sp, &sp, 0, 60)) < 0) {
			goto fucked;
		}
		break;
	case DT_SPFL_N_NANO: {
		/* nanoseconds */
		const char *on;

		if ((d->ns = strtoi_lim(sp, &on, 0, 999999999)) < 0) {
			goto fucked;
		}
		switch (on - sp) {
		case 0:
			goto fucked;
		case 1:
			d->ns *= 10;
		case 2:
			d->ns *= 10;
		case 3:
			d->ns *= 10;
		case 4:
			d->ns *= 10;
		case 5:
			d->ns *= 10;
		case 6:
			d->ns *= 10;
		case 7:
			d->ns *= 10;
		case 8:
			d->ns *= 10;
		default:
		case 9:
			break;
		}
		sp = on;
		break;
	}
	case DT_SPFL_S_AMPM: {
		const unsigned int casebit = 0x20;

		d->flags.am_pm_bit = 1;
		if ((sp[0] | casebit) == 'a' &&
		    (sp[1] | casebit) == 'm') {
			d->flags.pm_p = 0;
		} else if ((sp[0] | casebit) == 'p' &&
			   (sp[1] | casebit) == 'm') {
			d->flags.pm_p = 1;
		} else {
			goto fucked;
		}
		sp += 2;
		break;
	}
	case DT_SPFL_LIT_PERCENT:
		if (*sp++ != '%') {
			goto fucked;
		}
		break;
	case DT_SPFL_LIT_TAB:
		if (*sp++ != '\t') {
			goto fucked;
		}
		break;
	case DT_SPFL_LIT_NL:
		if (*sp++ != '\n') {
			goto fucked;
		}
		break;
	}

	/* check if components got set */
	switch (s.spfl) {
	case DT_SPFL_N_TSTD:
		d->flags.h_set = 1;
		d->flags.m_set = 1;
		d->flags.s_set = 1;
		break;
	case DT_SPFL_N_HOUR:
		d->flags.h_set = 1;
		break;
	case DT_SPFL_N_MIN:
		d->flags.m_set = 1;
		break;
	case DT_SPFL_N_SEC:
		d->flags.s_set = 1;
		break;
	case DT_SPFL_N_NANO:
		d->flags.ns_set = 1;
		break;
	default:
		break;
	}

	/* assign end pointer */
	if (ep != NULL) {
		*ep = (char*)sp;
	}
	return 0;
fucked:
	if (ep != NULL) {
		*ep = (char*)str;
	}
	return -1;
}

DEFUN size_t
__strft_card(
	char *buf, size_t bsz, struct dt_spec_s s,
	struct strpt_s *d, struct dt_t_s UNUSED(that))
{
	size_t res = 0;

	switch (s.spfl) {
	default:
	case DT_SPFL_UNK:
		break;
	case DT_SPFL_N_TSTD:
		if (LIKELY(bsz >= 8)) {
			ui99topstr(buf + 0, bsz, d->h, 2, '0');
			buf[2] = ':';
			ui99topstr(buf + 3, bsz, d->m, 2, '0');
			buf[5] = ':';
			ui99topstr(buf + 6, bsz, d->s, 2, '0');
			res = 8;
		}
		break;
	case DT_SPFL_N_HOUR:
		if (!s.sc12 || (d->h >= 1 && d->h <= 12)) {
			res = ui99topstr(
				buf, bsz, d->h,
				2 - (s.pad == DT_SPPAD_OMIT), padchar(s));
		} else {
			unsigned int h = d->h ? d->h - 12 : 12;
			res = ui99topstr(
				buf, bsz, h,
				2 - (s.pad == DT_SPPAD_OMIT), padchar(s));
		}
		break;
	case DT_SPFL_N_MIN:
		res = ui99topstr(
			buf, bsz, d->m,
			2 - (s.pad == DT_SPPAD_OMIT), padchar(s));
		break;
	case DT_SPFL_N_SEC:
		res = ui99topstr(
			buf, bsz, d->s,
			2 - (s.pad == DT_SPPAD_OMIT), padchar(s));
		break;
	case DT_SPFL_S_AMPM: {
		unsigned int casebit = 0;

		if (UNLIKELY(!s.cap)) {
			casebit = 0x20;
		}
		if (d->h >= 12 && d->h < 24) {
			buf[res++] = (char)('P' | casebit);
		} else {
			buf[res++] = (char)('A' | casebit);
		}
		buf[res++] = (char)('M' | casebit);
		break;
	}
	case DT_SPFL_N_NANO:
		res = ui999999999tostr(buf, bsz, d->ns);
		break;

	case DT_SPFL_LIT_PERCENT:
		/* literal % */
		buf[res++] = '%';
		break;
	case DT_SPFL_LIT_TAB:
		/* literal tab */
		buf[res++] = '\t';
		break;
	case DT_SPFL_LIT_NL:
		/* literal \n */
		buf[res++] = '\n';
		break;
	}
	return res;
}

#endif	/* INCLUDED_time_core_strpf_c_ */