12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401 |
- /*##############################################################################
- HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- ############################################################################## */
- #include "jlib.hpp"
- #include "jexcept.hpp"
- #include "jmisc.hpp"
- #include "deftype.ipp"
- #include "defvalue.ipp"
- #include <stdio.h>
- #include <math.h>
- #include <limits.h>
- #include "rtlbcd.hpp"
- #include "eclrtl_imp.hpp"
- BoolValue *BoolValue::trueconst;
- BoolValue *BoolValue::falseconst;
- static IAtom * asciiAtom;
- static IAtom * ebcdicAtom;
- MODULE_INIT(INIT_PRIORITY_DEFVALUE)
- {
- asciiAtom = createLowerCaseAtom("ascii");
- ebcdicAtom = createLowerCaseAtom("ebcdic");
- BoolValue::trueconst = new BoolValue(true);
- BoolValue::falseconst = new BoolValue(false);
- return true;
- }
- MODULE_EXIT()
- {
- ::Release(BoolValue::trueconst);
- ::Release(BoolValue::falseconst);
- }
- union RealUnion
- {
- float r4;
- double r8;
- };
- //===========================================================================
- static const double powerOfTen[] = {1e0, 1e1, 1e2, 1e3, 1e4,
- 1e5, 1e6, 1e7, 1e8, 1e9,
- 1e10, 1e11, 1e12, 1e13, 1e14,
- 1e15, 1e16, 1e17, 1e18, 1e19,
- 1e20, 1e21, 1e22, 1e23, 1e24,
- 1e25, 1e26, 1e27, 1e28, 1e29,
- 1e30, 1e31, 1e32 };
- static const unsigned __int64 maxUIntValue[] = { I64C(0), I64C(0xFF), I64C(0xFFFF), I64C(0xFFFFFF), I64C(0xFFFFFFFF), I64C(0xFFFFFFFFFF), I64C(0xFFFFFFFFFFFF), I64C(0xFFFFFFFFFFFFFF), U64C(0xFFFFFFFFFFFFFFFF) };
- static const __uint64 maxIntValue[] = { I64C(0), I64C(0x7F), I64C(0x7FFF), I64C(0x7FFFFF), I64C(0x7FFFFFFF), I64C(0x7FFFFFFFFF), I64C(0x7FFFFFFFFFFF), I64C(0x7FFFFFFFFFFFFF), I64C(0x7FFFFFFFFFFFFFFF) };
- int rangeCompare(double value, ITypeInfo * targetType)
- {
- if (targetType->isSigned())
- {
- switch (targetType->getTypeCode())
- {
- case type_decimal:
- if (value <= -powerOfTen[targetType->getDigits()-targetType->getPrecision()])
- return -1;
- if (value >= powerOfTen[targetType->getDigits()-targetType->getPrecision()])
- return +1;
- break;
- case type_int:
- case type_swapint:
- if (value < 0)
- {
- if (-value > (double)maxIntValue[targetType->getSize()]+1)
- return -1;
- }
- if (value > (double)maxIntValue[targetType->getSize()])
- return +1;
- break;
- case type_packedint:
- return rangeCompare(value, targetType->queryPromotedType());
- }
- }
- else
- {
- if (value < 0)
- return -1;
- switch (targetType->getTypeCode())
- {
- case type_decimal:
- if (value >= powerOfTen[targetType->getDigits()-targetType->getPrecision()])
- return +1;
- break;
- case type_int:
- case type_swapint:
- if (value > (double)maxUIntValue[targetType->getSize()])
- return +1;
- break;
- case type_packedint:
- return rangeCompare(value, targetType->queryPromotedType());
- case type_bitfield:
- {
- unsigned __int64 maxValue = (U64C(1) << targetType->getBitSize()) - 1;
- if (value > maxValue)
- return +1;
- break;
- }
- }
- }
- return 0;
- }
- inline unsigned getTargetLength(ITypeInfo * type, unsigned dft)
- {
- unsigned length = type->getStringLen();
- if (length == UNKNOWN_LENGTH)
- length = dft;
- return length;
- }
- //===========================================================================
- CValue::CValue(ITypeInfo *_type)
- {
- type = _type;
- assertThrow(type);
- }
- CValue::~CValue()
- {
- type->Release();
- }
- int CValue::compare(const void *mem)
- {
- type->Link();
- IValue *to = createValueFromMem(type, mem);
- int ret = compare(to);
- to->Release();
- return ret;
- }
- double CValue::getRealValue()
- {
- return (double)getIntValue();
- }
- type_t CValue::getTypeCode(void)
- {
- return type->getTypeCode();
- }
- size32_t CValue::getSize()
- {
- return type->getSize();
- }
- ITypeInfo *CValue::queryType() const
- {
- return type;
- }
- ITypeInfo *CValue::getType()
- {
- return LINK(type);
- }
- IValue * CValue::doCastTo(unsigned osize, const char * text, ITypeInfo *t)
- {
- assertex(t == queryUnqualifiedType(t));
- switch (t->getTypeCode())
- {
- case type_string:
- return createStringValue(text, LINK(t), osize, type->queryCharset());
- case type_varstring:
- return createVarStringValue(text, LINK(t), osize, type->queryCharset());
- case type_data:
- return t->castFrom(osize, text); //NB: Must not go through translation in default case
- case type_char:
- return createCharValue(text[0], LINK(t));
- case type_int:
- case type_swapint:
- case type_packedint:
- break; // treat these using the default action in the type.
- case type_boolean:
- return createBoolValue(getBoolValue());
- case type_unicode:
- case type_varunicode:
- {
- rtlDataAttr buff(osize * sizeof(UChar));
- UChar * target = buff.getustr();
- rtlCodepageToUnicode(osize, target, osize, text, type->queryCharset()->queryCodepageName());
- return t->castFrom(osize, target);
- }
- case type_utf8:
- {
- unsigned tLen = getTargetLength(t, osize);
- rtlDataAttr buff(tLen * 4);
- char * target = buff.getstr();
- rtlCodepageToUtf8(tLen, target, osize, text, type->queryCharset()->queryCodepageName());
- return createUtf8Value(tLen, target, LINK(t));
- }
- }
- Owned<ICharsetInfo> charset = getCharset(asciiAtom);
- ITranslationInfo * translation = queryDefaultTranslation(charset, type->queryCharset());
- if (translation)
- {
- StringBuffer translated;
- translation->translate(translated, osize, text);
- return t->castFrom(osize, translated.str());
- }
- return t->castFrom(osize, text);
- }
- //===========================================================================
- VarStringValue::VarStringValue(unsigned len, const char *v, ITypeInfo *_type) : CValue(_type)
- {
- unsigned typeLen = type->getStringLen();
- assertex(typeLen != UNKNOWN_LENGTH);
- if (len >= typeLen)
- val.set(v, typeLen);
- else
- {
- char * temp = (char *)checked_malloc(typeLen+1, DEFVALUE_MALLOC_FAILED);
- memcpy_iflen(temp, v, len);
- temp[len] = 0;
- val.set(temp, typeLen);
- free(temp);
- }
- }
- const char *VarStringValue::generateECL(StringBuffer &out)
- {
- return appendStringAsQuotedECL(out, val.length(), val).str();
- }
- const char *VarStringValue::generateCPP(StringBuffer &out, CompilerType compiler)
- {
- unsigned len = val.length();
- return appendStringAsQuotedCPP(out, len, val.get(), len>120).str();
- }
- void VarStringValue::toMem(void *target)
- {
- unsigned copyLen = val.length();
- unsigned typeLen = type->getSize();
- //assertThrow(typeLen);
- if(typeLen > 0) {
- if (copyLen >= typeLen)
- copyLen = typeLen - 1;
-
- memcpy(target, val.get(), copyLen);
- memset_iflen((char *)target+copyLen, 0, (typeLen-copyLen));
- }
- else {
- memcpy(target, val.get(), copyLen + 1);
- }
- }
- unsigned VarStringValue::getHash(unsigned initval)
- {
- return hashc((unsigned char *) val.get(), val.length(), initval);
- }
- int VarStringValue::compare(IValue *_to)
- {
- assertThrow(_to->getTypeCode()==type->getTypeCode());
- VarStringValue *to = (VarStringValue *) _to;
- return rtlCompareVStrVStr(val.get(), to->val.get());
- }
- int VarStringValue::compare(const void *mem)
- {
- return rtlCompareVStrVStr(val.get(), (const char *)mem);
- }
- IValue *VarStringValue::castTo(ITypeInfo *t)
- {
- t = queryUnqualifiedType(t);
- if (t==type)
- return LINK(this);
- type_t tc = t->getTypeCode();
- if (tc == type_any)
- return LINK(this);
- return doCastTo(val.length(), (const char *)val.get(), t);
- }
- const void *VarStringValue::queryValue() const
- {
- return (const void *) val.get();
- }
- bool VarStringValue::getBoolValue()
- {
- return val.length() != 0;
- }
- __int64 VarStringValue::getIntValue()
- {
- return rtlVStrToInt8(val.get());
- }
- const char *VarStringValue::getStringValue(StringBuffer &out)
- {
- out.append(val);
- return out.str();
- }
- void VarStringValue::pushDecimalValue(void)
- {
- DecPushCString((char*)val.get());
- }
- void VarStringValue::serialize(MemoryBuffer &tgt)
- {
- tgt.append(val);
- }
- void VarStringValue::deserialize(MemoryBuffer &src)
- {
- src.read(val);
- }
- IValue * createVarStringValue(unsigned len, const char * value, ITypeInfo *type)
- {
- if (type->getSize() != UNKNOWN_LENGTH)
- return new VarStringValue(len, value, type);
- ITypeInfo * newType = getStretchedType(len, type);
- type->Release();
- return new VarStringValue(len, value, newType);
- }
- IValue *createVarStringValue(const char *val, ITypeInfo *type, int srcLength, ICharsetInfo * srcCharset)
- {
- ITranslationInfo * translation = queryDefaultTranslation(type->queryCharset(), srcCharset);
- if(translation)
- {
- StringBuffer translated;
- translation->translate(translated, srcLength, val);
- return createVarStringValue(srcLength, translated.str(), type);
- }
- else
- return createVarStringValue(srcLength, val, type);
- }
- int VarStringValue::rangeCompare(ITypeInfo * targetType)
- {
- //MORE - should probably check for unpadded length, and check the type as well.
- return 0;
- }
- //===========================================================================
- MemoryValue::MemoryValue(const void *v, ITypeInfo *_type) : CValue(_type)
- {
- assertex(_type->getSize() != UNKNOWN_LENGTH);
- val.set(_type->getSize(), v);
- }
- MemoryValue::MemoryValue(ITypeInfo *_type) : CValue(_type)
- {
- }
- void MemoryValue::toMem(void *target)
- {
- size32_t size = getSize();
- assertThrow(val.length()>=size);
- memcpy_iflen(target, val.get(), size);
- }
- unsigned MemoryValue::getHash(unsigned initval)
- {
- size32_t size = getSize();
- assertThrow(val.length()>=size);
- return hashc((unsigned char *) val.get(), size, initval);
- }
- int MemoryValue::compare(IValue * to)
- {
- ITypeInfo * toType = to->queryType();
- assertex(toType->getTypeCode()==type->getTypeCode());
- return rtlCompareDataData(type->getSize(), (const char *)val.get(), toType->getSize(), (const char *)to->queryValue());
- }
- int MemoryValue::compare(const void *mem)
- {
- return memcmp(val.get(), (const char *) mem, type->getSize());
- }
- const char *MemoryValue::generateCPP(StringBuffer &out, CompilerType compiler)
- {
- unsigned size = getSize();
- return appendStringAsQuotedCPP(out, size, (const char *)val.get(), size>120).str();
- }
- bool MemoryValue::getBoolValue()
- {
- //return true if any non zero character
- const char * cur = (const char *)val.get();
- unsigned len = getSize();
- while (len--)
- {
- if (*cur++ != 0)
- return true;
- }
- return false;
- }
- void MemoryValue::pushDecimalValue()
- {
- DecPushUlong(0);
- }
- void MemoryValue::serialize(MemoryBuffer &tgt)
- {
- size32_t serialLen = (size32_t)val.length();
- assertex(serialLen == val.length());
- tgt.append(serialLen);
- tgt.append(serialLen, val.get());
- }
- void MemoryValue::deserialize(MemoryBuffer &src)
- {
- size32_t size;
- src.read(size);
- void *mem = checked_malloc(size, DEFVALUE_MALLOC_FAILED);
- assertex(mem);
- src.read(size, mem);
- val.setOwn(size, mem);
- }
- int MemoryValue::rangeCompare(ITypeInfo * targetType)
- {
- //MORE - should probably check for unpadded length, and check the type as well.
- return 0;
- }
- //===========================================================================
- StringValue::StringValue(const char *v, ITypeInfo *_type) : MemoryValue(_type)
- {
- //store a null terminated string for ease of conversion
- unsigned len = _type->getSize();
- assertex(len != UNKNOWN_LENGTH);
- char * temp = (char *)val.allocate(len+1);
- memcpy_iflen(temp, v, len);
- temp[len] = 0;
- }
- const char *StringValue::generateECL(StringBuffer &out)
- {
- return appendStringAsQuotedECL(out, type->getSize(), (const char *)val.get()).str();
- }
- int StringValue::compare(IValue * to)
- {
- ITypeInfo * toType = to->queryType();
- assertex(toType->getTypeCode()==type->getTypeCode());
- return rtlCompareStrStr(type->getSize(), (const char *)val.get(), toType->getSize(), (const char *)to->queryValue());
- }
- int StringValue::compare(const void *mem)
- {
- // if (type->isCaseSensitive())
- return memcmp(val.get(), (const char *) mem, type->getSize());
- // return memicmp(val.get(), (const char *) mem, type->getSize());
- }
- IValue *StringValue::castTo(ITypeInfo *t)
- {
- t = queryUnqualifiedType(t);
- if (t==type)
- return LINK(this);
- type_t tc = t->getTypeCode();
- if (tc == type_any)
- return LINK(this);
- const char * str = (const char *)val.get();
- if (tc == type_data)
- return t->castFrom(type->getSize(), str); //NB: Must not go through translation in default case
- ICharsetInfo * srcCharset = type->queryCharset();
- Owned<ICharsetInfo> asciiCharset = getCharset(asciiAtom);
- if (queryDefaultTranslation(asciiCharset, srcCharset))
- {
- Owned<ITypeInfo> asciiType = getAsciiType(type);
- Owned<IValue> asciiValue = createStringValue(str, LINK(asciiType), type->getStringLen(), srcCharset);
- return asciiValue->castTo(t);
- }
- return doCastTo(type->getStringLen(), str, t);
- }
- const void *StringValue::queryValue() const
- {
- return (const void *) val.get();
- }
- bool StringValue::getBoolValue()
- {
- //return true if any character is non space
- //return true if any non zero character
- const char * cur = (const char *)val.get();
- unsigned len = type->getSize();
- char fill = type->queryCharset()->queryFillChar();
- while (len--)
- {
- if (*cur++ != fill)
- return true;
- }
- return false;
- }
- __int64 StringValue::getIntValue()
- {
- return rtlStrToInt8((size32_t)val.length(), (const char *)val.get());
- }
- const char *StringValue::getStringValue(StringBuffer &out)
- {
- out.append(type->getSize(), (const char *)val.get());
- return out.str();
- }
- const char *StringValue::getUTF8Value(StringBuffer &out)
- {
- ICharsetInfo * srcCharset = type->queryCharset();
- Owned<ICharsetInfo> asciiCharset = getCharset(asciiAtom);
- if (queryDefaultTranslation(asciiCharset, srcCharset))
- {
- Owned<ITypeInfo> asciiType = getAsciiType(type);
- Owned<IValue> asciiValue = castTo(asciiType);
- return asciiValue->getUTF8Value(out);
- }
- rtlDataAttr temp;
- unsigned bufflen;
- rtlStrToUtf8X(bufflen, temp.refstr(), type->getSize(), (const char *)val.get());
- out.append(rtlUtf8Size(bufflen, temp.getstr()), temp.getstr());
- return out.str();
- }
- void StringValue::pushDecimalValue()
- {
- DecPushString(type->getSize(),(char *)val.get());
- }
- IValue *createStringValue(const char *val, unsigned size)
- {
- return createStringValue(val, makeStringType(size, NULL, NULL));
- }
- IValue *createStringValue(const char *val, ITypeInfo *type)
- {
- assertex(type->getSize() != UNKNOWN_LENGTH);
- if (type->getSize() == UNKNOWN_LENGTH)
- {
- ITypeInfo * newType = getStretchedType((size32_t)strlen(val), type);
- type->Release();
- return new StringValue(val, newType);
- }
- return new StringValue(val, type);
- }
- IValue *createStringValue(const char *val, ITypeInfo *type, size32_t srcLength, ICharsetInfo *srcCharset)
- {
- ITranslationInfo * translation = queryDefaultTranslation(type->queryCharset(), srcCharset);
- size32_t tgtLength = type->getSize();
- if (tgtLength == UNKNOWN_LENGTH)
- {
- ITypeInfo * newType = getStretchedType(srcLength, type);
- type->Release();
- type = newType;
- tgtLength = srcLength;
- }
- if(translation)
- {
- StringBuffer translated;
- translation->translate(translated, srcLength, val);
- if(tgtLength > srcLength)
- translated.appendN(tgtLength-srcLength, type->queryCharset()->queryFillChar());
- return new StringValue(translated.str(), type);
- }
- else if (tgtLength > srcLength)
- {
- char * extended = (char *)checked_malloc(tgtLength, DEFVALUE_MALLOC_FAILED);
- memcpy_iflen(extended, val, srcLength);
- memset(extended+srcLength, type->queryCharset()->queryFillChar(), tgtLength-srcLength);
- IValue * ret = new StringValue(extended, type);
- free(extended);
- return ret;
- }
- else
- return new StringValue(val, type);
- }
- //==========================================================================
- UnicodeValue::UnicodeValue(UChar const * _value, ITypeInfo * _type) : MemoryValue(_type)
- {
- unsigned len = _type->getSize();
- assertex(len != UNKNOWN_LENGTH);
- val.set(len, _value);
- }
- const void *UnicodeValue::queryValue() const
- {
- return (const void *) val.get();
- }
- const char *UnicodeValue::generateCPP(StringBuffer &out, CompilerType compiler)
- {
- out.append("((UChar *)");
- MemoryValue::generateCPP(out, compiler);
- return out.append(")").str();
- }
- const char *UnicodeValue::generateECL(StringBuffer &out)
- {
- char * buff;
- unsigned bufflen;
- rtlUnicodeToUtf8X(bufflen, buff, type->getStringLen(), (UChar const *)val.get());
- out.append("U'");
- appendUtf8AsECL(out, rtlUtf8Size(bufflen, buff), buff);
- out.append('\'');
- rtlFree(buff);
- return out.str();
- }
- IValue *UnicodeValue::castTo(ITypeInfo *t)
- {
- t = queryUnqualifiedType(t);
- if (t==type)
- return LINK(this);
- type_t tc = t->getTypeCode();
- if (tc == type_any)
- return LINK(this);
- size32_t olen = type->getStringLen();
- UChar const * uchars = (const UChar *)val.get();
- switch (tc)
- {
- case type_unicode:
- if ((t->getSize() == UNKNOWN_LENGTH) && (type->queryLocale() == t->queryLocale()))
- return LINK(this);
- return createUnicodeValue(uchars, olen, LINK(t));
- case type_string:
- {
- char * buff;
- unsigned bufflen;
- rtlUnicodeToCodepageX(bufflen, buff, olen, uchars, t->queryCharset()->queryCodepageName());
- Owned<IValue> temp = createStringValue(buff, makeStringType(bufflen, LINK(t->queryCharset()), 0));
- rtlFree(buff);
- return temp->castTo(t);
- }
- }
- return t->castFrom(olen, uchars);
- }
- int UnicodeValue::compare(IValue * to)
- {
- ITypeInfo * toType = to->queryType();
- assertex(toType->getTypeCode()==type->getTypeCode());
- assertex(haveCommonLocale(type, toType));
- char const * locale = str(getCommonLocale(type, toType));
- return rtlCompareUnicodeUnicode(type->getStringLen(), (const UChar *)val.get(), toType->getStringLen(), (const UChar *)to->queryValue(), locale);
- }
- int UnicodeValue::compare(const void *mem)
- {
- size32_t len = type->getStringLen();
- return rtlCompareUnicodeUnicode(len, (const UChar *)val.get(), len, (const UChar *)mem, str(type->queryLocale()));
- }
- bool UnicodeValue::getBoolValue()
- {
- return rtlUnicodeToBool(type->getStringLen(), (UChar const *)val.get());
- }
- __int64 UnicodeValue::getIntValue()
- {
- return rtlUnicodeToInt8(type->getStringLen(), (UChar const *)val.get());
- }
- void UnicodeValue::pushDecimalValue()
- {
- rtlDecPushUnicode(type->getStringLen(), (UChar const *)val.get());
- }
- const char *UnicodeValue::getStringValue(StringBuffer &out)
- {
- return getCodepageValue(out, "US-ASCII");
- }
- void * UnicodeValue::getUCharStringValue(unsigned len, void * out)
- {
- size_t vallen = val.length()/2;
- if(vallen > len)
- vallen = len;
- memcpy_iflen(out, val.get(), vallen*2);
- if(len > vallen)
- ((UChar *)out)[vallen] = 0x0000;
- return out;
- }
- const char *UnicodeValue::getUTF8Value(StringBuffer &out)
- {
- return getCodepageValue(out, "UTF-8");
- }
- const char *UnicodeValue::getCodepageValue(StringBuffer &out, char const * codepage)
- {
- char * buff;
- unsigned bufflen;
- rtlUnicodeToCodepageX(bufflen, buff, (size32_t)(val.length()/2), (UChar const *)val.get(), codepage);
- out.append(bufflen, buff);
- rtlFree(buff);
- return out.str();
- }
- IValue *createUnicodeValue(char const * value, unsigned size, char const * locale, bool utf8, bool unescape)
- {
- UChar * buff = 0;
- unsigned bufflen = 0;
- if(unescape)
- rtlCodepageToUnicodeXUnescape(bufflen, buff, size, value, utf8 ? "UTF-8" : "US-ASCII");
- else
- rtlCodepageToUnicodeX(bufflen, buff, size, value, utf8 ? "UTF-8" : "US-ASCII");
- IValue * ret = new UnicodeValue(buff, makeUnicodeType(bufflen, createLowerCaseAtom(locale)));
- rtlFree(buff);
- return ret;
- }
- IValue *createUtf8Value(size32_t len, char const * value, char const * locale, bool unescape)
- {
- if (unescape)
- {
- rtlDataAttr temp;
- size32_t newlen = 0;
- size32_t size = rtlUtf8Size(len, value);
- rtlCodepageToUtf8XUnescape(newlen, temp.refstr(), size, value, "UTF-8");
- ITypeInfo * type = makeUtf8Type(newlen, createLowerCaseAtom(locale));
- return createUtf8Value(temp.getstr(), type);
- }
- else
- {
- ITypeInfo * type = makeUtf8Type(len, createLowerCaseAtom(locale));
- return createUtf8Value(value, type);
- }
- }
- IValue *createUnicodeValue(char const * value, ITypeInfo * type)
- {
- if(type->getSize() == UNKNOWN_LENGTH)
- {
- type->Release();
- return createUnicodeValue(value, (size32_t)strlen(value), str(type->queryLocale()), false);
- }
- return createUnicodeValue(value, type, type->getStringLen());
- }
- IValue *createUnicodeValue(char const * value, ITypeInfo * type, unsigned srclen)
- {
- if(type->getSize() == UNKNOWN_LENGTH)
- {
- type->Release();
- return createUnicodeValue(value, srclen, str(type->queryLocale()), false);
- }
- UChar * buff = (UChar *)checked_malloc(type->getSize(), DEFVALUE_MALLOC_FAILED);
- rtlCodepageToUnicode(type->getStringLen(), buff, srclen, value, "US-ASCII");
- IValue * ret = new UnicodeValue(buff, type);
- free(buff);
- return ret;
- }
- IValue * createUnicodeValue(UChar const * text, size32_t len, ITypeInfo * type)
- {
- size32_t nlen = type->getStringLen();
- if(nlen == UNKNOWN_LENGTH)
- {
- ITypeInfo * newType = getStretchedType(len, type);
- type->Release();
- type = newType;
- nlen = len;
- }
- if (nlen <= len)
- return new UnicodeValue(text, type);
- UChar * buff = 0;
- unsigned bufflen = 0;
- rtlUnicodeSubStrFTX(bufflen, buff, len, text, 1, nlen);
- assertex(bufflen == nlen);
- IValue * ret = new UnicodeValue(buff, type);
- rtlFree(buff);
- return ret;
- }
- IValue * createUnicodeValue(size32_t len, const void * text, ITypeInfo * type)
- {
- return createUnicodeValue((const UChar *)text, len, type);
- }
- //===========================================================================
- void UnicodeAttr::set(UChar const * _text, unsigned _len)
- {
- free(text);
- text = (UChar *) checked_malloc((_len+1)*2, DEFVALUE_MALLOC_FAILED);
- memcpy_iflen(text, _text, _len*2);
- text[_len] = 0x0000;
- }
- void UnicodeAttr::setown(UChar * _text)
- {
- free(text);
- text = _text;
- }
- VarUnicodeValue::VarUnicodeValue(unsigned len, const UChar * v, ITypeInfo * _type) : CValue(_type)
- {
- unsigned typeLen = type->getStringLen();
- assertex(typeLen != UNKNOWN_LENGTH);
- if (len >= typeLen)
- val.set(v, typeLen);
- else
- {
- UChar * temp = (UChar *)checked_malloc((typeLen+1)*2, DEFVALUE_MALLOC_FAILED);
- memcpy_iflen(temp, v, len*2);
- temp[len] = 0;
- val.set(temp, typeLen);
- free(temp);
- }
- }
- const void * VarUnicodeValue::queryValue() const
- {
- return val.get();
- }
- const char * VarUnicodeValue::generateCPP(StringBuffer & out, CompilerType compiler)
- {
- out.append("(UChar *)");
- unsigned len = val.length()*2+1; // not pretty, but adds one null, so that string is double-null-terminated as required
- return appendStringAsQuotedCPP(out, len, (char const *)val.get(), len>120).str();
- }
- const char * VarUnicodeValue::generateECL(StringBuffer & out)
- {
- char * buff;
- unsigned bufflen;
- rtlUnicodeToUtf8X(bufflen, buff, val.length(), val.get());
- out.append("U'");
- appendUtf8AsECL(out, rtlUtf8Size(bufflen, buff), buff);
- out.append('\'');
- rtlFree(buff);
- return out.str();
- }
- IValue * VarUnicodeValue::castTo(ITypeInfo * t)
- {
- t = queryUnqualifiedType(t);
- if (t==type)
- return LINK(this);
- type_t tc = t->getTypeCode();
- if (tc == type_any)
- return LINK(this);
- size32_t olen = val.length();
- UChar const * uchars = (UChar const *)val.get();
- switch (tc)
- {
- case type_string:
- {
- char * buff;
- unsigned bufflen;
- rtlUnicodeToCodepageX(bufflen, buff, olen, uchars, t->queryCharset()->queryCodepageName());
- Owned<IValue> temp = createStringValue(buff, makeStringType(bufflen, LINK(t->queryCharset()), 0));
- rtlFree(buff);
- return temp->castTo(t);
- }
- }
- return t->castFrom(olen, uchars);
- }
- int VarUnicodeValue::compare(IValue * to)
- {
- ITypeInfo * toType = to->queryType();
- assertex(toType->getTypeCode()==type->getTypeCode());
- assertex(haveCommonLocale(type, toType));
- char const * locale = str(getCommonLocale(type, toType));
- UChar const * rhs = (UChar const *) to->queryValue();
- return rtlCompareUnicodeUnicode(val.length(), val.get(), rtlUnicodeStrlen(rhs), rhs, locale);
- }
- int VarUnicodeValue::compare(const void * mem)
- {
- UChar const * rhs = (UChar const *) mem;
- return rtlCompareUnicodeUnicode(val.length(), val.get(), rtlUnicodeStrlen(rhs), rhs, str(type->queryLocale()));
- }
- bool VarUnicodeValue::getBoolValue()
- {
- return rtlUnicodeToBool(val.length(), val.get());
- }
- __int64 VarUnicodeValue::getIntValue()
- {
- return rtlUnicodeToInt8(val.length(), val.get());
- }
- const char * VarUnicodeValue::getStringValue(StringBuffer & out)
- {
- return getCodepageValue(out, "US-ASCII");
- }
- void * VarUnicodeValue::getUCharStringValue(unsigned len, void * out)
- {
- unsigned vallen = val.length();
- if(vallen > len)
- vallen = len;
- memcpy_iflen(out, val.get(), vallen*2);
- if(len > vallen)
- ((UChar *)out)[vallen] = 0x0000;
- return out;
- }
- const char * VarUnicodeValue::getUTF8Value(StringBuffer & out)
- {
- return getCodepageValue(out, "UTF-8");
- }
- const char * VarUnicodeValue::getCodepageValue(StringBuffer & out, char const * codepage)
- {
- char * buff;
- unsigned bufflen;
- rtlUnicodeToCodepageX(bufflen, buff, val.length(), val.get(), codepage);
- out.append(bufflen, buff);
- rtlFree(buff);
- return out.str();
- }
- void VarUnicodeValue::pushDecimalValue()
- {
- rtlDecPushUnicode(val.length(), val.get());
- }
- void VarUnicodeValue::toMem(void * target)
- {
- memcpy(target, val.get(), (val.length()+1)*2);
- }
- unsigned VarUnicodeValue::getHash(unsigned initval)
- {
- return hashc((unsigned char *) val.get(), val.length()*2, initval);
- }
- int VarUnicodeValue::rangeCompare(ITypeInfo * targetType)
- {
- return 0;
- }
- void VarUnicodeValue::serialize(MemoryBuffer & tgt)
- {
- tgt.append(val.length());
- tgt.append(val.length()*2, val.get());
- }
- void VarUnicodeValue::deserialize(MemoryBuffer & src)
- {
- size32_t len;
- src.read(len);
- UChar * buff = (UChar *) checked_malloc(len*2, DEFVALUE_MALLOC_FAILED);
- src.read(len*2, buff);
- val.setown(buff);
- }
- IValue *createVarUnicodeValue(char const * value, unsigned size, char const * locale, bool utf8, bool unescape)
- {
- UChar * buff = 0;
- unsigned bufflen = 0;
- if(unescape)
- rtlCodepageToUnicodeXUnescape(bufflen, buff, size, value, utf8 ? "UTF-8" : "US-ASCII");
- else
- rtlCodepageToUnicodeX(bufflen, buff, size, value, utf8 ? "UTF-8" : "US-ASCII");
- IValue * ret = new VarUnicodeValue(bufflen, buff, makeUnicodeType(bufflen, createLowerCaseAtom(locale)));
- rtlFree(buff);
- return ret;
- }
- IValue * createVarUnicodeValue(UChar const * text, size32_t len, ITypeInfo * type)
- {
- size32_t nlen = type->getStringLen();
- if(nlen == UNKNOWN_LENGTH)
- {
- ITypeInfo * newType = getStretchedType(len, type);
- type->Release();
- type = newType;
- nlen = len;
- }
- if(nlen > len)
- nlen = len;
- UChar * buff = 0;
- unsigned bufflen = 0;
- rtlUnicodeSubStrFTX(bufflen, buff, len, text, 1, nlen);
- assertex(bufflen == nlen);
- IValue * ret = new VarUnicodeValue(bufflen, buff, type);
- rtlFree(buff);
- return ret;
- }
- IValue * createVarUnicodeValue(size32_t len, const void * text, ITypeInfo * type)
- {
- return createVarUnicodeValue((UChar const *)text, len, type);
- }
- //==========================================================================
- Utf8Value::Utf8Value(const char * _value, ITypeInfo * _type) : MemoryValue(_type)
- {
- unsigned len = _type->getStringLen();
- assertex(len != UNKNOWN_LENGTH);
- unsigned size = rtlUtf8Size(len, _value);
- val.set(size, _value);
- }
- size32_t Utf8Value::getSize()
- {
- #ifdef _DEBUG
- unsigned size = rtlUtf8Size(type->getStringLen(), val.get());
- assertex(size == val.length());
- #endif
- return (size32_t)val.length();
- }
- const void *Utf8Value::queryValue() const
- {
- return (const void *) val.get();
- }
- const char *Utf8Value::generateCPP(StringBuffer &out, CompilerType compiler)
- {
- out.append("((char *)");
- MemoryValue::generateCPP(out, compiler);
- return out.append(")").str();
- }
- const char *Utf8Value::generateECL(StringBuffer &out)
- {
- unsigned size = rtlUtf8Size(type->getStringLen(), val.get());
- out.append("U");
- appendStringAsQuotedECL(out, size, (const char *)val.get());
- return out.str();
- }
- IValue *Utf8Value::castTo(ITypeInfo *t)
- {
- t = queryUnqualifiedType(t);
- if (t==type)
- return LINK(this);
- type_t tc = t->getTypeCode();
- if (tc == type_any)
- return LINK(this);
- size32_t olen = type->getStringLen();
- const char * data = (const char *)val.get();
- switch(tc)
- {
- case type_unicode:
- case type_varunicode:
- {
- Owned<IValue> temp = createUnicodeValue(data, rtlUtf8Size(olen, data), str(t->queryLocale()), true, false);
- return temp->castTo(t);
- }
- case type_utf8:
- {
- unsigned targetLength = t->getStringLen();
- if (targetLength == UNKNOWN_LENGTH)
- {
- if (type->queryLocale() == t->queryLocale())
- return LINK(this);
- }
- return createUtf8Value(olen, data, LINK(t));
- }
- case type_string:
- {
- rtlDataAttr buff;
- unsigned bufflen;
- rtlUtf8ToCodepageX(bufflen, buff.refstr(), olen, data, t->queryCharset()->queryCodepageName());
- Owned<IValue> temp = createStringValue(buff.getstr(), makeStringType(bufflen, LINK(t->queryCharset()), 0));
- return temp->castTo(t);
- }
- case type_data:
- {
- unsigned size = rtlUtf8Size(olen, data);
- if (size >= t->getSize())
- return createDataValue(data, LINK(t));
- Owned<IValue> temp = createDataValue(data, size);
- return temp->castTo(t);
- }
- }
- Owned<ITypeInfo> stringType = makeStringType(UNKNOWN_LENGTH, LINK(t->queryCharset()), LINK(t->queryCollation()));
- Owned<IValue> stringValue = castTo(stringType);
- return stringValue->castTo(t);
- }
- int Utf8Value::compare(IValue * to)
- {
- ITypeInfo * toType = to->queryType();
- assertex(toType->getTypeCode()==type->getTypeCode());
- assertex(haveCommonLocale(type, toType));
- char const * locale = str(getCommonLocale(type, toType));
- return rtlCompareUtf8Utf8(type->getStringLen(), (const char *)val.get(), toType->getStringLen(), (const char *)to->queryValue(), locale);
- }
- int Utf8Value::compare(const void *mem)
- {
- size32_t len = type->getStringLen();
- return rtlCompareUtf8Utf8(len, (const char *)val.get(), len, (const char *)mem, str(type->queryLocale()));
- }
- bool Utf8Value::getBoolValue()
- {
- return rtlUtf8ToBool(type->getStringLen(), (const char *)val.get());
- }
- __int64 Utf8Value::getIntValue()
- {
- return rtlUtf8ToInt(type->getStringLen(), (const char *)val.get());
- }
- void Utf8Value::pushDecimalValue()
- {
- rtlDecPushUtf8(type->getStringLen(), (const char *)val.get());
- }
- const char * Utf8Value::getStringValue(StringBuffer &out)
- {
- return getCodepageValue(out, "US-ASCII");
- }
- const char * Utf8Value::getCodepageValue(StringBuffer &out, char const * codepage)
- {
- unsigned bufflen;
- rtlDataAttr buff;
- rtlUtf8ToCodepageX(bufflen, buff.refstr(), type->getStringLen(), (const char *)val.get(), codepage);
- out.append(bufflen, buff.getstr());
- return out.str();
- }
- void * Utf8Value::getUCharStringValue(unsigned len, void * _out)
- {
- unsigned thisLen = type->getStringLen();
- UChar * out = (UChar *)_out;
- rtlUtf8ToUnicode(len, out, thisLen, (const char *)val.get());
- //wierd semantics.
- if (len > thisLen)
- out[thisLen] = 0x0000;
- return out;
- }
- const char *Utf8Value::getUTF8Value(StringBuffer &out)
- {
- unsigned size = rtlUtf8Size(type->getStringLen(), val.get());
- return out.append(size, (const char *)val.get()).str();
- }
- IValue * createUtf8Value(const char * text, ITypeInfo * type)
- {
- return new Utf8Value(text, type);
- }
- IValue * createUtf8Value(size32_t len, const char * text, ITypeInfo * type)
- {
- size32_t nlen = type->getStringLen();
- if(nlen == UNKNOWN_LENGTH)
- {
- ITypeInfo * newType = getStretchedType(len, type);
- type->Release();
- type = newType;
- nlen = len;
- }
- if (nlen <= len)
- return createUtf8Value(text, type);
- rtlDataAttr buff(nlen * 4);
- rtlUtf8SubStrFT(nlen, buff.getstr(), len, text, 1, nlen);
- return new Utf8Value(buff.getstr(), type);
- }
- //===========================================================================
- DataValue::DataValue(const void *v, ITypeInfo *_type) : MemoryValue(v, _type)
- {
- }
- const char *DataValue::generateECL(StringBuffer &out)
- {
- out.append("X'");
- appendDataAsHex(out, (size32_t)val.length(), val.get());
- out.append('\'');
- return out.str();
- }
- IValue *DataValue::castTo(ITypeInfo *t)
- {
- t = queryUnqualifiedType(t);
- if (t==type)
- return LINK(this);
- type_t tc = t->getTypeCode();
- if (tc == type_any)
- return LINK(this);
- size32_t osize = type->getSize();
- size32_t nsize = t->getSize();
- switch (tc)
- {
- case type_data:
- if (nsize == UNKNOWN_LENGTH)
- return LINK(this);
- if (nsize <= osize)
- return new DataValue(val.get(), LINK(t));
- else
- {
- char *newstr = (char *) checked_malloc(nsize, DEFVALUE_MALLOC_FAILED);
- memcpy_iflen(newstr, val.get(), osize);
- memset(newstr+osize, 0, nsize-osize);
- IValue * ret = new DataValue(newstr, LINK(t));
- free(newstr);
- return ret;
- }
- break;
- case type_string:
- if (nsize == UNKNOWN_LENGTH)
- {
- nsize = osize;
- t = getStretchedType(osize, t);
- }
- else
- t->Link();
- if (nsize <= osize)
- return new StringValue((char *)val.get(), t);
- else
- {
- char *newstr = (char *) checked_malloc(nsize, DEFVALUE_MALLOC_FAILED);
- memcpy_iflen(newstr, val.get(), osize);
- memset(newstr+osize, t->queryCharset()->queryFillChar(), nsize-osize);
- IValue * ret = new StringValue(newstr, t);
- free(newstr);
- return ret;
- }
- break;
- case type_decimal:
- return t->castFrom(osize, (const char *)val.get());
- case type_boolean:
- return createBoolValue(getBoolValue());
- }
- ITypeInfo * tempType = makeStringType(getSize(), NULL, NULL);
- IValue * temp = castTo(tempType);
- IValue * ret = temp->castTo(t);
- temp->Release();
- tempType->Release();
- return ret;
- }
- const void *DataValue::queryValue() const
- {
- return val.get();
- }
- bool DataValue::getBoolValue()
- {
- //return true if any character is non space
- //return true if any non zero character
- const char * cur = (const char *)val.get();
- unsigned len = type->getSize();
- char fill = 0;
- while (len--)
- {
- if (*cur++ != fill)
- return true;
- }
- return false;
- }
- const char *DataValue::getStringValue(StringBuffer &out)
- {
- appendDataAsHex(out, (size32_t)val.length(), val.get());
- return out.str();
- }
- IValue *createDataValue(const char *val, unsigned size)
- {
- return createDataValue(val, makeDataType(size));
- }
- IValue *createDataValue(const char *val, ITypeInfo *type)
- {
- assertex(type->getSize() != UNKNOWN_LENGTH);
- return new DataValue(val, type);
- }
- IValue *createDataValue(const char *val, ITypeInfo *type, size32_t srcLen)
- {
- size32_t targetSize = type->getSize();
- assertex(targetSize != UNKNOWN_LENGTH);
- if (srcLen >= targetSize)
- return new DataValue(val, type);
- MemoryAttr stretched(targetSize);
- rtlDataToData(targetSize, stretched.mem(), srcLen, val);
- return new DataValue(stretched.get(), type);
- }
- //===========================================================================
- QStringValue::QStringValue(unsigned len, const void *v, ITypeInfo *_type) : MemoryValue(_type)
- {
- unsigned newSize = _type->getSize();
- char * temp = (char *)val.allocate(newSize);
- rtlStrToQStr(_type->getStringLen(), temp, len, v);
- }
- QStringValue::QStringValue(const char *v, ITypeInfo *_type) : MemoryValue(_type)
- {
- unsigned newSize = _type->getSize();
- char * temp = (char *)val.allocate(newSize);
- memcpy_iflen(temp, v, newSize);
- }
- const char *QStringValue::generateECL(StringBuffer &out)
- {
- unsigned strLen = type->getStringLen();
- char * strData = (char *)checked_malloc(strLen, DEFVALUE_MALLOC_FAILED);
- rtlQStrToStr(strLen, strData, strLen, (const char *)val.get());
- out.append('Q');
- appendStringAsQuotedECL(out, strLen, strData);
- free(strData);
- return out.str();
- }
- int QStringValue::compare(IValue * to)
- {
- return rtlCompareQStrQStr(type->getStringLen(), val.get(), to->queryType()->getStringLen(), to->queryValue());
- }
- int QStringValue::compare(const void *mem)
- {
- size32_t len = type->getStringLen();
- return rtlCompareQStrQStr(len, val.get(), len, mem);
- }
- IValue *QStringValue::castTo(ITypeInfo *t)
- {
- t = queryUnqualifiedType(t);
- if (t==type)
- return LINK(this);
- type_t tc = t->getTypeCode();
- if (tc == type_any)
- return LINK(this);
- switch (tc)
- {
- case type_boolean:
- return createBoolValue(getBoolValue());
- }
- unsigned strLen = type->getStringLen();
- char * strData = (char *)checked_malloc(strLen, DEFVALUE_MALLOC_FAILED);
- rtlQStrToStr(strLen, strData, strLen, (const char *)val.get());
- IValue * ret = t->castFrom(strLen, strData);
- free(strData);
- return ret;
- }
- const void *QStringValue::queryValue() const
- {
- return (const void *) val.get();
- }
- bool QStringValue::getBoolValue()
- {
- //return true if any character is non space
- //return true if any non zero character
- const char * cur = (const char *)val.get();
- unsigned len = type->getSize();
- while (len--)
- {
- if (*cur++)
- return true;
- }
- return false;
- }
- __int64 QStringValue::getIntValue()
- {
- unsigned strLen = type->getStringLen();
- char * strData = (char *)checked_malloc(strLen, DEFVALUE_MALLOC_FAILED);
- rtlQStrToStr(strLen, strData, strLen, (const char *)val.get());
- __int64 ret = rtlStrToInt8(strLen, strData);
- free(strData);
- return ret;
- }
- const char *QStringValue::getStringValue(StringBuffer &out)
- {
- unsigned strLen = type->getStringLen();
- char * strData = (char *)checked_malloc(strLen, DEFVALUE_MALLOC_FAILED);
- rtlQStrToStr(strLen, strData, strLen, (const char *)val.get());
- out.append(strLen, strData);
- free(strData);
- return out.str();
- }
- void QStringValue::pushDecimalValue()
- {
- unsigned strLen = type->getStringLen();
- char * strData = (char *)checked_malloc(strLen, DEFVALUE_MALLOC_FAILED);
- rtlQStrToStr(strLen, strData, strLen, (const char *)val.get());
- DecPushString(strLen, strData);
- free(strData);
- }
- IValue *createQStringValue(unsigned len, const char *val, ITypeInfo *type)
- {
- if (type->getSize() == UNKNOWN_LENGTH)
- {
- ITypeInfo * newType = getStretchedType(len, type);
- type->Release();
- return new QStringValue(len, val, newType);
- }
- return new QStringValue(len, val, type);
- }
- //===========================================================================
- CharValue::CharValue(char _val, ITypeInfo * _type) : CValue(_type)
- {
- val = _val;
- }
- const char *CharValue::generateECL(StringBuffer &out)
- {
- appendStringAsQuotedECL(out, 1, &val);
- return out.str();
- }
- const char *CharValue::generateCPP(StringBuffer &out, CompilerType compiler)
- {
- switch (val)
- {
- case '\n': out.append("'\\n'"); break;
- case '\r': out.append("'\\r'"); break;
- case '\t': out.append("'\\t'"); break;
- case '\'': out.append("'\\''"); break;
- default:
- if ((val >= ' ') && (val <= 126))
- out.append('\'').append(val).append('\'');
- else
- out.append((int)val);
- break;
- }
- return out.str();
- }
- void CharValue::toMem(void *target)
- {
- *(char *)target = val;
- }
- unsigned CharValue::getHash(unsigned initval)
- {
- char buf = val;;
- return hashc((unsigned char *)&buf, 1, initval);
- }
- int CharValue::compare(IValue *_to)
- {
- assertThrow(_to->queryType()==type);
- CharValue *to = (CharValue *) _to;
- return memcmp(&val, &to->val, 1);
- }
- int CharValue::compare(const void *mem)
- {
- return memcmp(&val, (const char *) mem, 1);
- }
- IValue *CharValue::castTo(ITypeInfo *t)
- {
- t = queryUnqualifiedType(t);
- if (t==type)
- return LINK(this);
- type_t tc = t->getTypeCode();
- if (tc == type_any)
- return LINK(this);
- switch (tc)
- {
- case type_boolean:
- return createBoolValue(val != 0);
- }
- return t->castFrom(1, &val);
- }
- bool CharValue::getBoolValue()
- {
- return (val != ' ');
- }
- const char *CharValue::getStringValue(StringBuffer &out)
- {
- return out.append(val).str();
- }
- void CharValue::pushDecimalValue()
- {
- DecPushString(1,&val);
- }
- void CharValue::serialize(MemoryBuffer &tgt)
- {
- tgt.append(val);
- }
- void CharValue::deserialize(MemoryBuffer &src)
- {
- src.read(val);
- }
- int CharValue::rangeCompare(ITypeInfo * targetType)
- {
- //MORE: to integegral types?
- return 0;
- }
- IValue *createCharValue(char val, bool caseSensitive)
- {
- return createCharValue(val, makeCharType(caseSensitive));
- }
- IValue *createCharValue(char val, ITypeInfo *type)
- {
- return new CharValue(val, type);
- }
- //===========================================================================
- int IntValue::compare(IValue *_to)
- {
- assertThrow(_to->queryType()==type);
- IntValue *to = (IntValue *) _to;
- if (val == to->val)
- return 0;
- else if (type->isSigned())
- return (__int64) val > (__int64) to->val ? 1 : -1;
- else
- return val > to->val ? 1 : -1;
- }
- IValue * IntValue::castViaString(ITypeInfo * t)
- {
- size32_t len;
- char * text;
- if (type->isSigned())
- rtlInt8ToStrX(len, text, val);
- else
- rtlUInt8ToStrX(len, text, (unsigned __int64)val);
- IValue * ret = t->castFrom(len, text); // Include EBCDIC conversion, and creating correct type
- rtlFree(text);
- return ret;
- }
- IValue *IntValue::castTo(ITypeInfo *t)
- {
- t = queryUnqualifiedType(t);
- if (t==type)
- return LINK(this);
- type_t tc = t->getTypeCode();
- if (tc == type_any)
- return LINK(this);
- unsigned nLen = t->getStringLen();
- switch (tc)
- {
- case type_qstring:
- case type_utf8:
- case type_unicode:
- case type_varstring:
- case type_varunicode:
- return castViaString(t);
- case type_string:
- {
- if (nLen == UNKNOWN_LENGTH)
- return castViaString(t);
- char *newstr = (char *) checked_malloc(nLen, DEFVALUE_MALLOC_FAILED);
- if (type->isSigned())
- rtlInt8ToStr(nLen, newstr, val);
- else
- rtlUInt8ToStr(nLen, newstr, (unsigned __int64)val);
- IValue * ret = t->castFrom(nLen, newstr);
- free(newstr);
- return ret;
- }
- case type_int:
- case type_swapint:
- return createTruncIntValue(val, LINK(t));
- case type_boolean:
- return createBoolValue(val!=0);
- }
- return t->castFrom(type->isSigned(), (__int64)val);
- }
- byte * IntValue::getAddressValue()
- {
- #if __BYTE_ORDER == __LITTLE_ENDIAN
- return (byte *)&val;
- #else
- return (byte *)&val + (8 - type->getSize());
- #endif
- }
- const void * IntValue::queryValue() const
- {
- #if __BYTE_ORDER == __LITTLE_ENDIAN
- return reinterpret_cast<const byte *>(&val);
- #else
- return reinterpret_cast<const byte *>(&val) + (8 - type->getSize());
- #endif
- }
- void IntValue::toMem(void *target)
- {
- size32_t size = type->getSize();
- const byte * data = getAddressValue();
-
- if (type->isSwappedEndian())
- _cpyrevn(target, data, size);
- else
- memcpy_iflen(target, data, size);
- }
- unsigned IntValue::getHash(unsigned initval)
- {
- return hashc(getAddressValue(), type->getSize(), initval);
- }
- const char *IntValue::generateECL(StringBuffer &s)
- {
- return getStringValue(s);
- }
- static void generateUnsignedCPP(StringBuffer &s, __uint64 val, unsigned size, CompilerType compiler)
- {
- s.append(val);
- switch (compiler)
- {
- case GccCppCompiler:
- if (val && (size > sizeof(unsigned)))
- s.append("LLU");
- else
- s.append("U");
- break;
- case Vs6CppCompiler:
- s.append("U");
- if (val && (size > sizeof(unsigned)))
- s.append("i64");
- break;
- default:
- throwUnexpected();
- }
- }
- static void generateSignedCPP(StringBuffer &s, __int64 val, unsigned size, CompilerType compiler)
- {
- // Special case needed for MININT etc
- if (val && (size > sizeof(unsigned)))
- {
- if (val == LLONG_MIN)
- s.append("LLONG_MIN");
- else
- {
- s.append(val);
- switch (compiler)
- {
- case GccCppCompiler:
- s.append("LL");
- break;
- case Vs6CppCompiler:
- s.append("i64");
- break;
- default:
- throwUnexpected();
- }
- }
- }
- else
- {
- if (val == INT_MIN)
- s.append("INT_MIN");
- else
- s.append(val);
- }
- }
- const char *IntValue::generateCPP(StringBuffer &s, CompilerType compiler)
- {
- unsigned size = type->getSize();
- if (type->isSwappedEndian())
- {
- if (type->isSigned())
- generateSignedCPP(s, rtlReadSwapInt(getAddressValue(), size), size, compiler);
- else
- generateUnsignedCPP(s, rtlReadSwapUInt(getAddressValue(), size), size, compiler);
- }
- else
- {
- if (type->isSigned())
- generateSignedCPP(s, (__int64) val, size, compiler);
- else
- generateUnsignedCPP(s, val, size, compiler);
- }
- return s.str();
- }
- const char *IntValue::getStringValue(StringBuffer &s)
- {
- if (type->isSigned())
- s.append((__int64)val);
- else
- s.append(val);
- return s.str();
- }
- bool IntValue::getBoolValue()
- {
- return val != 0;
- }
- __int64 IntValue::getIntValue()
- {
- return val;
- }
- void IntValue::pushDecimalValue(void)
- {
- if (type->isSigned())
- DecPushInt64(val);
- else
- DecPushUInt64(val);
- }
- int IntValue::rangeCompare(ITypeInfo * targetType)
- {
- if (type->isSigned())
- return ::rangeCompare((double)(__int64)val, targetType);
- return ::rangeCompare((double)(unsigned __int64)val, targetType);
- }
- void IntValue::serialize(MemoryBuffer &tgt)
- {
- tgt.append(val);
- }
- void IntValue::deserialize(MemoryBuffer &src)
- {
- src.read(val);
- }
- //===========================================================================
- int PackedIntValue::compare(IValue *_to)
- {
- assertThrow(_to->queryType()==type);
- unsigned __int64 val = value->getIntValue();
- unsigned __int64 toVal = _to->getIntValue();
- if (val == toVal)
- return 0;
- else if (type->isSigned())
- return (__int64) val > (__int64) toVal ? 1 : -1;
- else
- return val > toVal ? 1 : -1;
- }
- void PackedIntValue::toMem(void *target)
- {
- unsigned __int64 val = value->getIntValue();
- if (type->isSigned())
- rtlSetPackedSigned(target, val);
- else
- rtlSetPackedUnsigned(target, val);
- }
- //---------------------------------------------------------------------------
- bool isInRange(__uint64 value, bool isSigned, unsigned size)
- {
- if (isSigned)
- value += (maxIntValue[size]+1);
- if (value & ~maxUIntValue[size])
- return false;
- return true;
- }
- inline unsigned getMinSize(unsigned __int64 value, bool isSigned)
- {
- if (isSigned)
- {
- if ((value + 0x80) <= 0xff) return 1;
- if ((value + 0x8000) <= 0xffff) return 2;
- if ((value + 0x80000000) <= 0xffffffff) return 4;
- }
- else
- {
- if (value <= 0xff) return 1;
- if (value <= 0xffff) return 2;
- if (value <= 0xffffffff) return 4;
- }
- return 8;
- }
- unsigned getMinimumIntegerSize(unsigned __int64 value, bool isSigned)
- {
- return getMinSize(value, isSigned);
- }
- static unsigned __int64 truncateInt(unsigned __int64 value, unsigned size, bool isSigned)
- {
- if (isSigned)
- {
- unsigned shift = (8-size)*8;
- value <<= shift;
- value = ((__int64)value) >> shift;
- }
- else
- {
- value &= maxUIntValue[size];
- }
- return value;
- }
- IValue *createIntValue(__int64 val, ITypeInfo * type)
- {
- #ifdef _DEBUG
- if (!isInRange(val, type->isSigned(), type->getSize()))
- throw MakeStringException(1, "Out of range value");
- #endif
- return new IntValue(val, type);
- }
- IValue *createIntValue(__int64 val, unsigned size, bool isSigned)
- {
- #ifdef _DEBUG
- if (!isInRange(val, isSigned, size))
- throw MakeStringException(0, "Out of range value");
- #endif
- ITypeInfo * type = makeIntType(size, isSigned);
- return new IntValue(val, type);
- }
- IValue *createTruncIntValue(__int64 val, ITypeInfo * type)
- {
- if (type->getTypeCode() == type_packedint)
- return createPackedIntValue(val, type);
- return new IntValue(truncateInt(val, type->getSize(), type->isSigned()), type);
- }
- IValue *createPackedIntValue(__int64 val, ITypeInfo * type)
- {
- assertex(type->getTypeCode() == type_packedint);
- ITypeInfo * promotedType = type->queryPromotedType();
- IValue * value = createTruncIntValue(val, LINK(promotedType));
- return new PackedIntValue(value, type);
- }
- IValue *createPackedIntValue(IValue * val, ITypeInfo * type)
- {
- assertex(type->getTypeCode() == type_packedint);
- return new PackedIntValue(val, type);
- }
- IValue *createTruncIntValue(__int64 val, unsigned size, bool isSigned)
- {
- val = truncateInt(val, size, isSigned);
- ITypeInfo * type = makeIntType(size, isSigned);
- return new IntValue(val, type);
- }
- IValue * createMinIntValue(__int64 value)
- {
- return createIntValue(value, getMinSize(value, true), true);
- }
- IValue * createMinUIntValue(unsigned __int64 value)
- {
- return createIntValue(value, getMinSize(value, false), false);
- }
- IValue *createBitfieldValue(__int64 val, ITypeInfo * type)
- {
- #ifdef _DEBUG
- size32_t bitsize = type->getBitSize();
- if (bitsize != sizeof(__uint64) * 8)
- val = val & (((__uint64)1 << bitsize)-1);
- #endif
- return new BitfieldValue(val, type);
- }
- //===========================================================================
- IValue *EnumValue::castTo(ITypeInfo * t)
- {
- t = queryUnqualifiedType(t);
- if (t == type)
- return LINK(this);
- type_t tc = t->getTypeCode();
- if (tc == type_any)
- return LINK(this);
- if (t->isInteger())
- return IntValue::castTo(t);
- CEnumeratedTypeInfo * et = (CEnumeratedTypeInfo *) type;
- IValue *ret = et->queryValue((int) val);
- if (ret)
- return ret->castTo(t);
- // cope better with unknown index
- Owned<IValue> temp = createStringValue("", 0U);
- return temp->castTo(t);
- }
- IValue * createEnumValue(int value, ITypeInfo * type)
- {
- return new EnumValue(value, type);
- }
- //===========================================================================
- int RealValue::compare(IValue *_to)
- {
- assertThrow(_to->queryType()==type);
- RealValue *to = (RealValue *) _to;
- if (val == to->val)
- return 0;
- else
- return val > to->val ? 1 : -1;
- }
- IValue *RealValue::castTo(ITypeInfo *t)
- {
- t = queryUnqualifiedType(t);
- if (t==type)
- return LINK(this);
- type_t tc = t->getTypeCode();
- if (tc == type_any)
- return LINK(this);
- switch (tc)
- {
- case type_real:
- {
- double value = val;
- switch (t->getSize())
- {
- case 4: value = (float)value; break;
- case 8: value = (double)value; break;
- }
- return createRealValue(value, LINK(t));
- }
- case type_int:
- case type_swapint:
- case type_packedint:
- return createTruncIntValue((__int64)val, LINK(t));
- case type_boolean:
- return createBoolValue(val!=0);
- }
- return t->castFrom(val);
- }
- void RealValue::toMem(void *target)
- {
- RealUnion u;
- size32_t size = type->getSize();
- switch (size)
- {
- case 4:
- u.r4 = (float)val;
- break;
- case 8:
- u.r8 = val;
- break;
- };
- memcpy(target, &u, size);
- }
- unsigned RealValue::getHash(unsigned initval)
- {
- RealUnion u;
- size32_t size = type->getSize();
- switch (size)
- {
- case 4:
- u.r4 = (float)val;
- break;
- case 8:
- u.r8 = val;
- break;
- };
- return hashc((unsigned char *) &u, size, initval);
- }
- const char *RealValue::generateECL(StringBuffer &s)
- {
- if (isinf(val))
- return s.append("Std.Math.INFINITY");
- else if (isnan(val))
- return s.append("Std.Math.NaN");
- else
- return getStringValue(s);
- }
- const char *RealValue::generateCPP(StringBuffer &s, CompilerType compiler)
- {
- if (isinf(val))
- return s.append("rtlCreateRealInf()");
- else if (isnan(val))
- return s.append("rtlCreateRealNull()");
- else
- return getStringValue(s);
- }
- const char *RealValue::getStringValue(StringBuffer &s)
- {
- size32_t size = type->getSize();
- if (size==4)
- return s.append((float) val);
- else
- return s.append(val);
- }
- bool RealValue::getBoolValue()
- {
- return val != 0;
- }
- __int64 RealValue::getIntValue()
- {
- return (__int64)(unsigned __int64)val;
- }
- double RealValue::getRealValue()
- {
- return val;
- }
- void RealValue::pushDecimalValue()
- {
- DecPushReal(val);
- }
- void RealValue::serialize(MemoryBuffer &tgt)
- {
- tgt.append(val);
- }
- void RealValue::deserialize(MemoryBuffer &src)
- {
- src.read((double &)val);
- }
- int RealValue::rangeCompare(ITypeInfo * targetType)
- {
- return ::rangeCompare(val, targetType);
- }
- IValue *createRealValue(double val, unsigned size)
- {
- return new RealValue(val, size);
- }
- IValue *createRealValue(double val, ITypeInfo * type)
- {
- return new RealValue(val, type);
- }
- //===========================================================================
- DecimalValue::DecimalValue(const void * v, ITypeInfo * _type) : CValue(_type)
- {
- unsigned len = _type->getSize();
- val = (char *)checked_malloc(len, DEFVALUE_MALLOC_FAILED);
- memcpy(val, v, len);
- }
- DecimalValue::~DecimalValue()
- {
- free(val);
- }
- int DecimalValue::compare(IValue *_to)
- {
- assertThrow(_to->getTypeCode()==type_decimal);
- BcdCriticalBlock bcdBlock;
- pushDecimalValue();
- _to->pushDecimalValue();
- return DecDistinct();
- }
- IValue *DecimalValue::castTo(ITypeInfo *t)
- {
- t = queryUnqualifiedType(t);
- if (t==type)
- return LINK(this);
- type_t tc = t->getTypeCode();
- if (tc == type_any)
- return LINK(this);
- BcdCriticalBlock bcdBlock;
- char newstr[400];
- pushDecimalValue();
- switch (tc)
- {
- case type_real:
- {
- double value = DecPopReal();
- switch (t->getSize())
- {
- case 4: value = (float)value; break;
- case 8: value = (double)value; break;
- }
- return createRealValue(value, LINK(t));
- }
- case type_int:
- case type_swapint:
- case type_packedint:
- return createTruncIntValue(DecPopInt64(), LINK(t));
- case type_boolean:
- return createBoolValue(DecCompareNull() != 0);
- case type_decimal:
- return createDecimalValueFromStack(t);
- }
- DecPopCString(sizeof(newstr), newstr);
- return t->castFrom((size32_t)strlen(newstr), newstr);
- }
- void DecimalValue::toMem(void *target)
- {
- size32_t size = type->getSize();
- memcpy(target, val, size);
- }
- unsigned DecimalValue::getHash(unsigned initval)
- {
- size32_t size = type->getSize();
- return hashc((unsigned char *) val, size, initval);
- }
- const char *DecimalValue::generateECL(StringBuffer &s)
- {
- getStringValue(s);
- s.append('D');
- return s;
- }
- const char *DecimalValue::generateCPP(StringBuffer &s, CompilerType compiler)
- {
- size32_t size = type->getSize();
- s.append("\"");
- for (unsigned i=0;i<size;i++)
- {
- unsigned char c = ((unsigned char *)val)[i];
- s.append("\\x");
- s.appendhex(c, false);
- }
- s.append('"');
- return s.str();
- }
- const char *DecimalValue::getStringValue(StringBuffer &s)
- {
- char strval[64];
- BcdCriticalBlock bcdBlock;
- pushDecimalValue();
- DecPopCString(sizeof(strval), strval);
- s.append(strval);
- return s.str();
- }
- bool DecimalValue::getBoolValue()
- {
- BcdCriticalBlock bcdBlock;
- pushDecimalValue();
- return DecCompareNull() != 0;
- }
- __int64 DecimalValue::getIntValue()
- {
- BcdCriticalBlock bcdBlock;
- pushDecimalValue();
- return DecPopInt64();
- }
- double DecimalValue::getRealValue()
- {
- BcdCriticalBlock bcdBlock;
- pushDecimalValue();
- return DecPopReal();
- }
- void DecimalValue::pushDecimalValue()
- {
- if (type->isSigned())
- DecPushDecimal(val, type->getSize(), type->getPrecision());
- else
- DecPushUDecimal(val, type->getSize(), type->getPrecision());
- }
- const void * DecimalValue::queryValue() const
- {
- return (const void *)val;
- }
- void DecimalValue::serialize(MemoryBuffer &tgt)
- {
- tgt.append(type->getSize());
- tgt.append(type->getSize(), val);
- }
- void DecimalValue::deserialize(MemoryBuffer &src)
- {
- size32_t size;
- src.read(size);
- val = checked_malloc(size, DEFVALUE_MALLOC_FAILED);
- assertex(val);
- }
- int DecimalValue::rangeCompare(ITypeInfo * targetType)
- {
- return ::rangeCompare(getRealValue(), targetType);
- }
- IValue *createDecimalValue(void * val, ITypeInfo * type)
- {
- return new DecimalValue(val, type);
- }
- IValue *createDecimalValueFromStack(ITypeInfo * type)
- {
- return static_cast<CDecimalTypeInfo*>(type)->createValueFromStack();
- }
- //===========================================================================
- const char *BoolValue::generateECL(StringBuffer &s)
- {
- s.append(val ? "true" : "false");
- return s.str();
- }
- const char *BoolValue::generateCPP(StringBuffer &s, CompilerType compiler)
- {
- s.append(val ? "true" : "false");
- return s.str();
- }
- const char *BoolValue::getStringValue(StringBuffer &s)
- {
- s.append(val ? "true" : "false");
- return s.str();
- }
- bool BoolValue::getBoolValue()
- {
- return val;
- }
- __int64 BoolValue::getIntValue()
- {
- return val;
- }
- void BoolValue::pushDecimalValue()
- {
- DecPushUlong(val);
- }
- void BoolValue::toMem(void *target)
- {
- memcpy(target, &val, type->getSize());
- }
- unsigned BoolValue::getHash(unsigned initval)
- {
- size32_t size = type->getSize();
- return hashc((unsigned char *) &val, size, initval);
- }
- int BoolValue::compare(IValue *_to)
- {
- assertThrow(_to->queryType()==type);
- BoolValue *to = (BoolValue *) _to;
- return (int) val - (int) to->val;
- }
- int BoolValue::compare(const void *mem)
- {
- type->Link();
- IValue *to = createValueFromMem(type, mem);
- int ret = compare(to);
- to->Release();
- return ret;
- }
- IValue *BoolValue::castTo(ITypeInfo *t)
- {
- t = queryUnqualifiedType(t);
- if (t==type)
- return LINK(this);
- type_t tc = t->getTypeCode();
- if (tc == type_any)
- return LINK(this);
- switch (tc)
- {
- case type_string:
- case type_qstring:
- case type_varstring:
- case type_unicode:
- case type_varunicode:
- case type_utf8:
- if (!val)
- return t->castFrom(0, "");
- break;
- case type_int:
- case type_swapint:
- return createTruncIntValue(val, LINK(t));
- case type_packedint:
- return createPackedIntValue(val, LINK(t));
- }
- return t->castFrom(false, (__int64)val);
- }
- BoolValue *BoolValue::getTrue()
- {
- trueconst->Link();
- return trueconst;
- }
- BoolValue *BoolValue::getFalse()
- {
- falseconst->Link();
- return falseconst;
- }
- void BoolValue::serialize(MemoryBuffer &tgt)
- {
- tgt.append(val);
- }
- void BoolValue::deserialize(MemoryBuffer &src)
- {
- src.read(val);
- }
- IValue *createBoolValue(bool v)
- {
- return v ? BoolValue::getTrue() : BoolValue::getFalse();
- }
- int BoolValue::rangeCompare(ITypeInfo * targetType)
- {
- return 0; // can fit in anything
- }
- /* In type: linked */
- IValue *createValueFromMem(ITypeInfo *type, const void *mem)
- {
- int size = type->getSize();
- type_t code = type->getTypeCode();
- switch (code)
- {
- case type_string:
- return createStringValue((const char *) mem, type);
- case type_unicode:
- return new UnicodeValue((UChar const *) mem, type);
- case type_utf8:
- return new Utf8Value((char const *) mem, type);
- case type_data:
- type->Release();
- return createDataValue((const char *) mem, size);
- case type_varstring:
- return createVarStringValue((size32_t)strlen((const char *) mem), (const char *) mem, type);
- case type_varunicode:
- return new VarUnicodeValue(rtlUnicodeStrlen((UChar const *) mem), (UChar const *) mem, type);
- case type_qstring:
- return new QStringValue((const char *)mem, type);
- case type_decimal:
- return new DecimalValue(mem, type);
- case type_bitfield:
- UNIMPLEMENTED;
- //needs more thought;
- case type_int:
- {
- unsigned __int64 val;
- if (type->isSigned())
- val = rtlReadInt(mem, size);
- else
- val = rtlReadUInt(mem, size);
- return createIntValue(val, type);
- }
- case type_swapint:
- {
- unsigned __int64 val;
- if (type->isSigned())
- val = rtlReadSwapInt(mem, size);
- else
- val = rtlReadSwapUInt(mem, size);
- return createIntValue(val, type);
- }
- case type_packedint:
- {
- unsigned __int64 val = type->isSigned() ? rtlGetPackedSigned(mem) : rtlGetPackedUnsigned(mem);
- return createPackedIntValue(val, type);
- }
- case type_real:
- {
- RealUnion u;
- memcpy(&u, mem, size);
- double val = 0;
- switch (size)
- {
- case 4:
- val = u.r4;
- break;
- case 8:
- val = u.r8;
- break;
- }
- type->Release();
- return createRealValue(val, size);
- }
- case type_boolean:
- type->Release();
- return createBoolValue(*(bool *)mem);
- }
- type->Release();
- return NULL;
- }
- //----------------------------------------------------------------------------
- void appendValueToBuffer(MemoryBuffer & mem, IValue * value)
- {
- ITypeInfo * type = value->queryType();
- unsigned len = type->getSize();
- void * temp = checked_malloc(len, DEFVALUE_MALLOC_FAILED);
- value->toMem(temp);
- if (type->isSwappedEndian() != mem.needSwapEndian())
- mem.appendSwap(len, temp);
- else
- mem.append(len, temp);
- free(temp);
- }
- //============================================================================
- IValue * addValues(IValue * left, IValue * right)
- {
- IValue * ret;
- ITypeInfo * pnt = getPromotedAddSubType(left->queryType(), right->queryType());
-
- switch(pnt->getTypeCode())
- {
- case type_int:
- case type_swapint:
- case type_packedint:
- ret = createTruncIntValue((__int64)((__uint64)left->getIntValue() + (__uint64)right->getIntValue()), pnt);
- break;
- case type_real:
- ret = createRealValue(left->getRealValue() + right->getRealValue(), pnt);
- break;
- case type_decimal:
- {
- BcdCriticalBlock bcdBlock;
- left->pushDecimalValue();
- right->pushDecimalValue();
- DecAdd();
- ret = ((CDecimalTypeInfo*)pnt)->createValueFromStack();
- pnt->Release();
- break;
- }
- default:
- throwUnexpected();
- }
- return ret;
- }
- #define CALCULATE_AND_RETURN(op) IValue * res = LINK(*values); \
- while (--num) \
- { \
- values++; \
- IValue * tmp = op(res, *values);\
- res->Release(); \
- res = tmp; \
- } \
- return res;
- IValue * subtractValues(IValue * left, IValue * right)
- {
- IValue * ret;
- ITypeInfo * pnt = getPromotedAddSubType(left->queryType(), right->queryType());
-
- switch(pnt->getTypeCode())
- {
- case type_int:
- case type_swapint:
- case type_packedint:
- ret = createTruncIntValue((__int64)((__uint64)left->getIntValue() - (__uint64)right->getIntValue()), pnt);
- break;
- case type_real:
- ret = createRealValue(left->getRealValue() - right->getRealValue(), pnt);
- break;
- case type_decimal:
- {
- BcdCriticalBlock bcdBlock;
- left->pushDecimalValue();
- right->pushDecimalValue();
- DecSub();
- ret = ((CDecimalTypeInfo*)pnt)->createValueFromStack();
- pnt->Release();
- break;
- }
- default:
- throwUnexpected();
- }
- return ret;
- }
- IValue * multiplyValues(IValue * left, IValue * right)
- {
- IValue * ret;
- ITypeInfo * pnt = getPromotedMulDivType(left->queryType(), right->queryType());
-
- switch(pnt->getTypeCode())
- {
- case type_int:
- case type_swapint:
- case type_packedint:
- ret = createTruncIntValue(left->getIntValue() * right->getIntValue(), pnt);
- break;
- case type_real:
- ret = createRealValue(left->getRealValue() * right->getRealValue(), pnt);
- break;
- case type_decimal:
- {
- BcdCriticalBlock bcdBlock;
- left->pushDecimalValue();
- right->pushDecimalValue();
- DecMul();
- ret = ((CDecimalTypeInfo*)pnt)->createValueFromStack();
- pnt->Release();
- break;
- }
- default:
- throwUnexpected();
- }
- return ret;
- }
- IValue * divideValues(IValue * left, IValue * right, byte dbz)
- {
- Owned<ITypeInfo> pnt = getPromotedMulDivType(left->queryType(), right->queryType());
- //Use a cast to a boolean as a shortcut for testing against zero
- if (!right->getBoolValue())
- {
- //If no action is selected, return NULL so the expression doesn't get constant folded.
- if (dbz == DBZnone)
- return NULL;
- if (dbz == DBZfail)
- rtlFailDivideByZero();
- }
- switch(pnt->getTypeCode())
- {
- case type_int:
- case type_swapint:
- case type_packedint:
- {
- __int64 lv = left->getIntValue();
- __int64 rv = right->getIntValue();
- __int64 res = 0;
- if (rv)
- {
- if (pnt->isSigned())
- res = lv / rv;
- else
- res = (__int64)((unsigned __int64)lv / (unsigned __int64)rv);
- }
- return createTruncIntValue(res, pnt.getClear());
- }
- case type_real:
- {
- double lv = left->getRealValue();
- double rv = right->getRealValue();
- double res;
- if (rv)
- res = lv / rv;
- else if (dbz == DBZnan)
- res = rtlCreateRealInf();
- else
- res = 0.0;
- return createRealValue(res, pnt.getClear());
- }
- case type_decimal:
- {
- BcdCriticalBlock bcdBlock;
- left->pushDecimalValue();
- right->pushDecimalValue();
- DecDivide(dbz);
- return createDecimalValueFromStack(pnt);
- }
- default:
- throwUnexpected();
- return NULL;
- }
- }
- IValue * modulusValues(IValue * left, IValue * right, byte dbz)
- {
- Owned<ITypeInfo> pnt = getPromotedMulDivType(left->queryType(), right->queryType());
-
- //Use a cast to a boolean as a shortcut for testing against zero
- if (!right->getBoolValue())
- {
- //If no action is selected, return NULL so the expression doesn't get constant folded.
- if (dbz == DBZnone)
- return NULL;
- if (dbz == DBZfail)
- rtlFailDivideByZero();
- }
- switch(pnt->getTypeCode())
- {
- case type_int:
- case type_swapint:
- case type_packedint:
- {
- __int64 lv = left->getIntValue();
- __int64 rv = right->getIntValue();
- __int64 res = 0;
- if (rv)
- {
- if (pnt->isSigned())
- res = lv % rv;
- else
- res = (__int64)((unsigned __int64)lv % (unsigned __int64)rv);
- }
- return createTruncIntValue(res, pnt.getClear());
- }
- case type_real:
- {
- double rv = right->getRealValue();
- double res;
- if (rv)
- res = fmod(left->getRealValue(), rv);
- else if (dbz == DBZnan)
- res = rtlCreateRealNull();
- else
- res = 0.0;
- return createRealValue(res, pnt.getClear());
- }
- case type_decimal:
- {
- BcdCriticalBlock bcdBlock;
- left->pushDecimalValue();
- right->pushDecimalValue();
- DecModulus(dbz);
- return createDecimalValueFromStack(pnt);
- }
- default:
- throwUnexpected();
- return NULL;
- }
- }
- IValue * powerValues(IValue * left, IValue * right)
- {
- IValue * ret;
- ITypeInfo * pnt = makeRealType(8);
- switch(pnt->getTypeCode())
- {
- case type_int:
- case type_swapint:
- case type_packedint:
- case type_real:
- ret = createRealValue(pow(left->getRealValue(), right->getRealValue()), pnt);
- break;
- /*
- // TBD
- case type_decimal:
- BcdCriticalBlock bcdBlock;
- left->pushDecimalValue();
- DecLongPower(right->getIntValue());
- ret = ((CDecimalTypeInfo*)pnt)->createValueFromStack();
- pnt->Release();
- break
- */
- default:
- pnt->Release();
- throwUnexpected();
- }
- return ret;
- }
- IValue * negateValue(IValue * v)
- {
- switch(v->getTypeCode())
- {
- case type_int:
- case type_swapint:
- case type_packedint:
- {
- __uint64 value = - (__uint64)v->getIntValue(); // avoid undefined behaviour if value = int_min
- return createTruncIntValue((__int64)value, v->getType());
- }
- case type_real:
- return createRealValue(-(v->getRealValue()), v->getSize());
- case type_decimal:
- {
- BcdCriticalBlock bcdBlock;
- v->pushDecimalValue();
- DecNegate();
- return ((CDecimalTypeInfo*)v->queryType())->createValueFromStack();
- }
- }
- throwUnexpected();
- return NULL;
- }
- IValue * expValue(IValue * v)
- {
- return createRealValue(exp(v->getRealValue()), 8);
- }
- IValue * roundUpValue(IValue * v)
- {
- switch(v->getTypeCode())
- {
- case type_int:
- case type_swapint:
- case type_packedint:
- return LINK(v);
- case type_real:
- return createTruncIntValue(rtlRoundUp(v->getRealValue()), 8, true);
- case type_decimal:
- {
- BcdCriticalBlock bcdBlock;
- v->pushDecimalValue();
- DecRoundUp();
- OwnedITypeInfo resultType = getRoundType(v->queryType());
- return createDecimalValueFromStack(resultType);
- }
- }
- throwUnexpected();
- return NULL;
- }
- IValue * roundValue(IValue * v)
- {
- switch(v->getTypeCode())
- {
- case type_int:
- case type_swapint:
- case type_packedint:
- return LINK(v);
- case type_real:
- return createTruncIntValue(rtlRound(v->getRealValue()), 8, true);
- case type_decimal:
- {
- BcdCriticalBlock bcdBlock;
- v->pushDecimalValue();
- DecRound();
- Owned<ITypeInfo> resultType = getRoundType(v->queryType());
- return createDecimalValueFromStack(resultType);
- }
- }
- throwUnexpected();
- return NULL;
- }
- IValue * roundToValue(IValue * v, int places)
- {
- switch(v->getTypeCode())
- {
- case type_int:
- case type_swapint:
- case type_packedint:
- return createRealValue(rtlRoundTo(v->getRealValue(), places), 8);
- case type_real:
- return createRealValue(rtlRoundTo(v->getRealValue(), places), 8);
- case type_decimal:
- {
- BcdCriticalBlock bcdBlock;
- v->pushDecimalValue();
- DecRoundTo(places);
- OwnedITypeInfo resultType = getRoundToType(v->queryType());
- return createDecimalValueFromStack(resultType);
- }
- }
- throwUnexpected();
- return NULL;
- }
- IValue * truncateValue(IValue * v)
- {
- switch(v->getTypeCode())
- {
- case type_int:
- case type_swapint:
- case type_packedint:
- return LINK(v);
- case type_real:
- return createTruncIntValue(v->getIntValue(), 8, true);
- case type_decimal:
- {
- BcdCriticalBlock bcdBlock;
- v->pushDecimalValue();
- DecTruncate();
- OwnedITypeInfo resultType = getTruncType(v->queryType());
- return createDecimalValueFromStack(resultType);
- }
- }
- throwUnexpected();
- return NULL;
- }
- IValue * lnValue(IValue * v, byte onZero)
- {
- return createRealValue(rtlLog(v->getRealValue(), onZero), 8);
- }
- IValue * sinValue(IValue * v)
- {
- return createRealValue(sin(v->getRealValue()), 8);
- }
- IValue * cosValue(IValue * v)
- {
- return createRealValue(cos(v->getRealValue()), 8);
- }
- IValue * tanValue(IValue * v)
- {
- return createRealValue(tan(v->getRealValue()), 8);
- }
- IValue * sinhValue(IValue * v)
- {
- return createRealValue(sinh(v->getRealValue()), 8);
- }
- IValue * coshValue(IValue * v)
- {
- return createRealValue(cosh(v->getRealValue()), 8);
- }
- IValue * tanhValue(IValue * v)
- {
- return createRealValue(tanh(v->getRealValue()), 8);
- }
- IValue * asinValue(IValue * v, byte onZero)
- {
- return createRealValue(rtlASin(v->getRealValue(), onZero), 8);
- }
- IValue * acosValue(IValue * v, byte onZero)
- {
- return createRealValue(rtlACos(v->getRealValue(), onZero), 8);
- }
- IValue * atanValue(IValue * v)
- {
- return createRealValue(atan(v->getRealValue()), 8);
- }
- IValue * atan2Value(IValue * y, IValue* x)
- {
- return createRealValue(atan2(y->getRealValue(), x->getRealValue()), 8);
- }
- IValue * log10Value(IValue * v, byte onZero)
- {
- return createRealValue(rtlLog10(v->getRealValue(), onZero), 8);
- }
- IValue * sqrtValue(IValue * v, byte onZero)
- {
- switch(v->getTypeCode())
- {
- case type_decimal:
- //MORE: This should probably do this more accurately.
- //fall into
- case type_int:
- case type_swapint:
- case type_packedint:
- case type_real:
- return createRealValue(rtlSqrt(v->getRealValue(), onZero), 8);
- }
- throwUnexpected();
- return NULL;
- }
- IValue * absValue(IValue * v)
- {
- switch(v->getTypeCode())
- {
- case type_int:
- case type_swapint:
- case type_packedint:
- {
- ITypeInfo * type = v->queryType();
- if (type->isSigned())
- {
- __int64 val = v->getIntValue();
- if (val < 0)
- return createIntValue(-val, LINK(type));
- }
- return LINK(v);
- }
- case type_real:
- return createRealValue(fabs(v->getRealValue()), v->getSize());
- case type_decimal:
- {
- BcdCriticalBlock bcdBlock;
- v->pushDecimalValue();
- DecAbs();
- return ((CDecimalTypeInfo*)v->queryType())->createValueFromStack();
- }
- }
- throwUnexpected();
- return NULL;
- }
- IValue * substringValue(IValue * v, IValue * lower, IValue * higher)
- {
- ITypeInfo * type = v->queryType();
- unsigned srcLen = type->getStringLen();
- const void * raw = v->queryValue();
- unsigned low = lower ? (unsigned)lower->getIntValue() : 0;
- unsigned high = higher ? (unsigned)higher->getIntValue() : srcLen;
- unsigned retLen = 0;
- void * retPtr;
- ITypeInfo * retType = NULL;
- switch (type->getTypeCode())
- {
- case type_string:
- rtlSubStrFTX(retLen, *(char * *)&retPtr, srcLen, (const char *)raw, low, high);
- break;
- case type_varstring:
- rtlSubStrFTX(retLen, *(char * *)&retPtr, srcLen, (const char *)raw, low, high);
- retType = makeStringType(retLen, LINK(type->queryCharset()), LINK(type->queryCollation()));
- break;
- case type_data:
- rtlSubDataFTX(retLen, retPtr, srcLen, raw, low, high);
- break;
- case type_qstring:
- rtlSubQStrFTX(retLen, *(char * *)&retPtr, srcLen, (const char *)raw, low, high);
- break;
- case type_unicode:
- rtlUnicodeSubStrFTX(retLen, *(UChar * *)&retPtr, srcLen, (const UChar *)raw, low, high);
- break;
- case type_varunicode:
- rtlUnicodeSubStrFTX(retLen, *(UChar * *)&retPtr, srcLen, (const UChar *)raw, low, high);
- retType = makeUnicodeType(retLen, type->queryLocale());
- break;
- case type_utf8:
- rtlUtf8SubStrFTX(retLen, *(char * *)&retPtr, srcLen, (const char *)raw, low, high);
- break;
- default:
- UNIMPLEMENTED;
- }
- if (retType == NULL)
- retType = getStretchedType(retLen, type);
- IValue * ret = createValueFromMem(retType, retPtr);
- rtlFree(retPtr);
- return ret;
- }
- IValue * trimStringValue(IValue * v, char typecode, bool whitespace)
- {
- ITypeInfo * type = v->queryType();
- type_t tc = type->getTypeCode();
- if(isUnicodeType(type))
- {
- unsigned tlen = 0;
- rtlDataAttr resultstr;
- unsigned len = type->getStringLen();
- if (tc == type_utf8)
- {
- char const * str = (char const *)v->queryValue();
- if (whitespace)
- rtlTrimUtf8WS(tlen, resultstr.refstr(), len, str, typecode=='B'||typecode=='L', typecode=='A', typecode=='B'||typecode=='R');
- else switch(typecode)
- {
- case 'A':
- rtlTrimUtf8All(tlen, resultstr.refstr(), len, str);
- break;
- case 'B':
- rtlTrimUtf8Both(tlen, resultstr.refstr(), len, str);
- break;
- case 'L':
- rtlTrimUtf8Left(tlen, resultstr.refstr(), len, str);
- break;
- default:
- rtlTrimUtf8Right(tlen, resultstr.refstr(), len, str);
- break;
- }
- ITypeInfo * newtype = makeUtf8Type(tlen, type->queryLocale());
- return createUtf8Value(tlen, resultstr.getstr(), newtype);
- }
- else
- {
- UChar const * str = (UChar const *)v->queryValue();
- if (whitespace)
- rtlTrimUnicodeWS(tlen, resultstr.refustr(), len, str, typecode=='B'||typecode=='L', typecode=='A', typecode=='B'||typecode=='R');
- else switch(typecode)
- {
- case 'A':
- rtlTrimUnicodeAll(tlen, resultstr.refustr(), len, str);
- break;
- case 'B':
- rtlTrimUnicodeBoth(tlen, resultstr.refustr(), len, str);
- break;
- case 'L':
- rtlTrimUnicodeLeft(tlen, resultstr.refustr(), len, str);
- break;
- default:
- rtlTrimUnicodeRight(tlen, resultstr.refustr(), len, str);
- break;
- }
- ITypeInfo * newtype = makeUnicodeType(tlen, v->queryType()->queryLocale());
- return createUnicodeValue(resultstr.getustr(), tlen, newtype);
- }
- }
- else
- {
- Owned<ITypeInfo> st = getStringType(type);
- Owned<ITypeInfo> asciiType = getAsciiType(st);
- Owned<IValue> sv = v->castTo(asciiType);
- StringBuffer s;
- sv->getStringValue(s);
- unsigned tlen = 0;
- rtlDataAttr resultstr;
- unsigned len = s.length();
- char const * str = s.str();
- if (whitespace)
- rtlTrimWS(tlen, resultstr.refstr(), len, str, typecode=='B'||typecode=='L', typecode=='A', typecode=='B'||typecode=='R');
- else switch(typecode)
- {
- case 'A':
- rtlTrimAll(tlen, resultstr.refstr(), len, str);
- break;
- case 'B':
- rtlTrimBoth(tlen, resultstr.refstr(), len, str);
- break;
- case 'L':
- rtlTrimLeft(tlen, resultstr.refstr(), len, str);
- break;
- default:
- rtlTrimRight(tlen, resultstr.refstr(), len, str);
- break;
- }
- ITypeInfo * newtype = makeStringType(tlen, LINK(asciiType->queryCharset()), LINK(asciiType->queryCollation()));
- return createStringValue(resultstr.getstr(), newtype);
- }
- }
- //---------------------------------------------------------------------
- void getStringFromIValue(StringBuffer & s, IValue* val)
- {
- Owned<ITypeInfo> tp = getStringType(val->queryType());
- Owned<IValue> sv = val->castTo(tp);
- sv->getStringValue(s);
- }
- void getStringFromIValue(unsigned & len, char* & str, IValue* val)
- {
- StringBuffer s;
- getStringFromIValue(s, val);
- len = s.length();
- str = s.detach();
- }
- //---------------------------------------------------------------------
- IValue * concatValues(IValue * left, IValue * right)
- {
- ITypeInfo * leftType = left->queryType();
- ITypeInfo * rightType = right->queryType();
- type_t ltc = leftType->getTypeCode();
- type_t rtc = rightType->getTypeCode();
- if(isUnicodeType(leftType))
- {
- assertex(isUnicodeType(rightType));
- assertex(leftType->queryLocale() == rightType->queryLocale());
- rtlDataAttr out;
- unsigned outlen;
- if (ltc == type_utf8 && rtc == type_utf8)
- {
- rtlConcatUtf8(outlen, out.addrstr(), leftType->getStringLen(), (char const *)left->queryValue(), rightType->getStringLen(), (char const *)right->queryValue(), -1);
- ITypeInfo * newtype = makeUtf8Type(outlen, leftType->queryLocale());
- return createUtf8Value(outlen, out.getstr(), newtype);
- }
- else
- {
- rtlConcatUnicode(outlen, out.addrustr(), leftType->getStringLen(), (UChar const *)left->queryValue(), rightType->getStringLen(), (UChar const *)right->queryValue(), -1);
- ITypeInfo * newtype = makeUnicodeType(outlen, leftType->queryLocale());
- return createUnicodeValue(out.getustr(), outlen, newtype);
- }
- }
- else
- {
- Owned<ITypeInfo> lt = getStringType(leftType);
- Owned<ITypeInfo> rt = getStringType(rightType);
- Owned<IValue> lv = left->castTo(lt);
- Owned<IValue> rv = right->castTo(rt);
- assertex(!isUnicodeType(rt));
- assertex(lt->queryCharset() == rt->queryCharset());
- assertex(lt->queryCollation() == rt->queryCollation());
- size32_t len = lt->getStringLen() + rt->getStringLen();
- StringBuffer s;
- lv->getStringValue(s);
- rv->getStringValue(s);
- if (ltc == type_varstring || rtc == type_varstring)
- {
- ITypeInfo * newtype = makeVarStringType(len);
- return createVarStringValue(len, s.str(), newtype);
- }
- else if (ltc == type_string || rtc == type_string)
- {
- ITypeInfo * newtype = makeStringType(len, LINK(lt->queryCharset()), LINK(lt->queryCollation()));
- return createStringValue(s.str(), newtype);
- }
- else
- {
- return createDataValue(s.str(), len);
- }
- }
- }
- IValue * binaryAndValues(IValue * left, IValue * right)
- {
- IValue * ret;
- Owned<ITypeInfo> pnt = getBandType(left->queryType(), right->queryType());
- switch(pnt->getTypeCode())
- {
- case type_boolean:
- return createBoolValue(left->getBoolValue() && right->getBoolValue());
- case type_int:
- case type_swapint:
- case type_packedint:
- ret = createTruncIntValue(left->getIntValue() & right->getIntValue(), pnt.getClear());
- break;
- default:
- throwUnexpected();
- }
- return ret;
- }
- IValue * binaryOrValues(IValue * left, IValue * right)
- {
- IValue * ret;
- Owned<ITypeInfo> pnt = getBorType(left->queryType(), right->queryType());
- switch(pnt->getTypeCode())
- {
- case type_boolean:
- return createBoolValue(left->getBoolValue() || right->getBoolValue());
- case type_int:
- case type_swapint:
- case type_packedint:
- ret = createTruncIntValue(left->getIntValue() | right->getIntValue(), pnt.getClear());
- break;
- default:
- throwUnexpected();
- }
- return ret;
- }
- IValue * binaryXorValues(IValue * left, IValue * right)
- {
- IValue * ret;
- ITypeInfo * pnt = getPromotedNumericType(left->queryType(), right->queryType());
- switch(pnt->getTypeCode())
- {
- case type_int:
- case type_swapint:
- case type_packedint:
- ret = createTruncIntValue(left->getIntValue() ^ right->getIntValue(), pnt);
- break;
- default:
- throwUnexpected();
- }
- return ret;
- }
- IValue * binaryNotValues(IValue * v)
- {
- switch(v->getTypeCode())
- {
- case type_int:
- case type_swapint:
- case type_packedint:
- return createTruncIntValue(~v->getIntValue(), v->getType());
- }
- throwUnexpected();
- return NULL;
- }
- IValue * logicalNotValues(IValue * v)
- {
- return createBoolValue(!v->getBoolValue());
- }
- IValue * logicalAndValues(IValue * left, IValue * right)
- {
- return createBoolValue(left->getBoolValue() && right->getBoolValue());
- }
- IValue * logicalOrValues(IValue * left, IValue * right)
- {
- return createBoolValue(left->getBoolValue() || right->getBoolValue());
- }
- int orderValues(IValue * left, IValue * right)
- {
- //The following line can be uncommented to check that the types are consistent everywhere
- //but remains commented out to improve resilience when the types are wrong.
- // return left->compare(right);
- Owned<ITypeInfo> pt = getPromotedCompareType(left->queryType(), right->queryType());
- Owned<IValue> lv = left->castTo(pt);
- Owned<IValue> rv = right->castTo(pt);
- return lv->compare(rv);
- }
- #define COMPARE_AND_RETURN(op) \
- return createBoolValue(orderValues(left, right) op 0);
- IValue * equalValues(IValue * left, IValue * right)
- {
- COMPARE_AND_RETURN(==)
- }
- IValue * notEqualValues(IValue * left, IValue * right)
- {
- COMPARE_AND_RETURN(!=)
- }
- IValue * lessValues(IValue * left, IValue * right)
- {
- COMPARE_AND_RETURN(<)
- }
- IValue * lessEqualValues(IValue * left, IValue * right)
- {
- COMPARE_AND_RETURN(<=)
- }
- IValue * greaterValues(IValue * left, IValue * right)
- {
- COMPARE_AND_RETURN(>)
- }
- IValue * greaterEqualValues(IValue * left, IValue * right)
- {
- COMPARE_AND_RETURN(>=)
- }
- IValue * shiftLeftValues(IValue * left, IValue * right)
- {
- ITypeInfo * retType = left->getType();
- switch(retType->getTypeCode())
- {
- case type_int:
- case type_swapint:
- case type_packedint:
- {
- __uint64 value = (__uint64)left->getIntValue() << (__uint64)right->getIntValue();
- return createTruncIntValue((__int64)value, retType);
- }
- default:
- UNIMPLEMENTED;
- }
- }
- IValue * shiftRightValues(IValue * left, IValue * right)
- {
- ITypeInfo * retType = left->getType();
- switch(retType->getTypeCode())
- {
- case type_int:
- case type_swapint:
- case type_packedint:
- if (retType->isSigned())
- return createTruncIntValue(((__int64)left->getIntValue()) >> right->getIntValue(), retType);
- else
- return createTruncIntValue(((unsigned __int64)left->getIntValue()) >> right->getIntValue(), retType);
- default:
- UNIMPLEMENTED;
- }
- }
- extern DEFTYPE_API void serializeValue(MemoryBuffer & target, IValue * value)
- {
- ITypeInfo * type = value->queryType();
- type->serialize(target);
- void * buffer = target.reserve(type->getSize());
- value->toMem(buffer);
- }
- extern DEFTYPE_API IValue * deserializeValue(MemoryBuffer & source)
- {
- Owned<ITypeInfo> type = deserializeType(source);
- const void * buffer = source.readDirect(type->getSize());
- return createValueFromMem(LINK(type), buffer);
- }
|