示例#1
0
文件: typecast.c 项目: uiri/fffll
double valueToDouble(Value* v) {
  double d, n;
  BoolExpr* be;
  Value* u;
  List* node;
  n = 0.0;
  if (v->type == 'n')
    return *(double*)v->data;
  if (v->type == 's')
    return atof(((String*)v->data)->val);
  if (v->type == 'b') {
    be = evaluateBoolExpr((BoolExpr*)v);
    if (be == NULL) return NAN;
    return (double)(be->lasteval);
  }
  if (v->type == 'c') {
    u = (Value*)evaluateFuncVal((FuncVal*)v);
    if (u == NULL) return NAN;
    d = valueToDouble(u);
    freeValue(u);
    return d;
  }
  if (v->type == 'd')
    return valueToDouble(evaluateStatements(v->data));
  if (v->type == 'l') {
    if (!v->data)
      return 0.0;
    for (node = ((List*)v->data)->next;node != NULL;node = node->next) {
      u = evaluateValue(node->data);
      d = valueToDouble(u);
      freeValue(u);
      if (d < 0) return d;
      n += d;
    }
    return n;
  }
  if (v->type == 'v') {
    u = evaluateValue(v);
    if (u == NULL) return NAN;
    n = valueToDouble(u);
  }
  return n;
}
示例#2
0
geos::geom::Coordinate GeoJSONReader::getCoordinate(Handle<Value> value, bool acceptArrayOnly) {

    bool isArray = value->IsArray();

    if (acceptArrayOnly) {
        if (!isArray)
            throw "A coordinate must be an instance of Array";


    }
    else {
        if (
            !isArray
            && !value->IsNull()
            && !value->IsUndefined()
        )
            throw "A coordinate must be an instance of Array or null";


        if (!isArray)
            return geos::geom::Coordinate::getNull();
    }


    Handle<Array> array = Handle<Array>::Cast(value);
    uint32_t length = array->Length();
    if (length < 2)
        throw "A coordinate's length must be >= 2";


    geos::geom::Coordinate coord;

    coord.x = valueToDouble(array->Get(0));
    coord.y = valueToDouble(array->Get(1));
    if (length > 2) {
        coord.z = valueToDouble(array->Get(2));
    }
    precisionModel->makePrecise(&coord);

    return coord;
}
示例#3
0
StreamBase& JsonStream::operator<<(KeyValueReference<double> keyValue)
{
    if(serialise())
    {
        setType(TYPE_OBJECT);
        mObject[keyValue.key] = keyValue.value;
    }
    else
    {
        if(checkDeserialisation() && checkObjectMember(keyValue.key))
        {
            valueToDouble(mObject[keyValue.key], keyValue.value);
        }
    }
    return *this;
}
示例#4
0
StreamBase& JsonStream::operator<<(ValueReference<double> value)
{
    if(serialise())
    {
        setType(TYPE_ARRAY);
        mArray.push_back(value.value);
    }
    else
    {
        if(checkDeserialisation() && arrayBoundsOk())
        {
            valueToDouble(mArray[mArrayNextRead], value.value);
            mArrayNextRead++;
        }
    }
    return *this;
}
示例#5
0
文件: repl.c 项目: uiri/fffll
char* valueToString(Value* v) {
  char* s, *t;
  int i, j, l, freet;
  double a, b, c;
  BoolExpr* be;
  Value* u;
  List* node;
  freet = 0;
  l = 0;
  t = NULL;
  if (v->type == 'v') {
    u = evaluateValue(v);
    if (u == NULL) return NULL;
    s = valueToString(u);
    if (s == NULL) return NULL;
    return s;
  }
  if (v->type == 'i') {
    u = evaluateValue(v);
    if (u == NULL) return NULL;
    s = valueToString(u);
    return s;
  }
  if (v->type == 'n') {
    l = 32;
    i = l;
    s = malloc(l);
    l = snprintf(s, l, "%g", *(double*)v->data);
    if (i<l) {
      s = realloc(s, l);
      snprintf(s, l, "%g", *(double*)v->data);
    }
    return s;
  }
  if (v->type == '0' || v->type == 'b') {
    if (v->type == 'b') {
      be = evaluateBoolExpr((BoolExpr*)v);
      if (be == NULL) return NULL;
      l = be->lasteval;
    }
    if (l) {
      s = malloc(5);
      s[0] = 't';
      s[1] = 'r';
      s[2] = 'u';
      s[3] = 'e';
      s[4] = '\0';
    } else {
      s = malloc(6);
      s[0] = 'f';
      s[1] = 'a';
      s[2] = 'l';
      s[3] = 's';
      s[4] = 'e';
      s[5] = '\0';
    }
    return s;
  }
  if (v->type == 'r') {
    a = valueToDouble(((Range*)v)->start);
    c = valueToDouble(((Range*)v)->end);
    if (((Range*)v)->increment == falsevalue) {
      b = 1.0;
      if (c<a)
	b = -1.0;
    } else {
      b = valueToDouble(((Range*)v)->increment);
    }
    s = malloc(1);
    if ((c-a)/b < 0) {
      s[0] = '\0';
      return s;
    }
    l = 1;
    if (a<c) {
      for (;a<c;a += b) {
	l += snprintf(s, 0, "%d, ", (int)a);
      }
    } else if (c<a) {
      for (;c<a;a += b) {
	l += snprintf(s, 0, "%d, ", (int)a);
      }
    }
    free(s);
    s = malloc(l);
    a = valueToDouble(((Range*)v)->start);
    i = 0;
    if (a<c) {
      for (;a<c;a += b) {
	  i += snprintf(s+i, l, "%d, ", (int)a);
      }
    } else if (c<a) {
      for (;c<a;a += b) {
	i += snprintf(s+i, l-i, "%d, ", (int)a);
      }
    }
    i--;
    s[--i] = '\0';
    return s;
  }
  if (v->type == 'l') {
    s = malloc(4);
    s[0] = '[';
    i = 0;
    node = NULL;
    if (v->data)
      node = ((List*)v->data)->next;
    for (;node != NULL;node = node->next) {
      if (i) s[i] = ' ';
      i++;
      u = evaluateValue(node->data);
      if (u == NULL) {
	free(s);
	return NULL;
      }
      t = valueToString(u);
      freeValue(u);
      if (t == NULL) {
	free(s);
	return NULL;
      }
      l = strlen(t);
      s = realloc(s, i+l+2);
      for (j=0;j<l;j++) {
	s[i+j] = t[j];
      }
      s[i+++j] = ',';
      s[i+j] = '\0';
      i += l;
      free(t);
    }
    if (!i) i = 2;
    s[--i] = ']';
    s[++i] = '\0';
    return s;
  }
  if (v->type == 'c') {
    u = evaluateFuncVal((FuncVal*)v);
    if (u == NULL) return NULL;
    s = valueToString(u);
    freeValue(u);
    return s;
  }
  if (v->type == 'd') {
    u = evaluateStatements(v->data);
    if (u == NULL) return NULL;
    s = valueToString(u);
    freeValue(u);
    return s;
  }
  if (v->type == 's' || v->type == 'x') {
    t = ((String*)v->data)->val;
  }
  if (t == NULL) return NULL;
  l = strlen(t);
  s = malloc(l+1);
  for (i=0;i<l;i++) {
    s[i] = t[i];
  }
  s[i] = '\0';
  if (freet) free(t);
  return s;
}