コード例 #1
0
/**
 * 文字列を数値に変換
 *
 * @param[in] calc calcinfo構造体
 * @return 値
 */
static dbl
number(calcinfo *calc)
{
    dbl x = 0.0, y = 1.0; /* 値 */

    dbglog("start");

    x = calc->ch - '0';
    while (readch(calc), isdigit(calc->ch)) /* 整数 */
        x = (x * 10) + (calc->ch - '0');
    dbglog(calc->fmt, x);

    if (calc->ch == '.') { /* 小数 */
        while (readch(calc), isdigit(calc->ch))
            x += (y /= 10) * (calc->ch - '0');
    }
    dbglog(calc->fmt, x);

    check_validate(calc, x);

    return x;
}
コード例 #2
0
/**
 * 計算結果
 *
 * @param[in] calc calcinfo構造体
 * @param[in] expr 式
 * @return 新たに領域確保された結果文字列ポインタ
 * @retval NULL エラー
 * @attention destroy_answerを必ず呼ぶこと.
 */
uchar *
create_answer(calcinfo *calc, const uchar *expr)
{
    dbl val = 0.0;     /* 値 */
    size_t length = 0; /* 文字数 */
    int retval = 0;    /* 戻り値 */
    uint start = 0;    /* タイマ開始 */

    dbglog("start");

    calc->ptr = (uchar *)expr; /* 走査用ポインタ */
    dbglog("ptr=%p", calc->ptr);

    /* フォーマット設定 */
    retval = snprintf(calc->fmt, sizeof(calc->fmt),
                      "%s%ld%s", "%.", digit, "g");
    if (retval < 0) {
        outlog("snprintf");
        return NULL;
    }
    dbglog("fmt=%s", calc->fmt);

    readch(calc);

    if (g_tflag)
        start_timer(&start);

    val = expression(calc);
    dbglog(calc->fmt, val);
    dbglog("ptr=%p, ch=%c", calc->ptr, calc->ch);

    check_validate(calc, val);
    if (calc->ch != '\0') /* エラー */
        set_errorcode(calc, E_SYNTAX);

    if (g_tflag) {
        uint calc_time = stop_timer(&start);
        print_timer(calc_time);
    }

    if (is_error(calc)) { /* エラー */
        calc->answer = get_errormsg(calc);
        clear_error(calc);
        if (!calc->answer)
            return NULL;
        dbglog("answer=%p, length=%zu", calc->answer, length);
    } else {
        /* 文字数取得 */
        retval = get_strlen(val, calc->fmt);
        if (retval <= 0) { /* エラー */
            outlog("get_strlen=%d", retval);
            return NULL;
        }
        dbglog("get_strlen=%d, INT_MAX=%d", retval, INT_MAX);
        length = (size_t)retval + 1; /* 文字数 + 1 */

        /* メモリ確保 */
        calc->answer = (uchar *)malloc(length * sizeof(uchar));
        if (!calc->answer) {
            outlog("malloc: length=%zu", length);
            return NULL;
        }
        (void)memset(calc->answer, 0, length * sizeof(uchar));

        /* 値を文字列に変換 */
        retval = snprintf((char *)calc->answer, length, calc->fmt, val);
        if (retval < 0) {
            outlog("snprintf: answer=%p, length=%zu", calc->answer, length);
            return NULL;
        }
        dbglog(calc->fmt, val);
        dbglog("answer=%s, length=%zu", calc->answer, length);
    }
    return calc->answer;
}
コード例 #3
0
ファイル: check_libxml2.c プロジェクト: ryandavid/rotobox
int
main (int argc, char *argv[])
{
    int ret;
    sqlite3 *handle;
    void *cache = spatialite_alloc_connection ();

    if (argc > 1 || argv[0] == NULL)
	argc = 1;		/* silencing stupid compiler warnings */

    ret =
	sqlite3_open_v2 (":memory:", &handle,
			 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "cannot open in-memory db: %s\n",
		   sqlite3_errmsg (handle));
	  sqlite3_close (handle);
	  return -1;
      }

    spatialite_init_ex (handle, cache, 0);

#ifdef ENABLE_LIBXML2		/* only if LIBXML2 is supported */

    if (!check_parse (cache, "books.xml"))
      {
	  fprintf (stderr, "unable to parse \"books.xml\"\n");
	  return -2;
      }
    if (!check_parse (cache, "opera.xml"))
      {
	  fprintf (stderr, "unable to parse \"opera.xml\"\n");
	  return -3;
      }
    if (!check_parse (cache, "movies.xml"))
      {
	  fprintf (stderr, "unable to parse \"movies.xml\"\n");
	  return -4;
      }

    if (!check_validate (cache, "books.xml"))
      {
	  fprintf (stderr, "unable to validate \"books.xml\"\n");
	  return -5;
      }
    if (!check_validate (cache, "opera.xml"))
      {
	  fprintf (stderr, "unable to validate \"opera.xml\"\n");
	  return -6;
      }
    if (!check_validate (cache, "movies.xml"))
      {
	  fprintf (stderr, "unable to validate \"movies.xml\"\n");
	  return -7;
      }
    if (!check_extended (cache, "inspire-data-example.xml", ISO_METADATA))
      {
	  fprintf (stderr, "unable to parse \"inspire-data-example.xml\"\n");
	  return -8;
      }
    if (!check_extended (cache, "stazioni_se.xml", SLD_SE_STYLE))
      {
	  fprintf (stderr, "unable to parse \"stazioni_se.xml\"\n");
	  return -9;
      }
    if (!check_extended (cache, "thunderstorm_mild.svg", SVG))
      {
	  fprintf (stderr, "unable to parse \"thunderstorm_mild.svg\"\n");
	  return -10;
      }

    if (!check_bad_xml (cache))
      {
	  fprintf (stderr, "unable to test not well-formed XML\n");
	  return -11;
      }
    if (!check_bad_schema (cache))
      {
	  fprintf (stderr, "unable to test invalid Schema\n");
	  return -12;
      }

    if (!check_mline_gpx (handle, cache, "000323485.gpx"))
      {
	  fprintf (stderr, "unable to test \"000323485.gpx\"\n");
	  return -13;
      }
    if (!check_mline_gpx (handle, cache, "Gpx-sample.gpx"))
      {
	  fprintf (stderr, "unable to test \"Gpx-sample.gpx\"\n");
	  return -14;
      }

#endif

    ret = sqlite3_close (handle);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "sqlite3_close() error: %s\n",
		   sqlite3_errmsg (handle));
	  return -10;
      }

    spatialite_cleanup_ex (cache);

    spatialite_shutdown ();

    return 0;
}