regify utility  2.0.0-0
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
50 extern "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
160 typedef DWORD ru_pid;
161 #else
162 #include <sys/types.h>
163 typedef pid_t ru_pid;
164 #endif
165 
166 
176 typedef const char* perm_chars;
177 
186 typedef const char* trans_chars;
187 
199 typedef char* alloc_chars;
200 
210 typedef const uint16_t* perm_uni;
211 
220 typedef const uint16_t* trans_uni;
221 
233 typedef uint16_t* alloc_uni;
234 
244 typedef const unsigned char* perm_bytes;
245 
254 typedef const unsigned char* trans_bytes;
255 
267 typedef unsigned char* alloc_bytes;
268 
277 typedef const void* perm_ptr;
278 
287 typedef const void* trans_ptr;
288 
299 typedef void* alloc_ptr;
300 
304 typedef void* ptr;
305 
309 #ifdef __EMSCRIPTEN__
310 typedef long long sec_t;
311 #else
312 typedef long sec_t;
313 #endif
314 
318 typedef int64_t msec_t;
319 
323 typedef int64_t usec_t;
324 
328 typedef intptr_t ru_int;
329 
333 typedef uintptr_t ru_uint;
334 
338 typedef size_t rusize;
339 
344 typedef void* ruType;
345 
349 #define RU_SIZE_AUTO (~0LU)
350 
356 typedef ptr (*ruClearFunc)(ptr o);
357 
363 typedef void (*ruFreeFunc)(ptr o);
364 
368 typedef struct {
371 } ruTimeVal;
372 
373 
374 typedef struct {
378 } ruTryLoop;
379 
385 RUAPI const char* ruVersion(void);
386 
397 RUAPI const char* ruLastError(void);
398 
406 RUAPI perm_chars ruGetOs(void);
407 
414 
423 
429 RUAPI ru_pid ruProcessId(void);
430 
435 #define RU_NON_BLOCK (-1)
436 
441 #define RU_NO_TIMEOUT 0
442 
462 RUAPI int32_t ruRunProg(const char **argv, sec_t timeout);
463 
470 
481 RUAPI unsigned long ruSemiRandomNumber(unsigned long max, long offset);
482 
496 RUAPI int ruVersionComp(trans_chars ver1, trans_chars ver2);
497 
504 RUAPI int32_t ruGetTimeVal(ruTimeVal *result);
505 
511 RUAPI usec_t ruTimeUs(void);
512 
517 RUAPI msec_t ruTimeMs(void);
518 
523 RUAPI sec_t ruTimeSec(void);
524 
530 RUAPI bool ruTimeUsEllapsed(usec_t stamp);
531 
537 RUAPI bool ruTimeMsEllapsed(msec_t stamp);
538 
544 RUAPI bool ruTimeEllapsed(sec_t stamp);
545 
550 RUAPI void ruSleepUs(usec_t microseconds);
551 
556 RUAPI void ruSleepMs(msec_t milliseconds);
557 
562 #define ruSleep(secs) ruSleepMs(((msec_t)(secs))*1000)
563 
581 RUAPI void ruTryLoopInit(ruTryLoop* cycle, msec_t retryMs, msec_t timeoutMs);
582 
589 RUAPI bool ruTryLoopDone(ruTryLoop* cycle);
590 
597 RUAPI sec_t ruTimeParse(trans_chars dateformat, trans_chars datestr);
598 
605 RUAPI sec_t ruUtcParse(trans_chars dateformat, trans_chars datestr);
606 
615 RUAPI int ruTimeFormat(trans_chars format, rusize len, alloc_chars timeStr, sec_t timesecs);
616 
625 RUAPI int ruUtcFormat(trans_chars format, rusize len, alloc_chars timeStr, sec_t timesecs);
626 
633 
640 
646 RUAPI ptr ruCharPtrDup(char* in);
647 
648 
650 #define ruMacStart do
651 #define ruMacEnd while(0)
660 RUAPI 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 
690 typedef 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
719 extern "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 
747 RUAPI ruList ruIpAddrs(int32_t ipfilter);
749 #endif
750 
768 RUAPI int32_t ruGetOptMap(ruMap* parms, trans_chars opts, int argc, char** argv);
769 
770 #ifdef _WIN32
788 RUAPI int32_t ruGetOptMapW(ruMap* parms, trans_chars opts, DWORD wargc, LPWSTR* wargv);
789 #endif
790 
803 RUAPI alloc_ptr ruMallocSize(rusize count, rusize ofsize);
804 
812 #define ruMalloc0(count, ctype) (ctype*) ruMallocSize((rusize)(count), sizeof(ctype));
813 
822 RUAPI alloc_ptr ruReallocSize(alloc_ptr buf, rusize count, rusize ofsize);
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) \
854 ctype* 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) \
869 ctype* 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.
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.
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.
RUAPI const char * ruVersion(void)
Returns the build version of this package.
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 const char * ruLastError(void)
Returns an English textual representation of the last error this thread generated.
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