defvalue.cpp 84 KB

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