1 /**
2 * Copyright (c) 2014, Timothy Stack
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * * Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * * Neither the name of Timothy Stack nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * @file ptimec_rt.cc
30 */
31
32 #include "config.h"
33 #include <string.h>
34 #include <algorithm>
35
36 #include "ptimec.hh"
37
ptime_b_slow(struct exttm * dst,const char * str,off_t & off_inout,ssize_t len)38 bool ptime_b_slow(struct exttm *dst, const char *str, off_t &off_inout, ssize_t len)
39 {
40 size_t zone_len = len - off_inout;
41 char zone[zone_len + 1];
42 const char *end_of_date;
43
44 memcpy(zone, &str[off_inout], zone_len);
45 zone[zone_len] = '\0';
46 if ((end_of_date = strptime(zone, "%b", &dst->et_tm)) != NULL) {
47 off_inout += end_of_date - zone;
48 return true;
49 }
50
51 return false;
52 }
53
54 #define FMT_CASE(ch, c) \
55 case ch: \
56 if (!ptime_ ## c(dst, str, off, len)) return false; \
57 lpc += 1; \
58 break
59
ptime_fmt(const char * fmt,struct exttm * dst,const char * str,off_t & off,ssize_t len)60 bool ptime_fmt(const char *fmt, struct exttm *dst, const char *str, off_t &off, ssize_t len)
61 {
62 for (ssize_t lpc = 0; fmt[lpc]; lpc++) {
63 if (fmt[lpc] == '%') {
64 switch (fmt[lpc + 1]) {
65 case 'a':
66 case 'Z':
67 if (fmt[lpc + 2]) {
68 if (!ptime_upto(fmt[lpc + 2], str, off, len)) {
69 return false;
70 }
71 lpc += 1;
72 }
73 else {
74 if (!ptime_upto_end(str, off, len)) {
75 return false;
76 }
77 lpc += 1;
78 }
79 break;
80 FMT_CASE('b', b);
81 FMT_CASE('S', S);
82 FMT_CASE('s', s);
83 FMT_CASE('L', L);
84 FMT_CASE('M', M);
85 FMT_CASE('H', H);
86 FMT_CASE('i', i);
87 FMT_CASE('6', 6);
88 FMT_CASE('I', I);
89 FMT_CASE('d', d);
90 FMT_CASE('e', e);
91 FMT_CASE('f', f);
92 FMT_CASE('k', k);
93 FMT_CASE('l', l);
94 FMT_CASE('m', m);
95 FMT_CASE('N', N);
96 FMT_CASE('p', p);
97 FMT_CASE('q', q);
98 FMT_CASE('Y', Y);
99 FMT_CASE('y', y);
100 FMT_CASE('z', z);
101 FMT_CASE('@', at);
102 }
103 }
104 else {
105 if (!ptime_char(fmt[lpc], str, off, len)) return false;
106 }
107 }
108
109 return true;
110 }
111
112 #define FTIME_FMT_CASE(ch, c) \
113 case ch: \
114 ftime_ ## c(dst, off_inout, len, tm); \
115 lpc += 1; \
116 break
117
ftime_fmt(char * dst,size_t len,const char * fmt,const struct exttm & tm)118 size_t ftime_fmt(char *dst, size_t len, const char *fmt, const struct exttm &tm)
119 {
120 off_t off_inout = 0;
121
122 for (ssize_t lpc = 0; fmt[lpc]; lpc++) {
123 if (fmt[lpc] == '%') {
124 switch (fmt[lpc + 1]) {
125 case '%':
126 ftime_char(dst, off_inout, len, '%');
127 break;
128 FTIME_FMT_CASE('a', a);
129 FTIME_FMT_CASE('b', b);
130 FTIME_FMT_CASE('S', S);
131 FTIME_FMT_CASE('s', s);
132 FTIME_FMT_CASE('L', L);
133 FTIME_FMT_CASE('M', M);
134 FTIME_FMT_CASE('H', H);
135 FTIME_FMT_CASE('i', i);
136 FTIME_FMT_CASE('6', 6);
137 FTIME_FMT_CASE('I', I);
138 FTIME_FMT_CASE('d', d);
139 FTIME_FMT_CASE('e', e);
140 FTIME_FMT_CASE('f', f);
141 FTIME_FMT_CASE('k', k);
142 FTIME_FMT_CASE('l', l);
143 FTIME_FMT_CASE('m', m);
144 FTIME_FMT_CASE('N', N);
145 FTIME_FMT_CASE('p', p);
146 FTIME_FMT_CASE('q', q);
147 FTIME_FMT_CASE('Y', Y);
148 FTIME_FMT_CASE('y', y);
149 FTIME_FMT_CASE('z', z);
150 }
151 }
152 else {
153 ftime_char(dst, off_inout, len, fmt[lpc]);
154 }
155 }
156
157 dst[off_inout] = '\0';
158
159 return (size_t) off_inout;
160 }
161