autoarr
This commit is contained in:
parent
cf8aa8bff0
commit
7b0426fa75
@ -1,31 +1,27 @@
|
||||
SRC_LIB=src/autosize_array/Autoarr.c src/throw.c src/strict_types.c
|
||||
SRC_COM=src/test.c
|
||||
SRC=src/*.c
|
||||
OUTDIR=bin/
|
||||
OUTFILE=dtsodc.com
|
||||
|
||||
all: clear_c std_build test
|
||||
all: clear_c clear_bin test
|
||||
|
||||
clear_c:
|
||||
clear
|
||||
|
||||
clear:
|
||||
@echo "\e[36m-----------------[clear]-------------------\e[0m"
|
||||
rm bin/*.com
|
||||
clear_bin:
|
||||
@echo "\e[36m-----------------[clear_bin]-------------------\e[0m"
|
||||
touch $(OUTDIR)_.com
|
||||
rm $(OUTDIR)*.com
|
||||
|
||||
# using sdlib
|
||||
STDARGS=-D STDLIB
|
||||
|
||||
std_build: std_build_test
|
||||
std_build:
|
||||
@echo "\e[36m-------------[std_build]---------------\e[0m"
|
||||
gcc $(STDARGS) $(SRC) -o $(OUTDIR)std_$(OUTFILE)
|
||||
|
||||
std_build_lib:
|
||||
@echo "\e[36m-------------[std_build_lib]---------------\e[0m"
|
||||
gcc $(STDARGS) $(SRC_LIB) -o bin/dtsodc_test_STD.dll
|
||||
|
||||
std_build_test:
|
||||
@echo "\e[36m-------------[std_build_test]---------------\e[0m"
|
||||
gcc $(STDARGS) $(SRC_LIB) $(SRC_COM) -o bin/dtsodc_test_STD.com
|
||||
|
||||
std_test: std_build_test
|
||||
std_test: std_build
|
||||
@echo "\e[36m----------------[std_test]------------------\e[0m"
|
||||
bin/dtsodc_test_STD.com
|
||||
$(OUTDIR)std_$(OUTFILE)
|
||||
|
||||
|
||||
# using cosmopolitan
|
||||
@ -33,16 +29,10 @@ COSMARGS_PRE=-g -O -static -fno-pie -no-pie -mno-red-zone -nostdlib -nostdinc -D
|
||||
COSMARGS_POST=-Wl,--oformat=binary -Wl,--gc-sections -Wl,-z,max-page-size=0x1000 -fuse-ld=bfd -Wl,-T,cosmopolitan/ape.lds \
|
||||
-include cosmopolitan/cosmopolitan.h cosmopolitan/crt.o ./cosmopolitan/ape.o cosmopolitan/cosmopolitan.a
|
||||
|
||||
build: build_test
|
||||
build:
|
||||
@echo "\e[36m----------------[build]----------------\e[0m"
|
||||
gcc $(COSMARGS_PRE) $(SRC) $(COSMARGS_POST) -o $(OUTDIR)$(OUTFILE)
|
||||
|
||||
build_lib:
|
||||
@echo "\e[36m----------------[build_lib]-----------------\e[0m"
|
||||
gcc $(COSMARGS_PRE) $(SRC_LIB) $(COSMARGS_POST) -o bin/dtsodc_test.dll
|
||||
|
||||
build_test:
|
||||
@echo "\e[36m----------------[build_test]----------------\e[0m"
|
||||
gcc $(COSMARGS_PRE) $(SRC_LIB) $(SRC_COM) $(COSMARGS_POST) -o bin/dtsodc_test.com
|
||||
|
||||
test: build_test
|
||||
test: build
|
||||
@echo "\e[36m-----------------[test]----------------\e[0m"
|
||||
bin/dtsodc_test.com
|
||||
$(OUTDIR)$(OUTFILE)
|
||||
@ -1,60 +0,0 @@
|
||||
#ifndef STDLIB
|
||||
#include "../../cosmopolitan/cosmopolitan.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#endif
|
||||
#include "Autoarr.h"
|
||||
|
||||
|
||||
Autoarr* Autoarr_create(uint16 max_block_count, uint16 max_block_length, strict_type type){
|
||||
Autoarr* rezult=malloc(sizeof(Autoarr));
|
||||
rezult->type=type;
|
||||
rezult->max_block_length=max_block_length;
|
||||
rezult->curr_block_length=0;
|
||||
rezult->max_block_count=max_block_count;
|
||||
rezult->block_count=0;
|
||||
rezult->values=malloc(rezult->max_block_count*GetTypeSize(type));
|
||||
return rezult;
|
||||
}
|
||||
|
||||
uint32 Autoarr_length(Autoarr *a){
|
||||
return (uint32)((a->block_count-1)*a->max_block_length+a->curr_block_length);
|
||||
}
|
||||
uint32 Autoarr_max_length(Autoarr *a){
|
||||
return a->max_block_count*a->max_block_length;
|
||||
}
|
||||
|
||||
void __Autoarr_add_block(Autoarr *autoarr){
|
||||
if (autoarr->block_count>=autoarr->max_block_count)
|
||||
throwerr(ERR_MAXLENGTH);
|
||||
if (autoarr->curr_block_length==(autoarr->max_block_length|0))
|
||||
autoarr->curr_block_length=0;
|
||||
else if (autoarr->curr_block_length>0)
|
||||
throwerr(ERR_NOTFULL);
|
||||
*(autoarr->values+autoarr->block_count)=malloc(autoarr->max_block_length*GetTypeSize(autoarr->type));
|
||||
autoarr->block_count++;
|
||||
}
|
||||
|
||||
void Autoarr_add(Autoarr *autoarr, void *element){
|
||||
if(Autoarr_length(autoarr)<Autoarr_max_length(autoarr))
|
||||
throwerr(ERR_MAXLENGTH);
|
||||
if (autoarr->curr_block_length==autoarr->max_block_length)
|
||||
__Autoarr_add_block(autoarr);
|
||||
AssignVoidToVoid((autoarr->values+autoarr->block_count-1)+autoarr->curr_block_length, element, autoarr->type);
|
||||
}
|
||||
|
||||
void Autoarr_set(Autoarr *autoarr, uint32 index, void *element){
|
||||
|
||||
|
||||
}
|
||||
|
||||
void Autoarr_remove(Autoarr *autoarr, uint32 index){
|
||||
|
||||
|
||||
}
|
||||
|
||||
void Autoarr_removeRange(Autoarr *autoarr, uint32 startIndex, uint32 length){
|
||||
|
||||
}
|
||||
@ -1,36 +0,0 @@
|
||||
#include "../!headers.h"
|
||||
#include "../strict_types.h"
|
||||
#include "../errors.h"
|
||||
|
||||
typedef struct Autoarr{
|
||||
void** values;
|
||||
strict_type type;
|
||||
uint16 max_block_length;
|
||||
uint16 curr_block_length;
|
||||
uint16 block_count;
|
||||
uint16 max_block_count;
|
||||
} Autoarr;
|
||||
typedef Autoarr Autoarr;
|
||||
|
||||
|
||||
Autoarr* Autoarr_create(uint16 max_length, uint16 max_block_length, strict_type type);
|
||||
|
||||
uint32 Autoarr_length(Autoarr *a);
|
||||
uint32 Autoarr_max_length(Autoarr *a);
|
||||
|
||||
void Autoarr_add(Autoarr *autoarr, void *element);
|
||||
|
||||
void Autoarr_set(Autoarr *autoarr, uint32 index, void *element);
|
||||
|
||||
void Autoarr_remove(Autoarr *autoarr, uint32 index);
|
||||
|
||||
void Autoarr_removeRange(Autoarr *autoarr, uint32 startIndex, uint32 length);
|
||||
|
||||
int8 Autoarr_get_int8(Autoarr *autoarr, uint32 index);
|
||||
uint8 Autoarr_get_uint8(Autoarr *autoarr, uint32 index);
|
||||
int16 Autoarr_get_int16(Autoarr *autoarr, uint32 index);
|
||||
uint16 Autoarr_get_uint16(Autoarr *autoarr, uint32 index);
|
||||
int32 Autoarr_get_int32(Autoarr *autoarr, uint32 index);
|
||||
uint32 Autoarr_get_uint32(Autoarr *autoarr, uint32 index);
|
||||
int64 Autoarr_get_int64(Autoarr *autoarr, uint32 index);
|
||||
uint64 Autoarr_get_uint64(Autoarr *autoarr, uint32 index);
|
||||
183
DtsodC/src/base/Autoarr.c
Normal file
183
DtsodC/src/base/Autoarr.c
Normal file
@ -0,0 +1,183 @@
|
||||
#include "Autoarr.h"
|
||||
|
||||
Autoarr Autoarr_create(uint16 _max_block_count, uint16 _max_block_length, base_type _type){
|
||||
Autoarr ar={
|
||||
.type=_type,
|
||||
.max_block_count=_max_block_count,
|
||||
.max_block_length=_max_block_length,
|
||||
.curr_block_count=1,
|
||||
.curr_block_length=0,
|
||||
.max_length=_max_block_count*_max_block_length,
|
||||
.curr_length=0,
|
||||
.values=malloc(_max_block_count*typesize(_type))
|
||||
};
|
||||
*ar.values=malloc(ar.max_block_length*typesize(ar.type));
|
||||
return ar;
|
||||
}
|
||||
|
||||
// creates new block if the current one is filled
|
||||
void __Autoarr_create_block(Autoarr *ar){
|
||||
if (ar->curr_block_count>=ar->max_block_count) throw(ERR_MAXLENGTH);
|
||||
//if (ar->curr_block_length==ar->max_block_length)
|
||||
ar->curr_block_length=0;
|
||||
//else throw("current block isn't filled");
|
||||
*(ar->values+ar->curr_block_count)=malloc(ar->max_block_length*typesize(ar->type));
|
||||
ar->curr_block_count++;
|
||||
}
|
||||
|
||||
void __Autoarr_add_pre(Autoarr* ar, base_type t){
|
||||
if(ar->type!=t) throw(ERR_WRONGTYPE);
|
||||
if(ar->curr_length>=ar->max_length) throw(ERR_MAXLENGTH);
|
||||
if (ar->curr_block_length==ar->max_block_length)
|
||||
__Autoarr_create_block(ar);
|
||||
ar->curr_block_length++;
|
||||
ar->curr_length++;
|
||||
}
|
||||
|
||||
void Autoarr_add_int8(Autoarr *ar, int8 element){
|
||||
__Autoarr_add_pre(ar,Int8);
|
||||
*(*((int8**)ar->values+ar->curr_block_count-1)+ar->curr_block_length-1)=element;
|
||||
}
|
||||
void Autoarr_add_uint8(Autoarr *ar, uint8 element){
|
||||
__Autoarr_add_pre(ar,UInt8);
|
||||
*(*((uint8**)ar->values+ar->curr_block_count-1)+ar->curr_block_length-1)=element;
|
||||
}
|
||||
void Autoarr_add_int16(Autoarr *ar, int16 element){
|
||||
__Autoarr_add_pre(ar,Int16);
|
||||
*(*((int16**)ar->values+ar->curr_block_count-1)+ar->curr_block_length-1)=element;
|
||||
}
|
||||
void Autoarr_add_uint16(Autoarr *ar, uint16 element){
|
||||
__Autoarr_add_pre(ar,UInt16);
|
||||
*(*((uint16**)ar->values+ar->curr_block_count-1)+ar->curr_block_length-1)=element;
|
||||
}
|
||||
void Autoarr_add_int32(Autoarr *ar, int32 element){
|
||||
__Autoarr_add_pre(ar,Int32);
|
||||
*(*((int32**)ar->values+ar->curr_block_count-1)+ar->curr_block_length-1)=element;
|
||||
}
|
||||
void Autoarr_add_uint32(Autoarr *ar, uint32 element){
|
||||
__Autoarr_add_pre(ar,UInt32);
|
||||
*(*((uint32**)ar->values+ar->curr_block_count-1)+ar->curr_block_length-1)=element;
|
||||
}
|
||||
void Autoarr_add_int64(Autoarr *ar, int64 element){
|
||||
__Autoarr_add_pre(ar,Int64);
|
||||
*(*((int64**)ar->values+ar->curr_block_count-1)+ar->curr_block_length-1)=element;
|
||||
}
|
||||
void Autoarr_add_uint64(Autoarr *ar, uint64 element){
|
||||
__Autoarr_add_pre(ar,UInt64);
|
||||
*(*((uint64**)ar->values+ar->curr_block_count-1)+ar->curr_block_length-1)=element;
|
||||
}
|
||||
|
||||
// calculates a block number and element position in the block
|
||||
// also verifies type of array
|
||||
div_t __Autoarr_div_index(Autoarr* ar, uint32 i, base_type t){
|
||||
if(ar->type!=t) throw(ERR_WRONGTYPE);
|
||||
if(i>=ar->curr_length) throw(ERR_WRONGINDEX);
|
||||
return (div_t){
|
||||
.quot=i/ar->max_block_length,
|
||||
.rem=i%ar->max_block_length
|
||||
};;
|
||||
}
|
||||
|
||||
int8 Autoarr_get_int8(Autoarr *ar, uint32 index){
|
||||
div_t d = __Autoarr_div_index(ar, index, Int8);
|
||||
return *(*((int8**)ar->values+d.quot)+d.rem);
|
||||
}
|
||||
uint8 Autoarr_get_uint8(Autoarr *ar, uint32 index){
|
||||
div_t d = __Autoarr_div_index(ar, index, UInt8);
|
||||
return *(*((uint8**)ar->values+d.quot)+d.rem);
|
||||
}
|
||||
int16 Autoarr_get_int16(Autoarr *ar, uint32 index){
|
||||
div_t d = __Autoarr_div_index(ar, index, Int16);
|
||||
return *(*((int16**)ar->values+d.quot)+d.rem);
|
||||
}
|
||||
uint16 Autoarr_get_uint16(Autoarr *ar, uint32 index){
|
||||
div_t d = __Autoarr_div_index(ar, index, UInt16);
|
||||
return *(*((uint16**)ar->values+d.quot)+d.rem);
|
||||
}
|
||||
int32 Autoarr_get_int32(Autoarr *ar, uint32 index){
|
||||
div_t d = __Autoarr_div_index(ar, index, Int32);
|
||||
return *(*((int32**)ar->values+d.quot)+d.rem);
|
||||
}
|
||||
uint32 Autoarr_get_uint32(Autoarr *ar, uint32 index){
|
||||
div_t d = __Autoarr_div_index(ar, index, UInt32);
|
||||
return *(*((uint32**)ar->values+d.quot)+d.rem);
|
||||
}
|
||||
int64 Autoarr_get_int64(Autoarr *ar, uint32 index){
|
||||
div_t d = __Autoarr_div_index(ar, index, Int64);
|
||||
return *(*((int64**)ar->values+d.quot)+d.rem);
|
||||
}
|
||||
uint64 Autoarr_get_uint64(Autoarr *ar, uint32 index){
|
||||
div_t d = __Autoarr_div_index(ar, index, UInt64);
|
||||
return *(*((uint64**)ar->values+d.quot)+d.rem);
|
||||
}
|
||||
|
||||
void Autoarr_set_int8(Autoarr *ar, uint32 index, int8 element){
|
||||
div_t d =__Autoarr_div_index(ar, index, Int8);
|
||||
*(*((int8**)ar->values+d.quot)+d.rem)=element;
|
||||
}
|
||||
void Autoarr_set_uint8(Autoarr *ar, uint32 index, uint8 element){
|
||||
div_t d =__Autoarr_div_index(ar, index, UInt8);
|
||||
*(*((uint8**)ar->values+d.quot)+d.rem)=element;
|
||||
}
|
||||
void Autoarr_set_int16(Autoarr *ar, uint32 index, int16 element){
|
||||
div_t d =__Autoarr_div_index(ar, index, Int16);
|
||||
*(*((int16**)ar->values+d.quot)+d.rem)=element;
|
||||
}
|
||||
void Autoarr_set_uint16(Autoarr *ar, uint32 index, uint16 element){
|
||||
div_t d =__Autoarr_div_index(ar, index, UInt16);
|
||||
*(*((uint16**)ar->values+d.quot)+d.rem)=element;
|
||||
}
|
||||
void Autoarr_set_int32(Autoarr *ar, uint32 index, int32 element){
|
||||
div_t d =__Autoarr_div_index(ar, index, Int32);
|
||||
*(*((int32**)ar->values+d.quot)+d.rem)=element;
|
||||
}
|
||||
void Autoarr_set_uint32(Autoarr *ar, uint32 index, uint32 element){
|
||||
div_t d =__Autoarr_div_index(ar, index, UInt32);
|
||||
*(*((uint32**)ar->values+d.quot)+d.rem)=element;
|
||||
}
|
||||
void Autoarr_set_int64(Autoarr *ar, uint32 index, int64 element){
|
||||
div_t d =__Autoarr_div_index(ar, index, Int64);
|
||||
*(*((int64**)ar->values+d.quot)+d.rem)=element;
|
||||
}
|
||||
void Autoarr_set_uint64(Autoarr *ar, uint32 index, uint64 element){
|
||||
div_t d =__Autoarr_div_index(ar, index, UInt64);
|
||||
*(*((uint64**)ar->values+d.quot)+d.rem)=element;
|
||||
}
|
||||
|
||||
void Autoarr_free(Autoarr* ar){
|
||||
switch (ar->type) {
|
||||
case Int8:
|
||||
for(uint16 i = 0; i < ar->curr_block_count-1;i++)
|
||||
free(*((int8**)ar->values+i)); break;
|
||||
case UInt8:
|
||||
for(uint16 i = 0; i < ar->curr_block_count-1;i++)
|
||||
free(*((uint8**)ar->values+i)); break;
|
||||
case Int16:
|
||||
for(uint16 i = 0; i < ar->curr_block_count-1;i++)
|
||||
free(*((int16**)ar->values+i)); break;
|
||||
case UInt16:
|
||||
for(uint16 i = 0; i < ar->curr_block_count-1;i++)
|
||||
free(*((uint16**)ar->values+i)); break;
|
||||
case Int32:
|
||||
for(uint16 i = 0; i < ar->curr_block_count-1;i++)
|
||||
free(*((int32**)ar->values+i)); break;
|
||||
case UInt32:
|
||||
for(uint16 i = 0; i < ar->curr_block_count-1;i++)
|
||||
free(*((uint32**)ar->values+i)); break;
|
||||
case Int64:
|
||||
for(uint16 i = 0; i < ar->curr_block_count-1;i++)
|
||||
free(*((int64**)ar->values+i)); break;
|
||||
case UInt64:
|
||||
for(uint16 i = 0; i < ar->curr_block_count-1;i++)
|
||||
free(*((uint64**)ar->values+i)); break;
|
||||
default: throw(ERR_WRONGTYPE); break;
|
||||
}
|
||||
free(ar->values);
|
||||
ar->type=0;
|
||||
ar->max_block_count=0;
|
||||
ar->max_block_length=0;
|
||||
ar->curr_block_count=0;
|
||||
ar->curr_block_length=0;
|
||||
ar->max_length=0;
|
||||
ar->curr_length=0;
|
||||
}
|
||||
47
DtsodC/src/base/Autoarr.h
Normal file
47
DtsodC/src/base/Autoarr.h
Normal file
@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#include "std.h"
|
||||
#include "base_types.h"
|
||||
#include "errors.h"
|
||||
|
||||
typedef struct Autoarr{
|
||||
base_type type; // type of data
|
||||
uint16 max_block_count; // max amount of blocks
|
||||
uint16 curr_block_count; // amount of blocks
|
||||
uint16 max_block_length; // max amount of data in block
|
||||
uint16 curr_block_length; // amount of data in the last block
|
||||
uint32 max_length; // max_block_count*max_block_length
|
||||
uint32 curr_length; // (curr_block_count-1)*max_block_length+curr_block_length
|
||||
void** values; // pointers to blocks
|
||||
} Autoarr;
|
||||
|
||||
Autoarr Autoarr_create(uint16 _max_block_count, uint16 _max_block_length, base_type _type);
|
||||
|
||||
void Autoarr_add_int8(Autoarr *ar, int8 element);
|
||||
void Autoarr_add_uint8(Autoarr *ar, uint8 element);
|
||||
void Autoarr_add_int16(Autoarr *ar, int16 element);
|
||||
void Autoarr_add_uint16(Autoarr *ar, uint16 element);
|
||||
void Autoarr_add_int32(Autoarr *ar, int32 element);
|
||||
void Autoarr_add_uint32(Autoarr *ar, uint32 element);
|
||||
void Autoarr_add_int64(Autoarr *ar, int64 element);
|
||||
void Autoarr_add_uint64(Autoarr *ar, uint64 element);
|
||||
|
||||
int8 Autoarr_get_int8(Autoarr *ar, uint32 index);
|
||||
uint8 Autoarr_get_uint8(Autoarr *ar, uint32 index);
|
||||
int16 Autoarr_get_int16(Autoarr *ar, uint32 index);
|
||||
uint16 Autoarr_get_uint16(Autoarr *ar, uint32 index);
|
||||
int32 Autoarr_get_int32(Autoarr *ar, uint32 index);
|
||||
uint32 Autoarr_get_uint32(Autoarr *ar, uint32 index);
|
||||
int64 Autoarr_get_int64(Autoarr *ar, uint32 index);
|
||||
uint64 Autoarr_get_uint64(Autoarr *ar, uint32 index);
|
||||
|
||||
void Autoarr_set_int8(Autoarr *ar, uint32 index, int8 element);
|
||||
void Autoarr_set_uint8(Autoarr *ar, uint32 index, uint8 element);
|
||||
void Autoarr_set_int16(Autoarr *ar, uint32 index, int16 element);
|
||||
void Autoarr_set_uint16(Autoarr *ar, uint32 index, uint16 element);
|
||||
void Autoarr_set_int32(Autoarr *ar, uint32 index, int32 element);
|
||||
void Autoarr_set_uint32(Autoarr *ar, uint32 index, uint32 element);
|
||||
void Autoarr_set_int64(Autoarr *ar, uint32 index, int64 element);
|
||||
void Autoarr_set_uint64(Autoarr *ar, uint32 index, uint64 element);
|
||||
|
||||
void Autoarr_free(Autoarr* ar);
|
||||
50
DtsodC/src/base/base_types.c
Normal file
50
DtsodC/src/base/base_types.c
Normal file
@ -0,0 +1,50 @@
|
||||
#include "std.h"
|
||||
#include "base_types.h"
|
||||
#include "errors.h"
|
||||
|
||||
const char* typename(base_type t){
|
||||
switch (t) {
|
||||
case Null: return "Null";
|
||||
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";
|
||||
default: throw(ERR_WRONGTYPE); break;
|
||||
}
|
||||
}
|
||||
|
||||
int8 typesize(base_type type){
|
||||
int8 type_size=0;
|
||||
switch (type)
|
||||
{
|
||||
case Int8: type_size=8; break;
|
||||
case UInt8: type_size=8; break;
|
||||
case Int16: type_size=16; break;
|
||||
case UInt16: type_size=16; break;
|
||||
case Int32: type_size=32; break;
|
||||
case UInt32: type_size=32; break;
|
||||
case Int64: type_size=64; break;
|
||||
case UInt64: type_size=64; break;
|
||||
default: throw(ERR_WRONGTYPE);
|
||||
}
|
||||
return type_size;
|
||||
}
|
||||
|
||||
void AssignVoidToVoid(void* a, void*b, base_type type){
|
||||
switch (type)
|
||||
{
|
||||
case Int8: *((int8*)a)=*((int8*)b); break;
|
||||
case UInt8: *((uint8*)a)=*((uint8*)b); break;
|
||||
case Int16: *((int16*)a)=*((int16*)b); break;
|
||||
case UInt16: *((uint16*)a)=*((uint16*)b); break;
|
||||
case Int32: *((int32*)a)=*((int32*)b); break;
|
||||
case UInt32: *((uint32*)a)=*((uint32*)b); break;
|
||||
case Int64: *((int64*)a)=*((int64*)b); break;
|
||||
case UInt64: *((uint64*)a)=*((uint64*)b); break;
|
||||
default: throw(ERR_WRONGTYPE);
|
||||
}
|
||||
}
|
||||
23
DtsodC/src/base/base_types.h
Normal file
23
DtsodC/src/base/base_types.h
Normal file
@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include "std.h"
|
||||
#include "errors.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 base_type{
|
||||
Null,Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64
|
||||
} base_type;
|
||||
|
||||
const char* typename(base_type t);
|
||||
|
||||
int8 typesize(base_type type);
|
||||
|
||||
void AssignVoidToVoid(void* a, void*b, base_type type);
|
||||
22
DtsodC/src/base/errors.c
Normal file
22
DtsodC/src/base/errors.c
Normal file
@ -0,0 +1,22 @@
|
||||
#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";
|
||||
default: return "UNKNOWN_ERROR";
|
||||
}
|
||||
}
|
||||
|
||||
void _throwint(int err, const char* srcfile, int line, const char* funcname){
|
||||
printf("\e[31m[%s:%d/%s] throwed error: %s\e[0m\n",srcfile,line,funcname,errname(err));
|
||||
exit(err);
|
||||
}
|
||||
void _throwstr(const char* errmesg, const char* srcfile, int line, const char* funcname){
|
||||
printf("\e[31m[%s:%d/%s] throwed error: %s\e[0m\n",srcfile,line,funcname,errmesg);
|
||||
exit(255);
|
||||
}
|
||||
14
DtsodC/src/base/errors.h
Normal file
14
DtsodC/src/base/errors.h
Normal file
@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
typedef enum err_t {
|
||||
SUCCESS, ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX, ERR_NOTIMPLEMENTED
|
||||
} 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("NONE\n")))
|
||||
|
||||
@ -1,12 +1,11 @@
|
||||
#include "!headers.h"
|
||||
#include "strict_types.h"
|
||||
#pragma once
|
||||
|
||||
#include "std.h"
|
||||
#include "base_types.h"
|
||||
|
||||
#ifndef __NSLEEP_DEFINED
|
||||
#define __NSLEEP_DEFINED
|
||||
void nsleep(uint8 sec, uint8 milisec){
|
||||
if (sec>0)
|
||||
sleep(sec);
|
||||
if (milisec>0)
|
||||
usleep(milisec*1000);
|
||||
}
|
||||
#endif
|
||||
@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#if COSMOPOLITAN
|
||||
#include "../cosmopolitan/cosmopolitan.h"
|
||||
#include "../../cosmopolitan/cosmopolitan.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@ -12,3 +14,6 @@
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#define CHOOSE(B, Y, N) __builtin_choose_expr(B, Y, N)
|
||||
#define IFTYPE(X, T) __builtin_types_compatible_p(typeof(X), T)
|
||||
@ -1,9 +0,0 @@
|
||||
#ifndef __ERR_AUTOARR_DEFINED
|
||||
#define __ERR_AUTOARR_DEFINED
|
||||
typedef enum err_t {
|
||||
SUCCESS, ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX, ERR_NOTFULL
|
||||
} err_t;
|
||||
|
||||
void throwstr(const char* errmesg);
|
||||
void throwerr(err_t err);
|
||||
#endif
|
||||
12
DtsodC/src/main.c
Normal file
12
DtsodC/src/main.c
Normal file
@ -0,0 +1,12 @@
|
||||
#include "base/std.h"
|
||||
#include "base/base_types.h"
|
||||
#include "base/errors.h"
|
||||
#include "base/Autoarr.h"
|
||||
|
||||
#define clrscr() printf("\e[1;1H\e[2J")
|
||||
|
||||
int main(){
|
||||
setlocale(LC_ALL, "en-US.Unicode");
|
||||
printf("\e[92mdtsod parser in c language!\e[94m\n");
|
||||
return 0;
|
||||
}
|
||||
@ -1,35 +0,0 @@
|
||||
#include "!headers.h"
|
||||
#include "strict_types.h"
|
||||
#include "errors.h"
|
||||
|
||||
int8 GetTypeSize(strict_type type){
|
||||
int8 type_size=0;
|
||||
switch (type)
|
||||
{
|
||||
case Int8: type_size=8; break;
|
||||
case Uint8: type_size=8; break;
|
||||
case Int16: type_size=16; break;
|
||||
case Uint16: type_size=16; break;
|
||||
case Int32: type_size=32; break;
|
||||
case Uint32: type_size=32; break;
|
||||
case Int64: type_size=64; break;
|
||||
case Uint64: type_size=64; break;
|
||||
default: throwerr(ERR_WRONGTYPE);
|
||||
}
|
||||
return type_size;
|
||||
}
|
||||
|
||||
void AssignVoidToVoid(void* a, void*b, strict_type type){
|
||||
switch (type)
|
||||
{
|
||||
case Int8: *((int8*)a)=*((int8*)b); break;
|
||||
case Uint8: *((uint8*)a)=*((uint8*)b); break;
|
||||
case Int16: *((int16*)a)=*((int16*)b); break;
|
||||
case Uint16: *((uint16*)a)=*((uint16*)b); break;
|
||||
case Int32: *((int32*)a)=*((int32*)b); break;
|
||||
case Uint32: *((uint32*)a)=*((uint32*)b); break;
|
||||
case Int64: *((int64*)a)=*((int64*)b); break;
|
||||
case Uint64: *((uint64*)a)=*((uint64*)b); break;
|
||||
default: throwerr(ERR_WRONGTYPE);
|
||||
}
|
||||
}
|
||||
@ -1,22 +0,0 @@
|
||||
#include "!headers.h"
|
||||
#include "errors.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;
|
||||
|
||||
#ifndef __STRICT_TYPES_DEFINED
|
||||
#define __STRICT_TYPES_DEFINED
|
||||
typedef enum strict_type{
|
||||
Int8, Int16, Int32, Int64, Uint8, Uint16, Uint32, Uint64
|
||||
} strict_type;
|
||||
|
||||
int8 GetTypeSize(strict_type type);
|
||||
|
||||
void AssignVoidToVoid(void* a, void*b, strict_type type);
|
||||
#endif
|
||||
@ -1,34 +0,0 @@
|
||||
#include "!headers.h"
|
||||
#include "strict_types.h"
|
||||
#include "errors.h"
|
||||
#include "autosize_array/Autoarr.h"
|
||||
#include "nsleep.h"
|
||||
#include "slimak.h"
|
||||
|
||||
#define clrscr() printf("\e[1;1H\e[2J")
|
||||
int main(){
|
||||
printf("\e[32mdtsod parser in c language!\n");
|
||||
//Autoarr* ar=Autoarr_create(100,10,Int8);
|
||||
//Autoarr_add_int8(&ar,-1);
|
||||
//printf("ar[0]==%d",Autoarr_get_int8(&ar,0));
|
||||
//free(ar);
|
||||
setlocale(LC_ALL, "en-US.Unicode");
|
||||
uint8 n = 0;
|
||||
clrscr();
|
||||
for (uint16 i = 0; i<40; i++){
|
||||
nsleep(0,70);
|
||||
printf("\e[1;1H%.*ls", 105,slimak+n*105);
|
||||
if (n>=10) n=0;
|
||||
else n++;
|
||||
}
|
||||
for (uint16 i = 0; i<120; i++){
|
||||
nsleep(0,20);
|
||||
printf("\e[1;1H%.*ls", 105,slimak+n*105);
|
||||
if (n>=10) n=0;
|
||||
else n++;
|
||||
}
|
||||
clrscr();
|
||||
printf("\e[32m\npress any key to exit...\n");
|
||||
getc(stdin);
|
||||
return 0;
|
||||
}
|
||||
@ -1,4 +1,6 @@
|
||||
#include "!headers.h"
|
||||
#pragma once
|
||||
|
||||
#include "std.h"
|
||||
|
||||
const wchar_t* slimak =
|
||||
U" ▄▄▄ \n"
|
||||
40
DtsodC/src/tests/test_autoarr.c
Normal file
40
DtsodC/src/tests/test_autoarr.c
Normal file
@ -0,0 +1,40 @@
|
||||
#include "tests.h"
|
||||
#include "../base/Autoarr.h"
|
||||
|
||||
void ardesc(Autoarr* ar){
|
||||
printf("AUTOARR:%lu\n"
|
||||
" type: %s\n"
|
||||
" max_block_count: %u\n"
|
||||
" curr_block_count: %u\n"
|
||||
" max_block_length: %u\n"
|
||||
" curr_block_length: %u\n"
|
||||
" max_length: %u\n"
|
||||
" curr_length: %u\n",
|
||||
sizeof(Autoarr),
|
||||
typename(ar->type),
|
||||
ar->max_block_count,
|
||||
ar->curr_block_count,
|
||||
ar->max_block_length,
|
||||
ar->curr_block_length,
|
||||
ar->max_length,
|
||||
ar->curr_length);
|
||||
}
|
||||
|
||||
void fillar(Autoarr* ar){
|
||||
for (uint16 i=0;i<ar->max_length;i++)
|
||||
Autoarr_add_uint16(ar,i);
|
||||
ardesc(ar);
|
||||
}
|
||||
|
||||
void printar(Autoarr* ar){
|
||||
for (uint16 i=0;i<ar->max_length;i++)
|
||||
printf("%u ", Autoarr_get_uint16(&ar,i));
|
||||
}
|
||||
|
||||
void testar(){
|
||||
Autoarr ar=Autoarr_create(10,16,UInt16);
|
||||
fillar(&ar);
|
||||
printar(&ar);
|
||||
Autoarr_free(&ar);
|
||||
ardesc(&ar);
|
||||
}
|
||||
5
DtsodC/src/tests/tests.h
Normal file
5
DtsodC/src/tests/tests.h
Normal file
@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "../base/std.h"
|
||||
|
||||
void test_autoarr(void);
|
||||
@ -1,12 +0,0 @@
|
||||
#include "!headers.h"
|
||||
#include "errors.h"
|
||||
|
||||
void throwstr(const char* errmesg){
|
||||
printf("\e[31mthrowed error: %s\e[0m\n",errmesg);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void throwerr(err_t err){
|
||||
printf("\e[31mthrowed error: err_t:%d\e[0m\n",err);
|
||||
exit(err);
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user