static void cmd_sd(void) { real sd, variance; int units; unsigned long qmask, m; int quantity; qmask = get_qlist(BIT(Q_DECLINATION)); if (!qmask) return; /* no quantities found - error already reported */ if (qmask == BIT(Q_DEFAULT)) { default_grade(pcs); return; } sd = read_numeric(fFalse, NULL); if (sd <= (real)0.0) { compile_error_skip(-/*Standard deviation must be positive*/48); return; } units = get_units(qmask, fFalse); if (units == UNITS_NULL) return; sd *= factor_tab[units]; variance = sqrd(sd); for (quantity = 0, m = BIT(quantity); m <= qmask; quantity++, m <<= 1) if (qmask & m) pcs->Var[quantity] = variance; }
static void cmd_units(void) { int units, quantity; unsigned long qmask; unsigned long m; /* mask with bit x set to indicate quantity x specified */ real factor; qmask = get_qlist(0); if (!qmask) return; if (qmask == BIT(Q_DEFAULT)) { default_units(pcs); return; } factor = read_numeric(fTrue, NULL); if (factor == 0.0) { compile_error_skip(-/**UNITS factor must be non-zero*/200); return; } if (factor == HUGE_REAL) factor = (real)1.0; units = get_units(qmask, fTrue); if (units == UNITS_NULL) return; if (TSTBIT(qmask, Q_GRADIENT)) pcs->f_clino_percent = (units == UNITS_PERCENT); if (TSTBIT(qmask, Q_BACKGRADIENT)) pcs->f_backclino_percent = (units == UNITS_PERCENT); factor *= factor_tab[units]; for (quantity = 0, m = BIT(quantity); m <= qmask; quantity++, m <<= 1) if (qmask & m) pcs->units[quantity] = factor; }
static void cmd_calibrate(void) { real sc, z; unsigned long qmask, m; int quantity; qmask = get_qlist(BIT(Q_POS)|BIT(Q_PLUMB)|BIT(Q_LEVEL)); if (!qmask) return; /* error already reported */ if (qmask == BIT(Q_DEFAULT)) { default_calib(pcs); return; } if (((qmask & LEN_QMASK)) && ((qmask & ANG_QMASK))) { compile_error_skip(/*Can’t calibrate angular and length quantities together*/227); return; } z = read_numeric(fFalse, NULL); sc = read_numeric(fTrue, NULL); if (sc == HUGE_REAL) sc = (real)1.0; /* check for declination scale */ /* perhaps "*calibrate declination XXX" should be "*declination XXX" ? */ if (TSTBIT(qmask, Q_DECLINATION) && sc != 1.0) { compile_error_skip(-/*Scale factor must be 1.0 for DECLINATION*/40); return; } if (sc == 0.0) { compile_error_skip(-/*Scale factor must be non-zero*/391); return; } for (quantity = 0, m = BIT(quantity); m <= qmask; quantity++, m <<= 1) { if (qmask & m) { pcs->z[quantity] = pcs->units[quantity] * z; pcs->sc[quantity] = sc; } } }
/* read numeric expr or omit (return HUGE_REAL); else longjmp */ extern real read_numeric_or_omit(int *p_n_readings) { real v = read_numeric(fTrue, p_n_readings); if (v == HUGE_REAL) { if (!isOmit(ch)) { compile_error_token(-/*Expecting numeric field, found “%s”*/9); LONGJMP(file.jbSkipLine); return 0.0; /* for brain-fried compilers */ } nextch(); } return v; }
int main() { try { std::string conninfo = "dbname=mloskot user=mloskot"; auto qconn = tinypq::connect(conninfo); tinypq::command(qconn, "DROP TABLE IF EXISTS testn CASCADE"); tinypq::command(qconn, "CREATE TABLE testn (n1 NUMERIC, n2 NUMERIC(10, 10), n3 NUMERIC(10, 5), n4 NUMERIC(10, 0))"); tinypq::command(qconn, "INSERT INTO testn (n1, n2, n3, n4) VALUES (random()::NUMERIC*1000, random(), random()::NUMERIC*1000, 1230456078)"); auto qres = tinypq::query(qconn, "SELECT n1, n2, n3, n4 FROM testn LIMIT 1"); std::array<char const*, 4> cols = { "n1", "n2", "n3", "n4" }; std::for_each(cols.cbegin(), cols.cend(), [&qres](char const* col) { char* val = tinypq::fetch(qres, 0, col); auto n = read_numeric(val); // make a decimal value std::string scoeff; scoeff.reserve(n.digits.size()); std::for_each(n.digits.cbegin(), n.digits.cend(), [&scoeff](short c) { scoeff += std::to_string(c); }); long long const coeff = std::stoll(scoeff); double const value = std::pow(-1.0, n.sign) * coeff * std::pow(10, n.exp); echo(col, n, value); }); return EXIT_SUCCESS; } catch (std::exception const& e) { std::clog << e.what() << std::endl; return EXIT_FAILURE; } }
static void cmd_fix(void) { prefix *fix_name; node *stn = NULL; static node *stnOmitAlready = NULL; real x, y, z; int nx, ny, nz; filepos fp; fix_name = read_prefix(PFX_STATION|PFX_ALLOW_ROOT); fix_name->sflags |= BIT(SFLAGS_FIXED); get_pos(&fp); get_token(); if (strcmp(ucbuffer, "REFERENCE") == 0) { /* suppress "unused fixed point" warnings for this station */ fix_name->sflags |= BIT(SFLAGS_USED); } else { if (*ucbuffer) set_pos(&fp); } x = read_numeric(fTrue, &nx); if (x == HUGE_REAL) { /* If the end of the line isn't blank, read a number after all to * get a more helpful error message */ if (!isEol(ch) && !isComm(ch)) x = read_numeric(fFalse, &nx); } if (x == HUGE_REAL) { if (stnOmitAlready) { if (fix_name != stnOmitAlready->name) { compile_error_skip(/*More than one FIX command with no coordinates*/56); } else { compile_warning(/*Same station fixed twice with no coordinates*/61); } return; } stn = StnFromPfx(fix_name); compile_warning(/*FIX command with no coordinates - fixing at (0,0,0)*/54); x = y = z = (real)0.0; stnOmitAlready = stn; } else { real sdx; y = read_numeric(fFalse, &ny); z = read_numeric(fFalse, &nz); sdx = read_numeric(fTrue, NULL); if (sdx != HUGE_REAL) { real sdy, sdz; real cxy = 0, cyz = 0, czx = 0; sdy = read_numeric(fTrue, NULL); if (sdy == HUGE_REAL) { /* only one variance given */ sdy = sdz = sdx; } else { sdz = read_numeric(fTrue, NULL); if (sdz == HUGE_REAL) { /* two variances given - horizontal & vertical */ sdz = sdy; sdy = sdx; } else { cxy = read_numeric(fTrue, NULL); if (cxy != HUGE_REAL) { /* covariances given */ cyz = read_numeric(fFalse, NULL); czx = read_numeric(fFalse, NULL); } else { cxy = 0; } } } stn = StnFromPfx(fix_name); if (!fixed(stn)) { node *fixpt = osnew(node); prefix *name; name = osnew(prefix); name->pos = osnew(pos); name->ident = NULL; name->shape = 0; fixpt->name = name; name->stn = fixpt; name->up = NULL; if (TSTBIT(pcs->infer, INFER_EXPORTS)) { name->min_export = USHRT_MAX; } else { name->min_export = 0; } name->max_export = 0; name->sflags = 0; add_stn_to_list(&stnlist, fixpt); POS(fixpt, 0) = x; POS(fixpt, 1) = y; POS(fixpt, 2) = z; fix(fixpt); fixpt->leg[0] = fixpt->leg[1] = fixpt->leg[2] = NULL; addfakeleg(fixpt, stn, 0, 0, 0, sdx * sdx, sdy * sdy, sdz * sdz #ifndef NO_COVARIANCES , cxy, cyz, czx #endif ); } return; } stn = StnFromPfx(fix_name); } if (!fixed(stn)) { POS(stn, 0) = x; POS(stn, 1) = y; POS(stn, 2) = z; fix(stn); return; } if (x != POS(stn, 0) || y != POS(stn, 1) || z != POS(stn, 2)) { compile_error(/*Station already fixed or equated to a fixed point*/46); return; } compile_warning(/*Station already fixed at the same coordinates*/55); }
static int lex(lexer_state *ls, token_info *info) { buffer_reset(ls->buf); if(setjmp(ls->error.buf)) return TK_ERROR; for(;;) { switch(ls->current) { case '\n': case '\r': { // newline inc_line(ls); break; } case ' ': case '\t': { // whitespace next(ls); break; } case '-': { // comment or minus // minus if(next(ls) != '-') return '-'; // comment, skip line while(next(ls) != EOS && !isnewline(ls)); break; } case '=': { // EQ next(ls); return '='; } case '<': { // LT, LTE, ASSIGN next(ls); if(ls->current == '=') { next(ls); return TK_LTE; } else if(ls->current == '-'){ next(ls); return TK_ASSIGN; } else return '<'; } case '>': { // GT, GTE next(ls); if(ls->current == '=') { next(ls); return TK_GTE; } else return '>'; } case '/': { // NEQ, DIV next(ls); if(ls->current == '=') { next(ls); return TK_NEQ; } else return '/'; } case '"': { // STRING read_string(ls, info); return TK_STRING; } case EOS: { // EOS return TK_EOS; } default: { if(lisdigit(ls->current)) { // NUMERIC read_numeric(ls, info); return TK_REAL; } if(lisalpha(ls->current)) { // ID or RESERVED return read_id_or_reserved(ls, info); } int c = ls->current; // valid operators, single character tokens, etc. switch(ls->current) { case '+': case '-': case '*': case '/': case '!': case '>': case '<': case '=': case '(': case ')': case '[': case ']': case '{': case '}': case ':': case '.': case ',': next(ls); return c; default: lexer_error(ls, "unrecognized symbol %c", c); next(ls); } } } } }