project structure changed for clion
This commit is contained in:
36
src/base/base.h
Normal file
36
src/base/base.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "std.h"
|
||||
#include "types.h"
|
||||
#include "errors.h"
|
||||
#include "cptr.h"
|
||||
|
||||
// executes codeblock and prints execution time
|
||||
#ifdef CLOCK_REALTIME // non-standard high-precision clock
|
||||
#define optime(opname,repeats,codeblock) ({\
|
||||
struct timespec start, stop;\
|
||||
clock_gettime(CLOCK_REALTIME, &start);\
|
||||
for(uint64 ___OPREP=0;___OPREP<(uint64)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);\
|
||||
})
|
||||
#else //
|
||||
#define optime(opname,repeats,codeblock) ({\
|
||||
clock_t start=clock();\
|
||||
for(uint64 ___OPREP=0;___OPREP<(uint64)repeats;___OPREP++)\
|
||||
(codeblock);\
|
||||
clock_t stop=clock();\
|
||||
double t=(double)(stop-start)/CLOCKS_PER_SEC/repeats;\
|
||||
printf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\
|
||||
})
|
||||
#endif
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
36
src/base/cptr.c
Normal file
36
src/base/cptr.c
Normal file
@@ -0,0 +1,36 @@
|
||||
#include "base.h"
|
||||
|
||||
// returns length of char buffer (without \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+1);
|
||||
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;
|
||||
}
|
||||
23
src/base/cptr.h
Normal file
23
src/base/cptr.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "types.h"
|
||||
|
||||
// returns length of char buffer (without \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);
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
53
src/base/errors.c
Normal file
53
src/base/errors.c
Normal file
@@ -0,0 +1,53 @@
|
||||
#include "std.h"
|
||||
#include "errors.h"
|
||||
#include "cptr.h"
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
||||
#define ERRMSG_MAXLENGTH 1024
|
||||
|
||||
char* __genErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname){
|
||||
size_t bufsize=ERRMSG_MAXLENGTH;
|
||||
char* rezult=malloc(bufsize);
|
||||
IFWIN(
|
||||
sprintf_s(rezult,bufsize,"[%s:%d] %s() throwed error: %s",srcfile,line,funcname,errmsg),
|
||||
sprintf(rezult,"[%s:%d] %s() throwed error: %s",srcfile,line,funcname,errmsg)
|
||||
);
|
||||
return rezult;
|
||||
}
|
||||
|
||||
char* __extendErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname){
|
||||
size_t bufsize=cptr_length(errmsg)+ERRMSG_MAXLENGTH;
|
||||
char* rezult=malloc(bufsize);
|
||||
IFWIN(
|
||||
sprintf_s(rezult,bufsize,"%s\n \\___[%s:%d] %s()",errmsg,srcfile,line,funcname),
|
||||
sprintf(rezult,"%s\n \\___[%s:%d] %s()",errmsg,srcfile,line,funcname)
|
||||
);
|
||||
free(errmsg);
|
||||
return rezult;
|
||||
}
|
||||
|
||||
void Maybe_free(Maybe e){
|
||||
free(e.errmsg);
|
||||
Unitype_free(e.value);
|
||||
}
|
||||
|
||||
void printMaybe(Maybe e){
|
||||
if(e.errmsg) printf("%s\n",e.errmsg);
|
||||
else printuni(e.value);
|
||||
}
|
||||
|
||||
char* __doNothing(char* a) {return a;}
|
||||
|
||||
char* __unknownErr() {return "UNKNOWN ERROR";}
|
||||
64
src/base/errors.h
Normal file
64
src/base/errors.h
Normal file
@@ -0,0 +1,64 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "std.h"
|
||||
#include "types.h"
|
||||
|
||||
typedef enum err_t {
|
||||
SUCCESS, // not an error
|
||||
ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX, ERR_NOTIMPLEMENTED, ERR_NULLPTR, ERR_ENDOFSTR
|
||||
} err_t;
|
||||
|
||||
char* errname(err_t err);
|
||||
|
||||
char* __genErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname);
|
||||
char* __extendErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname);
|
||||
|
||||
typedef struct Maybe{
|
||||
Unitype value;
|
||||
char* errmsg;
|
||||
} Maybe;
|
||||
// return it if func doesn't return anything
|
||||
static const Maybe MaybeNull={.value.type=Null, .value.VoidPtr=NULL,.errmsg=NULL};
|
||||
|
||||
void Maybe_free(Maybe e);
|
||||
void printMaybe(Maybe e);
|
||||
|
||||
|
||||
#define SUCCESS(REZLT) (Maybe){.errmsg=NULL, .value=REZLT}
|
||||
|
||||
#define __RETURN_EXCEPTION(ERRMSG) return (Maybe){.errmsg=ERRMSG, .value=UniNull}
|
||||
|
||||
#define __EXIT(ERRMSG) ({ printf("\e[91m%s\e[0m \n", ERRMSG); free(ERRMSG); exit(128); })
|
||||
|
||||
char* __doNothing(char* a);
|
||||
char* __unknownErr( );
|
||||
|
||||
#define __stringify_err(E) _Generic(\
|
||||
(E),\
|
||||
char*: __doNothing,\
|
||||
int: errname,\
|
||||
default: __unknownErr\
|
||||
)(E)
|
||||
|
||||
#define throw(E) __EXIT(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__)))
|
||||
#define safethrow(E, FREEMEM) { FREEMEM; __RETURN_EXCEPTION(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__))); }
|
||||
|
||||
|
||||
#define try(_funcCall, _rezult, freeMem) Maybe _rezult=_funcCall; if(_rezult.errmsg){\
|
||||
freeMem;\
|
||||
_rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\
|
||||
return _rezult;\
|
||||
}else
|
||||
|
||||
#define tryLast(_funcCall, _rezult) Maybe _rezult=_funcCall; if(_rezult.errmsg){\
|
||||
_rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\
|
||||
__EXIT(_rezult.errmsg);\
|
||||
}else
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
53
src/base/std.h
Normal file
53
src/base/std.h
Normal file
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <locale.h>
|
||||
#include <time.h>
|
||||
#include <setjmp.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)
|
||||
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma comment(lib, "mincore_downlevel.lib") // Support OS older than SDK
|
||||
#define _CRT_SECURE_NO_WARNINGS 1
|
||||
#define EXPORT __declspec(dllexport)
|
||||
#define CALL __cdecl
|
||||
#elif defined(__GNUC__)
|
||||
#define EXPORT __attribute__((visibility("default")))
|
||||
#if __SIZEOF_POINTER__ == 4
|
||||
#define CALL __attribute__((__cdecl__))
|
||||
#else
|
||||
#define CALL
|
||||
#endif
|
||||
#ifndef typeof
|
||||
#define typeof __typeof__
|
||||
#endif
|
||||
#else
|
||||
#pragma GCC error "unknown compiler"
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define IFWIN(YES, NO) YES
|
||||
#elif defined(__GNUC__)
|
||||
#define IFWIN(YES, NO) NO
|
||||
#else
|
||||
#pragma GCC error "unknown compiler"
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
154
src/base/types.c
Normal file
154
src/base/types.c
Normal file
@@ -0,0 +1,154 @@
|
||||
#include "types.h"
|
||||
#include "errors.h"
|
||||
#include "../Autoarr/Autoarr.h"
|
||||
#include "../Hashtable/Hashtable.h"
|
||||
#include "../SearchTree/SearchTree.h"
|
||||
|
||||
const char* my_type_name(my_type t){
|
||||
switch (t) {
|
||||
case Null: return "Null";
|
||||
case Float64: return "Float64";
|
||||
case Float32: return "Float32";
|
||||
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 Float32:
|
||||
case Float64:
|
||||
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_free_int8(u.VoidPtr);
|
||||
break;
|
||||
case AutoarrUInt8Ptr:
|
||||
__Autoarr_free_uint8(u.VoidPtr);
|
||||
break;
|
||||
case AutoarrInt16Ptr:
|
||||
__Autoarr_free_int16(u.VoidPtr);
|
||||
break;
|
||||
case AutoarrUInt16Ptr:
|
||||
__Autoarr_free_uint16(u.VoidPtr);
|
||||
break;
|
||||
case AutoarrInt32Ptr:
|
||||
__Autoarr_free_int32(u.VoidPtr);
|
||||
break;
|
||||
case AutoarrUInt32Ptr:
|
||||
__Autoarr_free_uint32(u.VoidPtr);
|
||||
break;
|
||||
case AutoarrInt64Ptr:
|
||||
__Autoarr_free_int64(u.VoidPtr);
|
||||
break;
|
||||
case AutoarrUInt64Ptr:
|
||||
__Autoarr_free_uint64(u.VoidPtr);
|
||||
break;
|
||||
case AutoarrUnitypePtr:
|
||||
Autoarr_free_Unitype(u.VoidPtr);
|
||||
break;
|
||||
case AutoarrKVPairPtr:
|
||||
Autoarr_free_KVPair(u.VoidPtr);
|
||||
break;
|
||||
default: throw(ERR_WRONGTYPE);
|
||||
}
|
||||
}
|
||||
|
||||
#define BUFSIZE 64
|
||||
char* sprintuni(Unitype v){
|
||||
char* buf=malloc(BUFSIZE);
|
||||
IFWIN(
|
||||
switch (v.type) {
|
||||
case Null: sprintf_s(buf, BUFSIZE, "{Null}");break;
|
||||
case Float64: sprintf_s(buf, BUFSIZE, "{%s : %lf}", my_type_name(v.type),v.Float64);break;
|
||||
case Bool:
|
||||
case UInt64: sprintf_s(buf, BUFSIZE, "{%s : %lu}", my_type_name(v.type),v.UInt64);break;
|
||||
case Int64: sprintf_s(buf, BUFSIZE, "{%s : %ld}", my_type_name(v.type),v.Int64);break;
|
||||
case CharPtr: ;
|
||||
size_t newBUFSIZE=cptr_length(v.VoidPtr) + BUFSIZE/2;
|
||||
buf=realloc(buf, newBUFSIZE);
|
||||
sprintf_s(buf, newBUFSIZE, "{%s : \"%s\"}", my_type_name(v.type),(char*)v.VoidPtr);
|
||||
break;
|
||||
default: sprintf_s(buf, BUFSIZE, "{%s : %p}", my_type_name(v.type),v.VoidPtr);break;
|
||||
},
|
||||
switch (v.type) {
|
||||
case Null: sprintf(buf, "{Null}");break;
|
||||
case Float64: sprintf(buf, "{%s : %lf}", my_type_name(v.type),v.Float64);break;
|
||||
case Bool:
|
||||
case UInt64: sprintf(buf, "{%s : %lu}", my_type_name(v.type),v.UInt64);break;
|
||||
case Int64: sprintf(buf, "{%s : %ld}", my_type_name(v.type),v.Int64);break;
|
||||
case CharPtr: ;
|
||||
size_t newBUFSIZE=cptr_length(v.VoidPtr) + BUFSIZE/2;
|
||||
buf=realloc(buf, newBUFSIZE);
|
||||
sprintf(buf, "{%s : \"%s\"}", my_type_name(v.type),(char*)v.VoidPtr);
|
||||
break;
|
||||
default: sprintf(buf, "{%s : %p}", my_type_name(v.type),v.VoidPtr);break;
|
||||
}
|
||||
);
|
||||
return buf;
|
||||
}
|
||||
|
||||
void printuni(Unitype v){
|
||||
char* s=sprintuni(v);
|
||||
fputs(s, stdout);
|
||||
free(s);
|
||||
}
|
||||
57
src/base/types.h
Normal file
57
src/base/types.h
Normal file
@@ -0,0 +1,57 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#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 float float32;
|
||||
typedef double float64;
|
||||
typedef enum __attribute__((__packed__)) my_type {
|
||||
Null, Float32, Float64, 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
|
||||
} my_type;
|
||||
|
||||
const char* my_type_name(my_type t);
|
||||
|
||||
typedef struct Unitype{
|
||||
union {
|
||||
int64 Int64;
|
||||
uint64 UInt64;
|
||||
double Float64;
|
||||
bool Bool;
|
||||
void* VoidPtr;
|
||||
};
|
||||
my_type type;
|
||||
} Unitype;
|
||||
|
||||
static const Unitype UniNull={.VoidPtr=NULL,.type=Null};
|
||||
static const Unitype UniTrue={.Bool=true,.type=Bool};
|
||||
static const Unitype UniFalse={.Bool=false,.type=Bool};
|
||||
|
||||
#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);
|
||||
char* sprintuni(Unitype v);
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user