258 lines
6.2 KiB
C
258 lines
6.2 KiB
C
#include <ctype.h>
|
|
#include <fx/comparable.h>
|
|
#include <fx/convertible.h>
|
|
#include <fx/hash.h>
|
|
#include <fx/operable.h>
|
|
#include <fx/stream.h>
|
|
#include <fx/value-type.h>
|
|
#include <fx/value.h>
|
|
#include <fx/wstr.h>
|
|
#include <inttypes.h>
|
|
#include <limits.h>
|
|
|
|
static fx_status to_string(
|
|
const fx_value *v,
|
|
fx_stream *out,
|
|
const char *format)
|
|
{
|
|
const fx_wchar s[] = {v->v_wchar, 0};
|
|
return fx_stream_write_wstr(out, s, NULL);
|
|
}
|
|
|
|
static i32 compare(const fx_value *left, const fx_value *right)
|
|
{
|
|
fx_wchar s;
|
|
if (!FX_OK(fx_value_get_wchar(right, &s))) {
|
|
return -1;
|
|
}
|
|
|
|
return memcmp(&left->v_wchar, &s, sizeof s);
|
|
}
|
|
|
|
static fx_status hash(const fx_value *v, uint64_t *out_hash)
|
|
{
|
|
*out_hash = v->v_wchar;
|
|
return FX_SUCCESS;
|
|
}
|
|
|
|
fx_status wchar_to_char(fx_wchar in, fx_wchar *out)
|
|
{
|
|
return FX_ERR_NOT_SUPPORTED;
|
|
}
|
|
|
|
fx_status wchar_to_bool(fx_wchar in, bool *out)
|
|
{
|
|
return FX_ERR_NOT_SUPPORTED;
|
|
}
|
|
|
|
fx_status wchar_to_i16(fx_wchar in, i16 *out)
|
|
{
|
|
return FX_ERR_NOT_SUPPORTED;
|
|
}
|
|
|
|
fx_status wchar_to_u16(fx_wchar in, u16 *out)
|
|
{
|
|
return FX_ERR_NOT_SUPPORTED;
|
|
}
|
|
|
|
fx_status wchar_to_i32(fx_wchar in, i32 *out)
|
|
{
|
|
return FX_ERR_NOT_SUPPORTED;
|
|
}
|
|
|
|
fx_status wchar_to_u32(fx_wchar in, u32 *out)
|
|
{
|
|
return FX_ERR_NOT_SUPPORTED;
|
|
}
|
|
|
|
fx_status wchar_to_i64(fx_wchar in, i64 *out)
|
|
{
|
|
return FX_ERR_NOT_SUPPORTED;
|
|
}
|
|
|
|
fx_status wchar_to_u64(fx_wchar in, u64 *out)
|
|
{
|
|
return FX_ERR_NOT_SUPPORTED;
|
|
}
|
|
|
|
fx_status wchar_to_iptr(fx_wchar in, iptr *out)
|
|
{
|
|
return FX_ERR_NOT_SUPPORTED;
|
|
}
|
|
|
|
fx_status wchar_to_uptr(fx_wchar in, uptr *out)
|
|
{
|
|
return FX_ERR_NOT_SUPPORTED;
|
|
}
|
|
|
|
fx_status wchar_to_sbyte(fx_wchar in, sbyte *out)
|
|
{
|
|
return FX_ERR_NOT_SUPPORTED;
|
|
}
|
|
|
|
fx_status wchar_to_byte(fx_wchar in, byte *out)
|
|
{
|
|
return FX_ERR_NOT_SUPPORTED;
|
|
}
|
|
|
|
fx_status wchar_to_short(fx_wchar in, short *out)
|
|
{
|
|
return FX_ERR_NOT_SUPPORTED;
|
|
}
|
|
|
|
fx_status wchar_to_ushort(fx_wchar in, unsigned short *out)
|
|
{
|
|
return FX_ERR_NOT_SUPPORTED;
|
|
}
|
|
|
|
fx_status wchar_to_int(fx_wchar in, int *out)
|
|
{
|
|
return FX_ERR_NOT_SUPPORTED;
|
|
}
|
|
|
|
fx_status wchar_to_uint(fx_wchar in, unsigned int *out)
|
|
{
|
|
return FX_ERR_NOT_SUPPORTED;
|
|
}
|
|
|
|
fx_status wchar_to_long(fx_wchar in, long *out)
|
|
{
|
|
return FX_ERR_NOT_SUPPORTED;
|
|
}
|
|
|
|
fx_status wchar_to_ulong(fx_wchar in, unsigned long *out)
|
|
{
|
|
return FX_ERR_NOT_SUPPORTED;
|
|
}
|
|
|
|
fx_status wchar_to_longlong(fx_wchar in, long long *out)
|
|
{
|
|
return FX_ERR_NOT_SUPPORTED;
|
|
}
|
|
|
|
fx_status wchar_to_ulonglong(fx_wchar in, unsigned long long *out)
|
|
{
|
|
return FX_ERR_NOT_SUPPORTED;
|
|
}
|
|
|
|
fx_status wchar_to_size(fx_wchar in, size_t *out)
|
|
{
|
|
return FX_ERR_NOT_SUPPORTED;
|
|
}
|
|
|
|
fx_status wchar_to_float(fx_wchar in, float *out)
|
|
{
|
|
return FX_ERR_NOT_SUPPORTED;
|
|
}
|
|
|
|
fx_status wchar_to_double(fx_wchar in, double *out)
|
|
{
|
|
return FX_ERR_NOT_SUPPORTED;
|
|
}
|
|
|
|
fx_status wchar_to_wchar(fx_wchar in, fx_wchar *out)
|
|
{
|
|
*out = in;
|
|
return FX_SUCCESS;
|
|
}
|
|
|
|
#define WCHAR_CONVERTER(fx_type, c_type) \
|
|
static fx_status to_##fx_type(const fx_value *in, c_type *out) \
|
|
{ \
|
|
fx_wchar s = FX_WCHAR_INVALID; \
|
|
fx_value_get_wchar(in, &s); \
|
|
return wchar_to_##fx_type(s, out); \
|
|
}
|
|
|
|
WCHAR_CONVERTER(bool, bool)
|
|
WCHAR_CONVERTER(i16, i16)
|
|
WCHAR_CONVERTER(u16, u16)
|
|
WCHAR_CONVERTER(i32, i32)
|
|
WCHAR_CONVERTER(u32, u32)
|
|
WCHAR_CONVERTER(i64, i64)
|
|
WCHAR_CONVERTER(u64, u64)
|
|
WCHAR_CONVERTER(iptr, iptr)
|
|
WCHAR_CONVERTER(uptr, uptr)
|
|
WCHAR_CONVERTER(sbyte, sbyte)
|
|
WCHAR_CONVERTER(byte, byte)
|
|
WCHAR_CONVERTER(short, short)
|
|
WCHAR_CONVERTER(ushort, unsigned short)
|
|
WCHAR_CONVERTER(int, int)
|
|
WCHAR_CONVERTER(uint, unsigned int)
|
|
WCHAR_CONVERTER(long, long)
|
|
WCHAR_CONVERTER(ulong, unsigned long)
|
|
WCHAR_CONVERTER(longlong, long long)
|
|
WCHAR_CONVERTER(ulonglong, unsigned long long)
|
|
WCHAR_CONVERTER(size, size_t)
|
|
WCHAR_CONVERTER(float, float)
|
|
WCHAR_CONVERTER(double, double)
|
|
|
|
static fx_status add(const fx_value *l, const fx_value *r, fx_value *out)
|
|
{
|
|
fx_wchar left = l->v_wchar;
|
|
fx_wchar right = r->v_wchar;
|
|
fx_string *result = fx_string_create();
|
|
if (!result) {
|
|
return FX_ERR_NO_MEMORY;
|
|
}
|
|
|
|
fx_string_append_wc(result, left);
|
|
fx_string_append_wc(result, right);
|
|
*out = FX_VALUE_OBJECT(result);
|
|
|
|
return FX_SUCCESS;
|
|
}
|
|
|
|
/*** CLASS DEFINITION *********************************************************/
|
|
|
|
FX_TYPE_CLASS_BEGIN(fx_wchar)
|
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
|
FX_INTERFACE_ENTRY(to_string) = to_string;
|
|
FX_INTERFACE_ENTRY(hash) = hash;
|
|
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
|
|
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
|
|
FX_INTERFACE_ENTRY(op_add) = add;
|
|
FX_TYPE_VTABLE_INTERFACE_END(fx_operable, FX_TYPE_OPERABLE)
|
|
|
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_comparable, FX_TYPE_COMPARABLE)
|
|
FX_INTERFACE_ENTRY(c_compare) = compare;
|
|
FX_TYPE_VTABLE_INTERFACE_END(fx_comparable, FX_TYPE_COMPARABLE)
|
|
|
|
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_convertible, FX_TYPE_CONVERTIBLE)
|
|
FX_INTERFACE_ENTRY(c_to_bool) = to_bool;
|
|
FX_INTERFACE_ENTRY(c_to_bool) = to_bool;
|
|
FX_INTERFACE_ENTRY(c_to_i16) = to_i16;
|
|
FX_INTERFACE_ENTRY(c_to_u16) = to_u16;
|
|
FX_INTERFACE_ENTRY(c_to_u32) = to_u32;
|
|
FX_INTERFACE_ENTRY(c_to_i64) = to_i64;
|
|
FX_INTERFACE_ENTRY(c_to_u64) = to_u64;
|
|
FX_INTERFACE_ENTRY(c_to_iptr) = to_iptr;
|
|
FX_INTERFACE_ENTRY(c_to_uptr) = to_uptr;
|
|
FX_INTERFACE_ENTRY(c_to_sbyte) = to_sbyte;
|
|
FX_INTERFACE_ENTRY(c_to_byte) = to_byte;
|
|
FX_INTERFACE_ENTRY(c_to_short) = to_short;
|
|
FX_INTERFACE_ENTRY(c_to_ushort) = to_ushort;
|
|
FX_INTERFACE_ENTRY(c_to_int) = to_int;
|
|
FX_INTERFACE_ENTRY(c_to_uint) = to_uint;
|
|
FX_INTERFACE_ENTRY(c_to_long) = to_long;
|
|
FX_INTERFACE_ENTRY(c_to_ulong) = to_ulong;
|
|
FX_INTERFACE_ENTRY(c_to_longlong) = to_longlong;
|
|
FX_INTERFACE_ENTRY(c_to_ulonglong) = to_ulonglong;
|
|
FX_INTERFACE_ENTRY(c_to_size) = to_size;
|
|
FX_INTERFACE_ENTRY(c_to_float) = to_float;
|
|
FX_INTERFACE_ENTRY(c_to_double) = to_double;
|
|
FX_TYPE_VTABLE_INTERFACE_END(fx_convertible, FX_TYPE_CONVERTIBLE)
|
|
FX_TYPE_CLASS_END(fx_wchar)
|
|
|
|
FX_TYPE_DEFINITION_BEGIN(fx_wchar)
|
|
__FX_VALUE_TYPE_ID(WCHAR);
|
|
FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE);
|
|
FX_TYPE_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
|
|
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
|
|
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
|
|
FX_TYPE_NAME("fx.wchar");
|
|
FX_TYPE_CLASS(fx_wchar_class);
|
|
FX_TYPE_INSTANCE_PRIVATE(char *);
|
|
FX_TYPE_DEFINITION_END(fx_wchar)
|