moved files from DTLib repo
This commit is contained in:
17
base/base.h
Normal file
17
base/base.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include "std.h"
|
||||
#include "types.h"
|
||||
#include "errors.h"
|
||||
#include "mystr.h"
|
||||
|
||||
// executes codeblock and prints execution time
|
||||
#define optime(opname,repeats,codeblock) ({\
|
||||
struct timespec start, stop;\
|
||||
clock_gettime(CLOCK_REALTIME, &start);\
|
||||
for(uint64 ___OPREP=0;___OPREP<repeats;___OPREP++)\
|
||||
(codeblock);\
|
||||
clock_gettime(CLOCK_REALTIME, &stop);\
|
||||
double t=(double)(stop.tv_sec-start.tv_sec+(double)(stop.tv_nsec-start.tv_nsec)/1000000000)/repeats;\
|
||||
printf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\
|
||||
})
|
||||
27
base/errors.c
Normal file
27
base/errors.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "std.h"
|
||||
#include "errors.h"
|
||||
|
||||
const char* errname(err_t err){
|
||||
switch(err){
|
||||
case SUCCESS: return "SUCCESS";
|
||||
case ERR_MAXLENGTH: return "ERR_MAXLENGTH";
|
||||
case ERR_WRONGTYPE: return "ERR_WRONGTYPE";
|
||||
case ERR_WRONGINDEX: return "ERR_WRONGINDEX";
|
||||
case ERR_NOTIMPLEMENTED: return "ERR_NOTIMPLEMENTED";
|
||||
case ERR_NULLPTR: return "ERR_NULLPTR";
|
||||
case ERR_ENDOFSTR: return "ERR_ENDOFSTR";
|
||||
default: return "UNKNOWN_ERROR";
|
||||
}
|
||||
}
|
||||
|
||||
void _throwint(int err, const char* srcfile, int line, const char* funcname){
|
||||
if(err){ // SUCCESS=0 is not an error
|
||||
printf("\e[91m[%s:%d %s] throwed error: %s\e[0m\n",srcfile,line,funcname,errname(err));
|
||||
exit(err);
|
||||
}
|
||||
else printf("\e[93m[%s:%d %s] throwed SUCCESS as an error",srcfile,line,funcname);
|
||||
}
|
||||
void _throwstr(const char* errmesg, const char* srcfile, int line, const char* funcname){
|
||||
printf("\e[91m[%s:%d %s] throwed error: %s\e[0m\n",srcfile,line,funcname,errmesg);
|
||||
exit(255);
|
||||
}
|
||||
16
base/errors.h
Normal file
16
base/errors.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
typedef enum err_t {
|
||||
SUCCESS, //not an error
|
||||
ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX, ERR_NOTIMPLEMENTED, ERR_NULLPTR, ERR_ENDOFSTR
|
||||
} err_t;
|
||||
|
||||
const char* errname(err_t err);
|
||||
|
||||
void _throwint(int err, const char* srcfile, int line, const char* funcname) ;
|
||||
void _throwstr(const char* errmesg, const char* srcfile, int line, const char* funcname);
|
||||
#pragma GCC diagnostic ignored "-Wint-conversion"
|
||||
#define throw(E) \
|
||||
CHOOSE(IFTYPE(E,int), _throwint(E,__FILE__,__LINE__,__func__), \
|
||||
CHOOSE(IFTYPE(E,char[]), _throwstr(E,__FILE__,__LINE__,__func__), \
|
||||
printf("\e[31m[%s:%d/%s] UNKNOWN ERROR\n",__FILE__,__LINE__,__func__)))
|
||||
76
base/mystr.c
Normal file
76
base/mystr.c
Normal file
@@ -0,0 +1,76 @@
|
||||
#include "base.h"
|
||||
|
||||
//returns length of string (including \0)
|
||||
uint32 cptr_length(char* str){
|
||||
uint32 len=0;
|
||||
while(*(str++)) len++;
|
||||
return ++len;
|
||||
}
|
||||
|
||||
//allocates new char[] and copies src there
|
||||
char* cptr_copy(char* src){
|
||||
uint32 len=cptr_length(src);
|
||||
char* dst=malloc(len*sizeof(char));
|
||||
while(len-->0)
|
||||
dst[len]=src[len];
|
||||
return dst;
|
||||
}
|
||||
|
||||
//compares two char buffers, NullPtr-friendly
|
||||
bool cptr_compare(char* key0, char* key1){
|
||||
if(!key0) return key1 ? false : true;
|
||||
if(!key1) return false;
|
||||
while(*key0&&*key1)
|
||||
if(*key0++ != *key1++)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
//multiplies char n times
|
||||
char* char_multiply(char c, uint32 n){
|
||||
char* rez=malloc(n+1);
|
||||
rez[n]=0;
|
||||
while(n-->0)
|
||||
rez[n]=c;
|
||||
return rez;
|
||||
}
|
||||
|
||||
//copies str content to new char pointer value (adding '\0' at the end)
|
||||
char* string_cpToCharPtr(string str){
|
||||
char* cptr=malloc(str.length*sizeof(char)+1);
|
||||
if(str.length==0) return NULL;
|
||||
cptr[str.length]=0;
|
||||
while(str.length-->0)
|
||||
cptr[str.length]=str.ptr[str.length];
|
||||
return cptr;
|
||||
}
|
||||
|
||||
//copies cptr content (excluding '\0' at the end) to new string
|
||||
string string_cpFromCharPtr(char* cptr){
|
||||
if(!cptr) return stringNull;
|
||||
string str;
|
||||
str.length=cptr_length(cptr)-1;
|
||||
str.ptr=malloc(str.length);
|
||||
for(uint32 i=0;i<str.length;i++)
|
||||
str.ptr[i]=cptr[i];
|
||||
return str;
|
||||
}
|
||||
|
||||
//compares two strings, NullPtr-friendly
|
||||
bool string_compare(string str0, string str1){
|
||||
if(str0.length!=str1.length) return false;
|
||||
if(!str0.ptr) return str1.ptr ? false : true;
|
||||
else if(!str1.ptr) return false;
|
||||
while(str0.length-->0)
|
||||
if(*str0.ptr++ != *str1.ptr++)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
//creates new string which is reversed variant of <s>
|
||||
string string_reverse(string s){
|
||||
string r={malloc(s.length), s.length};
|
||||
for(uint32 i=0; i<s.length; i++)
|
||||
r.ptr[i]=s.ptr[s.length-i-1];
|
||||
return r;
|
||||
}
|
||||
36
base/mystr.h
Normal file
36
base/mystr.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include "types.h"
|
||||
|
||||
//returns length of string (including \0)
|
||||
uint32 cptr_length(char* str);
|
||||
|
||||
//allocates new char[] and copies src there
|
||||
char* cptr_copy(char* src);
|
||||
|
||||
//compares two char buffers, NullPtr-friendly
|
||||
bool cptr_compare(char* key0, char* key1);
|
||||
|
||||
//multiplies char n times
|
||||
char* char_multiply(char c, uint32 n);
|
||||
|
||||
//my fixed length string struct
|
||||
//doesn't store '\0' at the end
|
||||
typedef struct string{
|
||||
char* ptr; //char pointer
|
||||
uint32 length; //amount of chars in ptr value
|
||||
} string;
|
||||
|
||||
static const string stringNull={NULL,0};
|
||||
|
||||
//copies str content to new char pointer value (adding '\0' at the end)
|
||||
char* string_cpToCharPtr(string str);
|
||||
|
||||
//copies cptr content (excluding '\0' at the end) to new string
|
||||
string string_cpFromCharPtr(char* cptr);
|
||||
|
||||
//compares two strings, NullPtr-friendly
|
||||
bool string_compare(string str0, string str1);
|
||||
|
||||
//creates new string which is reversed variant of <s>
|
||||
string string_reverse(string s);
|
||||
13
base/std.h
Normal file
13
base/std.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <locale.h>
|
||||
#include <time.h>
|
||||
|
||||
#define CHOOSE(B, Y, N) __builtin_choose_expr(B, Y, N)
|
||||
|
||||
#define IFTYPE(X, T) __builtin_types_compatible_p(typeof(X), T)
|
||||
|
||||
#define dbg(N) printf("\e[95m%d\n",N)
|
||||
130
base/types.c
Normal file
130
base/types.c
Normal file
@@ -0,0 +1,130 @@
|
||||
#include "types.h"
|
||||
#include "errors.h"
|
||||
#include "../Autoarr/Autoarr.h"
|
||||
#include "../Hashtable/Hashtable.h"
|
||||
#include "../SearchTree/SearchTree.h"
|
||||
|
||||
const char* typename(my_type t){
|
||||
switch (t) {
|
||||
case Null: return "Null";
|
||||
case Double: return "Double";
|
||||
case Float: return "Float";
|
||||
case Bool: return "Bool";
|
||||
case Char: return "Char";
|
||||
case Int8: return "Int8";
|
||||
case UInt8: return "UInt8";
|
||||
case Int16: return "Int16";
|
||||
case UInt16: return "UInt16";
|
||||
case Int32: return "Int32";
|
||||
case UInt32: return "UInt32";
|
||||
case Int64: return "Int64";
|
||||
case UInt64: return "UInt64";
|
||||
case Int8Ptr: return "Int8Ptr";
|
||||
case UInt8Ptr: return "UInt8Ptr";
|
||||
case Int16Ptr: return "Int16Ptr";
|
||||
case UInt16Ptr: return "UInt16Ptr";
|
||||
case Int32Ptr: return "Int32Ptr";
|
||||
case UInt32Ptr: return "UInt32Ptr";
|
||||
case Int64Ptr: return "Int64Ptr";
|
||||
case UInt64Ptr: return "UInt64Ptr";
|
||||
case CharPtr: return "CharPtr";
|
||||
case STNodePtr: return "STNodePtr";
|
||||
case HashtablePtr: return "HashtablePtr";
|
||||
case UniversalType: return "Unitype";
|
||||
case AutoarrInt8Ptr: return "AutoarrInt8Ptr";
|
||||
case AutoarrUInt8Ptr: return "AutoarrUInt8Ptr";
|
||||
case AutoarrInt16Ptr: return "AutoarrInt16Ptr";
|
||||
case AutoarrUInt16Ptr: return "AutoarrUInt16Ptr";
|
||||
case AutoarrInt32Ptr: return "AutoarrInt32Ptr";
|
||||
case AutoarrUInt32Ptr: return "AutoarrUInt32Ptr";
|
||||
case AutoarrInt64Ptr: return "AutoarrInt64Ptr";
|
||||
case AutoarrUInt64Ptr: return "AutoarrUInt64Ptr";
|
||||
case AutoarrUnitypePtr: return "AutoarrUnitypePtr";
|
||||
case AutoarrKVPairPtr: return "AutoarrKVPairPtr";
|
||||
default: throw(ERR_WRONGTYPE); return "ERROR";
|
||||
}
|
||||
}
|
||||
|
||||
//frees VoidPtr value or does nothing if type isn't pointer
|
||||
void Unitype_free(Unitype u){
|
||||
switch (u.type) {
|
||||
case Null:
|
||||
case Float:
|
||||
case Double:
|
||||
case Char:
|
||||
case Bool:
|
||||
case Int8:
|
||||
case UInt8:
|
||||
case Int16:
|
||||
case UInt16:
|
||||
case Int32:
|
||||
case UInt32:
|
||||
case Int64:
|
||||
case UInt64:
|
||||
break;
|
||||
case Int8Ptr:
|
||||
case UInt8Ptr:
|
||||
case Int16Ptr:
|
||||
case UInt16Ptr:
|
||||
case Int32Ptr:
|
||||
case UInt32Ptr:
|
||||
case Int64Ptr:
|
||||
case UInt64Ptr:
|
||||
case CharPtr:
|
||||
free(u.VoidPtr);
|
||||
break;
|
||||
case HashtablePtr:
|
||||
Hashtable_free(u.VoidPtr);
|
||||
break;
|
||||
case STNodePtr:
|
||||
STNode_free(u.VoidPtr);
|
||||
break;
|
||||
case AutoarrInt8Ptr:
|
||||
Autoarr_clear(((Autoarr(int8)*)u.VoidPtr));
|
||||
break;
|
||||
case AutoarrUInt8Ptr:
|
||||
Autoarr_clear(((Autoarr(uint8)*)u.VoidPtr));
|
||||
break;
|
||||
case AutoarrInt16Ptr:
|
||||
Autoarr_clear(((Autoarr(int16)*)u.VoidPtr));
|
||||
break;
|
||||
case AutoarrUInt16Ptr:
|
||||
Autoarr_clear(((Autoarr(uint16)*)u.VoidPtr));
|
||||
break;
|
||||
case AutoarrInt32Ptr:
|
||||
Autoarr_clear(((Autoarr(int32)*)u.VoidPtr));
|
||||
break;
|
||||
case AutoarrUInt32Ptr:
|
||||
Autoarr_clear(((Autoarr(uint32)*)u.VoidPtr));
|
||||
break;
|
||||
case AutoarrInt64Ptr:
|
||||
Autoarr_clear(((Autoarr(int64)*)u.VoidPtr));
|
||||
break;
|
||||
case AutoarrUInt64Ptr:
|
||||
Autoarr_clear(((Autoarr(uint64)*)u.VoidPtr));
|
||||
break;
|
||||
case AutoarrUnitypePtr:
|
||||
Autoarr_Unitype_clear(u.VoidPtr);
|
||||
free((Autoarr(Unitype)*)u.VoidPtr);
|
||||
break;
|
||||
case AutoarrKVPairPtr:
|
||||
Autoarr_KeyValuePair_clear(u.VoidPtr);
|
||||
free((Autoarr(KeyValuePair)*)u.VoidPtr);
|
||||
break;
|
||||
default: throw(ERR_WRONGTYPE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void printuni(Unitype v){
|
||||
switch (v.type) {
|
||||
case Null: printf("{Null}");break;
|
||||
case Double: printf("{%s : %lf}",typename(v.type),v.Double);break;
|
||||
case Char: printf("{%s : '%c'}",typename(v.type),v.Char);break;
|
||||
case Bool:
|
||||
case UInt64: printf("{%s : %lu}",typename(v.type),v.UInt64);break;
|
||||
case Int64: printf("{%s : %ld}",typename(v.type),v.Int64);break;
|
||||
case CharPtr: printf("{%s : \"%s\"}",typename(v.type),(char*)v.VoidPtr);break;
|
||||
default: printf("{%s : %p}",typename(v.type),v.VoidPtr);break;
|
||||
}
|
||||
}
|
||||
52
base/types.h
Normal file
52
base/types.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
|
||||
#include "std.h"
|
||||
|
||||
typedef int8_t int8;
|
||||
typedef uint8_t uint8;
|
||||
typedef int16_t int16;
|
||||
typedef uint16_t uint16;
|
||||
typedef int32_t int32;
|
||||
typedef uint32_t uint32;
|
||||
typedef int64_t int64;
|
||||
typedef uint64_t uint64;
|
||||
typedef enum my_type{
|
||||
Null, Float, Double, Char, Bool,
|
||||
UInt8, Int8, UInt16, Int16, UInt32, Int32, UInt64, Int64,
|
||||
UInt8Ptr, Int8Ptr, UInt16Ptr, Int16Ptr, UInt32Ptr, Int32Ptr, UInt64Ptr, Int64Ptr,
|
||||
CharPtr, STNodePtr, HashtablePtr,
|
||||
UniversalType,
|
||||
AutoarrInt8Ptr, AutoarrUInt8Ptr, AutoarrInt16Ptr, AutoarrUInt16Ptr,
|
||||
AutoarrInt32Ptr, AutoarrUInt32Ptr, AutoarrInt64Ptr, AutoarrUInt64Ptr,
|
||||
AutoarrUnitypePtr, AutoarrKVPairPtr
|
||||
} __attribute__ ((__packed__)) my_type;
|
||||
|
||||
//returns type name
|
||||
const char* typename(my_type t);
|
||||
|
||||
// returns size of type in bytes
|
||||
int8 typesize(my_type type);
|
||||
|
||||
// can store any base type
|
||||
typedef struct Unitype{
|
||||
my_type type;
|
||||
union {
|
||||
int64 Int64;
|
||||
uint64 UInt64;
|
||||
double Double;
|
||||
char Char;
|
||||
bool Bool;
|
||||
void* VoidPtr;
|
||||
};
|
||||
} Unitype;
|
||||
|
||||
static const Unitype UniNull={Null,.VoidPtr=NULL};
|
||||
static const Unitype UniTrue={Bool,.Bool=true};
|
||||
static const Unitype UniFalse={Bool,.Bool=false};
|
||||
|
||||
#define Uni(TYPE,VAL) (Unitype){.type=TYPE,.TYPE=VAL}
|
||||
#define UniPtr(TYPE,VAL) (Unitype){.type=TYPE,.VoidPtr=VAL}
|
||||
|
||||
//frees VoidPtr value or does nothing if type isn't pointer
|
||||
void Unitype_free(Unitype u);
|
||||
void printuni(Unitype v);
|
||||
Reference in New Issue
Block a user