CPPMyth
Library to interoperate with MythTV server
Loading...
Searching...
No Matches
builtin.h
1/*
2 * Copyright (C) 2014-2023 Jean-Luc Barriere
3 *
4 * This Program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
7 * any later version.
8 *
9 * This Program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 */
21
22#ifndef BUILTIN_H
23#define BUILTIN_H
24
25#if defined __cplusplus
26#define __STDC_LIMIT_MACROS
27extern "C" {
28#endif
29
30#include "local_config.h"
31
32#include <string.h>
33#include <stdint.h>
34#include <time.h>
35#include <stdio.h>
36#include <stddef.h>
37
38typedef struct { char data[32]; } BUILTIN_BUFFER;
39
40#define string_to_int64 __str2int64
41extern int string_to_int64(const char *str, int64_t *num);
42
43#define string_to_int32 __str2int32
44extern int string_to_int32(const char *str, int32_t *num);
45
46#define string_to_int16 __str2int16
47extern int string_to_int16(const char *str, int16_t *num);
48
49#define string_to_int8 __str2int8
50extern int string_to_int8(const char *str, int8_t *num);
51
52#define string_to_uint32 __str2uint32
53extern int string_to_uint32(const char *str, uint32_t *num);
54
55#define string_to_uint16 __str2uint16
56extern int string_to_uint16(const char *str, uint16_t *num);
57
58#define string_to_uint8 __str2uint8
59extern int string_to_uint8(const char *str, uint8_t *num);
60
61#define string_to_double __str2double
62extern int string_to_double(const char *str, double *dbl);
63
64#define string_to_timeout __str2timeout
65extern int string_to_timeout(const char *str, unsigned *ms);
66
67#define string_to_size __str2size
68extern int string_to_size(const char *str, int64_t *sz);
69
70#define hex_to_num __hex2num
71extern int hex_to_num(const char *str, int *num);
72
73#define char_to_hex __charhex
74extern void char_to_hex(char c, BUILTIN_BUFFER *str);
75
76#define char_to_uhex __charuhex
77extern void char_to_uhex(char c, BUILTIN_BUFFER *str);
78
79#define int64_to_string __int64str
80static CC_INLINE void int64_to_string(int64_t num, BUILTIN_BUFFER *str)
81{
82 snprintf(str->data, sizeof(BUILTIN_BUFFER), "%lld", (long long)num);
83}
84
85#define int32_to_string __int32str
86static CC_INLINE void int32_to_string(int32_t num, BUILTIN_BUFFER *str)
87{
88 snprintf(str->data, sizeof(BUILTIN_BUFFER), "%ld", (long)num);
89}
90
91#define int16_to_string __int16str
92static CC_INLINE void int16_to_string(int16_t num, BUILTIN_BUFFER *str)
93{
94 snprintf(str->data, sizeof(BUILTIN_BUFFER), "%d", num);
95}
96
97#define int8_to_string __int8str
98static CC_INLINE void int8_to_string(int8_t num, BUILTIN_BUFFER *str)
99{
100 snprintf(str->data, sizeof(BUILTIN_BUFFER), "%d", num);
101}
102
103#define uint_to_strdec __uintstrdec
104extern unsigned uint_to_strdec(unsigned u, char *str, unsigned len, int pad);
105
106#define uint32_to_string __uint32str
107static CC_INLINE void uint32_to_string(uint32_t num, BUILTIN_BUFFER *str)
108{
109 unsigned len = uint_to_strdec(num, str->data, 10, 0);
110 str->data[len] = '\0';
111}
112
113#define uint16_to_string __uint16str
114static CC_INLINE void uint16_to_string(uint16_t num, BUILTIN_BUFFER *str)
115{
116 unsigned len = uint_to_strdec(num, str->data, 5, 0);
117 str->data[len] = '\0';
118}
119
120#define uint8_to_string __uint8str
121static CC_INLINE void uint8_to_string(uint8_t num, BUILTIN_BUFFER *str)
122{
123 unsigned len = uint_to_strdec(num, str->data, 3, 0);
124 str->data[len] = '\0';
125}
126
127#define uint64_to_strdec __uint64strdec
128extern unsigned uint64_to_strdec(uint64_t u, char *str, unsigned len, int pad);
129
130#define uint64_to_string __uint64str
131static CC_INLINE void uint64_to_string(uint64_t num, BUILTIN_BUFFER *str)
132{
133 unsigned len = uint64_to_strdec(num, str->data, 20, 0);
134 str->data[len] = '\0';
135}
136
137#define double_to_string __doublestr
138static CC_INLINE void double_to_string(double dbl, BUILTIN_BUFFER *str)
139{
140 snprintf(str->data, sizeof(BUILTIN_BUFFER), "%.12g", dbl);
141}
142
143#define TIMESTAMP_UTC_LEN (sizeof("YYYY-MM-DDTHH:MM:SSZ") - 1)
144#define TIMESTAMP_LEN (sizeof("YYYY-MM-DDTHH:MM:SS") - 1)
145#define DATESTAMP_LEN (sizeof("YYYY-MM-DD") - 1)
146#define INVALID_TIME (time_t)(0)
147
148#if !HAVE_TIMEGM && !defined(timegm)
149#define timegm __timegm
150extern time_t timegm(struct tm *utctime_tm);
151#endif
152
153#if !HAVE_LOCALTIME_R && !defined(localtime_r)
154#define localtime_r __localtime_r
155static CC_INLINE struct tm *localtime_r(const time_t *clock, struct tm *result)
156{
157 struct tm *data;
158 if (!clock || !result)
159 return NULL;
160 data = localtime(clock);
161 if (!data)
162 return NULL;
163 memcpy(result, data, sizeof(*result));
164 return result;
165}
166#endif
167
168#if !HAVE_GMTIME_R && !defined(gmtime_r)
169#define gmtime_r __gmtime_r
170static CC_INLINE struct tm *gmtime_r(const time_t *clock, struct tm *result)
171{
172 struct tm *data;
173 if (!clock || !result)
174 return NULL;
175 data = gmtime(clock);
176 if (!data)
177 return NULL;
178 memcpy(result, data, sizeof(*result));
179 return result;
180}
181#endif
182
183#define string_to_time __str2time
184extern int string_to_time(const char *str, time_t *time);
185
186#define time_to_iso8601utc __time2iso8601utc
187extern void time_to_iso8601utc(time_t time, BUILTIN_BUFFER *str);
188
189#define time_to_iso8601 __time2iso8601
190extern void time_to_iso8601(time_t time, BUILTIN_BUFFER *str);
191
192#define time_to_isodate __time2isodate
193extern void time_to_isodate(time_t time, BUILTIN_BUFFER *str);
194
195#define time_to_httptime __time2httptime
196extern void time_to_httptime(time_t time, BUILTIN_BUFFER *str);
197
198typedef struct { int tz_dir; int tz_hour; int tz_min; char tz_str[8]; } tz_t;
199#define time_tz __timetz
200extern tz_t *time_tz(time_t time, tz_t* tz);
201
202#ifdef __cplusplus
203}
204#endif
205
206#endif /* BUILTIN_H */