7#ifndef __REISLIB_COMMMON_H__
8#define __REISLIB_COMMMON_H__
18#include <sys/resource.h>
29#elif defined(__linux__)
35#define VERISION "0.1.0"
37#if defined(BENCHMARK) || defined(DEBUG)
38 #pragma message "You are currently using a debug version of reis library."
42#define REISLIB_INT_MAX 2147483647
43#define CHARSET "abcdefghijklmnopqrstuvwxyz"
45#define MAX_CHARSET_SIZE 256
50#define MAX_DATASTRUCT_STRSIZ 100
53#define MULTIMAP_DEFAULT_SIZE 16
54#define MULTIMAP_RESIZE_FACTOR 2
56#define REISLIB_HASHTABLE_INITIAL_CAPACITY 16
57#define REISLIB_HASH_FNV_OFFSET 14695981039346656037UL
58#define REISLIB_HASH_FNV_PRIME 1099511628211UL
62#define BD_NO_CLOSE_FILES 02
63#define BD_NO_REOPEN_STD_FDS 04
65#define BD_NO_UMASK0 010
66#define BD_MAX_CLOSE 8192
69#define ANSI_RED "\x1b[31m"
70#define ANSI_GREEN "\x1b[32m"
71#define ANSI_YELLOW "\x1b[33m"
72#define ANSI_BLUE "\x1b[34m"
73#define ANSI_MAGENTA "\x1b[35m"
74#define ANSI_CYAN "\x1b[36m"
75#define ASNI_WHITE "\x1b[37m"
77#define ANSI_RESET "\x1b[0m"
79#if defined(__GNUC__) || defined(__clang__)
80#define CHECK_PRINTF_FMT(a, b) __attribute__ ((format (printf, a, b)))
82#define CHECK_PRINTF_FMT(...)
98typedef long double f128;
104#if !defined(__cplusplus)
106 typedef enum __attribute__((__packed__)) {
false,
true} bool;
110#define fnname(name) #name
120Pair * reisPairCreate(
wchar_t *first,
wchar_t *second );
121void reisPairFree(
Pair *p );
141 char keys[MAP_MAX][MAX_DATASTRUCT_STRSIZ];
146void reisMapInsert(
Map *m,
char key[],
int value);
147int reisMapGet(
Map *m,
char key[]);
148void reisMapPrint(
Map *m);
150void reisMapDel(
Map *m);
161List* reisListNew(
size_t n );
162void reisListDel(
List *a );
169 size_t allocated_size;
188void reisMultiMapInsert(
MultiMap *m,
wchar_t *key,
void *value,
size_t size);
189void** reisMultiMapGet(
MultiMap *m,
wchar_t *key,
size_t *numValues);
202static int xtTrieNumOfLetters = 26;
211TrieNode * TrieNodeInit(
wchar_t data,
int numOfLetters);
220bool TriePrintSearch(
TrieNode *root,
wchar_t *word);
257void * HashTableGet_str(
hashtable_t *table,
const char *key);
258void * HashTableGet_wcs(
hashtable_t *table,
const wchar_t *key);
259#define reisHashTableGet(table,key) _Generic((key), \
260 char *: HashTableGet_str, \
261 const char *: HashTableGet_str, \
262 wchar_t *: HashTableGet_wcs \
267const char * HashTableSet_str(
hashtable_t *table,
const char *key,
void *value);
268const wchar_t * HashTableSet_wcs(
hashtable_t *table,
const wchar_t *key,
void *value);
269#define reisHashTableSet(table,key,value) _Generic((key), \
270 char *: HashTableSet_str, \
271 const char *: HashTableSet_str, \
272 wchar_t *: HashTableSet_wcs \
292#define HT_New() reisNewHashTable()
293#define HT_Del(...) reisDelHashTable(__VA_ARGS__)
294#define HT_Get(...) reisHashTableGet(__VA_ARGS__)
295#define HT_Set(...) reisHashTableSet(__VA_ARGS__)
296#define HT_Length(...) reisHashTableLength(__VA_ARGS__)
303int reisDaemonize(
const char *name,
char* path,
int flags);
312static inline size_t rng(
size_t lb,
size_t ub) {
313#if defined(__linux__) || defined(__APPLE__)
314 struct timespec spec;
315 timespec_get(&spec, TIME_UTC);
318 int seed = lb + ((22695477 * ns) % 4294967296) % (ub - lb + 1);
325static inline size_t unix_rng(
size_t lb,
size_t ub) {
326 int fd = open(
"/dev/urandom", O_RDONLY);
333 ssize_t result = read(fd, &random,
sizeof(random));
336 if(result !=
sizeof(random)) {
340 return lb + (random % (ub - lb + 1));
347 return (
int)rng(0, REISLIB_INT_MAX-1);
350inline int randib(
int lb,
int ub) {
351 return (
int)rng(lb, ub);
356 return CHARSET[rng(0, CHARSET_LEN - 1)];
360inline char* rands(
size_t len) {
361 char *str = (
char*)malloc(len + 1);
374int isNum(
const char *str);
389char fpeek( FILE *stream );
391char fspeek( FILE *stream,
long int offset,
int position );
392int frpeek( FILE *stream,
char c );
393int frdpeek( FILE *stream,
char d );
395void fcopy( FILE *dest, FILE *src );
396bool fexists(
const char *file );
397bool fmove(
char *oldpath,
char *newpath );
400bool dexists(
const char *path );
404void sgets(
char* str,
int n);
410CHECK_PRINTF_FMT(1, 2) void eprintf(const
char *fmt, ...);
411CHECK_PRINTF_FMT(1, 3)
void cprintf(const
char * color, const
char * fmt, ...);
415char *
strdupl(const
char *str);
416char *
strcov(
char *str, const
char *charset);
417wchar_t * wcscov(
wchar_t *str, const
wchar_t *charset);
418const
char *
strset(const
char *
string, const
char *charset);
419const
wchar_t * wcsset(const
wchar_t *
string, const
wchar_t *charset);
420char *
strpcat(
char *dest, const
char *src,
size_t pos);
421wchar_t * wcspcat(
wchar_t *dest, const
wchar_t *src,
size_t pos);
422char *
chrcat(
char *dest, const
char src);
423wchar_t * wccat(
wchar_t *dest, const
wchar_t src);
424int strchrn(const
char *
string,
char ch);
425int wcswcn(const
wchar_t *
string,
wchar_t ch);
426wchar_t *
wcsrev(
wchar_t *str);
427char * strpre(
char *str,
char chr);
428wchar_t *
wcspre(
wchar_t *str,
wchar_t chr);
429char * strpres(
char *str,
char* pre);
430wchar_t * wcspres(
wchar_t *str,
wchar_t *pre);
433long hash(const
char* str);
439#define MALLOC(size) reisMalloc(size)
443#define CALLOC(nitems,size) reisCalloc(nitems, size)
447#define REALLOC(ptr,size) reisRealloc(ptr, size)
451#define MGET(ptr) reisMemGet(ptr)
455#define FREE(ptr) reisFree(ptr)
460void * reisRealloc(
void *ptr,
size_t size);
461size_t reisMemGet(
void *ptr);
462size_t reisFree(
void *ptr);
463size_t reisMemGetAll();
466void reisInsertionSort(
int arr[],
int len );
467void reisMergeSort(
int arr[],
int len );
468void reisSort(
int arr[],
int len );
475#ifdef REIS_SHORT_NAMES
476#define sort(...) reisSort(__VA_ARGS__)
void * reisMalloc(size_t size)
Definition memory.c:8
const char char * strdupl(const char *str)
Implmentation of 'strdup,' using my MALLOC.
Definition string.c:16
int wcswcn(const wchar_t *string, wchar_t ch)
(Wide-char variant) Get number of times that ch appears in string.
Definition string.c:308
int fcounts(FILE *stream)
Character count of current line of buffer.
Definition io.c:106
Vec2 * reisNewVec2(int x, int y)
Create a new Vec2 datatype instance.
Definition data.c:20
bool fexists(const char *file)
Checks if file exists.
Definition io.c:239
int frpeek(FILE *stream, char c)
A recursive peek that goes to end of line or EOF to get # of occurences.
Definition io.c:61
char fspeek(FILE *stream, long int offset, int position)
View a character at position without moving pointer; Peeks a seek.
Definition io.c:42
char * strcov(char *str, const char *charset)
Checks if string covers all of charset.
Definition string.c:29
char * strisdigit(char *str)
Confirms that string is entirely composed on numbers.
Definition string.c:337
const char * ExtractFileExtension(const char *filename)
Returns file extension.
Definition io.c:140
char * strpcat(char *dest, const char *src, size_t pos)
concatenate but at a give 'p' position.
Definition string.c:131
int strchrn(const char *string, char ch)
Get number of times that ch appears in string.
Definition string.c:292
bool fmove(char *oldpath, char *newpath)
Move data from oldpath file to newpath.
Definition io.c:197
int frdpeek(FILE *stream, char d)
A recursive peek that goes till the delimter d.
Definition io.c:82
int randi()
Uses the defined INT_MAX instead of passing bounds.
Definition reis.h:346
wchar_t * wcsisdigit(wchar_t *str)
Confirms that wcstring is entirely composed on numbers.
Definition string.c:323
void fcopy(FILE *dest, FILE *src)
Copies data from src file to dest file.
Definition io.c:124
TrieNode * TrieInsert(TrieNode *root, wchar_t *word)
Inserts word onto the trie.
Definition trie.c:40
char fpeek(FILE *stream)
View the next character in stream, doesn't move pointer.
Definition io.c:21
filesystem_t * FS_Create(char *path)
Create a filesystem_t object.
Definition io.c:354
char * chrcat(char *dest, const char src)
Concatenate a char on to the end of a string.
Definition string.c:163
void FS_Destroy(filesystem_t *fs)
Terminates a filesystem_t object.
Definition io.c:377
const char * strset(const char *string, const char *charset)
Checks to see if string contains only characters in charset.
Definition string.c:85
wchar_t * wcsrev(wchar_t *str)
Reverses the wide character string.
Definition string.c:193
bool TrieSearch(TrieNode *root, wchar_t *word)
Searches for a given word.
Definition trie.c:82
bool PromptYesOrNo(const char *question)
Prompts the user with a yes or no question.
Definition io.c:291
wchar_t fpeek_wc(FILE *stream)
Same as fpeek but for wchar_t.
Definition io.c:32
const char * ExtractFileName(const char *path)
Returns filename.
Definition io.c:167
void * reisCalloc(size_t nitems, size_t size)
Definition memory.c:25
void sgets(char *str, int n)
A safe way to read input that ensures no misc LF or breaks in read string.
Definition io.c:274
wchar_t * wcspre(wchar_t *str, wchar_t chr)
Prepends a character to a wide character string.
Definition string.c:216
bool dexists(const char *path)
Checks if directory exists.
Definition io.c:251
void reisDelVec2(Vec2 *v)
Destroy a Vec2 instance.
Definition data.c:31
TrieNode * TrieDelete(TrieNode *root, wchar_t *word)
Deletes words from trie. Will try to delete the word sequence from trie only if it ends up in a leaf ...
Definition trie.c:64