?? value.c
字號:
else return 1;
case hex:
case integer:
case octal:
if (v1.value.integer < v2.value.integer)
return -1;
else if (v1.value.integer == v2.value.integer)
return 0;
else return 1;
case big:
return bcompare(v1.value.big, v2.value.big);
default:
a_assert(0);
return 0;
}
}
/******************************************************************************/
/*
* If type mismatch, then coerce types to big.
* Note: Known bug, casting of negative bigs to floats doesn't work.
*/
static void coerce_types(register value_t* v1, register value_t* v2)
{
#ifdef FLOATING_POINT_SUPPORT
if (v1->type == floating) {
v2->type = floating;
v2->value.floating = (double) v2->value.integer;
if (v2->type == big)
v2->value.floating = (double) v2->value.big[BLOW] +
(double) v2->value.big[BHIGH] * (double) MAXINT;
} else if (v2->type == floating) {
v1->type = floating;
v1->value.floating = (double) v1->value.integer;
if (v1->type == big)
v1->value.floating = (double) v1->value.big[BLOW] +
(double) v1->value.big[BHIGH] * (double) MAXINT;
} else if (v1->type == big) {
#else
if (v1->type == big) {
#endif /* FLOATING_POINT_SUPPORT */
v2->value.big[BLOW] = value_to_integer(v2);
if (valueNegative(v2))
v2->value.big[BHIGH] = -1;
else
v2->value.big[BHIGH] = 0;
v2->type = big;
} else if (v2->type == big) {
if (valueNegative(v1))
v1->value.big[BHIGH] = -1;
else
v1->value.big[BHIGH] = 0;
v1->value.big[BLOW] = value_to_integer(v1);
v1->type = big;
} else if (v1->type == integer) {
v2->value.integer = value_to_integer(v2);
v2->type = integer;
} else if (v2->type == integer) {
v1->value.integer = value_to_integer(v1);
v1->type = integer;
} else if (v1->type != integer) {
v2->type = v1->type;
} else if (v2->type != integer) {
v1->type = v2->type;
}
a_assert(v1->type == v2->type);
}
/******************************************************************************/
/*
* Return true if the value is numeric and negative. Otherwise return 0.
*/
int valueNegative(value_t* vp)
{
switch (vp->type) {
default:
case string:
case bytes:
return 0;
#ifdef FLOATING_POINT_SUPPORT
case floating:
if (vp->value.floating < 0)
return 1;
return 0;
#endif
case flag:
if ((signed char)vp->value.flag < 0)
return 1;
return 0;
case byteint:
case percent:
if ((signed char)vp->value.byteint < 0)
return 1;
return 0;
case shortint:
if (vp->value.shortint < 0)
return 1;
return 0;
case hex:
case integer:
case octal:
if (vp->value.integer < 0)
return 1;
return 0;
case big:
if (vp->value.big[BHIGH] < 0)
return 1;
return 0;
}
}
/******************************************************************************/
/*
* Return true if the value is numeric and zero. Otherwise return 0.
*/
int valueZero(value_t* vp)
{
switch (vp->type) {
default:
case string:
case bytes:
return 0;
#ifdef FLOATING_POINT_SUPPORT
case floating:
if (vp->value.floating == 0)
return 1;
return 0;
#endif
case flag:
if (vp->value.flag == 0)
return 1;
return 0;
case byteint:
case percent:
if (vp->value.byteint == 0)
return 1;
return 0;
case shortint:
if (vp->value.shortint == 0)
return 1;
return 0;
case hex:
case integer:
case octal:
if (vp->value.integer == 0)
return 1;
return 0;
case big:
if (vp->value.big[BHIGH] == 0 && vp->value.big[BLOW] == 0)
return 1;
return 0;
}
}
/******************************************************************************/
/*
* Cast a value to an integer. Cannot be called for floating, non-numerics
* or bigs.
*/
static int value_to_integer(value_t* vp)
{
switch (vp->type) {
default:
case string:
case bytes:
case big:
#ifdef FLOATING_POINT_SUPPORT
case floating:
a_assert(0);
return -1;
#endif
case flag:
return (int) vp->value.flag;
case byteint:
case percent:
return (int) vp->value.byteint;
case shortint:
return (int) vp->value.shortint;
case hex:
case integer:
case octal:
return (int) vp->value.integer;
}
}
/******************************************************************************/
/*
* Convert a value to a text based representation of its value
*/
void valueSprintf(char_t** out, int size, char_t* fmt, value_t vp)
{
char_t *src, *dst, *tmp, *dst_start;
a_assert(out);
*out = NULL;
if (! vp.valid) {
*out = bstrdup(B_L, T("Invalid"));
return;
}
switch (vp.type) {
case flag:
if (fmt == NULL || *fmt == '\0') {
*out = bstrdup(B_L, (vp.value.flag) ? T("true") : T("false"));
} else {
fmtAlloc(out, size, fmt, (vp.value.flag) ? T("true") : T("false"));
}
break;
#ifdef FLOATING_POINT_SUPPORT
case floating:
if (fmt == NULL || *fmt == '\0') {
fmtAlloc(out, size, T("%f"), vp.value.floating);
} else {
fmtAlloc(out, size, fmt, vp.value.floating);
}
break;
#endif
case hex:
if (fmt == NULL || *fmt == '\0') {
fmtAlloc(out, size, T("0x%lx"), vp.value.hex);
} else {
fmtAlloc(out, size, fmt, vp.value.hex);
}
break;
case big:
if (*out == NULL) {
*out = btoa(vp.value.big, NULL, 0);
} else {
btoa(vp.value.big, *out, size);
}
break;
case integer:
if (fmt == NULL || *fmt == '\0') {
fmtAlloc(out, size, T("%ld"), vp.value.integer);
} else {
fmtAlloc(out, size, fmt, vp.value.integer);
}
break;
case octal:
if (fmt == NULL || *fmt == '\0') {
fmtAlloc(out, size, T("0%lo"), vp.value.octal);
} else {
fmtAlloc(out, size, fmt, vp.value.octal);
}
break;
case percent:
if (fmt == NULL || *fmt == '\0') {
fmtAlloc(out, size, T("%d%%"), vp.value.percent);
} else {
fmtAlloc(out, size, fmt, vp.value.percent);
}
break;
case byteint:
if (fmt == NULL || *fmt == '\0') {
fmtAlloc(out, size, T("%d"), (int) vp.value.byteint);
} else {
fmtAlloc(out, size, fmt, (int) vp.value.byteint);
}
break;
case shortint:
if (fmt == NULL || *fmt == '\0') {
fmtAlloc(out, size, T("%d"), (int) vp.value.shortint);
} else {
fmtAlloc(out, size, fmt, (int) vp.value.shortint);
}
break;
case string:
case errmsg:
src = vp.value.string;
if (src == NULL) {
*out = bstrdup(B_L, T("NULL"));
} else if (fmt && *fmt) {
fmtAlloc(out, size, fmt, src);
} else {
*out = balloc(B_L, size);
dst_start = dst = *out;
for (; *src != '\0'; src++) {
if (dst >= &dst_start[VALUE_MAX_STRING - 5])
break;
switch (*src) {
case '\a': *dst++ = '\\'; *dst++ = 'a'; break;
case '\b': *dst++ = '\\'; *dst++ = 'b'; break;
case '\f': *dst++ = '\\'; *dst++ = 'f'; break;
case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
case '\t': *dst++ = '\\'; *dst++ = 't'; break;
case '\v': *dst++ = '\\'; *dst++ = 'v'; break;
case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
case '"': *dst++ = '\\'; *dst++ = '\"'; break;
default:
if (gisprint(*src)) {
*dst++ = *src;
} else {
fmtAlloc(&tmp, size, T("\\x%02x"),
(unsigned int) *src);
gstrcpy(dst, tmp);
bfreeSafe(B_L, tmp);
dst += 4;
}
break;
}
}
*dst++ = '\0';
}
break;
#ifdef UNUSED
case bytes:
asrc = vp.value.bytes;
if (asrc == NULL) {
*out = bstrdup(B_L, T("NULL"));
} else if (fmt && *fmt) {
fmtAlloc(out, size, fmt, asrc);
} else {
dst_start = dst;
for (; *asrc != '\0'; asrc++) {
if (dst >= &dst_start[VALUE_MAX_STRING - 5])
break;
switch (*asrc) {
case '\a': *dst++ = '\\'; *dst++ = 'a'; break;
case '\b': *dst++ = '\\'; *dst++ = 'b'; break;
case '\f': *dst++ = '\\'; *dst++ = 'f'; break;
case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
case '\t': *dst++ = '\\'; *dst++ = 't'; break;
case '\v': *dst++ = '\\'; *dst++ = 'v'; break;
case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
case '"': *dst++ = '\\'; *dst++ = '\"'; break;
default:
if (gisprint(*asrc)) {
*dst++ = *asrc;
} else {
fmtAlloc(dst, size,
T("\\x%02x"), (unsigned int) *asrc);
dst += 4;
}
break;
}
}
*dst++ = '\0';
}
break;
#endif
default:
a_assert(0);
}
}
/******************************************************************************/
/*
* Print a value to the named file descriptor
*/
void valueFprintf(FILE* fp, char_t* fmt, value_t vp)
{
char_t *buf;
buf = NULL;
valueSprintf(&buf, VALUE_MAX_STRING, fmt, vp);
gfputs(buf, fp);
bfreeSafe(B_L, buf);
fflush(fp);
}
/******************************************************************************/
/*
* Ascii to value conversion
*/
value_t valueAtov(char_t* s, int pref_type)
{
vtype_t type;
value_t v;
long tmp[2], tmp2[2], base[2];
int i, len, num;
a_assert(0 <= pref_type && pref_type < 99); /* Sanity check */
a_assert(s);
v = value_null;
if (s == NULL) {
return value_null;
}
base[BLOW] = 10;
base[BHIGH] = 0;
len = gstrlen(s);
/*
* Determine the value type
*/
type = undefined;
if (pref_type <= 0) {
if (gisdigit(*s)) {
base[BHIGH] = 0;
if (s[len - 1] == '%') {
type = percent;
len --;
base[BLOW] = 10;
} else if (*s == '0') {
if (s[1] == 'x') {
type = hex;
s += 2;
len -= 2;
base[BLOW] = 16;
} else if (s[1] == '\0') {
type = integer;
base[BLOW] = 10;
} else {
type = octal;
s++;
len--;
base[BLOW] = 8;
}
} else {
type = integer;
base[BLOW] = 10;
}
} else {
if (gstrcmp(s, T("true")) == 0 || gstrcmp(s, T("false")) == 0) {
type = flag;
} else if (*s == '\'' && s[len - 1] == '\'') {
type = string;
s++;
len -= 2;
} else if (*s == '\"' && s[len - 1] == '\"') {
type = string;
s++;
len -= 2;
} else {
type = string;
}
}
v.type = type;
} else
v.type = pref_type;
v.valid = 1;
/*
* Do the conversion. Always use big arithmetic
*/
switch (v.type) {
case hex:
if (!isdigit(s[0])) {
if (gtolower(s[0]) >= 'a' || gtolower(s[0]) <= 'f') {
v.value.big[BLOW] = 10 + gtolower(s[0]) - 'a';
} else {
v.value.big[BLOW] = 0;
}
} else {
v.value.big[BLOW] = s[0] - '0';
}
v.value.big[BHIGH] = 0;
for (i = 1; i < len; i++) {
if (!isdigit(s[i])) {
if (gtolower(s[i]) < 'a' || gtolower(s[i]) > 'f') {
break;
}
num = 10 + gtolower(s[i]) - 'a';
} else {
num = s[i] - '0';
}
bmul(tmp, v.value.big, base);
binit(tmp2, 0, num);
badd(v.value.big, tmp, tmp2);
}
v.value.hex = v.value.big[BLOW];
break;
case shortint:
case byteint:
case integer:
case percent:
case octal:
case big:
v.value.big[BHIGH] = 0;
if (gisdigit(s[0]))
v.value.big[BLOW] = s[0] - '0';
else
v.value.big[BLOW] = 0;
for (i = 1; i < len && gisdigit(s[i]); i++) {
bmul(tmp, v.value.big, base);
binit(tmp2, 0, s[i] - '0');
badd(v.value.big, tmp, tmp2);
}
switch (v.type) {
case shortint:
v.value.shortint = (short) v.value.big[BLOW];
break;
case byteint:
v.value.byteint = (char) v.value.big[BLOW];
break;
case integer:
v.value.integer = (int) v.value.big[BLOW];
break;
case percent:
v.value.percent = (char) v.value.big[BLOW];
break;
case octal:
v.value.octal = (int) v.value.big[BLOW];
break;
default:
break;
}
break;
#ifdef FLOATING_POINT_SUPPORT
case floating:
gsscanf(s, T("%f"), &v.value.floating);
break;
#endif
case flag:
if (*s == 't')
v.value.flag = 1;
else v.value.flag = 0;
break;
case string:
/*
* Note this always ballocs a string
*/
v = valueString(s, VALUE_ALLOCATE);
break;
case bytes:
v = valueBytes((char*) s, VALUE_ALLOCATE);
break;
#ifdef UNUSED
case literal:
v = value_literal(bstrdup(B_L, s));
v.value.literal[len] = '\0';
break;
#endif
case undefined:
case symbol:
default:
v.valid = 0;
a_assert(0);
}
return v;
}
#endif /* !UEMF */
/******************************************************************************/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -