regify utility 2.1.0-0
 
Loading...
Searching...
No Matches
regify-util.h
1/*
2 * Copyright regify
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
39#ifndef UTIL_REGIFY_UTIL_H
40#define UTIL_REGIFY_UTIL_H
41
42/* for our error codes only */
43#include <stdlib.h>
44#include <stdint.h>
45#include <stdio.h>
46#include <stdbool.h>
47
48/* Only need to export C interface if used by C++ source code */
49#ifdef __cplusplus
50extern "C" {
51#endif /* __cplusplus */
52
54#if defined(regify_util_EXPORTS)
55 #if (defined(WINDOWS) || defined(WIN32) || defined(__BORLANDC__))
56 #ifdef RU_BUILDING
57 #define RUAPI extern __declspec(dllexport)
58 #else
59 #define RUAPI extern __declspec(dllimport)
60 #endif
61 #else
62 #if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 303
63 #define RUAPI __attribute__ ((visibility ("default")))
64 #else
65 #define RUAPI
66 #endif
67 #endif
68#else
69 #define RUAPI
70#endif
73#if defined(WINDOWS) || defined(WIN32) || defined(__BORLANDC__)
75 #ifdef _MSC_VER
76 // running non posix
77 #define RUMS
78 #endif
79
80 #include <windows.h>
81
82 #ifndef int32_t
83 typedef int int32_t;
84 #endif
85 #ifndef int64_t
86 typedef long long int64_t;
87 #endif
94 #ifdef _WIN64
95 typedef int64_t rusize_s;
96 #else
97 typedef int rusize_s;
98 #endif
99
110 RUAPI int32_t ruGetRegistryEntry(HKEY topKey, const char* tree,
111 const char* leaf, char** value);
112
127 RUAPI int32_t ruGetVolumeInfo(const char* mountPoint,
128 unsigned long* serialNo, unsigned long* maxCompLen, unsigned long* fsFlags,
129 char** volumeName, char** fsName);
130
131#else
132
133#if defined(__APPLE__) && defined(__MACH__) // Apple OSX and iOS (Darwin)
134#include <TargetConditionals.h>
135 #if TARGET_OS_IPHONE == 1
136 #ifndef ITS_IOS
137 #define ITS_IOS 1
138 #endif
139 #elif TARGET_OS_MAC == 1
140 #ifndef ITS_OSX
141 #define ITS_OSX 1
142 #endif
143 #endif
144#endif
145
146 /* for folderwalk */
147 #include <dirent.h>
148
152 typedef signed long rusize_s;
153
154#endif
155
159#ifdef _WIN32
160typedef DWORD ru_pid;
161#else
162#include <sys/types.h>
163typedef pid_t ru_pid;
164#endif
165
166
176typedef const char* perm_chars;
177
186typedef const char* trans_chars;
187
199typedef char* alloc_chars;
200
210typedef const uint16_t* perm_uni;
211
220typedef const uint16_t* trans_uni;
221
233typedef uint16_t* alloc_uni;
234
244typedef const unsigned char* perm_bytes;
245
254typedef const unsigned char* trans_bytes;
255
267typedef unsigned char* alloc_bytes;
268
277typedef const void* perm_ptr;
278
287typedef const void* trans_ptr;
288
299typedef void* alloc_ptr;
300
304typedef void* ptr;
305
309#ifdef __EMSCRIPTEN__
310typedef long long sec_t;
311#else
312typedef long sec_t;
313#endif
314
318typedef int64_t msec_t;
319
323typedef int64_t usec_t;
324
328typedef intptr_t ru_int;
329
333typedef uintptr_t ru_uint;
334
338typedef size_t rusize;
339
344typedef void* ruType;
345
349#define RU_SIZE_AUTO (~0LU)
350
356typedef ptr (*ruClearFunc)(ptr o);
357
363typedef void (*ruFreeFunc)(ptr o);
364
368typedef struct {
371} ruTimeVal;
372
373
379
385RUAPI const char* ruVersion(void);
386
397RUAPI const char* ruLastError(void);
398
406RUAPI perm_chars ruGetOs(void);
407
414
423
429RUAPI ru_pid ruProcessId(void);
430
435#define RU_NON_BLOCK (-1)
436
441#define RU_NO_TIMEOUT 0
442
462RUAPI int32_t ruRunProg(const char **argv, sec_t timeout);
463
470
481RUAPI unsigned long ruSemiRandomNumber(unsigned long max, long offset);
482
497
504RUAPI int32_t ruGetTimeVal(ruTimeVal *result);
505
511RUAPI usec_t ruTimeUs(void);
512
517RUAPI msec_t ruTimeMs(void);
518
523RUAPI sec_t ruTimeSec(void);
524
530RUAPI bool ruTimeUsEllapsed(usec_t stamp);
531
537RUAPI bool ruTimeMsEllapsed(msec_t stamp);
538
544RUAPI bool ruTimeEllapsed(sec_t stamp);
545
550RUAPI void ruSleepUs(usec_t microseconds);
551
556RUAPI void ruSleepMs(msec_t milliseconds);
557
562#define ruSleep(secs) ruSleepMs(((msec_t)(secs))*1000)
563
581RUAPI void ruTryLoopInit(ruTryLoop* cycle, msec_t retryMs, msec_t timeoutMs);
582
589RUAPI bool ruTryLoopDone(ruTryLoop* cycle);
590
597RUAPI sec_t ruTimeParse(trans_chars dateformat, trans_chars datestr);
598
605RUAPI sec_t ruUtcParse(trans_chars dateformat, trans_chars datestr);
606
615RUAPI int ruTimeFormat(trans_chars format, rusize len, alloc_chars timeStr, sec_t timesecs);
616
625RUAPI int ruUtcFormat(trans_chars format, rusize len, alloc_chars timeStr, sec_t timesecs);
626
633
640
646RUAPI ptr ruCharPtrDup(char* in);
647
648
650#define ruMacStart do
651#define ruMacEnd while(0)
660RUAPI ptr ruClear(ptr o);
661
667#define ruFree(p) ruMacStart { if(p) { free((void*)p); p = NULL; } } ruMacEnd
668
675#define ruReplace(p, n) ruMacStart { if(p) { free((void*)p); } p = n; } ruMacEnd
676
682#define ruRetWithCode(ptr, code, res) ruMacStart { \
683 if (ptr) { *ptr = code; } return res; \
684 } ruMacEnd
685
690typedef void* ruCleaner;
691
696#ifdef __cplusplus
697}
698#endif /* __cplusplus */
699
700#include <regify-util/errors.h>
701#include <regify-util/logging.h>
702#include <regify-util/types.h>
703#include <regify-util/list.h>
704#include <regify-util/thread.h>
705#include <regify-util/string.h>
706#include <regify-util/map.h>
707#include <regify-util/cleaner.h>
708#include <regify-util/ini.h>
709#include <regify-util/io.h>
710#include <regify-util/json.h>
711#include <regify-util/kvstore.h>
712#include <regify-util/regex.h>
713#include <regify-util/html.h>
714#if !defined(__ANDROID__) && defined(__linux__) || defined(ITS_OSX) || defined(_WIN32)
715#include <regify-util/fam.h>
716#endif
717
718#ifdef __cplusplus
719extern "C" {
720#endif /* __cplusplus */
721
722#if !defined(__ANDROID__)
728#define RU_IP4 0x1
733#define RU_IP6 0x2
738#define RU_IP (RU_IP4 | RU_IP6)
739
747RUAPI ruList ruIpAddrs(int32_t ipfilter);
749#endif
750
768RUAPI int32_t ruGetOptMap(ruMap* parms, trans_chars opts, int argc, char** argv);
769
770#ifdef _WIN32
788RUAPI int32_t ruGetOptMapW(ruMap* parms, trans_chars opts, DWORD wargc, LPWSTR* wargv);
789#endif
790
803RUAPI alloc_ptr ruMallocSize(rusize count, rusize ofsize);
804
812#define ruMalloc0(count, ctype) (ctype*) ruMallocSize((rusize)(count), sizeof(ctype));
813
823
832#define ruRealloc(buf, count, ctype) (ctype*) ruReallocSize(buf, count, sizeof(ctype))
833
842
848/*
849 * Some convenience Macros
850 */
851
852// the ptr < (void*)0xffff is feeble attempt at address validation
853#define ruMakeTypeGetter(ctype, magic) \
854ctype* ctype ## Get(void* ptr, int32_t* code) { \
855 ctype* ret = (ctype*) ptr; \
856 if (!ptr) { \
857 ruRetWithCode(code, RUE_PARAMETER_NOT_SET, NULL); \
858 } \
859 if (ptr < (void*)0xffff || magic != ret->type) { \
860 ruRetWithCode(code, RUE_INVALID_PARAMETER, NULL); \
861 } \
862 ruRetWithCode(code, RUE_OK, ret); \
863}
864
865#define ruZeroedStruct(type, var) type var; \
866 memset(&(var), 0, sizeof(type))
867
868#define ruMakeTypeGetHeader(ctype) \
869ctype* ctype ## Get(void* ptr, int32_t* code)
870
873#ifdef __cplusplus
874} /* extern "C" */
875#endif /* __cplusplus */
876
877#include <regify-util/logging.h>
878
879#endif //UTIL_REGIFY_UTIL_H
void * ruCleaner
Definition regify-util.h:690
void * ruMap
An opaque type representing a map object.
Definition map.h:33
void * ruList
Opaque pointer to list object. See List Collection.
Definition list.h:103
ptr(* ruClearFunc)(ptr o)
Signature of a generic free function returning NULL.
Definition regify-util.h:356
RUAPI ptr ruClear(ptr o)
Frees given resource if set and returns NULL.
RUAPI alloc_ptr ruMemDup(trans_ptr buf, rusize size)
Returns a copy of the given memory block.
RUAPI alloc_ptr ruReallocSize(alloc_ptr buf, rusize count, rusize ofsize)
Reallocate requested memory without zeroing.
RUAPI alloc_ptr ruMallocSize(rusize count, rusize ofsize)
Allocate and zero requested memory.
void(* ruFreeFunc)(ptr o)
Signature of a generic free function.
Definition regify-util.h:363
int64_t usec_t
A signed type for expressing micro seconds.
Definition regify-util.h:323
RUAPI msec_t ruTimeMs(void)
Return the current local time in milliseconds since Jan. 1 1970.
intptr_t ru_int
A pointer sized integer type for collections like ruMap or ruList.
Definition regify-util.h:328
const void * trans_ptr
A transient pointer.
Definition regify-util.h:287
RUAPI void ruTryLoopInit(ruTryLoop *cycle, msec_t retryMs, msec_t timeoutMs)
RUAPI int32_t ruGetTimeVal(ruTimeVal *result)
Returns a ruTimeVal representing the current time.
unsigned char * alloc_bytes
An allocated data bytes pointer.
Definition regify-util.h:267
RUAPI alloc_chars ruGetLanguage(void)
Returns the ISO-639-1 2 letter country code pertaining to the running system,.
uint16_t * alloc_uni
An allocated UTF16 wchar_t character type.
Definition regify-util.h:233
RUAPI sec_t ruTimeUtcToLocal(sec_t stamp)
Converts given UTC time stamp to local time.
RUAPI void ruSleepUs(usec_t microseconds)
Sleeps for the given number of micro seconds.
RUAPI const char * ruVersion(void)
Returns the build version of this package.
const void * perm_ptr
A permanent pointer.
Definition regify-util.h:277
RUAPI int ruTimeFormat(trans_chars format, rusize len, alloc_chars timeStr, sec_t timesecs)
Return the current local time formatted in given buffer.
const uint16_t * trans_uni
A transient UTF16 wchar_t character type.
Definition regify-util.h:220
const char * trans_chars
A transient NULL terminated string pointer.
Definition regify-util.h:186
RUAPI sec_t ruTimeSec(void)
Return the current local time in seconds since Jan. 1 1970.
RUAPI sec_t ruTimeParse(trans_chars dateformat, trans_chars datestr)
Convert a string representation of time to a time stamp.
RUAPI const char * ruLastError(void)
Returns an English textual representation of the last error this thread generated.
uintptr_t ru_uint
A pointer sized unsigned integer type for collections like ruMap or ruList.
Definition regify-util.h:333
pid_t ru_pid
Abstracted process id type.
Definition regify-util.h:163
RUAPI sec_t ruTimeLocalToUtc(sec_t stamp)
Converts given local time stamp to UTC.
RUAPI int32_t ruGetOptMap(ruMap *parms, trans_chars opts, int argc, char **argv)
Runs getopt short with given parameters.
const char * perm_chars
A permanent NULL terminated string pointer.
Definition regify-util.h:176
RUAPI bool ruTimeMsEllapsed(msec_t stamp)
Checks if given stamp has elapsed.
long sec_t
A signed type for expressing seconds.
Definition regify-util.h:312
const uint16_t * perm_uni
A permanent UTF16 wchar_t character type.
Definition regify-util.h:210
RUAPI perm_chars ruGetOs(void)
Returns a lowercase string representation of the current OS.
RUAPI bool ruTryLoopDone(ruTryLoop *cycle)
char * alloc_chars
An allocated NULL terminated string pointer.
Definition regify-util.h:199
void * alloc_ptr
An allocated pointer.
Definition regify-util.h:299
RUAPI int32_t ruRunProg(const char **argv, sec_t timeout)
Runs given program.
size_t rusize
Abstracted version of size_t.
Definition regify-util.h:338
const unsigned char * perm_bytes
A permanent data bytes pointer.
Definition regify-util.h:244
RUAPI alloc_chars ruGetHostname(void)
Returns the name of this host.
void * ptr
A generic pointer.
Definition regify-util.h:304
const unsigned char * trans_bytes
A transient data bytes pointer.
Definition regify-util.h:254
RUAPI sec_t ruUtcParse(trans_chars dateformat, trans_chars datestr)
UTC version of ruTimeParse.
RUAPI ptr ruCharPtrDup(char *in)
Copies the given string and returns the pointer.
signed long rusize_s
Abstracted version of size_t but signed.
Definition regify-util.h:152
RUAPI ruList ruIpAddrs(int32_t ipfilter)
Returns the IP numbers bound to the host.
RUAPI bool ruTimeEllapsed(sec_t stamp)
Checks if given stamp has elapsed.
RUAPI bool ruTimeUsEllapsed(usec_t stamp)
Checks if given stamp has elapsed.
RUAPI trans_chars ruGetenv(trans_chars variable)
Returns the value of the requested environment vartiable or NULL if it is not set.
RUAPI void ruSleepMs(msec_t milliseconds)
Sleeps for the given number of milli seconds.
RUAPI int ruVersionComp(trans_chars ver1, trans_chars ver2)
Compares 2 version strings This function compares version number strings that are made up of real num...
int64_t msec_t
A signed type for expressing milli seconds.
Definition regify-util.h:318
RUAPI usec_t ruTimeUs(void)
Return the current local time in microseconds since Jan. 1 1970.
RUAPI int ruUtcFormat(trans_chars format, rusize len, alloc_chars timeStr, sec_t timesecs)
UTC version of ruTimeFormat.
RUAPI unsigned long ruSemiRandomNumber(unsigned long max, long offset)
Returns a quasi ramdom number between 0 and max + offset.
RUAPI ru_pid ruProcessId(void)
Return the current process id.
void * ruType
An opaque type representing a type specification.
Definition regify-util.h:344
Abstracted version of the Posix struct timeval.
Definition regify-util.h:368
sec_t sec
Definition regify-util.h:369
usec_t usec
Definition regify-util.h:370
Definition regify-util.h:374
msec_t finish
Definition regify-util.h:376
msec_t start
Definition regify-util.h:375
msec_t retry
Definition regify-util.h:377