2020-05-10 16:20:39 +00:00
|
|
|
/*
|
2020-02-20 15:35:16 +00:00
|
|
|
Copyright (c) 2016-2020 Chung, Hyung-Hwan. All rights reserved.
|
2019-01-24 09:53:10 +00:00
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
2022-06-11 05:32:01 +00:00
|
|
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
2019-01-24 09:53:10 +00:00
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
#include "hio-prv.h"
|
|
|
|
#include <hio-chr.h>
|
2019-01-24 09:53:10 +00:00
|
|
|
|
|
|
|
/* ========================================================================= */
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
int hio_comp_ucstr_bcstr (const hio_uch_t* str1, const hio_bch_t* str2, int ignorecase)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
2021-07-19 19:39:50 +00:00
|
|
|
if (ignorecase)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
while (hio_to_uch_lower(*str1) == hio_to_bch_lower(*str2))
|
2021-07-19 19:39:50 +00:00
|
|
|
{
|
|
|
|
if (*str1 == '\0') return 0;
|
|
|
|
str1++; str2++;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
return ((hio_uchu_t)hio_to_uch_lower(*str1) > (hio_bchu_t)hio_to_bch_lower(*str2))? 1: -1;
|
2019-01-24 09:53:10 +00:00
|
|
|
}
|
2021-07-19 19:39:50 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
while (*str1 == *str2)
|
|
|
|
{
|
|
|
|
if (*str1 == '\0') return 0;
|
|
|
|
str1++; str2++;
|
|
|
|
}
|
2019-01-24 09:53:10 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
return ((hio_uchu_t)*str1 > (hio_bchu_t)*str2)? 1: -1;
|
2021-07-19 19:39:50 +00:00
|
|
|
}
|
2019-01-24 09:53:10 +00:00
|
|
|
}
|
|
|
|
|
2022-07-16 08:14:25 +00:00
|
|
|
int hio_comp_ucstr_bcstr_limited (const hio_uch_t* str1, const hio_bch_t* str2, hio_oow_t maxlen, int ignorecase)
|
|
|
|
{
|
|
|
|
if (maxlen == 0) return 0;
|
|
|
|
|
|
|
|
if (ignorecase)
|
|
|
|
{
|
|
|
|
while (hio_to_uch_lower(*str1) == hio_to_bch_lower(*str2))
|
|
|
|
{
|
|
|
|
if (*str1 == '\0' || maxlen == 1) return 0;
|
|
|
|
|
|
|
|
str1++; str2++; maxlen--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ((hio_uchu_t)hio_to_uch_lower(*str1) > (hio_bchu_t)hio_to_bch_lower(*str2))? 1: -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (*str1 == *str2)
|
|
|
|
{
|
|
|
|
if (*str1 == '\0' || maxlen == 1) return 0;
|
|
|
|
str1++; str2++; maxlen--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ((hio_uchu_t)*str1 > (hio_bchu_t)*str2)? 1: -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
int hio_comp_uchars_bcstr (const hio_uch_t* str1, hio_oow_t len, const hio_bch_t* str2, int ignorecase)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
2020-05-01 18:01:29 +00:00
|
|
|
if (ignorecase)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
const hio_uch_t* end = str1 + len;
|
|
|
|
hio_uch_t c1;
|
|
|
|
hio_bch_t c2;
|
2023-01-11 14:59:41 +00:00
|
|
|
while (str1 < end && *str2 != '\0')
|
2020-05-01 18:01:29 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
c1 = hio_to_uch_lower(*str1);
|
|
|
|
c2 = hio_to_bch_lower(*str2);
|
|
|
|
if (c1 != c2) return ((hio_uchu_t)c1 > (hio_bchu_t)c2)? 1: -1;
|
2020-05-01 18:01:29 +00:00
|
|
|
str1++; str2++;
|
|
|
|
}
|
|
|
|
return (str1 < end)? 1: (*str2 == '\0'? 0: -1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
const hio_uch_t* end = str1 + len;
|
2023-01-11 14:59:41 +00:00
|
|
|
while (str1 < end && *str2 != '\0')
|
2020-05-01 18:01:29 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
if (*str1 != *str2) return ((hio_uchu_t)*str1 > (hio_bchu_t)*str2)? 1: -1;
|
2020-05-01 18:01:29 +00:00
|
|
|
str1++; str2++;
|
|
|
|
}
|
|
|
|
return (str1 < end)? 1: (*str2 == '\0'? 0: -1);
|
2019-01-24 09:53:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
int hio_comp_bchars_ucstr (const hio_bch_t* str1, hio_oow_t len, const hio_uch_t* str2, int ignorecase)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
2020-05-01 18:01:29 +00:00
|
|
|
if (ignorecase)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
const hio_bch_t* end = str1 + len;
|
|
|
|
hio_bch_t c1;
|
|
|
|
hio_uch_t c2;
|
2023-01-11 14:59:41 +00:00
|
|
|
while (str1 < end && *str2 != '\0')
|
2020-05-01 18:01:29 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
c1 = hio_to_bch_lower(*str1);
|
|
|
|
c2 = hio_to_uch_lower(*str2);
|
|
|
|
if (c1 != c2) return ((hio_bchu_t)c1 > (hio_uchu_t)c2)? 1: -1;
|
2020-05-01 18:01:29 +00:00
|
|
|
str1++; str2++;
|
|
|
|
}
|
|
|
|
return (str1 < end)? 1: (*str2 == '\0'? 0: -1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
const hio_bch_t* end = str1 + len;
|
2023-01-11 14:59:41 +00:00
|
|
|
while (str1 < end && *str2 != '\0')
|
2020-05-01 18:01:29 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
if (*str1 != *str2) return ((hio_bchu_t)*str1 > (hio_uchu_t)*str2)? 1: -1;
|
2020-05-01 18:01:29 +00:00
|
|
|
str1++; str2++;
|
|
|
|
}
|
|
|
|
return (str1 < end)? 1: (*str2 == '\0'? 0: -1);
|
2019-01-24 09:53:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-16 14:23:27 +00:00
|
|
|
/* ========================================================================= */
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
void hio_copy_bchars_to_uchars (hio_uch_t* dst, const hio_bch_t* src, hio_oow_t len)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
|
|
|
/* copy without conversions.
|
2021-07-22 07:30:20 +00:00
|
|
|
* use hio_convbtouchars() for conversion encoding */
|
|
|
|
hio_oow_t i;
|
2020-02-16 14:23:27 +00:00
|
|
|
for (i = 0; i < len; i++) dst[i] = src[i];
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
void hio_copy_uchars_to_bchars (hio_bch_t* dst, const hio_uch_t* src, hio_oow_t len)
|
2020-02-16 14:23:27 +00:00
|
|
|
{
|
|
|
|
/* copy without conversions.
|
2021-07-22 07:30:20 +00:00
|
|
|
* use hio_convutobchars() for conversion encoding */
|
|
|
|
hio_oow_t i;
|
2020-02-16 14:23:27 +00:00
|
|
|
for (i = 0; i < len; i++) dst[i] = src[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ========================================================================= */
|
2020-02-20 14:59:49 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
#define IS_BCH_WORD_DELIM(x,delim) (hio_is_bch_space(x) || (x) == delim)
|
|
|
|
#define IS_UCH_WORD_DELIM(x,delim) (hio_is_uch_space(x) || (x) == delim)
|
2020-05-01 18:01:29 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
const hio_bch_t* hio_find_bcstr_word_in_bcstr (const hio_bch_t* str, const hio_bch_t* word, hio_bch_t extra_delim, int ignorecase)
|
2020-05-01 18:01:29 +00:00
|
|
|
{
|
|
|
|
/* find a full word in a string */
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
const hio_bch_t* ptr = str;
|
2020-05-01 18:01:29 +00:00
|
|
|
|
|
|
|
if (extra_delim == '\0') extra_delim = ' ';
|
|
|
|
do
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
const hio_bch_t* s;
|
2020-05-01 18:01:29 +00:00
|
|
|
|
|
|
|
while (IS_BCH_WORD_DELIM(*ptr,extra_delim)) ptr++;
|
2021-07-22 07:30:20 +00:00
|
|
|
if (*ptr == '\0') return HIO_NULL;
|
2020-05-01 18:01:29 +00:00
|
|
|
|
|
|
|
s = ptr;
|
|
|
|
while (*ptr != '\0' && !IS_BCH_WORD_DELIM(*ptr,extra_delim)) ptr++;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if (hio_comp_bchars_bcstr(s, ptr - s, word, ignorecase) == 0) return s;
|
2020-05-01 18:01:29 +00:00
|
|
|
}
|
|
|
|
while (*ptr != '\0');
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_NULL;
|
2020-05-01 18:01:29 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
const hio_uch_t* hio_find_ucstr_word_in_ucstr (const hio_uch_t* str, const hio_uch_t* word, hio_uch_t extra_delim, int ignorecase)
|
2020-05-01 18:01:29 +00:00
|
|
|
{
|
|
|
|
/* find a full word in a string */
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
const hio_uch_t* ptr = str;
|
2020-05-01 18:01:29 +00:00
|
|
|
|
|
|
|
if (extra_delim == '\0') extra_delim = ' ';
|
|
|
|
do
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
const hio_uch_t* s;
|
2020-05-01 18:01:29 +00:00
|
|
|
|
|
|
|
while (IS_UCH_WORD_DELIM(*ptr,extra_delim)) ptr++;
|
2021-07-22 07:30:20 +00:00
|
|
|
if (*ptr == '\0') return HIO_NULL;
|
2020-05-01 18:01:29 +00:00
|
|
|
|
|
|
|
s = ptr;
|
|
|
|
while (*ptr != '\0' && !IS_UCH_WORD_DELIM(*ptr,extra_delim)) ptr++;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if (hio_comp_uchars_ucstr(s, ptr - s, word, ignorecase) == 0) return s;
|
2020-05-01 18:01:29 +00:00
|
|
|
}
|
|
|
|
while (*ptr != '\0');
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_NULL;
|
2020-05-01 18:01:29 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 14:59:49 +00:00
|
|
|
/* ========================================================================= */
|
2021-07-16 08:06:17 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INLINE int hio_conv_bchars_to_uchars_with_cmgr (
|
|
|
|
const hio_bch_t* bcs, hio_oow_t* bcslen,
|
|
|
|
hio_uch_t* ucs, hio_oow_t* ucslen, hio_cmgr_t* cmgr, int all)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
const hio_bch_t* p;
|
2019-01-24 09:53:10 +00:00
|
|
|
int ret = 0;
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_oow_t mlen;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
|
|
|
if (ucs)
|
|
|
|
{
|
2023-01-11 14:59:41 +00:00
|
|
|
/* destination buffer is specified.
|
2019-01-24 09:53:10 +00:00
|
|
|
* copy the conversion result to the buffer */
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_uch_t* q, * qend;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
|
|
|
p = bcs;
|
|
|
|
q = ucs;
|
|
|
|
qend = ucs + *ucslen;
|
|
|
|
mlen = *bcslen;
|
|
|
|
|
|
|
|
while (mlen > 0)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_oow_t n;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
|
|
|
if (q >= qend)
|
|
|
|
{
|
|
|
|
/* buffer too small */
|
|
|
|
ret = -2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-01-31 09:16:44 +00:00
|
|
|
n = cmgr->bctouc(p, mlen, q);
|
2019-01-24 09:53:10 +00:00
|
|
|
if (n == 0)
|
|
|
|
{
|
|
|
|
/* invalid sequence */
|
|
|
|
if (all)
|
|
|
|
{
|
|
|
|
n = 1;
|
|
|
|
*q = '?';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (n > mlen)
|
|
|
|
{
|
|
|
|
/* incomplete sequence */
|
|
|
|
if (all)
|
|
|
|
{
|
|
|
|
n = 1;
|
|
|
|
*q = '?';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
q++;
|
|
|
|
p += n;
|
|
|
|
mlen -= n;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ucslen = q - ucs;
|
|
|
|
*bcslen = p - bcs;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* no destination buffer is specified. perform conversion
|
|
|
|
* but don't copy the result. the caller can call this function
|
|
|
|
* without a buffer to find the required buffer size, allocate
|
2023-01-11 14:59:41 +00:00
|
|
|
* a buffer with the size and call this function again with
|
2019-01-24 09:53:10 +00:00
|
|
|
* the buffer. */
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_uch_t w;
|
|
|
|
hio_oow_t wlen = 0;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
|
|
|
p = bcs;
|
|
|
|
mlen = *bcslen;
|
|
|
|
|
|
|
|
while (mlen > 0)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_oow_t n;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
2019-01-31 09:16:44 +00:00
|
|
|
n = cmgr->bctouc(p, mlen, &w);
|
2019-01-24 09:53:10 +00:00
|
|
|
if (n == 0)
|
|
|
|
{
|
|
|
|
/* invalid sequence */
|
|
|
|
if (all) n = 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (n > mlen)
|
|
|
|
{
|
|
|
|
/* incomplete sequence */
|
|
|
|
if (all) n = 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p += n;
|
|
|
|
mlen -= n;
|
|
|
|
wlen += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ucslen = wlen;
|
|
|
|
*bcslen = p - bcs;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INLINE int hio_conv_bcstr_to_ucstr_with_cmgr (
|
|
|
|
const hio_bch_t* bcs, hio_oow_t* bcslen,
|
|
|
|
hio_uch_t* ucs, hio_oow_t* ucslen, hio_cmgr_t* cmgr, int all)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
const hio_bch_t* bp;
|
|
|
|
hio_oow_t mlen, wlen;
|
2019-01-24 09:53:10 +00:00
|
|
|
int n;
|
|
|
|
|
|
|
|
for (bp = bcs; *bp != '\0'; bp++) /* nothing */ ;
|
|
|
|
|
|
|
|
mlen = bp - bcs; wlen = *ucslen;
|
2021-07-22 07:30:20 +00:00
|
|
|
n = hio_conv_bchars_to_uchars_with_cmgr (bcs, &mlen, ucs, &wlen, cmgr, all);
|
2019-01-24 09:53:10 +00:00
|
|
|
if (ucs)
|
|
|
|
{
|
|
|
|
/* null-terminate the target buffer if it has room for it. */
|
|
|
|
if (wlen < *ucslen) ucs[wlen] = '\0';
|
|
|
|
else n = -2; /* buffer too small */
|
|
|
|
}
|
|
|
|
*bcslen = mlen; *ucslen = wlen;
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INLINE int hio_conv_uchars_to_bchars_with_cmgr (
|
|
|
|
const hio_uch_t* ucs, hio_oow_t* ucslen,
|
|
|
|
hio_bch_t* bcs, hio_oow_t* bcslen, hio_cmgr_t* cmgr)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
const hio_uch_t* p = ucs;
|
|
|
|
const hio_uch_t* end = ucs + *ucslen;
|
2023-01-11 14:59:41 +00:00
|
|
|
int ret = 0;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
|
|
|
if (bcs)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_oow_t rem = *bcslen;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
2023-01-11 14:59:41 +00:00
|
|
|
while (p < end)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_oow_t n;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
|
|
|
if (rem <= 0)
|
|
|
|
{
|
|
|
|
ret = -2; /* buffer too small */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-05-31 17:36:11 +00:00
|
|
|
n = cmgr->uctobc(*p, bcs, rem);
|
2023-01-11 14:59:41 +00:00
|
|
|
if (n == 0)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
break; /* illegal character */
|
|
|
|
}
|
2023-01-11 14:59:41 +00:00
|
|
|
if (n > rem)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
|
|
|
ret = -2; /* buffer too small */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bcs += n; rem -= n; p++;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:59:41 +00:00
|
|
|
*bcslen -= rem;
|
2019-01-24 09:53:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_bch_t bcsbuf[HIO_BCSIZE_MAX];
|
|
|
|
hio_oow_t mlen = 0;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
|
|
|
while (p < end)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_oow_t n;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
n = cmgr->uctobc(*p, bcsbuf, HIO_COUNTOF(bcsbuf));
|
2023-01-11 14:59:41 +00:00
|
|
|
if (n == 0)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
break; /* illegal character */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* it assumes that bcsbuf is large enough to hold a character */
|
2021-07-22 07:30:20 +00:00
|
|
|
/*HIO_ASSERT (hio, n <= HIO_COUNTOF(bcsbuf));*/
|
2019-01-24 09:53:10 +00:00
|
|
|
|
|
|
|
p++; mlen += n;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:59:41 +00:00
|
|
|
/* this length excludes the terminating null character.
|
2019-01-24 09:53:10 +00:00
|
|
|
* this function doesn't even null-terminate the result. */
|
|
|
|
*bcslen = mlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ucslen = p - ucs;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INLINE int hio_conv_ucstr_to_bcstr_with_cmgr (
|
|
|
|
const hio_uch_t* ucs, hio_oow_t* ucslen,
|
|
|
|
hio_bch_t* bcs, hio_oow_t* bcslen, hio_cmgr_t* cmgr)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
const hio_uch_t* p = ucs;
|
2019-01-24 09:53:10 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (bcs)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_oow_t rem = *bcslen;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
|
|
|
while (*p != '\0')
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_oow_t n;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
|
|
|
if (rem <= 0)
|
|
|
|
{
|
|
|
|
ret = -2;
|
|
|
|
break;
|
|
|
|
}
|
2023-01-11 14:59:41 +00:00
|
|
|
|
2019-01-24 09:53:10 +00:00
|
|
|
n = cmgr->uctobc(*p, bcs, rem);
|
2023-01-11 14:59:41 +00:00
|
|
|
if (n == 0)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
break; /* illegal character */
|
|
|
|
}
|
2023-01-11 14:59:41 +00:00
|
|
|
if (n > rem)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
|
|
|
ret = -2;
|
|
|
|
break; /* buffer too small */
|
|
|
|
}
|
|
|
|
|
|
|
|
bcs += n; rem -= n; p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* update bcslen to the length of the bcs string converted excluding
|
|
|
|
* terminating null */
|
2023-01-11 14:59:41 +00:00
|
|
|
*bcslen -= rem;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
|
|
|
/* null-terminate the multibyte sequence if it has sufficient space */
|
|
|
|
if (rem > 0) *bcs = '\0';
|
2023-01-11 14:59:41 +00:00
|
|
|
else
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
2023-01-11 14:59:41 +00:00
|
|
|
/* if ret is -2 and cs[cslen] == '\0',
|
2019-01-24 09:53:10 +00:00
|
|
|
* this means that the bcs buffer was lacking one
|
|
|
|
* slot for the terminating null */
|
|
|
|
ret = -2; /* buffer too small */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_bch_t bcsbuf[HIO_BCSIZE_MAX];
|
|
|
|
hio_oow_t mlen = 0;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
|
|
|
while (*p != '\0')
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_oow_t n;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
n = cmgr->uctobc(*p, bcsbuf, HIO_COUNTOF(bcsbuf));
|
2023-01-11 14:59:41 +00:00
|
|
|
if (n == 0)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
break; /* illegal character */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* it assumes that bcs is large enough to hold a character */
|
2021-07-22 07:30:20 +00:00
|
|
|
/*HIO_ASSERT (hio, n <= HIO_COUNTOF(bcs));*/
|
2019-01-24 09:53:10 +00:00
|
|
|
|
|
|
|
p++; mlen += n;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:59:41 +00:00
|
|
|
/* this length holds the number of resulting multi-byte characters
|
2019-01-24 09:53:10 +00:00
|
|
|
* excluding the terminating null character */
|
|
|
|
*bcslen = mlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ucslen = p - ucs; /* the number of wide characters handled. */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static hio_cmgr_t utf8_cmgr =
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_utf8_to_uc,
|
|
|
|
hio_uc_to_utf8
|
2019-01-24 09:53:10 +00:00
|
|
|
};
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_cmgr_t* hio_get_utf8_cmgr (void)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
|
|
|
return &utf8_cmgr;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
int hio_conv_utf8_to_uchars (const hio_bch_t* bcs, hio_oow_t* bcslen, hio_uch_t* ucs, hio_oow_t* ucslen)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
|
|
|
/* the source is length bound */
|
2021-07-22 07:30:20 +00:00
|
|
|
return hio_conv_bchars_to_uchars_with_cmgr(bcs, bcslen, ucs, ucslen, &utf8_cmgr, 0);
|
2019-01-24 09:53:10 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
int hio_conv_uchars_to_utf8 (const hio_uch_t* ucs, hio_oow_t* ucslen, hio_bch_t* bcs, hio_oow_t* bcslen)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
|
|
|
/* length bound */
|
2021-07-22 07:30:20 +00:00
|
|
|
return hio_conv_uchars_to_bchars_with_cmgr(ucs, ucslen, bcs, bcslen, &utf8_cmgr);
|
2019-01-24 09:53:10 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
int hio_conv_utf8_to_ucstr (const hio_bch_t* bcs, hio_oow_t* bcslen, hio_uch_t* ucs, hio_oow_t* ucslen)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
|
|
|
/* null-terminated. */
|
2021-07-22 07:30:20 +00:00
|
|
|
return hio_conv_bcstr_to_ucstr_with_cmgr(bcs, bcslen, ucs, ucslen, &utf8_cmgr, 0);
|
2019-01-24 09:53:10 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
int hio_conv_ucstr_to_utf8 (const hio_uch_t* ucs, hio_oow_t* ucslen, hio_bch_t* bcs, hio_oow_t* bcslen)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
|
|
|
/* null-terminated */
|
2021-07-22 07:30:20 +00:00
|
|
|
return hio_conv_ucstr_to_bcstr_with_cmgr(ucs, ucslen, bcs, bcslen, &utf8_cmgr);
|
2019-01-24 09:53:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
int hio_convbtouchars (hio_t* hio, const hio_bch_t* bcs, hio_oow_t* bcslen, hio_uch_t* ucs, hio_oow_t* ucslen, int all)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
|
|
|
/* length bound */
|
|
|
|
int n;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
n = hio_conv_bchars_to_uchars_with_cmgr(bcs, bcslen, ucs, ucslen, hio_getcmgr(hio), all);
|
2019-01-24 09:53:10 +00:00
|
|
|
|
|
|
|
if (n <= -1)
|
|
|
|
{
|
|
|
|
/* -1: illegal character, -2: buffer too small, -3: incomplete sequence */
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_seterrnum (hio, (n == -2)? HIO_EBUFFULL: HIO_EECERR);
|
2019-01-24 09:53:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
int hio_convutobchars (hio_t* hio, const hio_uch_t* ucs, hio_oow_t* ucslen, hio_bch_t* bcs, hio_oow_t* bcslen)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
|
|
|
/* length bound */
|
|
|
|
int n;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
n = hio_conv_uchars_to_bchars_with_cmgr(ucs, ucslen, bcs, bcslen, hio_getcmgr(hio));
|
2019-01-24 09:53:10 +00:00
|
|
|
|
|
|
|
if (n <= -1)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_seterrnum (hio, (n == -2)? HIO_EBUFFULL: HIO_EECERR);
|
2019-01-24 09:53:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
int hio_convbtoucstr (hio_t* hio, const hio_bch_t* bcs, hio_oow_t* bcslen, hio_uch_t* ucs, hio_oow_t* ucslen, int all)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
|
|
|
/* null-terminated. */
|
|
|
|
int n;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
n = hio_conv_bcstr_to_ucstr_with_cmgr(bcs, bcslen, ucs, ucslen, hio_getcmgr(hio), all);
|
2019-01-24 09:53:10 +00:00
|
|
|
|
|
|
|
if (n <= -1)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_seterrnum (hio, (n == -2)? HIO_EBUFFULL: HIO_EECERR);
|
2019-01-24 09:53:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
int hio_convutobcstr (hio_t* hio, const hio_uch_t* ucs, hio_oow_t* ucslen, hio_bch_t* bcs, hio_oow_t* bcslen)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
|
|
|
/* null-terminated */
|
|
|
|
int n;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
n = hio_conv_ucstr_to_bcstr_with_cmgr(ucs, ucslen, bcs, bcslen, hio_getcmgr(hio));
|
2019-01-24 09:53:10 +00:00
|
|
|
|
|
|
|
if (n <= -1)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_seterrnum (hio, (n == -2)? HIO_EBUFFULL: HIO_EECERR);
|
2019-01-24 09:53:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INLINE hio_uch_t* hio_dupbtoucharswithheadroom (hio_t* hio, hio_oow_t headroom_bytes, const hio_bch_t* bcs, hio_oow_t bcslen, hio_oow_t* ucslen, int all)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_oow_t inlen, outlen;
|
|
|
|
hio_uch_t* ptr;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
|
|
|
inlen = bcslen;
|
2023-01-11 14:59:41 +00:00
|
|
|
if (hio_convbtouchars(hio, bcs, &inlen, HIO_NULL, &outlen, all) <= -1)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
|
|
|
/* note it's also an error if no full conversion is made in this function */
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_NULL;
|
2019-01-24 09:53:10 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
ptr = (hio_uch_t*)hio_allocmem(hio, headroom_bytes + ((outlen + 1) * HIO_SIZEOF(hio_uch_t)));
|
|
|
|
if (!ptr) return HIO_NULL;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
|
|
|
inlen = bcslen;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
ptr = (hio_uch_t*)((hio_oob_t*)ptr + headroom_bytes);
|
|
|
|
hio_convbtouchars (hio, bcs, &inlen, ptr, &outlen, all);
|
2019-01-24 09:53:10 +00:00
|
|
|
|
2023-01-11 14:59:41 +00:00
|
|
|
/* hio_convbtouchars() doesn't null-terminate the target.
|
2021-07-22 07:30:20 +00:00
|
|
|
* but in hio_dupbtouchars(), i allocate space. so i don't mind
|
2019-01-24 09:53:10 +00:00
|
|
|
* null-terminating it with 1 extra character overhead */
|
2023-01-11 14:59:41 +00:00
|
|
|
ptr[outlen] = '\0';
|
2019-01-24 09:53:10 +00:00
|
|
|
if (ucslen) *ucslen = outlen;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_uch_t* hio_dupbtouchars (hio_t* hio, const hio_bch_t* bcs, hio_oow_t bcslen, hio_oow_t* ucslen, int all)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
return hio_dupbtoucharswithheadroom (hio, 0, bcs, bcslen, ucslen, all);
|
2019-01-24 09:53:10 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INLINE hio_bch_t* hio_duputobcharswithheadroom (hio_t* hio, hio_oow_t headroom_bytes, const hio_uch_t* ucs, hio_oow_t ucslen, hio_oow_t* bcslen)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_oow_t inlen, outlen;
|
|
|
|
hio_bch_t* ptr;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
|
|
|
inlen = ucslen;
|
2023-01-11 14:59:41 +00:00
|
|
|
if (hio_convutobchars(hio, ucs, &inlen, HIO_NULL, &outlen) <= -1)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
|
|
|
/* note it's also an error if no full conversion is made in this function */
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_NULL;
|
2019-01-24 09:53:10 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
ptr = (hio_bch_t*)hio_allocmem(hio, headroom_bytes + ((outlen + 1) * HIO_SIZEOF(hio_bch_t)));
|
|
|
|
if (!ptr) return HIO_NULL;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
|
|
|
inlen = ucslen;
|
2021-07-22 07:30:20 +00:00
|
|
|
ptr = (hio_bch_t*)((hio_oob_t*)ptr + headroom_bytes);
|
|
|
|
hio_convutobchars (hio, ucs, &inlen, ptr, &outlen);
|
2019-01-24 09:53:10 +00:00
|
|
|
|
|
|
|
ptr[outlen] = '\0';
|
|
|
|
if (bcslen) *bcslen = outlen;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_bch_t* hio_duputobchars (hio_t* hio, const hio_uch_t* ucs, hio_oow_t ucslen, hio_oow_t* bcslen)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
return hio_duputobcharswithheadroom (hio, 0, ucs, ucslen, bcslen);
|
2019-01-24 09:53:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INLINE hio_uch_t* hio_dupbtoucstrwithheadroom (hio_t* hio, hio_oow_t headroom_bytes, const hio_bch_t* bcs, hio_oow_t* ucslen, int all)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_oow_t inlen, outlen;
|
|
|
|
hio_uch_t* ptr;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
2023-01-11 14:59:41 +00:00
|
|
|
if (hio_convbtoucstr(hio, bcs, &inlen, HIO_NULL, &outlen, all) <= -1)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
|
|
|
/* note it's also an error if no full conversion is made in this function */
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_NULL;
|
2019-01-24 09:53:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
outlen++;
|
2021-07-22 07:30:20 +00:00
|
|
|
ptr = (hio_uch_t*)hio_allocmem(hio, headroom_bytes + (outlen * HIO_SIZEOF(hio_uch_t)));
|
|
|
|
if (!ptr) return HIO_NULL;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_convbtoucstr (hio, bcs, &inlen, ptr, &outlen, all);
|
2019-01-24 09:53:10 +00:00
|
|
|
if (ucslen) *ucslen = outlen;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_uch_t* hio_dupbtoucstr (hio_t* hio, const hio_bch_t* bcs, hio_oow_t* ucslen, int all)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
return hio_dupbtoucstrwithheadroom (hio, 0, bcs, ucslen, all);
|
2019-01-24 09:53:10 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INLINE hio_bch_t* hio_duputobcstrwithheadroom (hio_t* hio, hio_oow_t headroom_bytes, const hio_uch_t* ucs, hio_oow_t* bcslen)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_oow_t inlen, outlen;
|
|
|
|
hio_bch_t* ptr;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
2023-01-11 14:59:41 +00:00
|
|
|
if (hio_convutobcstr(hio, ucs, &inlen, HIO_NULL, &outlen) <= -1)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
|
|
|
/* note it's also an error if no full conversion is made in this function */
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_NULL;
|
2019-01-24 09:53:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
outlen++;
|
2021-07-22 07:30:20 +00:00
|
|
|
ptr = (hio_bch_t*)hio_allocmem(hio, headroom_bytes + (outlen * HIO_SIZEOF(hio_bch_t)));
|
|
|
|
if (!ptr) return HIO_NULL;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
ptr = (hio_bch_t*)((hio_oob_t*)ptr + headroom_bytes);
|
2019-01-24 09:53:10 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_convutobcstr (hio, ucs, &inlen, ptr, &outlen);
|
2019-01-24 09:53:10 +00:00
|
|
|
if (bcslen) *bcslen = outlen;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_bch_t* hio_duputobcstr (hio_t* hio, const hio_uch_t* ucs, hio_oow_t* bcslen)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
return hio_duputobcstrwithheadroom (hio, 0, ucs, bcslen);
|
2019-01-24 09:53:10 +00:00
|
|
|
}
|
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_uch_t* hio_dupuchars (hio_t* hio, const hio_uch_t* ucs, hio_oow_t ucslen)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_uch_t* ptr;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
ptr = (hio_uch_t*)hio_allocmem(hio, (ucslen + 1) * HIO_SIZEOF(hio_uch_t));
|
|
|
|
if (!ptr) return HIO_NULL;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_copy_uchars (ptr, ucs, ucslen);
|
2019-01-24 09:53:10 +00:00
|
|
|
ptr[ucslen] = '\0';
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_bch_t* hio_dupbchars (hio_t* hio, const hio_bch_t* bcs, hio_oow_t bcslen)
|
2019-01-24 09:53:10 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_bch_t* ptr;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
ptr = (hio_bch_t*)hio_allocmem(hio, (bcslen + 1) * HIO_SIZEOF(hio_bch_t));
|
|
|
|
if (!ptr) return HIO_NULL;
|
2019-01-24 09:53:10 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_copy_bchars (ptr, bcs, bcslen);
|
2019-01-24 09:53:10 +00:00
|
|
|
ptr[bcslen] = '\0';
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2020-02-20 14:59:49 +00:00
|
|
|
|
|
|
|
/* ========================================================================= */
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_uch_t* hio_dupucstr (hio_t* hio, const hio_uch_t* ucs, hio_oow_t* ucslen)
|
2020-02-20 14:59:49 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_uch_t* ptr;
|
|
|
|
hio_oow_t len;
|
2020-02-20 14:59:49 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
len = hio_count_ucstr(ucs);
|
2020-02-20 14:59:49 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
ptr = (hio_uch_t*)hio_allocmem(hio, (len + 1) * HIO_SIZEOF(hio_uch_t));
|
|
|
|
if (!ptr) return HIO_NULL;
|
2020-02-20 14:59:49 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_copy_uchars (ptr, ucs, len);
|
2020-02-20 14:59:49 +00:00
|
|
|
ptr[len] = '\0';
|
|
|
|
|
|
|
|
if (ucslen) *ucslen = len;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_bch_t* hio_dupbcstr (hio_t* hio, const hio_bch_t* bcs, hio_oow_t* bcslen)
|
2020-02-20 14:59:49 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_bch_t* ptr;
|
|
|
|
hio_oow_t len;
|
2020-02-20 14:59:49 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
len = hio_count_bcstr(bcs);
|
2020-02-20 14:59:49 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
ptr = (hio_bch_t*)hio_allocmem(hio, (len + 1) * HIO_SIZEOF(hio_bch_t));
|
|
|
|
if (!ptr) return HIO_NULL;
|
2020-02-20 14:59:49 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_copy_bchars (ptr, bcs, len);
|
2020-02-20 14:59:49 +00:00
|
|
|
ptr[len] = '\0';
|
|
|
|
|
|
|
|
if (bcslen) *bcslen = len;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_uch_t* hio_dupucstrs (hio_t* hio, const hio_uch_t* ucs[], hio_oow_t* ucslen)
|
2020-05-20 16:14:36 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_uch_t* ptr;
|
|
|
|
hio_oow_t len, i;
|
2020-05-20 16:14:36 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
for (i = 0, len = 0; ucs[i]; i++) len += hio_count_ucstr(ucs[i]);
|
2020-05-20 16:14:36 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
ptr = (hio_uch_t*)hio_allocmem(hio, (len + 1) * HIO_SIZEOF(hio_uch_t));
|
|
|
|
if (!ptr) return HIO_NULL;
|
2020-05-20 16:14:36 +00:00
|
|
|
|
2023-01-11 14:59:41 +00:00
|
|
|
for (i = 0, len = 0; ucs[i]; i++)
|
2021-07-22 07:30:20 +00:00
|
|
|
len += hio_copy_ucstr_unlimited(&ptr[len], ucs[i]);
|
2020-05-20 16:14:36 +00:00
|
|
|
ptr[len] = '\0';
|
|
|
|
|
|
|
|
if (ucslen) *ucslen = len;
|
|
|
|
return ptr;
|
|
|
|
}
|
2020-02-20 14:59:49 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_bch_t* hio_dupbcstrs (hio_t* hio, const hio_bch_t* bcs[], hio_oow_t* bcslen)
|
2020-05-20 16:14:36 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_bch_t* ptr;
|
|
|
|
hio_oow_t len, i;
|
2020-05-20 16:14:36 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
for (i = 0, len = 0; bcs[i]; i++) len += hio_count_bcstr(bcs[i]);
|
2020-05-20 16:14:36 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
ptr = (hio_bch_t*)hio_allocmem(hio, (len + 1) * HIO_SIZEOF(hio_bch_t));
|
|
|
|
if (!ptr) return HIO_NULL;
|
2020-05-20 16:14:36 +00:00
|
|
|
|
2023-01-11 14:59:41 +00:00
|
|
|
for (i = 0, len = 0; bcs[i]; i++)
|
2021-07-22 07:30:20 +00:00
|
|
|
len += hio_copy_bcstr_unlimited(&ptr[len], bcs[i]);
|
2020-05-20 16:14:36 +00:00
|
|
|
ptr[len] = '\0';
|
|
|
|
|
|
|
|
if (bcslen) *bcslen = len;
|
|
|
|
return ptr;
|
|
|
|
}
|
2019-01-24 09:53:10 +00:00
|
|
|
/* ========================================================================= */
|
2020-09-03 03:56:32 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
void hio_add_ntime (hio_ntime_t* z, const hio_ntime_t* x, const hio_ntime_t* y)
|
2020-09-03 03:56:32 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_ntime_sec_t xs, ys;
|
|
|
|
hio_ntime_nsec_t ns;
|
2020-09-03 03:56:32 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
/*HIO_ASSERT (x->nsec >= 0 && x->nsec < HIO_NSECS_PER_SEC);
|
|
|
|
HIO_ASSERT (y->nsec >= 0 && y->nsec < HIO_NSECS_PER_SEC);*/
|
2020-09-03 03:56:32 +00:00
|
|
|
|
|
|
|
ns = x->nsec + y->nsec;
|
2021-07-22 07:30:20 +00:00
|
|
|
if (ns >= HIO_NSECS_PER_SEC)
|
2020-09-03 03:56:32 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
ns = ns - HIO_NSECS_PER_SEC;
|
|
|
|
if (x->sec == HIO_TYPE_MAX(hio_ntime_sec_t))
|
2020-09-03 03:56:32 +00:00
|
|
|
{
|
|
|
|
if (y->sec >= 0) goto overflow;
|
|
|
|
xs = x->sec;
|
|
|
|
ys = y->sec + 1; /* this won't overflow */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xs = x->sec + 1; /* this won't overflow */
|
|
|
|
ys = y->sec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xs = x->sec;
|
|
|
|
ys = y->sec;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if ((ys >= 1 && xs > HIO_TYPE_MAX(hio_ntime_sec_t) - ys) ||
|
|
|
|
(ys <= -1 && xs < HIO_TYPE_MIN(hio_ntime_sec_t) - ys))
|
2020-09-03 03:56:32 +00:00
|
|
|
{
|
|
|
|
if (xs >= 0)
|
|
|
|
{
|
|
|
|
overflow:
|
2021-07-22 07:30:20 +00:00
|
|
|
xs = HIO_TYPE_MAX(hio_ntime_sec_t);
|
|
|
|
ns = HIO_NSECS_PER_SEC - 1;
|
2020-09-03 03:56:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
xs = HIO_TYPE_MIN(hio_ntime_sec_t);
|
2020-09-03 03:56:32 +00:00
|
|
|
ns = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xs = xs + ys;
|
|
|
|
}
|
|
|
|
|
|
|
|
z->sec = xs;
|
|
|
|
z->nsec = ns;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
void hio_sub_ntime (hio_ntime_t* z, const hio_ntime_t* x, const hio_ntime_t* y)
|
2020-09-03 03:56:32 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_ntime_sec_t xs, ys;
|
|
|
|
hio_ntime_nsec_t ns;
|
2020-09-03 03:56:32 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
/*HIO_ASSERT (x->nsec >= 0 && x->nsec < HIO_NSECS_PER_SEC);
|
|
|
|
HIO_ASSERT (y->nsec >= 0 && y->nsec < HIO_NSECS_PER_SEC);*/
|
2020-09-03 03:56:32 +00:00
|
|
|
|
|
|
|
ns = x->nsec - y->nsec;
|
|
|
|
if (ns < 0)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
ns = ns + HIO_NSECS_PER_SEC;
|
|
|
|
if (x->sec == HIO_TYPE_MIN(hio_ntime_sec_t))
|
2020-09-03 03:56:32 +00:00
|
|
|
{
|
|
|
|
if (y->sec <= 0) goto underflow;
|
|
|
|
xs = x->sec;
|
|
|
|
ys = y->sec - 1; /* this won't underflow */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xs = x->sec - 1; /* this won't underflow */
|
|
|
|
ys = y->sec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xs = x->sec;
|
|
|
|
ys = y->sec;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if ((ys >= 1 && xs < HIO_TYPE_MIN(hio_ntime_sec_t) + ys) ||
|
|
|
|
(ys <= -1 && xs > HIO_TYPE_MAX(hio_ntime_sec_t) + ys))
|
2020-09-03 03:56:32 +00:00
|
|
|
{
|
|
|
|
if (xs >= 0)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
xs = HIO_TYPE_MAX(hio_ntime_sec_t);
|
|
|
|
ns = HIO_NSECS_PER_SEC - 1;
|
2020-09-03 03:56:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
underflow:
|
2021-07-22 07:30:20 +00:00
|
|
|
xs = HIO_TYPE_MIN(hio_ntime_sec_t);
|
2020-09-03 03:56:32 +00:00
|
|
|
ns = 0;
|
|
|
|
}
|
2023-01-11 14:59:41 +00:00
|
|
|
}
|
2020-09-03 03:56:32 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
xs = xs - ys;
|
|
|
|
}
|
|
|
|
|
|
|
|
z->sec = xs;
|
|
|
|
z->nsec = ns;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ========================================================================= */
|
2022-10-07 05:08:40 +00:00
|
|
|
|
|
|
|
#if defined(_WIN32) || defined(__OS2__) || defined(__DOS__)
|
|
|
|
# define IS_PATH_SEP(c) ((c) == '/' || (c) == '\\')
|
|
|
|
#else
|
|
|
|
# define IS_PATH_SEP(c) ((c) == '/')
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const hio_uch_t* hio_get_base_name_ucstr (const hio_uch_t* path)
|
|
|
|
{
|
|
|
|
const hio_uch_t* p, * last = HIO_NULL;
|
|
|
|
|
|
|
|
for (p = path; *p != '\0'; p++)
|
|
|
|
{
|
|
|
|
if (IS_PATH_SEP(*p)) last = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
return last? (last +1): path;
|
|
|
|
}
|
|
|
|
|
|
|
|
const hio_bch_t* hio_get_base_name_bcstr (const hio_bch_t* path)
|
|
|
|
{
|
|
|
|
const hio_bch_t* p, * last = HIO_NULL;
|
|
|
|
|
|
|
|
for (p = path; *p != '\0'; p++)
|
|
|
|
{
|
|
|
|
if (IS_PATH_SEP(*p)) last = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
return last? (last +1): path;
|
|
|
|
}
|