CStr ReplaceStrI(CStr in, CStr from, CStr to) { char *p; int fl = from.Length(), tl = to.Length(), il = in.Length(); if (fl <= 0 || fl > il) return in; if (fl == tl) { char *i = in.SafeP(); char *b = i; while ((p = stristr(i, (il-(i-b)), from, fl))) { memcpy(p, to.Data(), tl); i = p + fl; } return in; } else { CStr res; char *i = in.SafeP(); while ((p = stristr(i, il-(i-in.Data()), from, fl))) { res.Append(i, p - i); if (tl > 0) res << to; i = p + fl; } if (i != in.Data()) { res.Append(i, il - (i - in.Data())); return res; } else { return in; } } }
void EvalMid(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { if (args->Count() >= 2) { CStr tmp = (*args)[0]; int index = ParseInt((*args)[1]); if (args->Count() >= 3) { int len = ParseInt((*args)[2]); if (abs(index) < tmp.Length()) { if (index >= 0) { out->PutS(tmp.SafeP() + index, min(tmp.Length()-index, len)); } else { out->PutS(tmp.SafeP() + max(tmp.Length() + index - abs(len), 0), min(tmp.Length(), len)); } } } else { if (abs(index) < tmp.Length()) { if (index >= 0) { out->PutS(tmp.SafeP() + index, tmp.Length()-index); } else { out->PutS(tmp.SafeP(), tmp.Length() + index); } } } } }
void EvalWcmX(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { if (args->Count() > 1) { CStr a = (*args)[0]; CStr b = (*args)[1]; if (IsWcmX(a.SafeP(),b.SafeP())) { out->PutC('T'); } } }
void EvalCsvQuote(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { if (args->Count() > 0) { CStr s = (*args)[0]; char *bi = s.SafeP(); const char *pi = bi; while (*pi) { if (*pi == '\"' || *pi == ',') { CStr o(s.Length()*2 + 2); char *po = o.SafeP(); *po++ = '\"'; memcpy(po, s.Data(), pi-bi); CsvQuoteQ(pi, po); *po++ = '\"'; o.Grow(po - o.Data()); out->PutS(o,o.Length()); return; } ++pi; } out->PutS(s,s.Length()); } }
void EvalFmt(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { CStr val = (*args)[0]; ColFmt col; memset(&col, 0, sizeof(col)); col.dec = args->Count() > 1 ? ParseInt((*args)[1]) : 2; if (args->Count() > 2) { CStr dch = (*args)[2]; col.dch = (dch.IsEmpty()) ? 0 : *dch; } else col.dch = '.'; if (args->Count() > 3) { CStr tho = (*args)[3]; col.tho = (tho.IsEmpty()) ? 0 : *tho; } else col.tho = ','; CStr res(256); double dval; const char *p = CTabFmt::NumFmt(col, val.SafeP(), res.GetBuffer(), 256, &dval); out->PutS(p); }
CStr EVP_decrypt(CStr passw, CStr strin, const char *cipher) { int len = strin.Length(); if (len > 0 && passw.Length() > 0) { strin.Grow(len + EVP_MAX_IV_LENGTH); return strin.Grow( EVP_decrypt(passw.SafeP(), passw.Length(), strin.GetBuffer(), len, cipher) ); } else return strin; }
void EvalEnumToken(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { if (args->Count() >= 3) { // todo, ParseFork arg zero into a blocking i/o stream... CStr dat = (*args)[0]; CStr toks = (*args)[1]; qCtxTmp sub(ctx); char *b = dat.SafeP(); char *t = toks.SafeP(); const char *p = b + strspn(b, t); char *e; bool ok = true; int skip = 0; sub.MapObj(&p, "token"); sub.MapObj(&ok, (QOBJFUNC) EvalBreak, "break"); sub.MapObj(&skip, (QOBJFUNC) EvalSkipTokens, "skip-tokens"); while (p && *p && ok) { e = strpbrk(p, t); if (e) { *e = 0; if (!skip) sub.Parse(args->GetAt(2),out); else --skip; p = (++e) + strspn(e, t); } else { if (!skip) sub.Parse(args->GetAt(2),out); else --skip; break; } } } }
void EvalLeft(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { if (args->Count() >= 2) { CStr tmp = (*args)[0]; int index = ParseInt((*args)[1]); if (index > 0) { if (index < tmp.Length()) { out->PutS(tmp.SafeP(), index); } else { out->PutS(tmp); } } } }
void EvalGetToken(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { if (args->Count() >= 3) { // todo, ParseFork arg zero into a blocking i/o stream... CStr dat = (*args)[0]; CStr toks = (*args)[1]; int ind = ParseInt((*args)[2]); qCtxTmp sub(ctx); char *b = dat.SafeP(); char *t = toks.SafeP(); const char *p = b + strspn(b, t); char *e; sub.MapObj(&p, "token"); while (p && *p) { e = strpbrk(p, t); if (e) { *e = 0; if (--ind < 0) break; p = (++e) + strspn(e, t); } else { if (ind > 0) return; else break; } } if (p) { out->PutS(p); } } }
void EvalCsvQuoteQ(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { if (args->Count() > 0) { CStr s = (*args)[0]; CStr o(s.Length()*2 + 2); char *bi = s.SafeP(); const char *pi = bi; char *po = o.SafeP(); *po++ = '\"'; CsvQuoteQ(pi, po); *po++ = '\"'; o.Grow(po - o.Data()); out->PutS(o,o.Length()); } }
CDbConn CDbLib::Connect(char *dsn) { char *uid, *pwd, *etc; ParseDSN(dsn, dsn, uid, pwd, etc); qObjTSRef ts = GetRef(); if (myHENV) { TRACE("sql: spawning connection\n"); SQLHDBC hdbc; SQLRETURN nResult; if ((nResult = SQLAllocHandle(SQL_HANDLE_DBC, myHENV, &hdbc) != SQL_SUCCESS)) { return CDbConn(this, (SQLHDBC)NULL); } if (etc && *etc) { CStr connIn; connIn = CStr("DSN=")<<dsn<<";UID="<<uid<<";PWD="<<pwd<<';'<<etc; CStr connOut(1024); short totLen; nResult = SQLDriverConnect(hdbc, NULL, (LPSQLC) connIn.SafeP(), connIn.Length(), (LPSQLC) connOut.SafeP(), connOut.Length(), &totLen, SQL_DRIVER_NOPROMPT); } else { nResult = SQLConnect(hdbc, (LPSQLC) dsn, SQL_NTS, (LPSQLC) uid, SQL_NTS, (LPSQLC) pwd, SQL_NTS); } // if failed to connect, free the allocated hdbc before return if (!SQL_SUCCEEDED(nResult)) { CDbConn conn(this, hdbc); conn.SetError(nResult); return conn; } return CDbConn(this, hdbc); } else { return CDbConn(this, (SQLHDBC)NULL); } }
void EvalPcase(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { if (args->Count() > 0) { CStr s = (*args)[0]; char *p = s.SafeP(); char *c=0; while (*p) { if (c) { if (!isalnum(*p)) { pcasew(c, p); c = 0; } } else { if (isalpha(*p)) c = p; } ++p; } if (c) { pcasew(c,p); } out->PutS(s,s.Length()); } }
double ParseDbl(const CStr &str) { char *ep; return strtod(str.SafeP(), &ep); }
int ParseInt(const CStr &str) { return atoi(str.SafeP()); }