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; } } }
// string position void EvalPos(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { CStr sub = (*args)[0]; CStr str = (*args)[1]; if (sub.Data() && str.Data()) { const char *p = strstr((const char *)str, (const char *)sub); if (p) { out->PutN(p - str.Data()); } } }
void EvalPack(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { VALID_ARGM("pack", 2); CStr templ = (*args)[0]; CStr sarg; char t; const char * p = templ; char *endp = 0; if (!p) return; int nlen, plen, i = 1, j; while (*p) { switch(*p) { case 'a': case 'A': if (isdigit(p[1])) { nlen = strtol(p+1, &endp, 0); p = endp-1; if (nlen <= 0) nlen =1; } else nlen =1; sarg = (*args)[i++]; plen = sarg.Length(); sarg.Grow(nlen); if (nlen > plen) memset(sarg.Data()+plen,(*p == 'a' ? ' ' : '\0'),nlen-plen); out->PutS(sarg); case 'l': case 'L': t = *p; if (isdigit(p[1])) { nlen = strtol(p+1, &endp, 0); p = endp-1; if (nlen <= 0) nlen =1; } else nlen =1; for(j = 0; j < nlen; ++j) { DWORD dw; sarg = (*args)[i++]; if (t == 'l') dw = (DWORD) strtoul(sarg.Data(), &endp, 0); else if (t == 'L') dw = (DWORD) strtol(sarg.Data(), &endp, 0); out->PutS((char *)&dw,sizeof(dw)); } } ++p; } }
// expand an unexpanded (quoted) macro void EvalExpand(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { VALID_ARGC("expand", 0, 1); CStr val = (*args)[0]; if (val.Length() > 0) { if (val.Length() > 4 && val.Data()[0] == CMAGIC_V1[0] && val.Data()[1] == CMAGIC_V1[1] && val.Data()[2] == CMAGIC_V1[2] && val.Data()[3] == CMAGIC_V1[3]) { qStrReadBuf tmp(val.Data()+4,val.Length()-4); RunCompiled(ctx, &tmp, out); } else { ctx->Parse(val, out); } } }
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 EvalTrim(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { VALID_ARGC("trim", 1, 2); if (args->Count() > 1) { CStr str = (*args)[0]; if (str.IsEmpty()) return; CStr toks = (*args)[1]; if (toks.Length() == 0) out->PutS(str); else if (toks.Length() == 1) out->PutS(str.Trim(*toks)); else { const char *b, *p = str; size_t i = strspn(p,toks); b = p += i; p += str.Length() - 1 - i; while(p >= b && strchr(toks.Data(), *p)) --p; ++p; out->PutS(b, p - b); } } else out->PutS((*args)[0].Trim()); }
bool ConfirmChecksum(CStr CardNumber) { int CheckSum; // Holds the value of the operation bool Flag; // used to indicate when ready int Counter; // index counter int Number; // used to convert each digit to integer /************************************************************************** function is extracting each digit of the number and subjecting it to the checksum formula established by the credit card companies. It works from the end to the front. **************************************************************************/ // get the starting value for our counter Counter = CardNumber.Length() - 1; CheckSum = 0; Number = 0; Flag = false; while ( Counter >= 0 ) { // get the current digit Number = CardNumber.Data()[Counter] - '0'; if ( Flag ) // only do every other digit { Number = Number * 2; if ( Number >= 10 ) Number = Number - 9; } CheckSum = CheckSum + Number; Flag = !Flag; Counter = Counter - 1; } return ( ( CheckSum % 10 ) == 0 ); }
void EvalRealPath(const void *mode, qCtx *ctx, qStr *out, qArgAry *args) { VALID_ARGC("realpath", 1, 1); CStr src = (*args)[0]; if (!src.Length()) return; CStr dest; dest.Grow(MAX_PATH); #ifdef WIN32 PathCanonicalize(dest.Data(), src.Data()); #else realpath(src.Data(), dest.Data()); #endif dest.Grow(strlen(dest.Data())); out->PutS(dest); }
int B64_decode(const char *strin, char *strout, int cbstr) { CStr altin; if ((cbstr % 4 == 3) || (cbstr % 4 == 2)) { altin.Copy(strin, cbstr); if (cbstr % 4 <= 3) altin << '='; if (cbstr % 4 == 2) altin << '='; strin = altin.Data(); cbstr = altin.Length(); } int len = EVP_DecodeBlock((unsigned char*)strout, (unsigned char*)strin, cbstr); if ( cbstr && (len > 0) ) { if (strin[cbstr-1] == '=') { if (strin[cbstr-2] == '=') return(len-2); else return(len-1); } } return len; }