defvalue.cpp 84 KB

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