diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/u8c/dat.c | 2 | ||||
-rw-r--r-- | src/u8c/dattyp.h | 11 | ||||
-rw-r--r-- | src/u8c/geterr.c | 2 | ||||
-rw-r--r-- | src/u8c/init.c | 6 | ||||
-rw-r--r-- | src/u8c/println.c | 3 | ||||
-rw-r--r-- | src/u8c/regerrhandl.c | 41 | ||||
-rw-r--r-- | src/u8c/seterr.c | 14 | ||||
-rw-r--r-- | src/u8c/u32alloc.c | 7 | ||||
-rw-r--r-- | src/u8c/u32cat.c | 8 | ||||
-rw-r--r-- | src/u8c/u32cmp.c | 3 | ||||
-rw-r--r-- | src/u8c/u32fndchr.c | 12 | ||||
-rw-r--r-- | src/u8c/u32free.c | 2 | ||||
-rw-r--r-- | src/u8c/u32ins.c | 49 | ||||
-rw-r--r-- | src/u8c/u32substr.c | 4 | ||||
-rw-r--r-- | src/u8c/u8alloc.c | 7 | ||||
-rw-r--r-- | src/u8c/u8dec.c | 5 | ||||
-rw-r--r-- | src/u8c/u8enc.c | 6 | ||||
-rw-r--r-- | src/u8c/vprint.c | 3 |
18 files changed, 154 insertions, 31 deletions
diff --git a/src/u8c/dat.c b/src/u8c/dat.c index 0fcf492..26d17ef 100644 --- a/src/u8c/dat.c +++ b/src/u8c/dat.c @@ -19,7 +19,7 @@ # include <stddef.h> # include <stdint.h> # include <u8c/SIZE_C.h> -struct u8c_dattyp u8c_dat = (struct u8c_dattyp){ +struct u8c_dattyp u8c_dat = { .err = NULL, .fmtendian = false, .stat = UINT8_C(0x0), diff --git a/src/u8c/dattyp.h b/src/u8c/dattyp.h index d094151..1a1e830 100644 --- a/src/u8c/dattyp.h +++ b/src/u8c/dattyp.h @@ -17,6 +17,8 @@ # define u8c_sym_dattyp # include <stdbool.h> # include <stdint.h> +# include <u8c/errhandltyp.h> +# include <u8c/errtyp.h> # include <u8c/SIZE_C.h> # include <uchar.h> # if defined(u8c_bethrdsafe) @@ -24,13 +26,16 @@ # endif struct u8c_dattyp { bool fmtendian; + unsigned char pad0[sizeof(void(*)(void)) - SIZE_C(0x1)]; /* Padding. */ char32_t const * err; - unsigned char pad0[sizeof(void(*)(void)) - SIZE_C(0x1)]; + u8c_errhandltyp errhandls[u8c_errtyp_maxerrtyp]; uint_least8_t fmtbase; uint_least8_t stat; # if defined(u8c_bethrdsafe) - mtx_t errlock; - mtx_t fmtlock; + unsigned char pad1[sizeof(void(*)(void)) - SIZE_C(0x2)]; /* Padding. */ + mtx_t errlock; + mtx_t errhandlslock; + mtx_t fmtlock; # endif }; # endif diff --git a/src/u8c/geterr.c b/src/u8c/geterr.c index a63ae20..65b8eba 100644 --- a/src/u8c/geterr.c +++ b/src/u8c/geterr.c @@ -27,9 +27,9 @@ bool u8c_geterr(size_t * const _sz,char32_t const * * const _out) { # endif u8c_u32cp(_sz,_out,u8c_dat.err); u8c_u32free(&u8c_dat.err); + u8c_u32cp(_sz,&u8c_dat.err,U""); # if defined(u8c_bethrdsafe) mtx_unlock(&u8c_dat.errlock); # endif - u8c_seterr((uint_least32_t[]){UINT32_C(0x0),}); return false; } diff --git a/src/u8c/init.c b/src/u8c/init.c index f625bce..f2e4948 100644 --- a/src/u8c/init.c +++ b/src/u8c/init.c @@ -14,16 +14,18 @@ If not, see <https://www.gnu.org/licenses/>. */ # include "dat.h" +# include <setjmp.h> # include <stdbool.h> # include <stddef.h> # include <stdint.h> +# include <u8c/errtyp.h> # include <u8c/init.h> # include <u8c/seterr.h> # include <u8c/u32cp.h> # if defined(u8c_bethrdsafe) # include <threads.h> # endif -bool u8c_init(void) { +bool u8c_init() { /* Initialise mutexes: */ # if defined(u8c_bethrdsafe) if(mtx_init(&u8c_dat.errlock,mtx_plain) == thrd_error) { @@ -35,7 +37,7 @@ bool u8c_init(void) { # endif /* Set default error message: */ u8c_dat.err = NULL; - u8c_seterr((uint_least32_t[]){UINT32_C(0x0),}); + u8c_seterr(U"",u8c_errtyp_deferr); /* Set status: */ u8c_dat.stat = UINT8_C(0x1); return false; diff --git a/src/u8c/println.c b/src/u8c/println.c index fbfd046..76b0f72 100644 --- a/src/u8c/println.c +++ b/src/u8c/println.c @@ -18,6 +18,7 @@ # include <stdbool.h> # include <stdint.h> # include <stdio.h> +# include <u8c/errtyp.h> # include <u8c/println.h> # include <u8c/seterr.h> # include <u8c/vprint.h> @@ -29,7 +30,7 @@ bool u8c_println(FILE * _fp,char32_t const * const _msg,...) { return true; } if(fputc(0xA,stdout) == EOF) { - u8c_seterr((uint_least32_t[]){UINT32_C(0x75),UINT32_C(0x38),UINT32_C(0x63),UINT32_C(0x5F),UINT32_C(0x70),UINT32_C(0x72),UINT32_C(0x69),UINT32_C(0x6E),UINT32_C(0x74),UINT32_C(0x6C),UINT32_C(0x6E),UINT32_C(0x3A),UINT32_C(0x20),UINT32_C(0x66),UINT32_C(0x70),UINT32_C(0x75),UINT32_C(0x74),UINT32_C(0x63),UINT32_C(0x3A),UINT32_C(0x20),UINT32_C(0x55),UINT32_C(0x6E),UINT32_C(0x61),UINT32_C(0x62),UINT32_C(0x6C),UINT32_C(0x65),UINT32_C(0x20),UINT32_C(0x74),UINT32_C(0x6F),UINT32_C(0x20),UINT32_C(0x77),UINT32_C(0x72),UINT32_C(0x69),UINT32_C(0x74),UINT32_C(0x65),UINT32_C(0x20),UINT32_C(0x74),UINT32_C(0x6F),UINT32_C(0x20),UINT32_C(0x73),UINT32_C(0x74),UINT32_C(0x64),UINT32_C(0x6F),UINT32_C(0x75),UINT32_C(0x74),UINT32_C(0x2E),UINT32_C(0x0),}); /* u8c_println: fputc: Unable to write to stdout. */ + u8c_seterr(U"u8c_println: Unable to write to stdout (end of file).",u8c_errtyp_badio); return false; } va_end(args); diff --git a/src/u8c/regerrhandl.c b/src/u8c/regerrhandl.c new file mode 100644 index 0000000..6a0d82f --- /dev/null +++ b/src/u8c/regerrhandl.c @@ -0,0 +1,41 @@ +/* + Copyright 2021 Gabriel Jensen + + This file is part of u8c. + + u8c is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + u8c is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with u8c. + + If not, see <https://www.gnu.org/licenses/>. +*/ +# include "dat.h" +# include <stdbool.h> +# include <stddef.h> +# include <u8c/errhandltyp.h> +# include <u8c/errtyp.h> +# include <u8c/regerrhandl.h> +static void u8c_regerrhandl_seterrhandl(enum u8c_errtyp _typ,u8c_errhandltyp _errhandl) { + u8c_dat.errhandls[(size_t)_typ] = _errhandl; +} +bool u8c_regerrhandl(enum u8c_errtyp _typ,u8c_errhandltyp _errhandl) { +# if defined(u8c_bethrdsafe) + mtx_lock(&u8c_dat.errhandlslock); +# endif + if(_typ == u8c_errtyp_all) { + for(register int n = 0x0;n < (int)u8c_errtyp_maxerrtyp;n += 0x1) { + u8c_regerrhandl_seterrhandl((enum u8c_errtyp)n,_errhandl); + } + } + else { + u8c_regerrhandl_seterrhandl(_typ,_errhandl); + } +# if defined(u8c_bethrdsafe) + mtx_unlock(&u8c_dat.errhandlslock); +# endif + return false; +} diff --git a/src/u8c/seterr.c b/src/u8c/seterr.c index 7c7fb12..adde683 100644 --- a/src/u8c/seterr.c +++ b/src/u8c/seterr.c @@ -16,16 +16,17 @@ # include "dat.h" # include <assert.h> # include <stdbool.h> +# include <stddef.h> # include <stdint.h> -# include <stdlib.h> # include <u8c/dbgprint.h> +# include <u8c/errtyp.h> # include <u8c/seterr.h> # include <u8c/u32cp.h> # include <u8c/u32free.h> # if defined(u8c_bethrdsafe) # include <threads.h> # endif -bool u8c_seterr(char32_t const * const _msg) { +bool u8c_seterr(char32_t const * const _msg,enum u8c_errtyp _typ) { assert(_msg != NULL); //u8c_dbgprint(_msg); # if defined(u8c_bethrdsafe) @@ -36,5 +37,14 @@ bool u8c_seterr(char32_t const * const _msg) { # if defined(u8c_bethrdsafe) mtx_unlock(&u8c_dat.errlock); # endif +# if defined(u8c_bethrdsafe) + mtx_lock(&u8c_dat.errhandlslock); +# endif + if(u8c_dat.errhandls[(size_t)_typ] != NULL) { + u8c_dat.errhandls[(size_t)_typ](_typ); + } +# if defined(u8c_bethrdsafe) + mtx_unlock(&u8c_dat.errhandlslock); +# endif return false; } diff --git a/src/u8c/u32alloc.c b/src/u8c/u32alloc.c index 804d564..ddd5493 100644 --- a/src/u8c/u32alloc.c +++ b/src/u8c/u32alloc.c @@ -13,16 +13,19 @@ If not, see <https://www.gnu.org/licenses/>. */ +# include "dat.h" +# include <setjmp.h> # include <stdbool.h> # include <stdlib.h> +# include <u8c/errtyp.h> # include <u8c/seterr.h> # include <u8c/u32alloc.h> # include <uchar.h> bool u8c_u32alloc(char32_t * * const _u32,size_t const _sz) { char32_t * arr = NULL; if((arr = calloc(sizeof *arr,_sz)) == NULL) { - u8c_seterr(U"u8c_u32alloc: Unable to allocate resources (not enough memory?)."); - return true; + u8c_seterr(U"u8c_u32alloc: Unable to allocate resources (not enough memory?).",u8c_errtyp_badalloc); + return false; } *_u32 = arr; return false; diff --git a/src/u8c/u32cat.c b/src/u8c/u32cat.c index 409030e..b2b6773 100644 --- a/src/u8c/u32cat.c +++ b/src/u8c/u32cat.c @@ -33,19 +33,19 @@ bool u8c_u32cat(size_t * const _sz,char32_t const * * const _out,char32_t const size_t rsz = SIZE_C(0x0); u8c_u32sz(&lsz,_lstr); u8c_u32sz(&rsz,_rstr); - sz = lsz + rsz - SIZE_C(0x1); + sz = lsz + rsz; if(_sz != NULL) { *_sz = sz; } char32_t * out = NULL; if(u8c_u32alloc(&out,sz + SIZE_C(0x1))) { - return false; + return true; } - for(register size_t n = SIZE_C(0x0);n < lsz - SIZE_C(0x1);n += SIZE_C(0x1)) { + for(register size_t n = SIZE_C(0x0);n < lsz;n += SIZE_C(0x1)) { out[n] = _lstr[n]; } for(register size_t n = SIZE_C(0x0);n < rsz;n += SIZE_C(0x1)) { - out[n + lsz - SIZE_C(0x1)] = _rstr[n]; + out[n + lsz] = _rstr[n]; } u8c_u32free(_out); *_out = out; diff --git a/src/u8c/u32cmp.c b/src/u8c/u32cmp.c index 1dabfef..5c06e30 100644 --- a/src/u8c/u32cmp.c +++ b/src/u8c/u32cmp.c @@ -17,6 +17,7 @@ # include <stdbool.h> # include <stddef.h> # include <stdint.h> +# include <u8c/errtyp.h> # include <u8c/seterr.h> # include <u8c/SIZE_C.h> # include <u8c/u32cmp.h> @@ -40,6 +41,6 @@ bool u8c_u32cmp(uint_least8_t * const _res,char32_t const * const _lstr,char32_t return false; } } - u8c_seterr((uint_least32_t[]){UINT32_C(0x75),UINT32_C(0x38),UINT32_C(0x63),UINT32_C(0x5F),UINT32_C(0x75),UINT32_C(0x33),UINT32_C(0x32),UINT32_C(0x63),UINT32_C(0x6D),UINT32_C(0x70),UINT32_C(0x3A),UINT32_C(0x20),UINT32_C(0x55),UINT32_C(0x6E),UINT32_C(0x74),UINT32_C(0x65),UINT32_C(0x72),UINT32_C(0x6D),UINT32_C(0x69),UINT32_C(0x6E),UINT32_C(0x61),UINT32_C(0x74),UINT32_C(0x65),UINT32_C(0x64),UINT32_C(0x20),UINT32_C(0x69),UINT32_C(0x6E),UINT32_C(0x70),UINT32_C(0x75),UINT32_C(0x74),UINT32_C(0x2E),UINT32_C(0x0),}); /* u8c_u32cmp: Unterminated input. */ + u8c_seterr(U"u8c_u32cmp: Unterminated input.",u8c_errtyp_untermin); return true; } diff --git a/src/u8c/u32fndchr.c b/src/u8c/u32fndchr.c index ca83998..ab4b5e1 100644 --- a/src/u8c/u32fndchr.c +++ b/src/u8c/u32fndchr.c @@ -18,6 +18,7 @@ # include <stddef.h> # include <stdint.h> # include <u8c/SIZE_C.h> +# include <u8c/errtyp.h> # include <u8c/seterr.h> # include <u8c/u32fndchr.h> # include <u8c/u32sz.h> @@ -25,16 +26,21 @@ bool u8c_u32fndchr(size_t * const _pos,char32_t const * const _in,char32_t const assert(_pos != NULL); assert(_in != NULL); for(register size_t n = SIZE_C(0x0);n <= SIZE_MAX;n += SIZE_C(0x1)) { - if(_in[n] == UINT32_C(0x0) && _chr != UINT32_C(0x0)) { + register uint_least32_t const tmp = _in[n]; + if(tmp == UINT32_C(0x0)) { + if(_chr == UINT32_C(0x0)) { + *_pos = n; + return false; + } *_pos = SIZE_C(-0x1); return true; } - if(_in[n] == _chr) { + if(tmp == _chr) { *_pos = n; return false; } } - u8c_seterr(U"u8c_u32fndchr: Unterminated input."); + u8c_seterr(U"u8c_u32fndchr: Unterminated input.",u8c_errtyp_badio); *_pos = SIZE_C(-0x1); return true; } diff --git a/src/u8c/u32free.c b/src/u8c/u32free.c index 62b43ef..b57e392 100644 --- a/src/u8c/u32free.c +++ b/src/u8c/u32free.c @@ -18,7 +18,7 @@ # include <stdlib.h> # include <u8c/u32free.h> bool u8c_u32free(char32_t const * * const _u32) { - free((char32_t *)*_u32); + free((char32_t *)*_u32); /* This cast does indeed discard a const-qualifier, but it is not undefined behaviour, as the array must have been allocated by calloc or malloc, meaning it's original type is not const-qualified. */ *_u32 = NULL; return false; } diff --git a/src/u8c/u32ins.c b/src/u8c/u32ins.c new file mode 100644 index 0000000..f5d9d4e --- /dev/null +++ b/src/u8c/u32ins.c @@ -0,0 +1,49 @@ +/* + Copyright 2021 Gabriel Jensen + + This file is part of u8c. + + u8c is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + u8c is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with u8c. + + If not, see <https://www.gnu.org/licenses/>. +*/ +# include <assert.h> +# include <stdbool.h> +# include <stddef.h> +# include <u8c/SIZE_C.h> +# include <u8c/u32alloc.h> +# include <u8c/u32cat.h> +# include <u8c/u32free.h> +# include <u8c/u32ins.h> +# include <u8c/u32substr.h> +# include <uchar.h> +bool u8c_u32ins(size_t * const _sz,char32_t const * * const _out,size_t const _pos,char32_t const * const _str0,char32_t const * const _str1) { + assert(_out != NULL); + assert(_str0 != NULL); + assert(_str1 != NULL); + char32_t const * lstr = NULL; + char32_t const * rstr = NULL; + u8c_u32substr(&lstr,SIZE_C(0x0),_pos - SIZE_C(0x1),_str0); + u8c_u32substr(&rstr,_pos,SIZE_C(0x0),_str0); + size_t sz = SIZE_C(0x0); + char32_t const * out = NULL; + { + char32_t const * tmp = NULL; + u8c_u32cat(NULL,&tmp,lstr,_str1); + u8c_u32free(&lstr); + u8c_u32cat(NULL,&out,tmp,rstr); + u8c_u32free(&rstr); + u8c_u32free(&tmp); + } + if(_sz != NULL) { + *_sz = sz; + } + *_out = out; + return false; +} diff --git a/src/u8c/u32substr.c b/src/u8c/u32substr.c index 2a30b3b..f23cffa 100644 --- a/src/u8c/u32substr.c +++ b/src/u8c/u32substr.c @@ -33,12 +33,12 @@ bool u8c_u32substr(char32_t const * * const _out,size_t const _start,size_t cons len = insz - _start; } if(insz < _start + len) { - return false; + return true; } size_t const outsz = len + SIZE_C(0x2); char32_t * out = NULL; if(u8c_u32alloc(&out,outsz)) { - return false; + return true; } for(register size_t n = SIZE_C(0x0);n <= len;n += SIZE_C(0x1)) { out[n] = _in[n + _start]; diff --git a/src/u8c/u8alloc.c b/src/u8c/u8alloc.c index a364acf..4e7bc3d 100644 --- a/src/u8c/u8alloc.c +++ b/src/u8c/u8alloc.c @@ -13,15 +13,18 @@ If not, see <https://www.gnu.org/licenses/>. */ +# include "dat.h" +# include <setjmp.h> # include <stdbool.h> # include <stdlib.h> +# include <u8c/errtyp.h> # include <u8c/seterr.h> # include <u8c/u8alloc.h> bool u8c_u8alloc(unsigned char * * const _u8,size_t const _sz) { unsigned char * arr = NULL; if((arr = calloc(sizeof *arr,_sz)) == NULL) { - u8c_seterr(U"u8c_u8alloc: Unable to allocate resources (not enough memory?)."); - return true; + u8c_seterr(U"u8c_u8alloc: Unable to allocate resources (not enough memory?).",u8c_errtyp_badalloc); + return false; } *_u8 = arr; return false; diff --git a/src/u8c/u8dec.c b/src/u8c/u8dec.c index bbc6e94..8e9c2f2 100644 --- a/src/u8c/u8dec.c +++ b/src/u8c/u8dec.c @@ -18,6 +18,7 @@ # include <stddef.h> # include <stdint.h> # include <u8c/SIZE_C.h> +# include <u8c/errtyp.h> # include <u8c/seterr.h> # include <u8c/u32alloc.h> # include <u8c/u32free.h> @@ -35,7 +36,7 @@ bool u8c_u8dec(size_t * const _sz,char32_t const * * const _out,unsigned char co goto nottoobig; } if(tmp >= UINT8_C(0xF8)) { /* Too big. */ - u8c_seterr(U"u8c_u8dec: Character out of range (too big)."); + u8c_seterr(U"u8c_u8dec: Character out of range (too big).",u8c_errtyp_u8oor); return true; } if(tmp >= UINT8_C(0xF0)) { /* Four byte. */ @@ -54,7 +55,7 @@ bool u8c_u8dec(size_t * const _sz,char32_t const * * const _out,unsigned char co n += SIZE_C(0x1); } /* Input is not null-terminated. */ - u8c_seterr(U"u8c_u8dec: Unterminated input."); + u8c_seterr(U"u8c_u8dec: Unterminated input.",u8c_errtyp_untermin); return true; nottoobig:; if(_sz != NULL) { diff --git a/src/u8c/u8enc.c b/src/u8c/u8enc.c index 60bc724..541dd8d 100644 --- a/src/u8c/u8enc.c +++ b/src/u8c/u8enc.c @@ -18,6 +18,7 @@ # include <stddef.h> # include <stdint.h> # include <u8c/SIZE_C.h> +# include <u8c/errtyp.h> # include <u8c/seterr.h> # include <u8c/u8alloc.h> # include <u8c/u8enc.h> @@ -32,7 +33,7 @@ bool u8c_u8enc(size_t * const _sz,unsigned char const * * const _out,char32_t co for(register size_t n = SIZE_C(0x0);n <= SIZE_MAX;n += SIZE_C(0x1)) { /* First pass: get size of input array, and determine size of output array. */ register char32_t const tmp = _in[n]; if(tmp > u8c_unimax) { /* Codepoint out of range. */ - u8c_seterr(U"u8c_u8enc: Codepoint out of range (too big)."); + u8c_seterr(U"u8c_u8enc: Codepoint out of range (too big).",u8c_errtyp_u32oor); return true; } if(tmp >= UINT32_C(0x10000)) { /* 4 bytes. */ @@ -54,7 +55,7 @@ bool u8c_u8enc(size_t * const _sz,unsigned char const * * const _out,char32_t co goto nottoobig; } } - u8c_seterr(U"u8c_u8enc: Unterminated input."); + u8c_seterr(U"u8c_u8enc: Unterminated input.",u8c_errtyp_untermin); return true; nottoobig:; if(_sz != NULL) { @@ -62,7 +63,6 @@ nottoobig:; } unsigned char * out = NULL; if(u8c_u8alloc(&out,outsz + SIZE_C(0x1))) { - u8c_seterr(U"u8c_u32enc: Unable to allocate resources (not enough memory?)."); return true; } for(register size_t n = SIZE_C(0x0), outn = SIZE_C(0x0);n < insz;n += SIZE_C(0x1),outn += SIZE_C(0x1)) { /* Second pass: encode each codepoint into UTF-8. */ diff --git a/src/u8c/vprint.c b/src/u8c/vprint.c index 6dc30ae..a86afcf 100644 --- a/src/u8c/vprint.c +++ b/src/u8c/vprint.c @@ -20,6 +20,7 @@ # include <stdio.h> # include <stdlib.h> # include <u8c/SIZE_C.h> +# include <u8c/errtyp.h> # include <u8c/seterr.h> # include <u8c/u32free.h> # include <u8c/u8enc.h> @@ -36,7 +37,7 @@ bool u8c_vprint(FILE * _fp,char32_t const * const _msg,va_list _args) { u8c_u8enc(&str1sz,&str1,str0); assert(str1sz > SIZE_C(0x0)); if(fwrite(str1,sizeof(uint_least8_t),str1sz - SIZE_C(0x1),_fp) < str1sz - SIZE_C(0x1)) { - u8c_seterr(U"u8c_vprint: fwrite: Unable to write to stdout."); + u8c_seterr(U"u8c_vprint: fwrite: Unable to write to stdout.",u8c_errtyp_badio); return true; } u8c_u32free(&str0); |