/* added by Wakamori */ static void opt_logcached(int mode, const char *optstr) { #if defined(HAVE_LIBMEMCACHED) knh_syslog = memcached_syslog; knh_vsyslog = memcached_vsyslog; kbytes_t host_port = B(optstr); kbytes_t host = knh_bytes_head(host_port, ':'); kint_t port; if (!knh_bytes_parseint(knh_bytes_next(host_port, ':'), &port)) { KNH_DIE("konoha: invalid arguments."); } memc = memcached_create(NULL); char tmp = host.buf[host.len]; host.buf[host.len] = '\0'; memcached_return rc; memcached_server_list_st servers; servers = memcached_server_list_append(NULL, host.text, port, &rc); if (rc != MEMCACHED_SUCCESS) { KNH_DIE("konoha: cannot append a memcached server."); } host.buf[host.len] = tmp; rc = memcached_server_push(memc, servers); if (rc != MEMCACHED_SUCCESS) { KNH_DIE("konoha: cannot push a memcached server."); } memcached_server_list_free(servers); #else KNH_DIE("konoha: memcached library is not installed."); #endif /* defined(HAVE_LIBMEMCACHED) */ }
//static int MYSQL_qnext(CTX ctx, kqcur_t *qcur, struct kResultSet *rs) int MYSQL_qnext(CTX ctx, kqcur_t *qcur, struct kResultSet *rs) { MYSQL_ROW row; if ((row = mysql_fetch_row((MYSQL_RES*)qcur)) != NULL) { KNH_NTRACE2(ctx, "mysql_fetch_row", K_OK, KNH_LDATA0); int i; kint_t ival; kfloat_t fval; for (i = 0; i < DP(rs)->column_size; i++) { if (row[i] == NULL) { ResultSet_setNULL(ctx, rs, i); continue; } switch (DP(rs)->column[i].dbtype) { case MYSQL_TYPE_TINY: case MYSQL_TYPE_SHORT: case MYSQL_TYPE_INT24: case MYSQL_TYPE_LONG: case MYSQL_TYPE_LONGLONG: case MYSQL_TYPE_YEAR: knh_bytes_parseint(B(row[i]), &ival); ResultSet_setInt(ctx, rs, i, ival); break; case MYSQL_TYPE_FLOAT: case MYSQL_TYPE_DOUBLE: knh_bytes_parsefloat(B(row[i]), &fval); ResultSet_setFloat(ctx, rs, i, fval); break; case MYSQL_TYPE_NEWDECIMAL: case MYSQL_TYPE_STRING: case MYSQL_TYPE_VAR_STRING: case MYSQL_TYPE_TINY_BLOB: case MYSQL_TYPE_BLOB: case MYSQL_TYPE_MEDIUM_BLOB: case MYSQL_TYPE_LONG_BLOB: case MYSQL_TYPE_BIT: case MYSQL_TYPE_TIME: case MYSQL_TYPE_DATE: case MYSQL_TYPE_DATETIME: case MYSQL_TYPE_TIMESTAMP: ResultSet_setText(ctx, rs, i, B(row[i])); break; case MYSQL_TYPE_NULL: default: //KNH_SYSLOG(ctx, LOG_WARNING, "mysql", "mysql_qnext(dbtype)='unknown datatype [%d]'", DP(rs)->column[i].dbtype); ResultSet_setNULL(ctx, rs, i); break; } } /* for */ return 1; /* CONTINUE */ } else { KNH_NTRACE2(ctx, "mysql_fetch_row", K_FAILED, KNH_LDATA0); } return 0; /* NOMORE */ }
int knh_bytes_parsefloat(kbytes_t t, kfloat_t *value) { #if defined(K_USING_NOFLOAT) { kint_t v = 0; knh_bytes_parseint(t, &v); *value = (kfloat_t)v; } #else *value = strtod(t.text, NULL); #endif return 1; }
static METHOD knh__String_format(Ctx *ctx, knh_sfp_t *sfp) { knh_bytes_t fmt = knh_String_tobytes(sfp[0].s); knh_sfp_t *param = sfp + 1; int ac = knh_stack_argc(ctx, param); knh_bytes_t mt, expr, next; if(!knh_bytes_findMT(ctx, fmt, &mt, &expr, &next)) { KNH_RETURN(ctx, sfp, sfp[0].s); } knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf); int count; for(count = 0; ; count++) { if(mt.buf > fmt.buf + 1) { fmt.len = (mt.buf - fmt.buf) - 1; knh_Bytes_write(ctx, cwb->ba, fmt); } int index = count; if(expr.len > 0) { knh_int_t num; if(knh_bytes_parseint(expr, &num)) { index = (int)num; } } if(0 <= index && index < ac) { knh_sfp_t *esp = KNH_LOCAL(ctx); KNH_SETv(ctx, esp[1].o, param[index].o); esp[1].data = param[index].data; Object *m = KNH_NULL; if(knh_bytes_isOptionalMT(mt)) m = UP(new_String(ctx, mt, NULL)); mt.buf = mt.buf - 1; mt.len++; /* 's' == > '%s' */ knh_methodn_t mn = knh_getmn(ctx, mt, METHODN__empty); knh_esp1_format(ctx, mn, cwb->w, m); } else { if(knh_Context_isDebug(ctx)) { KNH_THROW_OUTOFINDEX(ctx, index, ac); } } fmt.buf = next.buf; fmt.len = next.len; if(!knh_bytes_findMT(ctx, fmt, &mt, &expr, &next)) { break; } } if(fmt.len > 0) { knh_Bytes_write(ctx, cwb->ba, fmt); } KNH_RETURN(ctx, sfp, new_StringX__cwb(ctx, knh_Object_cid(sfp[0].o), cwb)); }
void konoha_ginit(int argc, const char **argv) { int n; stdlog = stderr; knh_syslog = pseudo_syslog; knh_vsyslog = pseudo_vsyslog; for(n = 1; n < argc; n++) { const char *t = argv[n]; if(t[0] == '-' && (isalnum(t[1]) || t[1] == '-')) { knh_optdata_t *d = knh_getoptdata(t); int optnum = 1; // default const char* optstr = NULL; // default if(d == NULL) continue; if(d->type == OPT_NUMBER) { t += d->len; if(t[0] == '=') t++; if(isalnum(t[0])) { kint_t v = 0; knh_bytes_parseint(B((char*)t), &v); optnum = (int)v; } } else if(d->type == OPT_STRING) { t += d->len; if(t[0] == '=') { optstr = t + 1; } else if(t[0] != 0) { optstr = t; } else if(n + 1 < argc) { n++; optstr = argv[n]; if(optstr[0] == '-') { n--; optstr = NULL; } if(knh_bytes_endsWith_(B(optstr), STEXT(".k"))) { break; } } } d->setopt(optnum, optstr); continue; } } }
static int knh_parseopt(CTX ctx, int argc, const char **argv) { int n; for(n = 1; n < argc; n++) { const char *t = argv[n]; if(t[0] == '-' && (isalnum(t[1]) || t[1] == '-')) { knh_optdata_t *d = knh_getoptdata(t); int optnum = 1; // default const char* optstr = NULL; // default if(d == NULL) { opt_help(ctx, optnum, optstr); } if(d->type == OPT_NUMBER) { t += d->len; if(t[0] == '=') t++; if(isalnum(t[0])) { kint_t v = 0; knh_bytes_parseint(B((char*)t), &v); optnum = (int)v; } } else if(d->type == OPT_STRING) { t += d->len; if(t[0] == '=') { optstr = t + 1; } else if(t[0] != 0) { optstr = t; } else if(n + 1 < argc) { n++; optstr = argv[n]; if(optstr[0] == '-') { n--; optstr = NULL; } } } d->setopt(ctx, optnum, optstr); continue; } break; } return n; }
KNHAPI(int) knh_bytes_parseURLport(knh_bytes_t url, int *port) { knh_index_t loc = knh_bytes_index(url, ':'); if(loc > 0 && url.buf[loc+1] == '/' && url.buf[loc+2] == '/') { knh_bytes_t t = knh_bytes_last(url, loc+3); loc = knh_bytes_index(t, '@'); if(loc > 0) t = knh_bytes_last(t, loc+1); loc = knh_bytes_index(t, '/'); if(loc > 0) t = knh_bytes_first(t, loc); loc = knh_bytes_index(t, ':'); if(loc > 0) { knh_int_t n = *port; if(knh_bytes_parseint(knh_bytes_last(t, loc+1), &n)) { *port = (int)n; return 1; } } } return 0; }
int knh_bytes_parsefloat(knh_bytes_t t, knh_float_t *value) { if(t.buf[0] == '0' && (t.buf[1] == 'x' || t.buf[1] == 'b')) { knh_int_t n = 0; int res = knh_bytes_parseint(t, &n); *value = (knh_float_t)n; return res; } knh_index_t loc = knh_bytes_index(t, 'E'); if(loc == -1) loc = knh_bytes_index(t, 'e'); if(loc != -1) { t = knh_bytes_first(t, loc); } size_t i = 0; knh_float_t v = 0.0, prev = 0.0, c = 1.0; if(t.buf[0] == '-') i = 1; for(;i < t.len; i++) { if('0' <= t.buf[i] && t.buf[i] <= '9') { prev = v; v = v * 10 + (t.buf[i] - '0'); #if defined(KNH_USING_MATH) && !defined(KONOHA_ON_WINDOWS) if(isinf(v)||isnan(v)) { *value = 0.0; return 1; } #endif } else if(t.buf[i] == '.') { i++; break; } else { *value = (t.buf[0] == '-') ? -v : v; return 1; } } for(; i < t.len; i++) { if('0' <= t.buf[i] && t.buf[i] <= '9') { prev = v; c *= 10; #if defined(KNH_USING_MATH) && !defined(KONOHA_ON_WINDOWS) if(isinf(c)||isnan(c)) { break; } #endif v = v + ((t.buf[i] - '0') / c); }else { break; } } v = (t.buf[0] == '-') ? -v : v ; if(loc != -1) { knh_bytes_t t2 = knh_bytes_last(t, loc + 1); knh_intptr_t scale = knh_bytes_toint(t2); int j; if(scale > 0) { for(j = 0; j < scale; j++) { v *= 10; } } else if(scale < 0) { scale = -scale; for(j = 0; j < scale; j++) { v /= 10; } } } *value = v; return 1; }