defvalue.cpp 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "jlib.hpp"
  14. #include "jexcept.hpp"
  15. #include "jmisc.hpp"
  16. #include "deftype.ipp"
  17. #include "defvalue.ipp"
  18. #include <stdio.h>
  19. #include <math.h>
  20. #include <limits.h>
  21. #include "rtlbcd.hpp"
  22. #include "eclrtl_imp.hpp"
  23. #if defined(_DEBUG) && defined(_WIN32) && !defined(USING_MPATROL)
  24. #undef new
  25. #define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
  26. #endif
  27. BoolValue *BoolValue::trueconst;
  28. BoolValue *BoolValue::falseconst;
  29. static IAtom * asciiAtom;
  30. static IAtom * ebcdicAtom;
  31. MODULE_INIT(INIT_PRIORITY_DEFVALUE)
  32. {
  33. asciiAtom = createLowerCaseAtom("ascii");
  34. ebcdicAtom = createLowerCaseAtom("ebcdic");
  35. BoolValue::trueconst = new BoolValue(true);
  36. BoolValue::falseconst = new BoolValue(false);
  37. return true;
  38. }
  39. MODULE_EXIT()
  40. {
  41. ::Release(BoolValue::trueconst);
  42. ::Release(BoolValue::falseconst);
  43. }
  44. union RealUnion
  45. {
  46. float r4;
  47. double r8;
  48. };
  49. //===========================================================================
  50. static double powerOfTen[] = {1e0, 1e1, 1e2, 1e3, 1e4,
  51. 1e5, 1e6, 1e7, 1e8, 1e9,
  52. 1e10, 1e11, 1e12, 1e13, 1e14,
  53. 1e15, 1e16, 1e17, 1e18, 1e19,
  54. 1e20, 1e21, 1e22, 1e23, 1e24,
  55. 1e25, 1e26, 1e27, 1e28, 1e29,
  56. 1e30, 1e31, 1e32 };
  57. static unsigned __int64 maxUIntValue[] = { I64C(0), I64C(0xFF), I64C(0xFFFF), I64C(0xFFFFFF), I64C(0xFFFFFFFF), I64C(0xFFFFFFFFFF), I64C(0xFFFFFFFFFFFF), I64C(0xFFFFFFFFFFFFFF), I64C(0xFFFFFFFFFFFFFFFF) };
  58. static __int64 maxIntValue[] = { I64C(0), I64C(0x7F), I64C(0x7FFF), I64C(0x7FFFFF), I64C(0x7FFFFFFF), I64C(0x7FFFFFFFFF), I64C(0x7FFFFFFFFFFF), I64C(0x7FFFFFFFFFFFFF), I64C(0x7FFFFFFFFFFFFFFF) };
  59. int rangeCompare(double value, ITypeInfo * targetType)
  60. {
  61. if (targetType->isSigned())
  62. {
  63. switch (targetType->getTypeCode())
  64. {
  65. case type_decimal:
  66. if (value <= -powerOfTen[targetType->getDigits()-targetType->getPrecision()])
  67. return -1;
  68. if (value >= powerOfTen[targetType->getDigits()-targetType->getPrecision()])
  69. return +1;
  70. break;
  71. case type_int:
  72. case type_swapint:
  73. if (value < 0)
  74. {
  75. if (-value > (double)maxIntValue[targetType->getSize()]+1)
  76. return -1;
  77. }
  78. if (value > (double)maxIntValue[targetType->getSize()])
  79. return +1;
  80. break;
  81. case type_packedint:
  82. return rangeCompare(value, targetType->queryPromotedType());
  83. }
  84. }
  85. else
  86. {
  87. if (value < 0)
  88. return -1;
  89. switch (targetType->getTypeCode())
  90. {
  91. case type_decimal:
  92. if (value >= powerOfTen[targetType->getDigits()-targetType->getPrecision()])
  93. return +1;
  94. break;
  95. case type_int:
  96. case type_swapint:
  97. if (value > (double)maxUIntValue[targetType->getSize()])
  98. return +1;
  99. break;
  100. case type_packedint:
  101. return rangeCompare(value, targetType->queryPromotedType());
  102. case type_bitfield:
  103. {
  104. unsigned __int64 maxValue = (1 << targetType->getBitSize()) - 1;
  105. if (value > maxValue)
  106. return +1;
  107. break;
  108. }
  109. }
  110. }
  111. return 0;
  112. }
  113. inline unsigned getTargetLength(ITypeInfo * type, unsigned dft)
  114. {
  115. unsigned length = type->getStringLen();
  116. if (length == UNKNOWN_LENGTH)
  117. length = dft;
  118. return length;
  119. }
  120. //===========================================================================
  121. CValue::CValue(ITypeInfo *_type)
  122. {
  123. type = _type;
  124. assertThrow(type);
  125. }
  126. CValue::~CValue()
  127. {
  128. type->Release();
  129. }
  130. int CValue::compare(const void *mem)
  131. {
  132. type->Link();
  133. IValue *to = createValueFromMem(type, mem);
  134. int ret = compare(to);
  135. to->Release();
  136. return ret;
  137. }
  138. double CValue::getRealValue()
  139. {
  140. return (double)getIntValue();
  141. }
  142. type_t CValue::getTypeCode(void)
  143. {
  144. return type->getTypeCode();
  145. }
  146. size32_t CValue::getSize()
  147. {
  148. return type->getSize();
  149. }
  150. ITypeInfo *CValue::queryType() const
  151. {
  152. return type;
  153. }
  154. ITypeInfo *CValue::getType()
  155. {
  156. return LINK(type);
  157. }
  158. IValue * CValue::doCastTo(unsigned osize, const char * text, ITypeInfo *t)
  159. {
  160. assertex(t == queryUnqualifiedType(t));
  161. switch (t->getTypeCode())
  162. {
  163. case type_string:
  164. return createStringValue(text, LINK(t), osize, type->queryCharset());
  165. case type_varstring:
  166. return createVarStringValue(text, LINK(t), osize, type->queryCharset());
  167. case type_data:
  168. return t->castFrom(osize, text); //NB: Must not go through translation in default case
  169. case type_char:
  170. return createCharValue(text[0], LINK(t));
  171. case type_int:
  172. case type_swapint:
  173. case type_packedint:
  174. break; // treat these using the default action in the type.
  175. case type_boolean:
  176. return createBoolValue(getBoolValue());
  177. case type_unicode:
  178. case type_varunicode:
  179. {
  180. rtlDataAttr buff(osize * sizeof(UChar));
  181. UChar * target = buff.getustr();
  182. rtlCodepageToUnicode(osize, target, osize, text, type->queryCharset()->queryCodepageName());
  183. return t->castFrom(osize, target);
  184. }
  185. case type_utf8:
  186. {
  187. unsigned tLen = getTargetLength(t, osize);
  188. rtlDataAttr buff(tLen * 4);
  189. char * target = buff.getstr();
  190. rtlCodepageToUtf8(tLen, target, osize, text, type->queryCharset()->queryCodepageName());
  191. return createUtf8Value(tLen, target, LINK(t));
  192. }
  193. }
  194. Owned<ICharsetInfo> charset = getCharset(asciiAtom);
  195. ITranslationInfo * translation = queryDefaultTranslation(charset, type->queryCharset());
  196. if (translation)
  197. {
  198. StringBuffer translated;
  199. translation->translate(translated, osize, text);
  200. return t->castFrom(osize, translated.str());
  201. }
  202. return t->castFrom(osize, text);
  203. }
  204. //===========================================================================
  205. VarStringValue::VarStringValue(unsigned len, const char *v, ITypeInfo *_type) : CValue(_type)
  206. {
  207. unsigned typeLen = type->getStringLen();
  208. assertex(typeLen != UNKNOWN_LENGTH);
  209. if (len >= typeLen)
  210. val.set(v, typeLen);
  211. else
  212. {
  213. char * temp = (char *)checked_malloc(typeLen+1, DEFVALUE_MALLOC_FAILED);
  214. memcpy(temp, v, len);
  215. temp[len] = 0;
  216. val.set(temp, typeLen);
  217. free(temp);
  218. }
  219. }
  220. const char *VarStringValue::generateECL(StringBuffer &out)
  221. {
  222. return appendStringAsQuotedECL(out, val.length(), val).str();
  223. }
  224. const char *VarStringValue::generateCPP(StringBuffer &out, CompilerType compiler)
  225. {
  226. unsigned len = val.length();
  227. return appendStringAsQuotedCPP(out, len, val.get(), len>120).str();
  228. }
  229. void VarStringValue::toMem(void *target)
  230. {
  231. unsigned copyLen = val.length();
  232. unsigned typeLen = type->getSize();
  233. //assertThrow(typeLen);
  234. if(typeLen > 0) {
  235. if (copyLen >= typeLen)
  236. copyLen = typeLen - 1;
  237. memcpy(target, val.get(), copyLen);
  238. memset((char *)target+copyLen, 0, (typeLen-copyLen));
  239. }
  240. else {
  241. memcpy(target, val.get(), copyLen + 1);
  242. }
  243. }
  244. unsigned VarStringValue::getHash(unsigned initval)
  245. {
  246. return hashc((unsigned char *) val.get(), val.length(), initval);
  247. }
  248. int VarStringValue::compare(IValue *_to)
  249. {
  250. assertThrow(_to->getTypeCode()==type->getTypeCode());
  251. VarStringValue *to = (VarStringValue *) _to;
  252. return rtlCompareVStrVStr(val.get(), to->val.get());
  253. }
  254. int VarStringValue::compare(const void *mem)
  255. {
  256. return rtlCompareVStrVStr(val.get(), (const char *)mem);
  257. }
  258. IValue *VarStringValue::castTo(ITypeInfo *t)
  259. {
  260. t = queryUnqualifiedType(t);
  261. if (t==type)
  262. return LINK(this);
  263. type_t tc = t->getTypeCode();
  264. if (tc == type_any)
  265. return LINK(this);
  266. return doCastTo(val.length(), (const char *)val.get(), t);
  267. }
  268. const void *VarStringValue::queryValue() const
  269. {
  270. return (const void *) val.get();
  271. }
  272. bool VarStringValue::getBoolValue()
  273. {
  274. return val.length() != 0;
  275. }
  276. __int64 VarStringValue::getIntValue()
  277. {
  278. return rtlVStrToInt8(val.get());
  279. }
  280. const char *VarStringValue::getStringValue(StringBuffer &out)
  281. {
  282. out.append(val);
  283. return out.str();
  284. }
  285. void VarStringValue::pushDecimalValue(void)
  286. {
  287. DecPushCString((char*)val.get());
  288. }
  289. void VarStringValue::serialize(MemoryBuffer &tgt)
  290. {
  291. tgt.append(val);
  292. }
  293. void VarStringValue::deserialize(MemoryBuffer &src)
  294. {
  295. src.read(val);
  296. }
  297. IValue * createVarStringValue(unsigned len, const char * value, ITypeInfo *type)
  298. {
  299. if (type->getSize() != UNKNOWN_LENGTH)
  300. return new VarStringValue(len, value, type);
  301. ITypeInfo * newType = getStretchedType(len, type);
  302. type->Release();
  303. return new VarStringValue(len, value, newType);
  304. }
  305. IValue *createVarStringValue(const char *val, ITypeInfo *type, int srcLength, ICharsetInfo * srcCharset)
  306. {
  307. ITranslationInfo * translation = queryDefaultTranslation(type->queryCharset(), srcCharset);
  308. if(translation)
  309. {
  310. StringBuffer translated;
  311. translation->translate(translated, srcLength, val);
  312. return createVarStringValue(srcLength, translated.str(), type);
  313. }
  314. else
  315. return createVarStringValue(srcLength, val, type);
  316. }
  317. int VarStringValue::rangeCompare(ITypeInfo * targetType)
  318. {
  319. //MORE - should probably check for unpadded length, and check the type as well.
  320. return 0;
  321. }
  322. //===========================================================================
  323. MemoryValue::MemoryValue(const void *v, ITypeInfo *_type) : CValue(_type)
  324. {
  325. assertex(_type->getSize() != UNKNOWN_LENGTH);
  326. val.set(_type->getSize(), v);
  327. }
  328. MemoryValue::MemoryValue(ITypeInfo *_type) : CValue(_type)
  329. {
  330. }
  331. void MemoryValue::toMem(void *target)
  332. {
  333. size32_t size = getSize();
  334. assertThrow(val.length()>=size);
  335. memcpy(target, val.get(), size);
  336. }
  337. unsigned MemoryValue::getHash(unsigned initval)
  338. {
  339. size32_t size = getSize();
  340. assertThrow(val.length()>=size);
  341. return hashc((unsigned char *) val.get(), size, initval);
  342. }
  343. int MemoryValue::compare(IValue * to)
  344. {
  345. ITypeInfo * toType = to->queryType();
  346. assertex(toType->getTypeCode()==type->getTypeCode());
  347. return rtlCompareDataData(type->getSize(), (const char *)val.get(), toType->getSize(), (const char *)to->queryValue());
  348. }
  349. int MemoryValue::compare(const void *mem)
  350. {
  351. return memcmp(val.get(), (const char *) mem, type->getSize());
  352. }
  353. const char *MemoryValue::generateCPP(StringBuffer &out, CompilerType compiler)
  354. {
  355. unsigned size = getSize();
  356. return appendStringAsQuotedCPP(out, size, (const char *)val.get(), size>120).str();
  357. }
  358. bool MemoryValue::getBoolValue()
  359. {
  360. //return true if any non zero character
  361. const char * cur = (const char *)val.get();
  362. unsigned len = getSize();
  363. while (len--)
  364. {
  365. if (*cur++ != 0)
  366. return true;
  367. }
  368. return false;
  369. }
  370. void MemoryValue::pushDecimalValue()
  371. {
  372. DecPushUlong(0);
  373. }
  374. void MemoryValue::serialize(MemoryBuffer &tgt)
  375. {
  376. tgt.append(val.length());
  377. tgt.append(val.length(), val.get());
  378. }
  379. void MemoryValue::deserialize(MemoryBuffer &src)
  380. {
  381. size32_t size;
  382. src.read(size);
  383. void *mem = checked_malloc(size, DEFVALUE_MALLOC_FAILED);
  384. assertex(mem);
  385. src.read(size, mem);
  386. val.setOwn(size, mem);
  387. }
  388. int MemoryValue::rangeCompare(ITypeInfo * targetType)
  389. {
  390. //MORE - should probably check for unpadded length, and check the type as well.
  391. return 0;
  392. }
  393. //===========================================================================
  394. StringValue::StringValue(const char *v, ITypeInfo *_type) : MemoryValue(_type)
  395. {
  396. //store a null terminated string for ease of conversion
  397. unsigned len = _type->getSize();
  398. assertex(len != UNKNOWN_LENGTH);
  399. char * temp = (char *)val.allocate(len+1);
  400. memcpy(temp, v, len);
  401. temp[len] = 0;
  402. }
  403. const char *StringValue::generateECL(StringBuffer &out)
  404. {
  405. return appendStringAsQuotedECL(out, type->getSize(), (const char *)val.get()).str();
  406. }
  407. int StringValue::compare(IValue * to)
  408. {
  409. ITypeInfo * toType = to->queryType();
  410. assertex(toType->getTypeCode()==type->getTypeCode());
  411. return rtlCompareStrStr(type->getSize(), (const char *)val.get(), toType->getSize(), (const char *)to->queryValue());
  412. }
  413. int StringValue::compare(const void *mem)
  414. {
  415. // if (type->isCaseSensitive())
  416. return memcmp(val.get(), (const char *) mem, type->getSize());
  417. // return memicmp(val.get(), (const char *) mem, type->getSize());
  418. }
  419. IValue *StringValue::castTo(ITypeInfo *t)
  420. {
  421. t = queryUnqualifiedType(t);
  422. if (t==type)
  423. return LINK(this);
  424. type_t tc = t->getTypeCode();
  425. if (tc == type_any)
  426. return LINK(this);
  427. const char * str = (const char *)val.get();
  428. if (tc == type_data)
  429. return t->castFrom(type->getSize(), str); //NB: Must not go through translation in default case
  430. ICharsetInfo * srcCharset = type->queryCharset();
  431. Owned<ICharsetInfo> asciiCharset = getCharset(asciiAtom);
  432. if (queryDefaultTranslation(asciiCharset, srcCharset))
  433. {
  434. Owned<ITypeInfo> asciiType = getAsciiType(type);
  435. Owned<IValue> asciiValue = createStringValue(str, LINK(asciiType), type->getStringLen(), srcCharset);
  436. return asciiValue->castTo(t);
  437. }
  438. return doCastTo(type->getStringLen(), str, t);
  439. }
  440. const void *StringValue::queryValue() const
  441. {
  442. return (const void *) val.get();
  443. }
  444. bool StringValue::getBoolValue()
  445. {
  446. //return true if any character is non space
  447. //return true if any non zero character
  448. const char * cur = (const char *)val.get();
  449. unsigned len = type->getSize();
  450. char fill = type->queryCharset()->queryFillChar();
  451. while (len--)
  452. {
  453. if (*cur++ != fill)
  454. return true;
  455. }
  456. return false;
  457. }
  458. __int64 StringValue::getIntValue()
  459. {
  460. return rtlStrToInt8(val.length(), (const char *)val.get());
  461. }
  462. const char *StringValue::getStringValue(StringBuffer &out)
  463. {
  464. out.append(type->getSize(), (const char *)val.get());
  465. return out.str();
  466. }
  467. const char *StringValue::getUTF8Value(StringBuffer &out)
  468. {
  469. ICharsetInfo * srcCharset = type->queryCharset();
  470. Owned<ICharsetInfo> asciiCharset = getCharset(asciiAtom);
  471. if (queryDefaultTranslation(asciiCharset, srcCharset))
  472. {
  473. Owned<ITypeInfo> asciiType = getAsciiType(type);
  474. Owned<IValue> asciiValue = castTo(asciiType);
  475. return asciiValue->getUTF8Value(out);
  476. }
  477. rtlDataAttr temp;
  478. unsigned bufflen;
  479. rtlStrToUtf8X(bufflen, temp.refstr(), type->getSize(), (const char *)val.get());
  480. out.append(rtlUtf8Size(bufflen, temp.getstr()), temp.getstr());
  481. return out.str();
  482. }
  483. void StringValue::pushDecimalValue()
  484. {
  485. DecPushString(type->getSize(),(char *)val.get());
  486. }
  487. IValue *createStringValue(const char *val, unsigned size)
  488. {
  489. return createStringValue(val, makeStringType(size, NULL, NULL));
  490. }
  491. IValue *createStringValue(const char *val, ITypeInfo *type)
  492. {
  493. assertex(type->getSize() != UNKNOWN_LENGTH);
  494. if (type->getSize() == UNKNOWN_LENGTH)
  495. {
  496. ITypeInfo * newType = getStretchedType((size32_t)strlen(val), type);
  497. type->Release();
  498. return new StringValue(val, newType);
  499. }
  500. return new StringValue(val, type);
  501. }
  502. IValue *createStringValue(const char *val, ITypeInfo *type, size32_t srcLength, ICharsetInfo *srcCharset)
  503. {
  504. ITranslationInfo * translation = queryDefaultTranslation(type->queryCharset(), srcCharset);
  505. size32_t tgtLength = type->getSize();
  506. if (tgtLength == UNKNOWN_LENGTH)
  507. {
  508. ITypeInfo * newType = getStretchedType(srcLength, type);
  509. type->Release();
  510. type = newType;
  511. tgtLength = srcLength;
  512. }
  513. if(translation)
  514. {
  515. StringBuffer translated;
  516. translation->translate(translated, srcLength, val);
  517. if(tgtLength > srcLength)
  518. translated.appendN(tgtLength-srcLength, type->queryCharset()->queryFillChar());
  519. return new StringValue(translated.str(), type);
  520. }
  521. else if (tgtLength > srcLength)
  522. {
  523. char * extended = (char *)checked_malloc(tgtLength, DEFVALUE_MALLOC_FAILED);
  524. memcpy(extended, val, srcLength);
  525. memset(extended+srcLength, type->queryCharset()->queryFillChar(), tgtLength-srcLength);
  526. IValue * ret = new StringValue(extended, type);
  527. free(extended);
  528. return ret;
  529. }
  530. else
  531. return new StringValue(val, type);
  532. }
  533. //==========================================================================
  534. UnicodeValue::UnicodeValue(UChar const * _value, ITypeInfo * _type) : MemoryValue(_type)
  535. {
  536. unsigned len = _type->getSize();
  537. assertex(len != UNKNOWN_LENGTH);
  538. val.set(len, _value);
  539. }
  540. const void *UnicodeValue::queryValue() const
  541. {
  542. return (const void *) val.get();
  543. }
  544. const char *UnicodeValue::generateCPP(StringBuffer &out, CompilerType compiler)
  545. {
  546. out.append("((UChar *)");
  547. MemoryValue::generateCPP(out, compiler);
  548. return out.append(")").str();
  549. }
  550. const char *UnicodeValue::generateECL(StringBuffer &out)
  551. {
  552. char * buff;
  553. unsigned bufflen;
  554. rtlUnicodeToUtf8X(bufflen, buff, type->getStringLen(), (UChar const *)val.get());
  555. out.append("U'");
  556. appendUtf8AsECL(out, rtlUtf8Size(bufflen, buff), buff);
  557. out.append('\'');
  558. rtlFree(buff);
  559. return out.str();
  560. }
  561. IValue *UnicodeValue::castTo(ITypeInfo *t)
  562. {
  563. t = queryUnqualifiedType(t);
  564. if (t==type)
  565. return LINK(this);
  566. type_t tc = t->getTypeCode();
  567. if (tc == type_any)
  568. return LINK(this);
  569. size32_t olen = type->getStringLen();
  570. UChar const * uchars = (const UChar *)val.get();
  571. switch (tc)
  572. {
  573. case type_unicode:
  574. if ((t->getSize() == UNKNOWN_LENGTH) && (type->queryLocale() == t->queryLocale()))
  575. return LINK(this);
  576. return createUnicodeValue(uchars, olen, LINK(t));
  577. case type_string:
  578. {
  579. char * buff;
  580. unsigned bufflen;
  581. rtlUnicodeToCodepageX(bufflen, buff, olen, uchars, t->queryCharset()->queryCodepageName());
  582. Owned<IValue> temp = createStringValue(buff, makeStringType(bufflen, LINK(t->queryCharset()), 0));
  583. rtlFree(buff);
  584. return temp->castTo(t);
  585. }
  586. }
  587. return t->castFrom(olen, uchars);
  588. }
  589. int UnicodeValue::compare(IValue * to)
  590. {
  591. ITypeInfo * toType = to->queryType();
  592. assertex(toType->getTypeCode()==type->getTypeCode());
  593. assertex(haveCommonLocale(type, toType));
  594. char const * locale = str(getCommonLocale(type, toType));
  595. return rtlCompareUnicodeUnicode(type->getStringLen(), (const UChar *)val.get(), toType->getStringLen(), (const UChar *)to->queryValue(), locale);
  596. }
  597. int UnicodeValue::compare(const void *mem)
  598. {
  599. size32_t len = type->getStringLen();
  600. return rtlCompareUnicodeUnicode(len, (const UChar *)val.get(), len, (const UChar *)mem, str(type->queryLocale()));
  601. }
  602. bool UnicodeValue::getBoolValue()
  603. {
  604. return rtlUnicodeToBool(type->getStringLen(), (UChar const *)val.get());
  605. }
  606. __int64 UnicodeValue::getIntValue()
  607. {
  608. return rtlUnicodeToInt8(type->getStringLen(), (UChar const *)val.get());
  609. }
  610. void UnicodeValue::pushDecimalValue()
  611. {
  612. rtlDecPushUnicode(type->getStringLen(), (UChar const *)val.get());
  613. }
  614. const char *UnicodeValue::getStringValue(StringBuffer &out)
  615. {
  616. return getCodepageValue(out, "US-ASCII");
  617. }
  618. void * UnicodeValue::getUCharStringValue(unsigned len, void * out)
  619. {
  620. unsigned vallen = val.length()/2;
  621. if(vallen > len)
  622. vallen = len;
  623. memcpy(out, val.get(), vallen*2);
  624. if(len > vallen)
  625. ((UChar *)out)[vallen] = 0x0000;
  626. return out;
  627. }
  628. const char *UnicodeValue::getUTF8Value(StringBuffer &out)
  629. {
  630. return getCodepageValue(out, "UTF-8");
  631. }
  632. const char *UnicodeValue::getCodepageValue(StringBuffer &out, char const * codepage)
  633. {
  634. char * buff;
  635. unsigned bufflen;
  636. rtlUnicodeToCodepageX(bufflen, buff, val.length()/2, (UChar const *)val.get(), codepage);
  637. out.append(bufflen, buff);
  638. rtlFree(buff);
  639. return out.str();
  640. }
  641. IValue *createUnicodeValue(char const * value, unsigned size, char const * locale, bool utf8, bool unescape)
  642. {
  643. UChar * buff = 0;
  644. unsigned bufflen = 0;
  645. if(unescape)
  646. rtlCodepageToUnicodeXUnescape(bufflen, buff, size, value, utf8 ? "UTF-8" : "US-ASCII");
  647. else
  648. rtlCodepageToUnicodeX(bufflen, buff, size, value, utf8 ? "UTF-8" : "US-ASCII");
  649. IValue * ret = new UnicodeValue(buff, makeUnicodeType(bufflen, createLowerCaseAtom(locale)));
  650. rtlFree(buff);
  651. return ret;
  652. }
  653. IValue *createUtf8Value(size32_t len, char const * value, char const * locale, bool unescape)
  654. {
  655. if (unescape)
  656. {
  657. rtlDataAttr temp;
  658. size32_t newlen = 0;
  659. size32_t size = rtlUtf8Size(len, value);
  660. rtlCodepageToUtf8XUnescape(newlen, temp.refstr(), size, value, "UTF-8");
  661. ITypeInfo * type = makeUtf8Type(newlen, createLowerCaseAtom(locale));
  662. return createUtf8Value(temp.getstr(), type);
  663. }
  664. else
  665. {
  666. ITypeInfo * type = makeUtf8Type(len, createLowerCaseAtom(locale));
  667. return createUtf8Value(value, type);
  668. }
  669. }
  670. IValue *createUnicodeValue(char const * value, ITypeInfo * type)
  671. {
  672. if(type->getSize() == UNKNOWN_LENGTH)
  673. {
  674. type->Release();
  675. return createUnicodeValue(value, (size32_t)strlen(value), str(type->queryLocale()), false);
  676. }
  677. return createUnicodeValue(value, type, type->getStringLen());
  678. }
  679. IValue *createUnicodeValue(char const * value, ITypeInfo * type, unsigned srclen)
  680. {
  681. if(type->getSize() == UNKNOWN_LENGTH)
  682. {
  683. type->Release();
  684. return createUnicodeValue(value, srclen, str(type->queryLocale()), false);
  685. }
  686. UChar * buff = (UChar *)checked_malloc(type->getSize(), DEFVALUE_MALLOC_FAILED);
  687. rtlCodepageToUnicode(type->getStringLen(), buff, srclen, value, "US-ASCII");
  688. IValue * ret = new UnicodeValue(buff, type);
  689. free(buff);
  690. return ret;
  691. }
  692. IValue * createUnicodeValue(UChar const * text, size32_t len, ITypeInfo * type)
  693. {
  694. size32_t nlen = type->getStringLen();
  695. if(nlen == UNKNOWN_LENGTH)
  696. {
  697. ITypeInfo * newType = getStretchedType(len, type);
  698. type->Release();
  699. type = newType;
  700. nlen = len;
  701. }
  702. if (nlen <= len)
  703. return new UnicodeValue(text, type);
  704. UChar * buff = 0;
  705. unsigned bufflen = 0;
  706. rtlUnicodeSubStrFTX(bufflen, buff, len, text, 1, nlen);
  707. assertex(bufflen == nlen);
  708. IValue * ret = new UnicodeValue(buff, type);
  709. rtlFree(buff);
  710. return ret;
  711. }
  712. IValue * createUnicodeValue(size32_t len, const void * text, ITypeInfo * type)
  713. {
  714. return createUnicodeValue((const UChar *)text, len, type);
  715. }
  716. //===========================================================================
  717. void UnicodeAttr::set(UChar const * _text, unsigned _len)
  718. {
  719. free(text);
  720. text = (UChar *) checked_malloc((_len+1)*2, DEFVALUE_MALLOC_FAILED);
  721. memcpy(text, _text, _len*2);
  722. text[_len] = 0x0000;
  723. }
  724. void UnicodeAttr::setown(UChar * _text)
  725. {
  726. free(text);
  727. text = _text;
  728. }
  729. VarUnicodeValue::VarUnicodeValue(unsigned len, const UChar * v, ITypeInfo * _type) : CValue(_type)
  730. {
  731. unsigned typeLen = type->getStringLen();
  732. assertex(typeLen != UNKNOWN_LENGTH);
  733. if (len >= typeLen)
  734. val.set(v, typeLen);
  735. else
  736. {
  737. UChar * temp = (UChar *)checked_malloc((typeLen+1)*2, DEFVALUE_MALLOC_FAILED);
  738. memcpy(temp, v, len*2);
  739. temp[len] = 0;
  740. val.set(temp, typeLen);
  741. free(temp);
  742. }
  743. }
  744. const void * VarUnicodeValue::queryValue() const
  745. {
  746. return val.get();
  747. }
  748. const char * VarUnicodeValue::generateCPP(StringBuffer & out, CompilerType compiler)
  749. {
  750. out.append("(UChar *)");
  751. unsigned len = val.length()*2+1; // not pretty, but adds one null, so that string is double-null-terminated as required
  752. return appendStringAsQuotedCPP(out, len, (char const *)val.get(), len>120).str();
  753. }
  754. const char * VarUnicodeValue::generateECL(StringBuffer & out)
  755. {
  756. char * buff;
  757. unsigned bufflen;
  758. rtlUnicodeToUtf8X(bufflen, buff, val.length(), val.get());
  759. out.append("U'");
  760. appendUtf8AsECL(out, rtlUtf8Size(bufflen, buff), buff);
  761. out.append('\'');
  762. rtlFree(buff);
  763. return out.str();
  764. }
  765. IValue * VarUnicodeValue::castTo(ITypeInfo * t)
  766. {
  767. t = queryUnqualifiedType(t);
  768. if (t==type)
  769. return LINK(this);
  770. type_t tc = t->getTypeCode();
  771. if (tc == type_any)
  772. return LINK(this);
  773. size32_t olen = val.length();
  774. UChar const * uchars = (UChar const *)val.get();
  775. switch (tc)
  776. {
  777. case type_string:
  778. {
  779. char * buff;
  780. unsigned bufflen;
  781. rtlUnicodeToCodepageX(bufflen, buff, olen, uchars, t->queryCharset()->queryCodepageName());
  782. Owned<IValue> temp = createStringValue(buff, makeStringType(bufflen, LINK(t->queryCharset()), 0));
  783. rtlFree(buff);
  784. return temp->castTo(t);
  785. }
  786. }
  787. return t->castFrom(olen, uchars);
  788. }
  789. int VarUnicodeValue::compare(IValue * to)
  790. {
  791. ITypeInfo * toType = to->queryType();
  792. assertex(toType->getTypeCode()==type->getTypeCode());
  793. assertex(haveCommonLocale(type, toType));
  794. char const * locale = str(getCommonLocale(type, toType));
  795. UChar const * rhs = (UChar const *) to->queryValue();
  796. return rtlCompareUnicodeUnicode(val.length(), val.get(), rtlUnicodeStrlen(rhs), rhs, locale);
  797. }
  798. int VarUnicodeValue::compare(const void * mem)
  799. {
  800. UChar const * rhs = (UChar const *) mem;
  801. return rtlCompareUnicodeUnicode(val.length(), val.get(), rtlUnicodeStrlen(rhs), rhs, str(type->queryLocale()));
  802. }
  803. bool VarUnicodeValue::getBoolValue()
  804. {
  805. return rtlUnicodeToBool(val.length(), val.get());
  806. }
  807. __int64 VarUnicodeValue::getIntValue()
  808. {
  809. return rtlUnicodeToInt8(val.length(), val.get());
  810. }
  811. const char * VarUnicodeValue::getStringValue(StringBuffer & out)
  812. {
  813. return getCodepageValue(out, "US-ASCII");
  814. }
  815. void * VarUnicodeValue::getUCharStringValue(unsigned len, void * out)
  816. {
  817. unsigned vallen = val.length();
  818. if(vallen > len)
  819. vallen = len;
  820. memcpy(out, val.get(), vallen*2);
  821. if(len > vallen)
  822. ((UChar *)out)[vallen] = 0x0000;
  823. return out;
  824. }
  825. const char * VarUnicodeValue::getUTF8Value(StringBuffer & out)
  826. {
  827. return getCodepageValue(out, "UTF-8");
  828. }
  829. const char * VarUnicodeValue::getCodepageValue(StringBuffer & out, char const * codepage)
  830. {
  831. char * buff;
  832. unsigned bufflen;
  833. rtlUnicodeToCodepageX(bufflen, buff, val.length(), val.get(), codepage);
  834. out.append(bufflen, buff);
  835. rtlFree(buff);
  836. return out.str();
  837. }
  838. void VarUnicodeValue::pushDecimalValue()
  839. {
  840. rtlDecPushUnicode(val.length(), val.get());
  841. }
  842. void VarUnicodeValue::toMem(void * target)
  843. {
  844. memcpy(target, val.get(), (val.length()+1)*2);
  845. }
  846. unsigned VarUnicodeValue::getHash(unsigned initval)
  847. {
  848. return hashc((unsigned char *) val.get(), val.length()*2, initval);
  849. }
  850. int VarUnicodeValue::rangeCompare(ITypeInfo * targetType)
  851. {
  852. return 0;
  853. }
  854. void VarUnicodeValue::serialize(MemoryBuffer & tgt)
  855. {
  856. tgt.append(val.length());
  857. tgt.append(val.length()*2, val.get());
  858. }
  859. void VarUnicodeValue::deserialize(MemoryBuffer & src)
  860. {
  861. size32_t len;
  862. src.read(len);
  863. UChar * buff = (UChar *) checked_malloc(len*2, DEFVALUE_MALLOC_FAILED);
  864. src.read(len*2, buff);
  865. val.setown(buff);
  866. }
  867. IValue *createVarUnicodeValue(char const * value, unsigned size, char const * locale, bool utf8, bool unescape)
  868. {
  869. UChar * buff = 0;
  870. unsigned bufflen = 0;
  871. if(unescape)
  872. rtlCodepageToUnicodeXUnescape(bufflen, buff, size, value, utf8 ? "UTF-8" : "US-ASCII");
  873. else
  874. rtlCodepageToUnicodeX(bufflen, buff, size, value, utf8 ? "UTF-8" : "US-ASCII");
  875. IValue * ret = new VarUnicodeValue(bufflen, buff, makeUnicodeType(bufflen, createLowerCaseAtom(locale)));
  876. rtlFree(buff);
  877. return ret;
  878. }
  879. IValue * createVarUnicodeValue(UChar const * text, size32_t len, ITypeInfo * type)
  880. {
  881. size32_t nlen = type->getStringLen();
  882. if(nlen == UNKNOWN_LENGTH)
  883. {
  884. ITypeInfo * newType = getStretchedType(len, type);
  885. type->Release();
  886. type = newType;
  887. nlen = len;
  888. }
  889. if(nlen > len)
  890. nlen = len;
  891. UChar * buff = 0;
  892. unsigned bufflen = 0;
  893. rtlUnicodeSubStrFTX(bufflen, buff, len, text, 1, nlen);
  894. assertex(bufflen == nlen);
  895. IValue * ret = new VarUnicodeValue(bufflen, buff, type);
  896. rtlFree(buff);
  897. return ret;
  898. }
  899. IValue * createVarUnicodeValue(size32_t len, const void * text, ITypeInfo * type)
  900. {
  901. return createVarUnicodeValue((UChar const *)text, len, type);
  902. }
  903. //==========================================================================
  904. Utf8Value::Utf8Value(const char * _value, ITypeInfo * _type) : MemoryValue(_type)
  905. {
  906. unsigned len = _type->getStringLen();
  907. assertex(len != UNKNOWN_LENGTH);
  908. unsigned size = rtlUtf8Size(len, _value);
  909. val.set(size, _value);
  910. }
  911. size32_t Utf8Value::getSize()
  912. {
  913. #ifdef _DEBUG
  914. unsigned size = rtlUtf8Size(type->getStringLen(), val.get());
  915. assertex(size == val.length());
  916. #endif
  917. return val.length();
  918. }
  919. const void *Utf8Value::queryValue() const
  920. {
  921. return (const void *) val.get();
  922. }
  923. const char *Utf8Value::generateCPP(StringBuffer &out, CompilerType compiler)
  924. {
  925. out.append("((char *)");
  926. MemoryValue::generateCPP(out, compiler);
  927. return out.append(")").str();
  928. }
  929. const char *Utf8Value::generateECL(StringBuffer &out)
  930. {
  931. unsigned size = rtlUtf8Size(type->getStringLen(), val.get());
  932. out.append("U");
  933. appendStringAsQuotedECL(out, size, (const char *)val.get());
  934. return out.str();
  935. }
  936. IValue *Utf8Value::castTo(ITypeInfo *t)
  937. {
  938. t = queryUnqualifiedType(t);
  939. if (t==type)
  940. return LINK(this);
  941. type_t tc = t->getTypeCode();
  942. if (tc == type_any)
  943. return LINK(this);
  944. size32_t olen = type->getStringLen();
  945. const char * data = (const char *)val.get();
  946. switch(tc)
  947. {
  948. case type_unicode:
  949. case type_varunicode:
  950. {
  951. Owned<IValue> temp = createUnicodeValue(data, rtlUtf8Size(olen, data), str(t->queryLocale()), true, false);
  952. return temp->castTo(t);
  953. }
  954. case type_utf8:
  955. {
  956. unsigned targetLength = t->getStringLen();
  957. if (targetLength == UNKNOWN_LENGTH)
  958. {
  959. if (type->queryLocale() == t->queryLocale())
  960. return LINK(this);
  961. }
  962. return createUtf8Value(olen, data, LINK(t));
  963. }
  964. case type_string:
  965. {
  966. rtlDataAttr buff;
  967. unsigned bufflen;
  968. rtlUtf8ToCodepageX(bufflen, buff.refstr(), olen, data, t->queryCharset()->queryCodepageName());
  969. Owned<IValue> temp = createStringValue(buff.getstr(), makeStringType(bufflen, LINK(t->queryCharset()), 0));
  970. return temp->castTo(t);
  971. }
  972. case type_data:
  973. {
  974. unsigned size = rtlUtf8Size(olen, data);
  975. if (size >= t->getSize())
  976. return createDataValue(data, LINK(t));
  977. Owned<IValue> temp = createDataValue(data, size);
  978. return temp->castTo(t);
  979. }
  980. }
  981. Owned<ITypeInfo> stringType = makeStringType(UNKNOWN_LENGTH, LINK(t->queryCharset()), LINK(t->queryCollation()));
  982. Owned<IValue> stringValue = castTo(stringType);
  983. return stringValue->castTo(t);
  984. }
  985. int Utf8Value::compare(IValue * to)
  986. {
  987. ITypeInfo * toType = to->queryType();
  988. assertex(toType->getTypeCode()==type->getTypeCode());
  989. assertex(haveCommonLocale(type, toType));
  990. char const * locale = str(getCommonLocale(type, toType));
  991. return rtlCompareUtf8Utf8(type->getStringLen(), (const char *)val.get(), toType->getStringLen(), (const char *)to->queryValue(), locale);
  992. }
  993. int Utf8Value::compare(const void *mem)
  994. {
  995. size32_t len = type->getStringLen();
  996. return rtlCompareUtf8Utf8(len, (const char *)val.get(), len, (const char *)mem, str(type->queryLocale()));
  997. }
  998. bool Utf8Value::getBoolValue()
  999. {
  1000. return rtlUtf8ToBool(type->getStringLen(), (const char *)val.get());
  1001. }
  1002. __int64 Utf8Value::getIntValue()
  1003. {
  1004. return rtlUtf8ToInt(type->getStringLen(), (const char *)val.get());
  1005. }
  1006. void Utf8Value::pushDecimalValue()
  1007. {
  1008. rtlDecPushUtf8(type->getStringLen(), (const char *)val.get());
  1009. }
  1010. const char * Utf8Value::getStringValue(StringBuffer &out)
  1011. {
  1012. return getCodepageValue(out, "US-ASCII");
  1013. }
  1014. const char * Utf8Value::getCodepageValue(StringBuffer &out, char const * codepage)
  1015. {
  1016. unsigned bufflen;
  1017. rtlDataAttr buff;
  1018. rtlUtf8ToCodepageX(bufflen, buff.refstr(), type->getStringLen(), (const char *)val.get(), codepage);
  1019. out.append(bufflen, buff.getstr());
  1020. return out.str();
  1021. }
  1022. void * Utf8Value::getUCharStringValue(unsigned len, void * _out)
  1023. {
  1024. unsigned thisLen = type->getStringLen();
  1025. UChar * out = (UChar *)_out;
  1026. rtlUtf8ToUnicode(len, out, thisLen, (const char *)val.get());
  1027. //wierd semantics.
  1028. if (len > thisLen)
  1029. out[thisLen] = 0x0000;
  1030. return out;
  1031. }
  1032. const char *Utf8Value::getUTF8Value(StringBuffer &out)
  1033. {
  1034. unsigned size = rtlUtf8Size(type->getStringLen(), val.get());
  1035. return out.append(size, (const char *)val.get()).str();
  1036. }
  1037. IValue * createUtf8Value(const char * text, ITypeInfo * type)
  1038. {
  1039. return new Utf8Value(text, type);
  1040. }
  1041. IValue * createUtf8Value(size32_t len, const char * text, ITypeInfo * type)
  1042. {
  1043. size32_t nlen = type->getStringLen();
  1044. if(nlen == UNKNOWN_LENGTH)
  1045. {
  1046. ITypeInfo * newType = getStretchedType(len, type);
  1047. type->Release();
  1048. type = newType;
  1049. nlen = len;
  1050. }
  1051. if (nlen <= len)
  1052. return createUtf8Value(text, type);
  1053. rtlDataAttr buff(nlen * 4);
  1054. rtlUtf8SubStrFT(nlen, buff.getstr(), len, text, 1, nlen);
  1055. return new Utf8Value(buff.getstr(), type);
  1056. }
  1057. //===========================================================================
  1058. DataValue::DataValue(const void *v, ITypeInfo *_type) : MemoryValue(v, _type)
  1059. {
  1060. }
  1061. const char *DataValue::generateECL(StringBuffer &out)
  1062. {
  1063. out.append("X'");
  1064. appendDataAsHex(out, val.length(), val.get());
  1065. out.append('\'');
  1066. return out.str();
  1067. }
  1068. IValue *DataValue::castTo(ITypeInfo *t)
  1069. {
  1070. t = queryUnqualifiedType(t);
  1071. if (t==type)
  1072. return LINK(this);
  1073. type_t tc = t->getTypeCode();
  1074. if (tc == type_any)
  1075. return LINK(this);
  1076. size32_t osize = type->getSize();
  1077. size32_t nsize = t->getSize();
  1078. switch (tc)
  1079. {
  1080. case type_data:
  1081. if (nsize == UNKNOWN_LENGTH)
  1082. return LINK(this);
  1083. if (nsize <= osize)
  1084. return new DataValue(val.get(), LINK(t));
  1085. else
  1086. {
  1087. char *newstr = (char *) checked_malloc(nsize, DEFVALUE_MALLOC_FAILED);
  1088. memcpy(newstr, val.get(), osize);
  1089. memset(newstr+osize, 0, nsize-osize);
  1090. IValue * ret = new DataValue(newstr, LINK(t));
  1091. free(newstr);
  1092. return ret;
  1093. }
  1094. break;
  1095. case type_string:
  1096. if (nsize == UNKNOWN_LENGTH)
  1097. {
  1098. nsize = osize;
  1099. t = getStretchedType(osize, t);
  1100. }
  1101. else
  1102. t->Link();
  1103. if (nsize <= osize)
  1104. return new StringValue((char *)val.get(), t);
  1105. else
  1106. {
  1107. char *newstr = (char *) checked_malloc(nsize, DEFVALUE_MALLOC_FAILED);
  1108. memcpy(newstr, val.get(), osize);
  1109. memset(newstr+osize, t->queryCharset()->queryFillChar(), nsize-osize);
  1110. IValue * ret = new StringValue(newstr, t);
  1111. free(newstr);
  1112. return ret;
  1113. }
  1114. break;
  1115. case type_decimal:
  1116. return t->castFrom(osize, (const char *)val.get());
  1117. case type_boolean:
  1118. return createBoolValue(getBoolValue());
  1119. }
  1120. ITypeInfo * tempType = makeStringType(getSize(), NULL, NULL);
  1121. IValue * temp = castTo(tempType);
  1122. IValue * ret = temp->castTo(t);
  1123. temp->Release();
  1124. tempType->Release();
  1125. return ret;
  1126. }
  1127. const void *DataValue::queryValue() const
  1128. {
  1129. return val.get();
  1130. }
  1131. bool DataValue::getBoolValue()
  1132. {
  1133. //return true if any character is non space
  1134. //return true if any non zero character
  1135. const char * cur = (const char *)val.get();
  1136. unsigned len = type->getSize();
  1137. char fill = 0;
  1138. while (len--)
  1139. {
  1140. if (*cur++ != fill)
  1141. return true;
  1142. }
  1143. return false;
  1144. }
  1145. const char *DataValue::getStringValue(StringBuffer &out)
  1146. {
  1147. appendDataAsHex(out, val.length(), val.get());
  1148. return out.str();
  1149. }
  1150. IValue *createDataValue(const char *val, unsigned size)
  1151. {
  1152. return createDataValue(val, makeDataType(size));
  1153. }
  1154. IValue *createDataValue(const char *val, ITypeInfo *type)
  1155. {
  1156. assertex(type->getSize() != UNKNOWN_LENGTH);
  1157. return new DataValue(val, type);
  1158. }
  1159. //===========================================================================
  1160. QStringValue::QStringValue(unsigned len, const void *v, ITypeInfo *_type) : MemoryValue(_type)
  1161. {
  1162. unsigned newSize = _type->getSize();
  1163. char * temp = (char *)val.allocate(newSize);
  1164. rtlStrToQStr(_type->getStringLen(), temp, len, v);
  1165. }
  1166. QStringValue::QStringValue(const char *v, ITypeInfo *_type) : MemoryValue(_type)
  1167. {
  1168. unsigned newSize = _type->getSize();
  1169. char * temp = (char *)val.allocate(newSize);
  1170. memcpy(temp, v, newSize);
  1171. }
  1172. const char *QStringValue::generateECL(StringBuffer &out)
  1173. {
  1174. unsigned strLen = type->getStringLen();
  1175. char * strData = (char *)checked_malloc(strLen, DEFVALUE_MALLOC_FAILED);
  1176. rtlQStrToStr(strLen, strData, strLen, (const char *)val.get());
  1177. out.append('Q');
  1178. appendStringAsQuotedECL(out, strLen, strData);
  1179. free(strData);
  1180. return out.str();
  1181. }
  1182. int QStringValue::compare(IValue * to)
  1183. {
  1184. return rtlCompareQStrQStr(type->getStringLen(), val.get(), to->queryType()->getStringLen(), to->queryValue());
  1185. }
  1186. int QStringValue::compare(const void *mem)
  1187. {
  1188. size32_t len = type->getStringLen();
  1189. return rtlCompareQStrQStr(len, val.get(), len, mem);
  1190. }
  1191. IValue *QStringValue::castTo(ITypeInfo *t)
  1192. {
  1193. t = queryUnqualifiedType(t);
  1194. if (t==type)
  1195. return LINK(this);
  1196. type_t tc = t->getTypeCode();
  1197. if (tc == type_any)
  1198. return LINK(this);
  1199. switch (tc)
  1200. {
  1201. case type_boolean:
  1202. return createBoolValue(getBoolValue());
  1203. }
  1204. unsigned strLen = type->getStringLen();
  1205. char * strData = (char *)checked_malloc(strLen, DEFVALUE_MALLOC_FAILED);
  1206. rtlQStrToStr(strLen, strData, strLen, (const char *)val.get());
  1207. IValue * ret = t->castFrom(strLen, strData);
  1208. free(strData);
  1209. return ret;
  1210. }
  1211. const void *QStringValue::queryValue() const
  1212. {
  1213. return (const void *) val.get();
  1214. }
  1215. bool QStringValue::getBoolValue()
  1216. {
  1217. //return true if any character is non space
  1218. //return true if any non zero character
  1219. const char * cur = (const char *)val.get();
  1220. unsigned len = type->getSize();
  1221. while (len--)
  1222. {
  1223. if (*cur++)
  1224. return true;
  1225. }
  1226. return false;
  1227. }
  1228. __int64 QStringValue::getIntValue()
  1229. {
  1230. unsigned strLen = type->getStringLen();
  1231. char * strData = (char *)checked_malloc(strLen, DEFVALUE_MALLOC_FAILED);
  1232. rtlQStrToStr(strLen, strData, strLen, (const char *)val.get());
  1233. __int64 ret = rtlStrToInt8(strLen, strData);
  1234. free(strData);
  1235. return ret;
  1236. }
  1237. const char *QStringValue::getStringValue(StringBuffer &out)
  1238. {
  1239. unsigned strLen = type->getStringLen();
  1240. char * strData = (char *)checked_malloc(strLen, DEFVALUE_MALLOC_FAILED);
  1241. rtlQStrToStr(strLen, strData, strLen, (const char *)val.get());
  1242. out.append(strLen, strData);
  1243. free(strData);
  1244. return out.str();
  1245. }
  1246. void QStringValue::pushDecimalValue()
  1247. {
  1248. unsigned strLen = type->getStringLen();
  1249. char * strData = (char *)checked_malloc(strLen, DEFVALUE_MALLOC_FAILED);
  1250. rtlQStrToStr(strLen, strData, strLen, (const char *)val.get());
  1251. DecPushString(strLen, strData);
  1252. free(strData);
  1253. }
  1254. IValue *createQStringValue(unsigned len, const char *val, ITypeInfo *type)
  1255. {
  1256. if (type->getSize() == UNKNOWN_LENGTH)
  1257. {
  1258. ITypeInfo * newType = getStretchedType(len, type);
  1259. type->Release();
  1260. return new QStringValue(len, val, newType);
  1261. }
  1262. return new QStringValue(len, val, type);
  1263. }
  1264. //===========================================================================
  1265. CharValue::CharValue(char _val, ITypeInfo * _type) : CValue(_type)
  1266. {
  1267. val = _val;
  1268. }
  1269. const char *CharValue::generateECL(StringBuffer &out)
  1270. {
  1271. appendStringAsQuotedECL(out, 1, &val);
  1272. return out.str();
  1273. }
  1274. const char *CharValue::generateCPP(StringBuffer &out, CompilerType compiler)
  1275. {
  1276. switch (val)
  1277. {
  1278. case '\n': out.append("'\\n'"); break;
  1279. case '\r': out.append("'\\r'"); break;
  1280. case '\t': out.append("'\\t'"); break;
  1281. case '\'': out.append("'\\''"); break;
  1282. default:
  1283. if ((val >= ' ') && (val <= 126))
  1284. out.append('\'').append(val).append('\'');
  1285. else
  1286. out.append((int)val);
  1287. break;
  1288. }
  1289. return out.str();
  1290. }
  1291. void CharValue::toMem(void *target)
  1292. {
  1293. *(char *)target = val;
  1294. }
  1295. unsigned CharValue::getHash(unsigned initval)
  1296. {
  1297. char buf = val;;
  1298. return hashc((unsigned char *)&buf, 1, initval);
  1299. }
  1300. int CharValue::compare(IValue *_to)
  1301. {
  1302. assertThrow(_to->queryType()==type);
  1303. CharValue *to = (CharValue *) _to;
  1304. return memcmp(&val, &to->val, 1);
  1305. }
  1306. int CharValue::compare(const void *mem)
  1307. {
  1308. return memcmp(&val, (const char *) mem, 1);
  1309. }
  1310. IValue *CharValue::castTo(ITypeInfo *t)
  1311. {
  1312. t = queryUnqualifiedType(t);
  1313. if (t==type)
  1314. return LINK(this);
  1315. type_t tc = t->getTypeCode();
  1316. if (tc == type_any)
  1317. return LINK(this);
  1318. switch (tc)
  1319. {
  1320. case type_boolean:
  1321. return createBoolValue(val != 0);
  1322. }
  1323. return t->castFrom(1, &val);
  1324. }
  1325. bool CharValue::getBoolValue()
  1326. {
  1327. return (val != ' ');
  1328. }
  1329. const char *CharValue::getStringValue(StringBuffer &out)
  1330. {
  1331. return out.append(val).str();
  1332. }
  1333. void CharValue::pushDecimalValue()
  1334. {
  1335. DecPushString(1,&val);
  1336. }
  1337. void CharValue::serialize(MemoryBuffer &tgt)
  1338. {
  1339. tgt.append(val);
  1340. }
  1341. void CharValue::deserialize(MemoryBuffer &src)
  1342. {
  1343. src.read(val);
  1344. }
  1345. int CharValue::rangeCompare(ITypeInfo * targetType)
  1346. {
  1347. //MORE: to integegral types?
  1348. return 0;
  1349. }
  1350. IValue *createCharValue(char val, bool caseSensitive)
  1351. {
  1352. return createCharValue(val, makeCharType(caseSensitive));
  1353. }
  1354. IValue *createCharValue(char val, ITypeInfo *type)
  1355. {
  1356. return new CharValue(val, type);
  1357. }
  1358. //===========================================================================
  1359. int IntValue::compare(IValue *_to)
  1360. {
  1361. assertThrow(_to->queryType()==type);
  1362. IntValue *to = (IntValue *) _to;
  1363. if (val == to->val)
  1364. return 0;
  1365. else if (type->isSigned())
  1366. return (__int64) val > (__int64) to->val ? 1 : -1;
  1367. else
  1368. return val > to->val ? 1 : -1;
  1369. }
  1370. IValue * IntValue::castViaString(ITypeInfo * t)
  1371. {
  1372. size32_t len;
  1373. char * text;
  1374. if (type->isSigned())
  1375. rtlInt8ToStrX(len, text, val);
  1376. else
  1377. rtlUInt8ToStrX(len, text, (unsigned __int64)val);
  1378. IValue * ret = t->castFrom(len, text); // Include EBCDIC conversion, and creating correct type
  1379. rtlFree(text);
  1380. return ret;
  1381. }
  1382. IValue *IntValue::castTo(ITypeInfo *t)
  1383. {
  1384. t = queryUnqualifiedType(t);
  1385. if (t==type)
  1386. return LINK(this);
  1387. type_t tc = t->getTypeCode();
  1388. if (tc == type_any)
  1389. return LINK(this);
  1390. unsigned nLen = t->getStringLen();
  1391. switch (tc)
  1392. {
  1393. case type_qstring:
  1394. case type_utf8:
  1395. case type_unicode:
  1396. return castViaString(t);
  1397. case type_string:
  1398. case type_varstring:
  1399. {
  1400. if (nLen == UNKNOWN_LENGTH)
  1401. return castViaString(t);
  1402. char *newstr = (char *) checked_malloc(nLen, DEFVALUE_MALLOC_FAILED);
  1403. if (type->isSigned())
  1404. rtlInt8ToStr(nLen, newstr, val);
  1405. else
  1406. rtlUInt8ToStr(nLen, newstr, (unsigned __int64)val);
  1407. IValue * ret = t->castFrom(nLen, newstr);
  1408. free(newstr);
  1409. return ret;
  1410. }
  1411. case type_int:
  1412. case type_swapint:
  1413. return createTruncIntValue(val, LINK(t));
  1414. case type_boolean:
  1415. return createBoolValue(val!=0);
  1416. }
  1417. return t->castFrom(type->isSigned(), (__int64)val);
  1418. }
  1419. byte * IntValue::getAddressValue()
  1420. {
  1421. #if __BYTE_ORDER == __LITTLE_ENDIAN
  1422. return (byte *)&val;
  1423. #else
  1424. return (byte *)&val + (8 - type->getSize());
  1425. #endif
  1426. }
  1427. const void * IntValue::queryValue() const
  1428. {
  1429. #if __BYTE_ORDER == __LITTLE_ENDIAN
  1430. return reinterpret_cast<const byte *>(&val);
  1431. #else
  1432. return reinterpret_cast<const byte *>(&val) + (8 - type->getSize());
  1433. #endif
  1434. }
  1435. void IntValue::toMem(void *target)
  1436. {
  1437. size32_t size = type->getSize();
  1438. const byte * data = getAddressValue();
  1439. if (type->isSwappedEndian())
  1440. _cpyrevn(target, data, size);
  1441. else
  1442. memcpy(target, data, size);
  1443. }
  1444. unsigned IntValue::getHash(unsigned initval)
  1445. {
  1446. return hashc(getAddressValue(), type->getSize(), initval);
  1447. }
  1448. const char *IntValue::generateECL(StringBuffer &s)
  1449. {
  1450. return getStringValue(s);
  1451. }
  1452. const char *IntValue::generateCPP(StringBuffer &s, CompilerType compiler)
  1453. {
  1454. if (type->isSwappedEndian())
  1455. {
  1456. if (type->isSigned())
  1457. s.append(rtlReadSwapInt(getAddressValue(), type->getSize()));
  1458. else
  1459. s.append(rtlReadSwapUInt(getAddressValue(), type->getSize()));
  1460. }
  1461. else
  1462. getStringValue(s);
  1463. switch (compiler)
  1464. {
  1465. case GccCppCompiler:
  1466. if (val && (type->getSize() > sizeof(unsigned)))
  1467. {
  1468. s.append("LL");
  1469. if (!type->isSigned())
  1470. s.append("U");
  1471. }
  1472. else if (!type->isSigned())
  1473. s.append("U");
  1474. break;
  1475. case Vs6CppCompiler:
  1476. if (val && (type->getSize() > sizeof(unsigned)))
  1477. {
  1478. if (!type->isSigned())
  1479. s.append("U");
  1480. s.append("i64");
  1481. }
  1482. else if (!type->isSigned())
  1483. s.append("U");
  1484. break;
  1485. default:
  1486. throwUnexpected();
  1487. }
  1488. return s.str();
  1489. }
  1490. const char *IntValue::getStringValue(StringBuffer &s)
  1491. {
  1492. if (type->isSigned())
  1493. s.append((__int64)val);
  1494. else
  1495. s.append(val);
  1496. return s.str();
  1497. }
  1498. bool IntValue::getBoolValue()
  1499. {
  1500. return val != 0;
  1501. }
  1502. __int64 IntValue::getIntValue()
  1503. {
  1504. return val;
  1505. }
  1506. void IntValue::pushDecimalValue(void)
  1507. {
  1508. if (type->isSigned())
  1509. DecPushInt64(val);
  1510. else
  1511. DecPushUInt64(val);
  1512. }
  1513. int IntValue::rangeCompare(ITypeInfo * targetType)
  1514. {
  1515. if (type->isSigned())
  1516. return ::rangeCompare((double)(__int64)val, targetType);
  1517. return ::rangeCompare((double)(unsigned __int64)val, targetType);
  1518. }
  1519. void IntValue::serialize(MemoryBuffer &tgt)
  1520. {
  1521. tgt.append(val);
  1522. }
  1523. void IntValue::deserialize(MemoryBuffer &src)
  1524. {
  1525. src.read(val);
  1526. }
  1527. //===========================================================================
  1528. int PackedIntValue::compare(IValue *_to)
  1529. {
  1530. assertThrow(_to->queryType()==type);
  1531. unsigned __int64 val = value->getIntValue();
  1532. unsigned __int64 toVal = _to->getIntValue();
  1533. if (val == toVal)
  1534. return 0;
  1535. else if (type->isSigned())
  1536. return (__int64) val > (__int64) toVal ? 1 : -1;
  1537. else
  1538. return val > toVal ? 1 : -1;
  1539. }
  1540. void PackedIntValue::toMem(void *target)
  1541. {
  1542. unsigned __int64 val = value->getIntValue();
  1543. if (type->isSigned())
  1544. rtlSetPackedSigned(target, val);
  1545. else
  1546. rtlSetPackedUnsigned(target, val);
  1547. }
  1548. //---------------------------------------------------------------------------
  1549. bool isInRange(__int64 value, bool isSigned, unsigned size)
  1550. {
  1551. if (isSigned)
  1552. value += (maxIntValue[size]+1);
  1553. if (value & ~maxUIntValue[size])
  1554. return false;
  1555. return true;
  1556. }
  1557. inline unsigned getMinSize(unsigned __int64 value, bool isSigned)
  1558. {
  1559. if (isSigned)
  1560. {
  1561. if ((value + 0x80) <= 0xff) return 1;
  1562. if ((value + 0x8000) <= 0xffff) return 2;
  1563. if ((value + 0x80000000) <= 0xffffffff) return 4;
  1564. }
  1565. else
  1566. {
  1567. if (value <= 0xff) return 1;
  1568. if (value <= 0xffff) return 2;
  1569. if (value <= 0xffffffff) return 4;
  1570. }
  1571. return 8;
  1572. }
  1573. unsigned getMinimumIntegerSize(unsigned __int64 value, bool isSigned)
  1574. {
  1575. return getMinSize(value, isSigned);
  1576. }
  1577. static unsigned __int64 truncateInt(unsigned __int64 value, unsigned size, bool isSigned)
  1578. {
  1579. if (isSigned)
  1580. {
  1581. unsigned shift = (8-size)*8;
  1582. value <<= shift;
  1583. value = ((__int64)value) >> shift;
  1584. }
  1585. else
  1586. {
  1587. value &= maxUIntValue[size];
  1588. }
  1589. return value;
  1590. }
  1591. IValue *createIntValue(__int64 val, ITypeInfo * type)
  1592. {
  1593. #ifdef _DEBUG
  1594. if (!isInRange(val, type->isSigned(), type->getSize()))
  1595. throw MakeStringException(1, "Out of range value");
  1596. #endif
  1597. return new IntValue(val, type);
  1598. }
  1599. IValue *createIntValue(__int64 val, unsigned size, bool isSigned)
  1600. {
  1601. #ifdef _DEBUG
  1602. if (!isInRange(val, isSigned, size))
  1603. throw MakeStringException(0, "Out of range value");
  1604. #endif
  1605. ITypeInfo * type = makeIntType(size, isSigned);
  1606. return new IntValue(val, type);
  1607. }
  1608. IValue *createTruncIntValue(__int64 val, ITypeInfo * type)
  1609. {
  1610. if (type->getTypeCode() == type_packedint)
  1611. return createPackedIntValue(val, type);
  1612. return new IntValue(truncateInt(val, type->getSize(), type->isSigned()), type);
  1613. }
  1614. IValue *createPackedIntValue(__int64 val, ITypeInfo * type)
  1615. {
  1616. assertex(type->getTypeCode() == type_packedint);
  1617. ITypeInfo * promotedType = type->queryPromotedType();
  1618. IValue * value = createTruncIntValue(val, LINK(promotedType));
  1619. return new PackedIntValue(value, type);
  1620. }
  1621. IValue *createPackedIntValue(IValue * val, ITypeInfo * type)
  1622. {
  1623. assertex(type->getTypeCode() == type_packedint);
  1624. return new PackedIntValue(val, type);
  1625. }
  1626. IValue *createTruncIntValue(__int64 val, unsigned size, bool isSigned)
  1627. {
  1628. val = truncateInt(val, size, isSigned);
  1629. ITypeInfo * type = makeIntType(size, isSigned);
  1630. return new IntValue(val, type);
  1631. }
  1632. IValue * createMinIntValue(__int64 value)
  1633. {
  1634. return createIntValue(value, getMinSize(value, true), true);
  1635. }
  1636. IValue * createMinUIntValue(unsigned __int64 value)
  1637. {
  1638. return createIntValue(value, getMinSize(value, false), false);
  1639. }
  1640. IValue *createBitfieldValue(__int64 val, ITypeInfo * type)
  1641. {
  1642. #ifdef _DEBUG
  1643. size32_t bitsize = type->getBitSize();
  1644. if (bitsize != sizeof(__int64) * 8)
  1645. val = val & (((__int64)1 << bitsize)-1);
  1646. #endif
  1647. return new BitfieldValue(val, type);
  1648. }
  1649. //===========================================================================
  1650. IValue *EnumValue::castTo(ITypeInfo * t)
  1651. {
  1652. t = queryUnqualifiedType(t);
  1653. if (t == type)
  1654. return LINK(this);
  1655. type_t tc = t->getTypeCode();
  1656. if (tc == type_any)
  1657. return LINK(this);
  1658. if (t->isInteger())
  1659. return IntValue::castTo(t);
  1660. CEnumeratedTypeInfo * et = (CEnumeratedTypeInfo *) type;
  1661. IValue *ret = et->queryValue((int) val);
  1662. if (ret)
  1663. return ret->castTo(t);
  1664. // cope better with unknown index
  1665. Owned<IValue> temp = createStringValue("", 0U);
  1666. return temp->castTo(t);
  1667. }
  1668. IValue * createEnumValue(int value, ITypeInfo * type)
  1669. {
  1670. return new EnumValue(value, type);
  1671. }
  1672. //===========================================================================
  1673. int RealValue::compare(IValue *_to)
  1674. {
  1675. assertThrow(_to->queryType()==type);
  1676. RealValue *to = (RealValue *) _to;
  1677. if (val == to->val)
  1678. return 0;
  1679. else
  1680. return val > to->val ? 1 : -1;
  1681. }
  1682. IValue *RealValue::castTo(ITypeInfo *t)
  1683. {
  1684. t = queryUnqualifiedType(t);
  1685. if (t==type)
  1686. return LINK(this);
  1687. type_t tc = t->getTypeCode();
  1688. if (tc == type_any)
  1689. return LINK(this);
  1690. switch (tc)
  1691. {
  1692. case type_real:
  1693. {
  1694. double value = val;
  1695. switch (t->getSize())
  1696. {
  1697. case 4: value = (float)value; break;
  1698. case 8: value = (double)value; break;
  1699. }
  1700. return createRealValue(value, LINK(t));
  1701. }
  1702. case type_int:
  1703. case type_swapint:
  1704. case type_packedint:
  1705. return createTruncIntValue((__int64)val, LINK(t));
  1706. case type_boolean:
  1707. return createBoolValue(val!=0);
  1708. }
  1709. return t->castFrom(val);
  1710. }
  1711. void RealValue::toMem(void *target)
  1712. {
  1713. RealUnion u;
  1714. size32_t size = type->getSize();
  1715. switch (size)
  1716. {
  1717. case 4:
  1718. u.r4 = (float)val;
  1719. break;
  1720. case 8:
  1721. u.r8 = val;
  1722. break;
  1723. };
  1724. memcpy(target, &u, size);
  1725. }
  1726. unsigned RealValue::getHash(unsigned initval)
  1727. {
  1728. RealUnion u;
  1729. size32_t size = type->getSize();
  1730. switch (size)
  1731. {
  1732. case 4:
  1733. u.r4 = (float)val;
  1734. break;
  1735. case 8:
  1736. u.r8 = val;
  1737. break;
  1738. };
  1739. return hashc((unsigned char *) &u, size, initval);
  1740. }
  1741. const char *RealValue::generateECL(StringBuffer &s)
  1742. {
  1743. return getStringValue(s);
  1744. }
  1745. const char *RealValue::generateCPP(StringBuffer &s, CompilerType compiler)
  1746. {
  1747. return getStringValue(s);
  1748. }
  1749. const char *RealValue::getStringValue(StringBuffer &s)
  1750. {
  1751. size32_t size = type->getSize();
  1752. if (size==4)
  1753. return s.append((float) val);
  1754. else
  1755. return s.append(val);
  1756. }
  1757. bool RealValue::getBoolValue()
  1758. {
  1759. return val != 0;
  1760. }
  1761. __int64 RealValue::getIntValue()
  1762. {
  1763. return (__int64)(unsigned __int64)val;
  1764. }
  1765. double RealValue::getRealValue()
  1766. {
  1767. return val;
  1768. }
  1769. void RealValue::pushDecimalValue()
  1770. {
  1771. DecPushReal(val);
  1772. }
  1773. void RealValue::serialize(MemoryBuffer &tgt)
  1774. {
  1775. tgt.append(val);
  1776. }
  1777. void RealValue::deserialize(MemoryBuffer &src)
  1778. {
  1779. src.read((double &)val);
  1780. }
  1781. int RealValue::rangeCompare(ITypeInfo * targetType)
  1782. {
  1783. return ::rangeCompare(val, targetType);
  1784. }
  1785. IValue *createRealValue(double val, unsigned size)
  1786. {
  1787. return new RealValue(val, size);
  1788. }
  1789. IValue *createRealValue(double val, ITypeInfo * type)
  1790. {
  1791. return new RealValue(val, type);
  1792. }
  1793. //===========================================================================
  1794. DecimalValue::DecimalValue(const void * v, ITypeInfo * _type) : CValue(_type)
  1795. {
  1796. unsigned len = _type->getSize();
  1797. val = (char *)checked_malloc(len, DEFVALUE_MALLOC_FAILED);
  1798. memcpy(val, v, len);
  1799. }
  1800. DecimalValue::~DecimalValue()
  1801. {
  1802. free(val);
  1803. }
  1804. int DecimalValue::compare(IValue *_to)
  1805. {
  1806. assertThrow(_to->getTypeCode()==type_decimal);
  1807. BcdCriticalBlock bcdBlock;
  1808. pushDecimalValue();
  1809. _to->pushDecimalValue();
  1810. return DecDistinct();
  1811. }
  1812. IValue *DecimalValue::castTo(ITypeInfo *t)
  1813. {
  1814. t = queryUnqualifiedType(t);
  1815. if (t==type)
  1816. return LINK(this);
  1817. type_t tc = t->getTypeCode();
  1818. if (tc == type_any)
  1819. return LINK(this);
  1820. BcdCriticalBlock bcdBlock;
  1821. char newstr[400];
  1822. pushDecimalValue();
  1823. switch (tc)
  1824. {
  1825. case type_real:
  1826. {
  1827. double value = DecPopReal();
  1828. switch (t->getSize())
  1829. {
  1830. case 4: value = (float)value; break;
  1831. case 8: value = (double)value; break;
  1832. }
  1833. return createRealValue(value, LINK(t));
  1834. }
  1835. case type_int:
  1836. case type_swapint:
  1837. case type_packedint:
  1838. return createTruncIntValue(DecPopInt64(), LINK(t));
  1839. case type_boolean:
  1840. return createBoolValue(DecCompareNull() != 0);
  1841. case type_decimal:
  1842. return createDecimalValueFromStack(t);
  1843. }
  1844. DecPopCString(sizeof(newstr), newstr);
  1845. return t->castFrom((size32_t)strlen(newstr), newstr);
  1846. }
  1847. void DecimalValue::toMem(void *target)
  1848. {
  1849. size32_t size = type->getSize();
  1850. memcpy(target, val, size);
  1851. }
  1852. unsigned DecimalValue::getHash(unsigned initval)
  1853. {
  1854. size32_t size = type->getSize();
  1855. return hashc((unsigned char *) val, size, initval);
  1856. }
  1857. const char *DecimalValue::generateECL(StringBuffer &s)
  1858. {
  1859. getStringValue(s);
  1860. s.append('D');
  1861. return s;
  1862. }
  1863. const char *DecimalValue::generateCPP(StringBuffer &s, CompilerType compiler)
  1864. {
  1865. size32_t size = type->getSize();
  1866. s.append("\"");
  1867. for (unsigned i=0;i<size;i++)
  1868. {
  1869. unsigned char c = ((unsigned char *)val)[i];
  1870. s.append("\\x");
  1871. s.appendhex(c, false);
  1872. }
  1873. s.append('"');
  1874. return s.str();
  1875. }
  1876. const char *DecimalValue::getStringValue(StringBuffer &s)
  1877. {
  1878. char strval[64];
  1879. BcdCriticalBlock bcdBlock;
  1880. pushDecimalValue();
  1881. DecPopCString(sizeof(strval), strval);
  1882. s.append(strval);
  1883. return s.str();
  1884. }
  1885. bool DecimalValue::getBoolValue()
  1886. {
  1887. BcdCriticalBlock bcdBlock;
  1888. pushDecimalValue();
  1889. return DecCompareNull() != 0;
  1890. }
  1891. __int64 DecimalValue::getIntValue()
  1892. {
  1893. BcdCriticalBlock bcdBlock;
  1894. pushDecimalValue();
  1895. return DecPopInt64();
  1896. }
  1897. double DecimalValue::getRealValue()
  1898. {
  1899. BcdCriticalBlock bcdBlock;
  1900. pushDecimalValue();
  1901. return DecPopReal();
  1902. }
  1903. void DecimalValue::pushDecimalValue()
  1904. {
  1905. if (type->isSigned())
  1906. DecPushDecimal(val, type->getSize(), type->getPrecision());
  1907. else
  1908. DecPushUDecimal(val, type->getSize(), type->getPrecision());
  1909. }
  1910. const void * DecimalValue::queryValue() const
  1911. {
  1912. return (const void *)val;
  1913. }
  1914. void DecimalValue::serialize(MemoryBuffer &tgt)
  1915. {
  1916. tgt.append(type->getSize());
  1917. tgt.append(type->getSize(), val);
  1918. }
  1919. void DecimalValue::deserialize(MemoryBuffer &src)
  1920. {
  1921. size32_t size;
  1922. src.read(size);
  1923. val = checked_malloc(size, DEFVALUE_MALLOC_FAILED);
  1924. assertex(val);
  1925. }
  1926. int DecimalValue::rangeCompare(ITypeInfo * targetType)
  1927. {
  1928. return ::rangeCompare(getRealValue(), targetType);
  1929. }
  1930. IValue *createDecimalValue(void * val, ITypeInfo * type)
  1931. {
  1932. return new DecimalValue(val, type);
  1933. }
  1934. IValue *createDecimalValueFromStack(ITypeInfo * type)
  1935. {
  1936. return static_cast<CDecimalTypeInfo*>(type)->createValueFromStack();
  1937. }
  1938. //===========================================================================
  1939. const char *BoolValue::generateECL(StringBuffer &s)
  1940. {
  1941. s.append(val ? "true" : "false");
  1942. return s.str();
  1943. }
  1944. const char *BoolValue::generateCPP(StringBuffer &s, CompilerType compiler)
  1945. {
  1946. s.append(val ? "true" : "false");
  1947. return s.str();
  1948. }
  1949. const char *BoolValue::getStringValue(StringBuffer &s)
  1950. {
  1951. s.append(val ? "true" : "false");
  1952. return s.str();
  1953. }
  1954. bool BoolValue::getBoolValue()
  1955. {
  1956. return val;
  1957. }
  1958. __int64 BoolValue::getIntValue()
  1959. {
  1960. return val;
  1961. }
  1962. void BoolValue::pushDecimalValue()
  1963. {
  1964. DecPushUlong(val);
  1965. }
  1966. void BoolValue::toMem(void *target)
  1967. {
  1968. memcpy(target, &val, type->getSize());
  1969. }
  1970. unsigned BoolValue::getHash(unsigned initval)
  1971. {
  1972. size32_t size = type->getSize();
  1973. return hashc((unsigned char *) &val, size, initval);
  1974. }
  1975. int BoolValue::compare(IValue *_to)
  1976. {
  1977. assertThrow(_to->queryType()==type);
  1978. BoolValue *to = (BoolValue *) _to;
  1979. return (int) val - (int) to->val;
  1980. }
  1981. int BoolValue::compare(const void *mem)
  1982. {
  1983. type->Link();
  1984. IValue *to = createValueFromMem(type, mem);
  1985. int ret = compare(to);
  1986. to->Release();
  1987. return ret;
  1988. }
  1989. IValue *BoolValue::castTo(ITypeInfo *t)
  1990. {
  1991. t = queryUnqualifiedType(t);
  1992. if (t==type)
  1993. return LINK(this);
  1994. type_t tc = t->getTypeCode();
  1995. if (tc == type_any)
  1996. return LINK(this);
  1997. switch (tc)
  1998. {
  1999. case type_string:
  2000. case type_qstring:
  2001. case type_varstring:
  2002. case type_unicode:
  2003. case type_varunicode:
  2004. case type_utf8:
  2005. if (!val)
  2006. return t->castFrom(0, "");
  2007. break;
  2008. case type_int:
  2009. case type_swapint:
  2010. return createTruncIntValue(val, LINK(t));
  2011. case type_packedint:
  2012. return createPackedIntValue(val, LINK(t));
  2013. }
  2014. return t->castFrom(false, (__int64)val);
  2015. }
  2016. BoolValue *BoolValue::getTrue()
  2017. {
  2018. trueconst->Link();
  2019. return trueconst;
  2020. }
  2021. BoolValue *BoolValue::getFalse()
  2022. {
  2023. falseconst->Link();
  2024. return falseconst;
  2025. }
  2026. void BoolValue::serialize(MemoryBuffer &tgt)
  2027. {
  2028. tgt.append(val);
  2029. }
  2030. void BoolValue::deserialize(MemoryBuffer &src)
  2031. {
  2032. src.read(val);
  2033. }
  2034. IValue *createBoolValue(bool v)
  2035. {
  2036. return v ? BoolValue::getTrue() : BoolValue::getFalse();
  2037. }
  2038. int BoolValue::rangeCompare(ITypeInfo * targetType)
  2039. {
  2040. return 0; // can fit in anything
  2041. }
  2042. /* In type: linked */
  2043. IValue *createValueFromMem(ITypeInfo *type, const void *mem)
  2044. {
  2045. int size = type->getSize();
  2046. type_t code = type->getTypeCode();
  2047. switch (code)
  2048. {
  2049. case type_string:
  2050. return createStringValue((const char *) mem, type);
  2051. case type_unicode:
  2052. return new UnicodeValue((UChar const *) mem, type);
  2053. case type_utf8:
  2054. return new Utf8Value((char const *) mem, type);
  2055. case type_data:
  2056. type->Release();
  2057. return createDataValue((const char *) mem, size);
  2058. case type_varstring:
  2059. return createVarStringValue((size32_t)strlen((const char *) mem), (const char *) mem, type);
  2060. case type_varunicode:
  2061. return new VarUnicodeValue(rtlUnicodeStrlen((UChar const *) mem), (UChar const *) mem, type);
  2062. case type_qstring:
  2063. return new QStringValue((const char *)mem, type);
  2064. case type_decimal:
  2065. return new DecimalValue(mem, type);
  2066. case type_bitfield:
  2067. UNIMPLEMENTED;
  2068. //needs more thought;
  2069. case type_int:
  2070. {
  2071. unsigned __int64 val;
  2072. if (type->isSigned())
  2073. val = rtlReadInt(mem, size);
  2074. else
  2075. val = rtlReadUInt(mem, size);
  2076. return createIntValue(val, type);
  2077. }
  2078. case type_swapint:
  2079. {
  2080. unsigned __int64 val;
  2081. if (type->isSigned())
  2082. val = rtlReadSwapInt(mem, size);
  2083. else
  2084. val = rtlReadSwapUInt(mem, size);
  2085. return createIntValue(val, type);
  2086. }
  2087. case type_packedint:
  2088. {
  2089. unsigned __int64 val = type->isSigned() ? rtlGetPackedSigned(mem) : rtlGetPackedUnsigned(mem);
  2090. return createPackedIntValue(val, type);
  2091. }
  2092. case type_real:
  2093. {
  2094. RealUnion u;
  2095. memcpy(&u, mem, size);
  2096. double val = 0;
  2097. switch (size)
  2098. {
  2099. case 4:
  2100. val = u.r4;
  2101. break;
  2102. case 8:
  2103. val = u.r8;
  2104. break;
  2105. }
  2106. type->Release();
  2107. return createRealValue(val, size);
  2108. }
  2109. case type_boolean:
  2110. type->Release();
  2111. return createBoolValue(*(bool *)mem);
  2112. }
  2113. type->Release();
  2114. return NULL;
  2115. }
  2116. //----------------------------------------------------------------------------
  2117. void appendValueToBuffer(MemoryBuffer & mem, IValue * value)
  2118. {
  2119. ITypeInfo * type = value->queryType();
  2120. unsigned len = type->getSize();
  2121. void * temp = checked_malloc(len, DEFVALUE_MALLOC_FAILED);
  2122. value->toMem(temp);
  2123. if (type->isSwappedEndian() != mem.needSwapEndian())
  2124. mem.appendSwap(len, temp);
  2125. else
  2126. mem.append(len, temp);
  2127. free(temp);
  2128. }
  2129. //============================================================================
  2130. IValue * addValues(IValue * left, IValue * right)
  2131. {
  2132. IValue * ret;
  2133. ITypeInfo * pnt = getPromotedAddSubType(left->queryType(), right->queryType());
  2134. switch(pnt->getTypeCode())
  2135. {
  2136. case type_int:
  2137. case type_swapint:
  2138. case type_packedint:
  2139. ret = createTruncIntValue(left->getIntValue() + right->getIntValue(), pnt);
  2140. break;
  2141. case type_real:
  2142. ret = createRealValue(left->getRealValue() + right->getRealValue(), pnt);
  2143. break;
  2144. case type_decimal:
  2145. {
  2146. BcdCriticalBlock bcdBlock;
  2147. left->pushDecimalValue();
  2148. right->pushDecimalValue();
  2149. DecAdd();
  2150. ret = ((CDecimalTypeInfo*)pnt)->createValueFromStack();
  2151. pnt->Release();
  2152. break;
  2153. }
  2154. default:
  2155. throwUnexpected();
  2156. }
  2157. return ret;
  2158. }
  2159. #define CALCULATE_AND_RETURN(op) IValue * res = LINK(*values); \
  2160. while (--num) \
  2161. { \
  2162. values++; \
  2163. IValue * tmp = op(res, *values);\
  2164. res->Release(); \
  2165. res = tmp; \
  2166. } \
  2167. return res;
  2168. IValue * subtractValues(IValue * left, IValue * right)
  2169. {
  2170. IValue * ret;
  2171. ITypeInfo * pnt = getPromotedAddSubType(left->queryType(), right->queryType());
  2172. switch(pnt->getTypeCode())
  2173. {
  2174. case type_int:
  2175. case type_swapint:
  2176. case type_packedint:
  2177. ret = createTruncIntValue(left->getIntValue() - right->getIntValue(), pnt);
  2178. break;
  2179. case type_real:
  2180. ret = createRealValue(left->getRealValue() - right->getRealValue(), pnt);
  2181. break;
  2182. case type_decimal:
  2183. {
  2184. BcdCriticalBlock bcdBlock;
  2185. left->pushDecimalValue();
  2186. right->pushDecimalValue();
  2187. DecSub();
  2188. ret = ((CDecimalTypeInfo*)pnt)->createValueFromStack();
  2189. pnt->Release();
  2190. break;
  2191. }
  2192. default:
  2193. throwUnexpected();
  2194. }
  2195. return ret;
  2196. }
  2197. IValue * multiplyValues(IValue * left, IValue * right)
  2198. {
  2199. IValue * ret;
  2200. ITypeInfo * pnt = getPromotedMulDivType(left->queryType(), right->queryType());
  2201. switch(pnt->getTypeCode())
  2202. {
  2203. case type_int:
  2204. case type_swapint:
  2205. case type_packedint:
  2206. ret = createTruncIntValue(left->getIntValue() * right->getIntValue(), pnt);
  2207. break;
  2208. case type_real:
  2209. ret = createRealValue(left->getRealValue() * right->getRealValue(), pnt);
  2210. break;
  2211. case type_decimal:
  2212. {
  2213. BcdCriticalBlock bcdBlock;
  2214. left->pushDecimalValue();
  2215. right->pushDecimalValue();
  2216. DecMul();
  2217. ret = ((CDecimalTypeInfo*)pnt)->createValueFromStack();
  2218. pnt->Release();
  2219. break;
  2220. }
  2221. default:
  2222. throwUnexpected();
  2223. }
  2224. return ret;
  2225. }
  2226. IValue * divideValues(IValue * left, IValue * right, byte dbz)
  2227. {
  2228. Owned<ITypeInfo> pnt = getPromotedMulDivType(left->queryType(), right->queryType());
  2229. //Use a cast to a boolean as a shortcut for testing against zero
  2230. if (!right->getBoolValue())
  2231. {
  2232. //If no action is selected, return NULL so the expression doesn't get constant folded.
  2233. if (dbz == DBZnone)
  2234. return NULL;
  2235. if (dbz == DBZfail)
  2236. rtlFailDivideByZero();
  2237. }
  2238. switch(pnt->getTypeCode())
  2239. {
  2240. case type_int:
  2241. case type_swapint:
  2242. case type_packedint:
  2243. {
  2244. __int64 lv = left->getIntValue();
  2245. __int64 rv = right->getIntValue();
  2246. __int64 res = 0;
  2247. if (rv)
  2248. {
  2249. if (pnt->isSigned())
  2250. res = lv / rv;
  2251. else
  2252. res = (__int64)((unsigned __int64)lv / (unsigned __int64)rv);
  2253. }
  2254. return createTruncIntValue(res, pnt.getClear());
  2255. }
  2256. case type_real:
  2257. {
  2258. double lv = left->getRealValue();
  2259. double rv = right->getRealValue();
  2260. double res;
  2261. if (rv)
  2262. res = lv / rv;
  2263. else if (dbz == DBZnan)
  2264. res = rtlCreateRealNull();
  2265. else
  2266. res = 0.0;
  2267. return createRealValue(res, pnt.getClear());
  2268. }
  2269. case type_decimal:
  2270. {
  2271. BcdCriticalBlock bcdBlock;
  2272. left->pushDecimalValue();
  2273. right->pushDecimalValue();
  2274. DecDivide(dbz);
  2275. return createDecimalValueFromStack(pnt);
  2276. }
  2277. default:
  2278. throwUnexpected();
  2279. return NULL;
  2280. }
  2281. }
  2282. IValue * modulusValues(IValue * left, IValue * right, byte dbz)
  2283. {
  2284. Owned<ITypeInfo> pnt = getPromotedMulDivType(left->queryType(), right->queryType());
  2285. //Use a cast to a boolean as a shortcut for testing against zero
  2286. if (!right->getBoolValue())
  2287. {
  2288. //If no action is selected, return NULL so the expression doesn't get constant folded.
  2289. if (dbz == DBZnone)
  2290. return NULL;
  2291. if (dbz == DBZfail)
  2292. rtlFailDivideByZero();
  2293. }
  2294. switch(pnt->getTypeCode())
  2295. {
  2296. case type_int:
  2297. case type_swapint:
  2298. case type_packedint:
  2299. {
  2300. __int64 lv = left->getIntValue();
  2301. __int64 rv = right->getIntValue();
  2302. __int64 res = 0;
  2303. if (rv)
  2304. {
  2305. if (pnt->isSigned())
  2306. res = lv % rv;
  2307. else
  2308. res = (__int64)((unsigned __int64)lv % (unsigned __int64)rv);
  2309. }
  2310. return createTruncIntValue(res, pnt.getClear());
  2311. }
  2312. case type_real:
  2313. {
  2314. double rv = right->getRealValue();
  2315. double res;
  2316. if (rv)
  2317. res = fmod(left->getRealValue(), rv);
  2318. else if (dbz == DBZnan)
  2319. res = rtlCreateRealNull();
  2320. else
  2321. res = 0.0;
  2322. return createRealValue(res, pnt.getClear());
  2323. }
  2324. case type_decimal:
  2325. {
  2326. BcdCriticalBlock bcdBlock;
  2327. left->pushDecimalValue();
  2328. right->pushDecimalValue();
  2329. DecModulus(dbz);
  2330. return createDecimalValueFromStack(pnt);
  2331. }
  2332. default:
  2333. throwUnexpected();
  2334. return NULL;
  2335. }
  2336. }
  2337. IValue * powerValues(IValue * left, IValue * right)
  2338. {
  2339. IValue * ret;
  2340. ITypeInfo * pnt = makeRealType(8);
  2341. switch(pnt->getTypeCode())
  2342. {
  2343. case type_int:
  2344. case type_swapint:
  2345. case type_packedint:
  2346. case type_real:
  2347. ret = createRealValue(pow(left->getRealValue(), right->getRealValue()), pnt);
  2348. break;
  2349. /*
  2350. // TBD
  2351. case type_decimal:
  2352. BcdCriticalBlock bcdBlock;
  2353. left->pushDecimalValue();
  2354. DecLongPower(right->getIntValue());
  2355. ret = ((CDecimalTypeInfo*)pnt)->createValueFromStack();
  2356. pnt->Release();
  2357. break
  2358. */
  2359. default:
  2360. pnt->Release();
  2361. throwUnexpected();
  2362. }
  2363. return ret;
  2364. }
  2365. IValue * negateValue(IValue * v)
  2366. {
  2367. switch(v->getTypeCode())
  2368. {
  2369. case type_int:
  2370. case type_swapint:
  2371. case type_packedint:
  2372. return createTruncIntValue(-(v->getIntValue()), v->getType());
  2373. case type_real:
  2374. return createRealValue(-(v->getRealValue()), v->getSize());
  2375. case type_decimal:
  2376. {
  2377. BcdCriticalBlock bcdBlock;
  2378. v->pushDecimalValue();
  2379. DecNegate();
  2380. return ((CDecimalTypeInfo*)v->queryType())->createValueFromStack();
  2381. }
  2382. }
  2383. throwUnexpected();
  2384. return NULL;
  2385. }
  2386. IValue * expValue(IValue * v)
  2387. {
  2388. return createRealValue(exp(v->getRealValue()), 8);
  2389. }
  2390. IValue * roundUpValue(IValue * v)
  2391. {
  2392. switch(v->getTypeCode())
  2393. {
  2394. case type_int:
  2395. case type_swapint:
  2396. case type_packedint:
  2397. return LINK(v);
  2398. case type_real:
  2399. return createTruncIntValue(rtlRoundUp(v->getRealValue()), 8, true);
  2400. case type_decimal:
  2401. {
  2402. BcdCriticalBlock bcdBlock;
  2403. v->pushDecimalValue();
  2404. DecRoundUp();
  2405. OwnedITypeInfo resultType = getRoundType(v->queryType());
  2406. return createDecimalValueFromStack(resultType);
  2407. }
  2408. }
  2409. throwUnexpected();
  2410. return NULL;
  2411. }
  2412. IValue * roundValue(IValue * v)
  2413. {
  2414. switch(v->getTypeCode())
  2415. {
  2416. case type_int:
  2417. case type_swapint:
  2418. case type_packedint:
  2419. return LINK(v);
  2420. case type_real:
  2421. return createTruncIntValue(rtlRound(v->getRealValue()), 8, true);
  2422. case type_decimal:
  2423. {
  2424. BcdCriticalBlock bcdBlock;
  2425. v->pushDecimalValue();
  2426. DecRound();
  2427. Owned<ITypeInfo> resultType = getRoundType(v->queryType());
  2428. return createDecimalValueFromStack(resultType);
  2429. }
  2430. }
  2431. throwUnexpected();
  2432. return NULL;
  2433. }
  2434. IValue * roundToValue(IValue * v, int places)
  2435. {
  2436. switch(v->getTypeCode())
  2437. {
  2438. case type_int:
  2439. case type_swapint:
  2440. case type_packedint:
  2441. return createRealValue(rtlRoundTo(v->getRealValue(), places), 8);
  2442. case type_real:
  2443. return createRealValue(rtlRoundTo(v->getRealValue(), places), 8);
  2444. case type_decimal:
  2445. {
  2446. BcdCriticalBlock bcdBlock;
  2447. v->pushDecimalValue();
  2448. DecRoundTo(places);
  2449. OwnedITypeInfo resultType = getRoundToType(v->queryType());
  2450. return createDecimalValueFromStack(resultType);
  2451. }
  2452. }
  2453. throwUnexpected();
  2454. return NULL;
  2455. }
  2456. IValue * truncateValue(IValue * v)
  2457. {
  2458. switch(v->getTypeCode())
  2459. {
  2460. case type_int:
  2461. case type_swapint:
  2462. case type_packedint:
  2463. return LINK(v);
  2464. case type_real:
  2465. return createTruncIntValue(v->getIntValue(), 8, true);
  2466. case type_decimal:
  2467. {
  2468. BcdCriticalBlock bcdBlock;
  2469. v->pushDecimalValue();
  2470. DecTruncate();
  2471. OwnedITypeInfo resultType = getTruncType(v->queryType());
  2472. return createDecimalValueFromStack(resultType);
  2473. }
  2474. }
  2475. throwUnexpected();
  2476. return NULL;
  2477. }
  2478. IValue * lnValue(IValue * v)
  2479. {
  2480. return createRealValue(rtlLog(v->getRealValue()), 8);
  2481. }
  2482. IValue * sinValue(IValue * v)
  2483. {
  2484. return createRealValue(sin(v->getRealValue()), 8);
  2485. }
  2486. IValue * cosValue(IValue * v)
  2487. {
  2488. return createRealValue(cos(v->getRealValue()), 8);
  2489. }
  2490. IValue * tanValue(IValue * v)
  2491. {
  2492. return createRealValue(tan(v->getRealValue()), 8);
  2493. }
  2494. IValue * sinhValue(IValue * v)
  2495. {
  2496. return createRealValue(sinh(v->getRealValue()), 8);
  2497. }
  2498. IValue * coshValue(IValue * v)
  2499. {
  2500. return createRealValue(cosh(v->getRealValue()), 8);
  2501. }
  2502. IValue * tanhValue(IValue * v)
  2503. {
  2504. return createRealValue(tanh(v->getRealValue()), 8);
  2505. }
  2506. IValue * asinValue(IValue * v)
  2507. {
  2508. return createRealValue(rtlASin(v->getRealValue()), 8);
  2509. }
  2510. IValue * acosValue(IValue * v)
  2511. {
  2512. return createRealValue(rtlACos(v->getRealValue()), 8);
  2513. }
  2514. IValue * atanValue(IValue * v)
  2515. {
  2516. return createRealValue(atan(v->getRealValue()), 8);
  2517. }
  2518. IValue * atan2Value(IValue * y, IValue* x)
  2519. {
  2520. return createRealValue(atan2(y->getRealValue(), x->getRealValue()), 8);
  2521. }
  2522. IValue * log10Value(IValue * v)
  2523. {
  2524. return createRealValue(rtlLog10(v->getRealValue()), 8);
  2525. }
  2526. IValue * sqrtValue(IValue * v)
  2527. {
  2528. switch(v->getTypeCode())
  2529. {
  2530. case type_int:
  2531. case type_swapint:
  2532. case type_packedint:
  2533. case type_real:
  2534. return createRealValue(rtlSqrt(v->getRealValue()), 8);
  2535. case type_decimal:
  2536. //MORE: This should probably do this more accurately.
  2537. return createRealValue(rtlSqrt(v->getRealValue()), 8);
  2538. }
  2539. throwUnexpected();
  2540. return NULL;
  2541. }
  2542. IValue * absValue(IValue * v)
  2543. {
  2544. switch(v->getTypeCode())
  2545. {
  2546. case type_int:
  2547. case type_swapint:
  2548. case type_packedint:
  2549. {
  2550. ITypeInfo * type = v->queryType();
  2551. if (type->isSigned())
  2552. {
  2553. __int64 val = v->getIntValue();
  2554. if (val < 0)
  2555. return createIntValue(-val, LINK(type));
  2556. }
  2557. return LINK(v);
  2558. }
  2559. case type_real:
  2560. return createRealValue(fabs(v->getRealValue()), v->getSize());
  2561. case type_decimal:
  2562. {
  2563. BcdCriticalBlock bcdBlock;
  2564. v->pushDecimalValue();
  2565. DecAbs();
  2566. return ((CDecimalTypeInfo*)v->queryType())->createValueFromStack();
  2567. }
  2568. }
  2569. throwUnexpected();
  2570. return NULL;
  2571. }
  2572. IValue * substringValue(IValue * v, IValue * lower, IValue * higher)
  2573. {
  2574. ITypeInfo * type = v->queryType();
  2575. unsigned srcLen = type->getStringLen();
  2576. const void * raw = v->queryValue();
  2577. unsigned low = lower ? (unsigned)lower->getIntValue() : 0;
  2578. unsigned high = higher ? (unsigned)higher->getIntValue() : srcLen;
  2579. unsigned retLen = 0;
  2580. void * retPtr;
  2581. ITypeInfo * retType = NULL;
  2582. switch (type->getTypeCode())
  2583. {
  2584. case type_string:
  2585. rtlSubStrFTX(retLen, *(char * *)&retPtr, srcLen, (const char *)raw, low, high);
  2586. break;
  2587. case type_varstring:
  2588. rtlSubStrFTX(retLen, *(char * *)&retPtr, srcLen, (const char *)raw, low, high);
  2589. retType = makeStringType(retLen, LINK(type->queryCharset()), LINK(type->queryCollation()));
  2590. break;
  2591. case type_data:
  2592. rtlSubDataFTX(retLen, retPtr, srcLen, raw, low, high);
  2593. break;
  2594. case type_qstring:
  2595. rtlSubQStrFTX(retLen, *(char * *)&retPtr, srcLen, (const char *)raw, low, high);
  2596. break;
  2597. case type_unicode:
  2598. rtlUnicodeSubStrFTX(retLen, *(UChar * *)&retPtr, srcLen, (const UChar *)raw, low, high);
  2599. break;
  2600. case type_varunicode:
  2601. rtlUnicodeSubStrFTX(retLen, *(UChar * *)&retPtr, srcLen, (const UChar *)raw, low, high);
  2602. retType = makeUnicodeType(retLen, type->queryLocale());
  2603. break;
  2604. case type_utf8:
  2605. rtlUtf8SubStrFTX(retLen, *(char * *)&retPtr, srcLen, (const char *)raw, low, high);
  2606. break;
  2607. default:
  2608. UNIMPLEMENTED;
  2609. }
  2610. if (retType == NULL)
  2611. retType = getStretchedType(retLen, type);
  2612. IValue * ret = createValueFromMem(retType, retPtr);
  2613. rtlFree(retPtr);
  2614. return ret;
  2615. }
  2616. IValue * trimStringValue(IValue * v, char typecode)
  2617. {
  2618. ITypeInfo * type = v->queryType();
  2619. type_t tc = type->getTypeCode();
  2620. if(isUnicodeType(type))
  2621. {
  2622. unsigned tlen = 0;
  2623. rtlDataAttr resultstr;
  2624. unsigned len = type->getStringLen();
  2625. if (tc == type_utf8)
  2626. {
  2627. char const * str = (char const *)v->queryValue();
  2628. switch(typecode)
  2629. {
  2630. case 'A':
  2631. rtlTrimUtf8All(tlen, resultstr.refstr(), len, str);
  2632. break;
  2633. case 'B':
  2634. rtlTrimUtf8Both(tlen, resultstr.refstr(), len, str);
  2635. break;
  2636. case 'L':
  2637. rtlTrimUtf8Left(tlen, resultstr.refstr(), len, str);
  2638. break;
  2639. default:
  2640. rtlTrimUtf8Right(tlen, resultstr.refstr(), len, str);
  2641. break;
  2642. }
  2643. ITypeInfo * newtype = makeUtf8Type(tlen, type->queryLocale());
  2644. return createUtf8Value(tlen, resultstr.getstr(), newtype);
  2645. }
  2646. else
  2647. {
  2648. UChar const * str = (UChar const *)v->queryValue();
  2649. switch(typecode)
  2650. {
  2651. case 'A':
  2652. rtlTrimUnicodeAll(tlen, resultstr.refustr(), len, str);
  2653. break;
  2654. case 'B':
  2655. rtlTrimUnicodeBoth(tlen, resultstr.refustr(), len, str);
  2656. break;
  2657. case 'L':
  2658. rtlTrimUnicodeLeft(tlen, resultstr.refustr(), len, str);
  2659. break;
  2660. default:
  2661. rtlTrimUnicodeRight(tlen, resultstr.refustr(), len, str);
  2662. break;
  2663. }
  2664. ITypeInfo * newtype = makeUnicodeType(tlen, v->queryType()->queryLocale());
  2665. return createUnicodeValue(resultstr.getustr(), tlen, newtype);
  2666. }
  2667. }
  2668. else
  2669. {
  2670. Owned<ITypeInfo> st = getStringType(type);
  2671. Owned<ITypeInfo> asciiType = getAsciiType(st);
  2672. Owned<IValue> sv = v->castTo(asciiType);
  2673. StringBuffer s;
  2674. sv->getStringValue(s);
  2675. unsigned tlen = 0;
  2676. rtlDataAttr resultstr;
  2677. unsigned len = s.length();
  2678. char const * str = s.str();
  2679. switch(typecode)
  2680. {
  2681. case 'A':
  2682. rtlTrimAll(tlen, resultstr.refstr(), len, str);
  2683. break;
  2684. case 'B':
  2685. rtlTrimBoth(tlen, resultstr.refstr(), len, str);
  2686. break;
  2687. case 'L':
  2688. rtlTrimLeft(tlen, resultstr.refstr(), len, str);
  2689. break;
  2690. default:
  2691. rtlTrimRight(tlen, resultstr.refstr(), len, str);
  2692. break;
  2693. }
  2694. ITypeInfo * newtype = makeStringType(tlen, LINK(asciiType->queryCharset()), LINK(asciiType->queryCollation()));
  2695. return createStringValue(resultstr.getstr(), newtype);
  2696. }
  2697. }
  2698. //---------------------------------------------------------------------
  2699. void getStringFromIValue(StringBuffer & s, IValue* val)
  2700. {
  2701. Owned<ITypeInfo> tp = getStringType(val->queryType());
  2702. Owned<IValue> sv = val->castTo(tp);
  2703. sv->getStringValue(s);
  2704. }
  2705. void getStringFromIValue(unsigned & len, char* & str, IValue* val)
  2706. {
  2707. StringBuffer s;
  2708. getStringFromIValue(s, val);
  2709. len = s.length();
  2710. str = s.detach();
  2711. }
  2712. //---------------------------------------------------------------------
  2713. IValue * concatValues(IValue * left, IValue * right)
  2714. {
  2715. ITypeInfo * leftType = left->queryType();
  2716. ITypeInfo * rightType = right->queryType();
  2717. type_t ltc = leftType->getTypeCode();
  2718. type_t rtc = rightType->getTypeCode();
  2719. if(isUnicodeType(leftType))
  2720. {
  2721. assertex(isUnicodeType(rightType));
  2722. assertex(leftType->queryLocale() == rightType->queryLocale());
  2723. rtlDataAttr out;
  2724. unsigned outlen;
  2725. if (ltc == type_utf8 && rtc == type_utf8)
  2726. {
  2727. rtlConcatUtf8(outlen, out.addrstr(), leftType->getStringLen(), (char const *)left->queryValue(), rightType->getStringLen(), (char const *)right->queryValue(), -1);
  2728. ITypeInfo * newtype = makeUtf8Type(outlen, leftType->queryLocale());
  2729. return createUtf8Value(outlen, out.getstr(), newtype);
  2730. }
  2731. else
  2732. {
  2733. rtlConcatUnicode(outlen, out.addrustr(), leftType->getStringLen(), (UChar const *)left->queryValue(), rightType->getStringLen(), (UChar const *)right->queryValue(), -1);
  2734. ITypeInfo * newtype = makeUnicodeType(outlen, leftType->queryLocale());
  2735. return createUnicodeValue(out.getustr(), outlen, newtype);
  2736. }
  2737. }
  2738. else
  2739. {
  2740. Owned<ITypeInfo> lt = getStringType(leftType);
  2741. Owned<ITypeInfo> rt = getStringType(rightType);
  2742. Owned<IValue> lv = left->castTo(lt);
  2743. Owned<IValue> rv = right->castTo(rt);
  2744. assertex(!isUnicodeType(rt));
  2745. assertex(lt->queryCharset() == rt->queryCharset());
  2746. assertex(lt->queryCollation() == rt->queryCollation());
  2747. size32_t len = lt->getStringLen() + rt->getStringLen();
  2748. StringBuffer s;
  2749. lv->getStringValue(s);
  2750. rv->getStringValue(s);
  2751. if (ltc == type_varstring || rtc == type_varstring)
  2752. {
  2753. ITypeInfo * newtype = makeVarStringType(len);
  2754. return createVarStringValue(len, s.str(), newtype);
  2755. }
  2756. else if (ltc == type_string || rtc == type_string)
  2757. {
  2758. ITypeInfo * newtype = makeStringType(len, LINK(lt->queryCharset()), LINK(lt->queryCollation()));
  2759. return createStringValue(s.str(), newtype);
  2760. }
  2761. else
  2762. {
  2763. return createDataValue(s.str(), len);
  2764. }
  2765. }
  2766. }
  2767. IValue * binaryAndValues(IValue * left, IValue * right)
  2768. {
  2769. IValue * ret;
  2770. Owned<ITypeInfo> pnt = getBandType(left->queryType(), right->queryType());
  2771. switch(pnt->getTypeCode())
  2772. {
  2773. case type_boolean:
  2774. return createBoolValue(left->getBoolValue() && right->getBoolValue());
  2775. case type_int:
  2776. case type_swapint:
  2777. case type_packedint:
  2778. ret = createTruncIntValue(left->getIntValue() & right->getIntValue(), pnt.getClear());
  2779. break;
  2780. default:
  2781. throwUnexpected();
  2782. }
  2783. return ret;
  2784. }
  2785. IValue * binaryOrValues(IValue * left, IValue * right)
  2786. {
  2787. IValue * ret;
  2788. Owned<ITypeInfo> pnt = getBorType(left->queryType(), right->queryType());
  2789. switch(pnt->getTypeCode())
  2790. {
  2791. case type_boolean:
  2792. return createBoolValue(left->getBoolValue() || right->getBoolValue());
  2793. case type_int:
  2794. case type_swapint:
  2795. case type_packedint:
  2796. ret = createTruncIntValue(left->getIntValue() | right->getIntValue(), pnt.getClear());
  2797. break;
  2798. default:
  2799. throwUnexpected();
  2800. }
  2801. return ret;
  2802. }
  2803. IValue * binaryXorValues(IValue * left, IValue * right)
  2804. {
  2805. IValue * ret;
  2806. ITypeInfo * pnt = getPromotedNumericType(left->queryType(), right->queryType());
  2807. switch(pnt->getTypeCode())
  2808. {
  2809. case type_int:
  2810. case type_swapint:
  2811. case type_packedint:
  2812. ret = createTruncIntValue(left->getIntValue() ^ right->getIntValue(), pnt);
  2813. break;
  2814. default:
  2815. throwUnexpected();
  2816. }
  2817. return ret;
  2818. }
  2819. IValue * binaryNotValues(IValue * v)
  2820. {
  2821. switch(v->getTypeCode())
  2822. {
  2823. case type_int:
  2824. case type_swapint:
  2825. case type_packedint:
  2826. return createTruncIntValue(~v->getIntValue(), v->getType());
  2827. }
  2828. throwUnexpected();
  2829. return NULL;
  2830. }
  2831. IValue * logicalNotValues(IValue * v)
  2832. {
  2833. return createBoolValue(!v->getBoolValue());
  2834. }
  2835. IValue * logicalAndValues(IValue * left, IValue * right)
  2836. {
  2837. return createBoolValue(left->getBoolValue() && right->getBoolValue());
  2838. }
  2839. IValue * logicalOrValues(IValue * left, IValue * right)
  2840. {
  2841. return createBoolValue(left->getBoolValue() || right->getBoolValue());
  2842. }
  2843. int orderValues(IValue * left, IValue * right)
  2844. {
  2845. //The following line can be uncommented to check that the types are consistent everywhere
  2846. //but remains commented out to improve resilience when the types are wrong.
  2847. // return left->compare(right);
  2848. Owned<ITypeInfo> pt = getPromotedCompareType(left->queryType(), right->queryType());
  2849. Owned<IValue> lv = left->castTo(pt);
  2850. Owned<IValue> rv = right->castTo(pt);
  2851. return lv->compare(rv);
  2852. }
  2853. #define COMPARE_AND_RETURN(op) \
  2854. return createBoolValue(orderValues(left, right) op 0);
  2855. IValue * equalValues(IValue * left, IValue * right)
  2856. {
  2857. COMPARE_AND_RETURN(==)
  2858. }
  2859. IValue * notEqualValues(IValue * left, IValue * right)
  2860. {
  2861. COMPARE_AND_RETURN(!=)
  2862. }
  2863. IValue * lessValues(IValue * left, IValue * right)
  2864. {
  2865. COMPARE_AND_RETURN(<)
  2866. }
  2867. IValue * lessEqualValues(IValue * left, IValue * right)
  2868. {
  2869. COMPARE_AND_RETURN(<=)
  2870. }
  2871. IValue * greaterValues(IValue * left, IValue * right)
  2872. {
  2873. COMPARE_AND_RETURN(>)
  2874. }
  2875. IValue * greaterEqualValues(IValue * left, IValue * right)
  2876. {
  2877. COMPARE_AND_RETURN(>=)
  2878. }
  2879. IValue * shiftLeftValues(IValue * left, IValue * right)
  2880. {
  2881. ITypeInfo * retType = left->getType();
  2882. switch(retType->getTypeCode())
  2883. {
  2884. case type_int:
  2885. case type_swapint:
  2886. case type_packedint:
  2887. return createTruncIntValue(left->getIntValue() << right->getIntValue(), retType);
  2888. default:
  2889. UNIMPLEMENTED;
  2890. }
  2891. }
  2892. IValue * shiftRightValues(IValue * left, IValue * right)
  2893. {
  2894. ITypeInfo * retType = left->getType();
  2895. switch(retType->getTypeCode())
  2896. {
  2897. case type_int:
  2898. case type_swapint:
  2899. case type_packedint:
  2900. if (retType->isSigned())
  2901. return createTruncIntValue(((__int64)left->getIntValue()) >> right->getIntValue(), retType);
  2902. else
  2903. return createTruncIntValue(((unsigned __int64)left->getIntValue()) >> right->getIntValue(), retType);
  2904. default:
  2905. UNIMPLEMENTED;
  2906. }
  2907. }
  2908. extern DEFTYPE_API void serializeValue(MemoryBuffer & target, IValue * value)
  2909. {
  2910. ITypeInfo * type = value->queryType();
  2911. type->serialize(target);
  2912. void * buffer = target.reserve(type->getSize());
  2913. value->toMem(buffer);
  2914. }
  2915. extern DEFTYPE_API IValue * deserializeValue(MemoryBuffer & source)
  2916. {
  2917. Owned<ITypeInfo> type = deserializeType(source);
  2918. const void * buffer = source.readDirect(type->getSize());
  2919. return createValueFromMem(LINK(type), buffer);
  2920. }