ksymbol_t knh_getfnq(CTX, kbytes_t tname, ksymbol_t def) { ksymbol_t mask = 0; kindex_t idx = knh_bytes_index(tname, ':'); if(idx > 0) { tname = knh_bytes_first(tname, idx); } else if(knh_bytes_startsWith_(tname, STEXT("super."))) { mask = (def == FN_NONAME) ? 0 : KFLAG_FN_SUPER; tname = knh_bytes_last(tname, 6); } else if(!knh_bytes_endsWith_(tname, STEXT("__"))) { if(tname.utext[0] == '_' && def != FN_NONAME) { mask = KFLAG_FN_U1; tname = knh_bytes_last(tname, 1); } if(tname.utext[0] == '_' && def != FN_NONAME) { mask = KFLAG_FN_U2; tname = knh_bytes_last(tname, 1); } while(tname.utext[0] == '_') { tname = knh_bytes_last(tname, 1); } } return getSymbol(_ctx, tname, def) | mask; }
kmethodn_t knh_getmn(CTX, kbytes_t tname, kmethodn_t def) { ksymbol_t mask = 0; if(tname.utext[0] == 'o' && tname.utext[1] == 'p') { kmethodn_t mn = MN_opNOT; for(; mn <= MN_opNEG; mn++) { const char *op = knh_getopMethodName(mn); if(knh_bytes_equals(tname, B(op))) { return mn; } } } if(tname.utext[0] == '%') { tname = knh_bytes_skipFMTOPT(tname); if(def != MN_NONAME) mask |= MN_FMT; } else if(tname.utext[0] == 'i' && tname.utext[1] == 's') { /* is => get */ tname = knh_bytes_last(tname, 2); if(def != MN_NONAME) mask |= MN_ISBOOL; } else if(tname.utext[0] == 'g' && tname.utext[1] == 'e' && tname.utext[2] == 't') { tname = knh_bytes_last(tname, 3); if(def != MN_NONAME) mask |= MN_GETTER; } else if(tname.utext[0] == 's' && tname.utext[1] == 'e' && tname.utext[2] == 't') { tname = knh_bytes_last(tname, 3); if(def != MN_NONAME) mask |= MN_SETTER; } return getSymbol(_ctx, tname, def) | mask; }
KNHAPI(knh_bytes_t) knh_bytes_substringURLpath(knh_bytes_t url) { 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) { return t = knh_bytes_last(t, loc); } } return STEXT("/"); }
static METHOD knh__String_substring(Ctx *ctx, knh_sfp_t *sfp) { knh_bytes_t base = knh_String_tobytes(sfp[0].s); knh_bytes_t sub; if(knh_String_isASCII(sfp[0].s)) { size_t offset = IS_NULL(sfp[1].o) ? 0 : knh_array_index(ctx, sfp[1].ivalue, base.len); sub = knh_bytes_last(base, offset); if(IS_NOTNULL(sfp[2].o)) { size_t len = (size_t)sfp[2].ivalue; if(len < sub.len) sub = knh_bytes_first(sub, len); } } else { // multibytes size_t mlen = knh_bytes_mlen(base); size_t offset = IS_NULL(sfp[1].o) ? 0 : knh_array_index(ctx, sfp[1].ivalue, mlen); size_t length = IS_NULL(sfp[2].o) ? (mlen - offset) : (size_t)sfp[2].ivalue; sub = knh_bytes_mofflen(base, offset, length); } String *s; if(sub.len == 0) { s = TS_EMPTY; } else if(sub.len == base.len) { s = sfp[0].s; } else { s = new_String(ctx, sub, sfp[0].s); } KNH_RETURN(ctx, sfp, s); }
//static kconn_t *MYSQL_qopen(CTX ctx, kbytes_t url) kconn_t *MYSQL_qopen(CTX ctx, kbytes_t url) { char *puser, user[MYSQL_USER_MAXLEN+1] = {0}; char *ppass, pass[MYSQL_PASS_MAXLEN+1] = {0}; // temporary defined char *phost, host[MYSQL_HOST_MAXLEN+1] = {0}; unsigned int port = 0; char *pdbnm, dbnm[MYSQL_DBNM_MAXLEN+1] = {0}; kbytes_t bt = knh_bytes_last(url, 8); // skip: mysql:// const char *btstr = bt.text; sscanf(btstr, "%16[^ :\r\n\t]:%255[^ @\r\n\t]@%255[^ :\r\n\t]:%5d/%64[^ \r\n\t]", (char*)&user, (char*)&pass, (char*)&host, &port, (char*)&dbnm); // consider to buffer over run puser = (user[0]) ? user : NULL; ppass = (pass[0]) ? pass : NULL; phost = (host[0]) ? host : NULL; pdbnm = (dbnm[0]) ? dbnm : NULL; MYSQL *db = mysql_init(NULL); KNH_NTRACE2(ctx, "mysql_init", (db != NULL) ? K_OK : K_FAILED, KNH_LDATA(LOG_s("init", mysql_error(db)))); db = mysql_real_connect(db, phost, puser, ppass, pdbnm, port, NULL, 0); KNH_NTRACE2(ctx, "mysql_real_connect", (db != NULL) ? K_OK : K_FAILED, KNH_LDATA(LOG_s("host", phost), LOG_s("user", puser), /* LOG_s("passwd", ppass), */ LOG_s("dbname", pdbnm), LOG_i("port", port), LOG_i("errno", mysql_errno(db)), LOG_s("error", mysql_error(db)))); //if (!mysql_real_connect(db, phost, puser, ppass, pdbnm, port, NULL, 0)) { // knh_mysql_perror(ctx, db, 0); // mysql_close(db); // db = NULL; //} return (kconn_t*)db; }
KNHAPI(int) knh_bytes_parseURLpath(knh_bytes_t url, char *buf, size_t bufsiz) { 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) { knh_format_bytes(buf, bufsiz, knh_bytes_last(t, loc)); } return 1; } knh_format_bytes(buf, bufsiz, STEXT("/")); return 0; }
KNHAPI(knh_bytes_t) knh_bytes_skipscheme(knh_bytes_t t) { knh_index_t loc = knh_bytes_index(t, ':'); if(loc >= 0) { return knh_bytes_last(t, loc + 1); } return t; }
KNHAPI(int) knh_bytes_parseURL(knh_bytes_t url, char *buf, size_t bufsiz) { knh_index_t loc = knh_bytes_index(url, '?'); if(loc > 0) url = knh_bytes_first(url, loc); loc = knh_bytes_index(url, ':'); if(loc > 0 && url.buf[loc+1] == '/' && url.buf[loc+2] == '/') { knh_bytes_t scheme = knh_bytes_first(url, loc + 3); knh_bytes_t t = knh_bytes_last(url, loc+3); loc = knh_bytes_index(t, '@'); if(loc > 0) { knh_bytes_t t = knh_bytes_last(t, loc+1); } knh_format_join2(buf, bufsiz, scheme, t); return 1; } knh_format_bytes(buf, bufsiz, url); // default return 0; }
static METHOD knh__String_endsWith__IgnoreCase(Ctx *ctx, knh_sfp_t *sfp) { knh_bytes_t base = knh_String_tobytes(sfp[0].s); knh_bytes_t expr = knh_String_tobytes(sfp[1].s); int res = 0; if(expr.len < base.len) { base = knh_bytes_last(base, base.len - expr.len); res = (knh_bytes_strcasecmp(base, expr) == 0); } KNH_RETURN_Boolean(ctx, sfp, res); }
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; }
knh_String_t* knh_getPropertyNULL(Ctx *ctx, knh_bytes_t key) { if(knh_bytes_startsWith(key, STEXT("env."))) { knh_cwb_t cwbbuf, *cwb = knh_cwb_open(ctx, &cwbbuf); knh_cwb_nzenvkey(ctx, cwb, knh_bytes_last(key, 4)); char *v = knh_getenv(knh_cwb_tochar(ctx, cwb)); knh_cwb_close(cwb); if(v == NULL) return NULL; return new_S(ctx, B(v)); } return (knh_String_t*)knh_DictMap_getNULL(ctx, DP(ctx->sys)->props, key); }
kString* knh_getPropertyNULL(CTX, kbytes_t key) { if(knh_bytes_startsWith_(key, STEXT("env."))) { CWB_t cwbbuf, *cwb = CWB_open(_ctx, &cwbbuf); CWB_nzenvkey(_ctx, cwb, knh_bytes_last(key, 4)); char *v = knh_getenv(CWB_totext(_ctx, cwb)); CWB_close(_ctx, cwb); if(v == NULL) return NULL; return new_kString(v, knh_strlen(v), SPOL_ASCII|SPOL_POOL); } return (kString*)knh_DictMap_getNULL(_ctx, ctx->share->props, key); }
static METHOD knh__String_opMod(Ctx *ctx, knh_sfp_t *sfp) { if(!IS_bString(sfp[0].o)) { KNH_RETURN(ctx, sfp, TS_EMPTY); } else { knh_bytes_t base = knh_String_tobytes(sfp[0].s); knh_bytes_t delim = knh_String_tobytes(sfp[1].s); knh_index_t index = knh_bytes_indexOf(base, delim); if(index == -1) { KNH_RETURN(ctx, sfp, TS_EMPTY); } else { base = knh_bytes_last(base, index + delim.len); KNH_RETURN(ctx, sfp, new_String(ctx, base, sfp[0].s)); } } }
KNHAPI(int) knh_bytes_parseURLuname(knh_bytes_t url, char *buf, size_t bufsiz) { 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_first(t, loc); loc = knh_bytes_index(t, ':'); if(loc > 0) { t = knh_bytes_first(t, loc); } knh_format_bytes(buf, bufsiz, t); return 1; } } knh_format_bytes(buf, bufsiz, STEXT("konoha")); // default return 0; }
static METHOD knh__Regex_new(Ctx *ctx, knh_sfp_t *sfp) { knh_Regex_t *o = (Regex*)sfp[0].o; knh_bytes_t p = knh_String_tobytes(sfp[1].s); knh_index_t loc = knh_bytes_index(p, ':'); KNH_SETv(ctx, o->pattern, sfp[1].s); if(loc == -1) { o->df = knh_System_getRegexDriver(ctx, STEXT("re")); } else { o->df = knh_System_getRegexDriver(ctx, knh_bytes_first(p, loc)); } o->reg = o->df->regmalloc(ctx); { char *ptn = (char*)(knh_bytes_last(p, loc+1).buf); char *opt = IS_NULL(sfp[2].o) ? "" : knh_String_tochar(sfp[2].s); o->df->regcomp(ctx, o->reg, ptn, opt); } KNH_RETURN(ctx, sfp, sfp[0].o); }
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; }