all files moved out from src/

This commit is contained in:
2022-03-02 22:55:19 +03:00
parent 9776a2c423
commit 8c9431460e
38 changed files with 18 additions and 63 deletions

19
DtsodC/base/base.h Normal file
View File

@@ -0,0 +1,19 @@
#pragma once
#include "std.h"
#include "types.h"
#include "errors.h"
#include "mystr.h"
// sleep function based on std nanosleep()
void fsleep(float sec);
// executes codeblock and prints execution time
#define optime(opname,repeats,codeblock) ({\
clock_t start=clock();\
for(uint64 ___OPREP=0;___OPREP<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);\
})

27
DtsodC/base/errors.c Normal file
View 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
DtsodC/base/errors.h Normal file
View 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__)))

8
DtsodC/base/fsleep.c Normal file
View File

@@ -0,0 +1,8 @@
#include "base.h"
void fsleep(float sec){
struct timespec t;
t.tv_sec=sec+0.0001f;
t.tv_nsec=(sec-t.tv_sec)*1000000000;
nanosleep(&t, NULL);
}

68
DtsodC/base/mystr.c Normal file
View File

@@ -0,0 +1,68 @@
#include "base.h"
//returns length of string (including \0)
uint32 mystrlen(char* str){
uint32 len=0;
while(*(str++)) len++;
return ++len;
}
//allocates new char[] and copies src there
char* charbuf_copy(char* src){
uint32 len=mystrlen(src);
char* dst=malloc(len*sizeof(char));
while(len-->0)
dst[len]=src[len];
return dst;
}
//compares two char buffers, NullPtr-friendly
bool charbuf_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=mystrlen(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;
}

33
DtsodC/base/mystr.h Normal file
View File

@@ -0,0 +1,33 @@
#pragma once
#include "types.h"
//returns length of string (including \0)
uint32 mystrlen(char* str);
//allocates new char[] and copies src there
char* charbuf_copy(char* src);
//compares two char buffers, NullPtr-friendly
bool charbuf_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);

15
DtsodC/base/std.h Normal file
View File

@@ -0,0 +1,15 @@
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <locale.h>
#include <uchar.h>
#include <wchar.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)

116
DtsodC/base/types.c Normal file
View File

@@ -0,0 +1,116 @@
#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);
}
}

51
DtsodC/base/types.h Normal file
View File

@@ -0,0 +1,51 @@
#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);