4 * @brief Generic types.
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
30 * String mappings for type status_t.
32 mapping_t status_m
[] = {
35 {OUT_OF_RES
, "OUT_OF_RES"},
36 {ALREADY_DONE
, "ALREADY_DONE"},
37 {NOT_SUPPORTED
, "NOT_SUPPORTED"},
38 {INVALID_ARG
, "INVALID_ARG"},
39 {NOT_FOUND
, "NOT_FOUND"},
40 {PARSE_ERROR
, "PARSE_ERROR"},
41 {VERIFY_ERROR
, "VERIFY_ERROR"},
42 {INVALID_STATE
, "INVALID_STATE"},
43 {DESTROY_ME
, "DESTROY_ME"},
48 #define UNDEFINED_TIME 0
51 * @brief Display a date either in local or UTC time
53 * @param buf buffer where displayed time will be written
54 * @param buflen buffer length
55 * @param time time to be displayed
56 * @param utc UTC (TRUE) or local time (FALSE)
59 void timetoa(char *buf
, size_t buflen
, const time_t *time
, bool utc
);
64 chunk_t CHUNK_INITIALIZER
= {NULL
,0};
67 * Described in header.
69 chunk_t
chunk_clone(chunk_t chunk
)
71 chunk_t clone
= CHUNK_INITIALIZER
;
73 if (chunk
.ptr
&& chunk
.len
> 0)
75 clone
.ptr
= malloc(chunk
.len
);
76 clone
.len
= chunk
.len
;
77 memcpy(clone
.ptr
, chunk
.ptr
, chunk
.len
);
84 * Described in header.
86 void chunk_free(chunk_t
*chunk
)
94 * Described in header.
96 chunk_t
chunk_alloc(size_t bytes
)
99 new_chunk
.ptr
= malloc(bytes
);
100 new_chunk
.len
= bytes
;
105 * Described in header.
107 bool chunk_equals(chunk_t a
, chunk_t b
)
109 return a
.len
== b
.len
&&
112 memcmp(a
.ptr
, b
.ptr
, a
.len
) == 0;
116 * Described in header.
118 void chunk_to_hex(char *buf
, size_t buflen
, chunk_t chunk
)
122 buflen
--; /* reserve space for null termination */
124 while (chunk
.len
> 0 && buflen
> 2)
126 static char hexdig
[] = "0123456789abcdef";
134 *buf
++ = ':'; buflen
--;
136 *buf
++ = hexdig
[(*chunk
.ptr
>> 4) & 0x0f];
137 *buf
++ = hexdig
[ *chunk
.ptr
++ & 0x0f];
138 buflen
-= 2; chunk
.len
--;
144 * Described in header.
146 void *clalloc(void * pointer
, size_t size
)
151 memcpy(data
, pointer
,size
);
157 * Names of the months used by timetoa()
159 static const char* months
[] = {
160 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
161 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
165 * Described in header file
167 void timetoa(char *buf
, size_t buflen
, const time_t *time
, bool utc
)
169 if (*time
== UNDEFINED_TIME
)
170 snprintf(buf
, buflen
, "--- -- --:--:--%s----", (utc
)?
" UTC ":" ");
173 struct tm
*t
= (utc
)?
gmtime(time
) : localtime(time
);
175 snprintf(buf
, buflen
, "%s %02d %02d:%02d:%02d%s%04d",
176 months
[t
->tm_mon
], t
->tm_mday
, t
->tm_hour
, t
->tm_min
, t
->tm_sec
,
177 (utc
)?
" UTC ":" ", t
->tm_year
+ 1900);