rtlfield.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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 "platform.h"
  15. #include <math.h>
  16. #include <stdio.h>
  17. #include "jmisc.hpp"
  18. #include "jlib.hpp"
  19. #include "eclhelper.hpp"
  20. #include "eclrtl_imp.hpp"
  21. #include "rtlfield_imp.hpp"
  22. static const char * queryXPath(const RtlFieldInfo * field)
  23. {
  24. const char * xpath = field->xpath;
  25. if (xpath)
  26. {
  27. const char * sep = strchr(xpath, xpathCompoundSeparatorChar);
  28. if (!sep)
  29. return xpath;
  30. return sep+1;
  31. }
  32. return field->name->str();
  33. }
  34. static bool hasOuterXPath(const RtlFieldInfo * field)
  35. {
  36. const char * xpath = field->xpath;
  37. assertex(xpath);
  38. return (*xpath != xpathCompoundSeparatorChar);
  39. }
  40. static void queryNestedOuterXPath(StringAttr & ret, const RtlFieldInfo * field)
  41. {
  42. const char * xpath = field->xpath;
  43. assertex(xpath);
  44. const char * sep = strchr(xpath, xpathCompoundSeparatorChar);
  45. assertex(sep);
  46. ret.set(xpath, (size32_t)(sep-xpath));
  47. }
  48. //-------------------------------------------------------------------------------------------------------------------
  49. size32_t RtlTypeInfoBase::size(const byte * self, const byte * selfrow) const
  50. {
  51. return length;
  52. }
  53. size32_t RtlTypeInfoBase::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  54. {
  55. rtlFailUnexpected();
  56. return 0;
  57. }
  58. size32_t RtlTypeInfoBase::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & out) const
  59. {
  60. rtlFailUnexpected();
  61. return 0;
  62. }
  63. void RtlTypeInfoBase::serialize(IRtlFieldTypeSerializer & out) const
  64. {
  65. rtlFailUnexpected();
  66. }
  67. void RtlTypeInfoBase::deserialize(IRtlFieldTypeDeserializer & in)
  68. {
  69. rtlFailUnexpected();
  70. }
  71. const char * RtlTypeInfoBase::queryLocale() const
  72. {
  73. return NULL;
  74. }
  75. const RtlFieldInfo * const * RtlTypeInfoBase::queryFields() const
  76. {
  77. return NULL;
  78. }
  79. const RtlTypeInfo * RtlTypeInfoBase::queryChildType() const
  80. {
  81. return NULL;
  82. }
  83. //-------------------------------------------------------------------------------------------------------------------
  84. size32_t RtlBoolTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  85. {
  86. target.processBool(*(const bool *)self, field);
  87. return sizeof(bool);
  88. }
  89. size32_t RtlBoolTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  90. {
  91. target.outputBool(*(const bool *)self, queryXPath(field));
  92. return sizeof(bool);
  93. }
  94. //-------------------------------------------------------------------------------------------------------------------
  95. double RtlRealTypeInfo::value(const byte * self) const
  96. {
  97. if (length == 4)
  98. return *(const float *)self;
  99. return *(const double *)self;
  100. }
  101. size32_t RtlRealTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  102. {
  103. target.processReal(value(self), field);
  104. return length;
  105. }
  106. size32_t RtlRealTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  107. {
  108. target.outputReal(value(self), queryXPath(field));
  109. return length;
  110. }
  111. //-------------------------------------------------------------------------------------------------------------------
  112. size32_t RtlIntTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  113. {
  114. if (isUnsigned())
  115. target.processUInt(rtlReadUInt(self, length), field);
  116. else
  117. target.processInt(rtlReadInt(self, length), field);
  118. return length;
  119. }
  120. size32_t RtlIntTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  121. {
  122. if (isUnsigned())
  123. target.outputUInt(rtlReadUInt(self, length), queryXPath(field));
  124. else
  125. target.outputInt(rtlReadInt(self, length), queryXPath(field));
  126. return length;
  127. }
  128. //-------------------------------------------------------------------------------------------------------------------
  129. size32_t RtlSwapIntTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  130. {
  131. if (isUnsigned())
  132. target.processUInt(rtlReadSwapUInt(self, length), field);
  133. else
  134. target.processInt(rtlReadSwapInt(self, length), field);
  135. return length;
  136. }
  137. size32_t RtlSwapIntTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  138. {
  139. if (isUnsigned())
  140. target.outputUInt(rtlReadSwapUInt(self, length), queryXPath(field));
  141. else
  142. target.outputInt(rtlReadSwapInt(self, length), queryXPath(field));
  143. return length;
  144. }
  145. //-------------------------------------------------------------------------------------------------------------------
  146. size32_t RtlPackedIntTypeInfo::size(const byte * self, const byte * selfrow) const
  147. {
  148. return rtlGetPackedSize(self);
  149. }
  150. size32_t RtlPackedIntTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  151. {
  152. if (isUnsigned())
  153. target.processUInt(rtlGetPackedUnsigned(self), field);
  154. else
  155. target.processInt(rtlGetPackedSigned(self), field);
  156. return rtlGetPackedSize(self);
  157. }
  158. size32_t RtlPackedIntTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  159. {
  160. if (isUnsigned())
  161. target.outputUInt(rtlGetPackedUnsigned(self), queryXPath(field));
  162. else
  163. target.outputInt(rtlGetPackedSigned(self), queryXPath(field));
  164. return rtlGetPackedSize(self);
  165. }
  166. //-------------------------------------------------------------------------------------------------------------------
  167. size32_t RtlStringTypeInfo::size(const byte * self, const byte * selfrow) const
  168. {
  169. if (isFixedSize())
  170. return length;
  171. return sizeof(size32_t) + rtlReadUInt4(self);
  172. }
  173. size32_t RtlStringTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  174. {
  175. const char * str = reinterpret_cast<const char *>(self);
  176. unsigned thisLength;
  177. unsigned thisSize;
  178. if (isFixedSize())
  179. {
  180. thisLength = length;
  181. thisSize = thisLength;
  182. }
  183. else
  184. {
  185. str = reinterpret_cast<const char *>(self + sizeof(size32_t));
  186. thisLength = rtlReadUInt4(self);
  187. thisSize = sizeof(size32_t) + thisLength;
  188. }
  189. if (isEbcdic())
  190. {
  191. unsigned lenAscii;
  192. rtlDataAttr ascii;
  193. rtlEStrToStrX(lenAscii, ascii.refstr(), thisLength, str);
  194. target.processString(lenAscii, ascii.getstr(), field);
  195. }
  196. else
  197. {
  198. target.processString(thisLength, str, field);
  199. }
  200. return thisSize;
  201. }
  202. size32_t RtlStringTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  203. {
  204. const char * str = reinterpret_cast<const char *>(self);
  205. unsigned thisLength;
  206. unsigned thisSize;
  207. if (isFixedSize())
  208. {
  209. thisLength = length;
  210. thisSize = thisLength;
  211. }
  212. else
  213. {
  214. str = reinterpret_cast<const char *>(self + sizeof(size32_t));
  215. thisLength = rtlReadUInt4(self);
  216. thisSize = sizeof(size32_t) + thisLength;
  217. }
  218. if (isEbcdic())
  219. {
  220. unsigned lenAscii;
  221. rtlDataAttr ascii;
  222. rtlEStrToStrX(lenAscii, ascii.refstr(), thisLength, str);
  223. target.outputString(lenAscii, ascii.getstr(), queryXPath(field));
  224. }
  225. else
  226. {
  227. target.outputString(thisLength, str, queryXPath(field));
  228. }
  229. return thisSize;
  230. }
  231. //-------------------------------------------------------------------------------------------------------------------
  232. size32_t RtlDataTypeInfo::size(const byte * self, const byte * selfrow) const
  233. {
  234. if (isFixedSize())
  235. return length;
  236. return sizeof(size32_t) + rtlReadUInt4(self);
  237. }
  238. size32_t RtlDataTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  239. {
  240. const char * str = reinterpret_cast<const char *>(self);
  241. unsigned thisLength;
  242. unsigned thisSize;
  243. if (isFixedSize())
  244. {
  245. thisLength = length;
  246. thisSize = thisLength;
  247. }
  248. else
  249. {
  250. str = reinterpret_cast<const char *>(self + sizeof(size32_t));
  251. thisLength = rtlReadUInt4(self);
  252. thisSize = sizeof(size32_t) + thisLength;
  253. }
  254. target.processData(thisLength, str, field);
  255. return thisSize;
  256. }
  257. size32_t RtlDataTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  258. {
  259. const char * str = reinterpret_cast<const char *>(self);
  260. unsigned thisLength;
  261. unsigned thisSize;
  262. if (isFixedSize())
  263. {
  264. thisLength = length;
  265. thisSize = thisLength;
  266. }
  267. else
  268. {
  269. str = reinterpret_cast<const char *>(self + sizeof(size32_t));
  270. thisLength = rtlReadUInt4(self);
  271. thisSize = sizeof(size32_t) + thisLength;
  272. }
  273. target.outputData(thisLength, str, queryXPath(field));
  274. return thisSize;
  275. }
  276. //-------------------------------------------------------------------------------------------------------------------
  277. size32_t RtlVarStringTypeInfo::size(const byte * self, const byte * selfrow) const
  278. {
  279. if (isFixedSize())
  280. return length + 1;
  281. const char * str = reinterpret_cast<const char *>(self);
  282. return (size32_t)strlen(str)+1;
  283. }
  284. size32_t RtlVarStringTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  285. {
  286. const char * str = reinterpret_cast<const char *>(self);
  287. unsigned thisLength = (size32_t)strlen(str);
  288. unsigned thisSize;
  289. if (isFixedSize())
  290. thisSize = length+1;
  291. else
  292. thisSize = thisLength+1;
  293. if (isEbcdic())
  294. {
  295. unsigned lenAscii;
  296. rtlDataAttr ascii;
  297. rtlEStrToStrX(lenAscii, ascii.refstr(), thisLength, str);
  298. target.processString(lenAscii, ascii.getstr(), field);
  299. }
  300. else
  301. target.processString(thisLength, str, field);
  302. return thisSize;
  303. }
  304. size32_t RtlVarStringTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  305. {
  306. const char * str = reinterpret_cast<const char *>(self);
  307. unsigned thisLength = (size32_t)strlen(str);
  308. unsigned thisSize;
  309. if (isFixedSize())
  310. thisSize = length+1;
  311. else
  312. thisSize = thisLength+1;
  313. if (isEbcdic())
  314. {
  315. unsigned lenAscii;
  316. rtlDataAttr ascii;
  317. rtlEStrToStrX(lenAscii, ascii.refstr(), thisLength, str);
  318. target.outputString(lenAscii, ascii.getstr(), queryXPath(field));
  319. }
  320. else
  321. target.outputString(thisLength, str, queryXPath(field));
  322. return thisSize;
  323. }
  324. //-------------------------------------------------------------------------------------------------------------------
  325. size32_t RtlQStringTypeInfo::size(const byte * self, const byte * selfrow) const
  326. {
  327. if (isFixedSize())
  328. return rtlQStrSize(length);
  329. return sizeof(size32_t) + rtlQStrSize(rtlReadUInt4(self));
  330. }
  331. size32_t RtlQStringTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  332. {
  333. const char * str = reinterpret_cast<const char *>(self);
  334. unsigned thisLength;
  335. unsigned thisSize;
  336. if (isFixedSize())
  337. {
  338. thisLength = length;
  339. thisSize = rtlQStrSize(thisLength);
  340. }
  341. else
  342. {
  343. str = reinterpret_cast<const char *>(self + sizeof(size32_t));
  344. thisLength = rtlReadUInt4(self);
  345. thisSize = sizeof(size32_t) + rtlQStrSize(thisLength);
  346. }
  347. target.processQString(thisLength, str, field);
  348. return thisSize;
  349. }
  350. size32_t RtlQStringTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  351. {
  352. const char * str = reinterpret_cast<const char *>(self);
  353. unsigned thisLength;
  354. unsigned thisSize;
  355. if (isFixedSize())
  356. {
  357. thisLength = length;
  358. thisSize = rtlQStrSize(thisLength);
  359. }
  360. else
  361. {
  362. str = reinterpret_cast<const char *>(self + sizeof(size32_t));
  363. thisLength = rtlReadUInt4(self);
  364. thisSize = sizeof(size32_t) + rtlQStrSize(thisLength);
  365. }
  366. target.outputQString(thisLength, str, queryXPath(field));
  367. return thisSize;
  368. }
  369. //-------------------------------------------------------------------------------------------------------------------
  370. size32_t RtlDecimalTypeInfo::calcSize() const
  371. {
  372. if (isUnsigned())
  373. return (getDecimalDigits()+1)/2;
  374. return (getDecimalDigits()+2)/2;
  375. }
  376. size32_t RtlDecimalTypeInfo::size(const byte * self, const byte * selfrow) const
  377. {
  378. return calcSize();
  379. }
  380. size32_t RtlDecimalTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  381. {
  382. size32_t thisSize = calcSize();
  383. if (isUnsigned())
  384. target.processUDecimal(self, thisSize, getDecimalPrecision(), field);
  385. else
  386. target.processDecimal(self, thisSize, getDecimalPrecision(), field);
  387. return thisSize;
  388. }
  389. size32_t RtlDecimalTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  390. {
  391. size32_t thisSize = calcSize();
  392. if (isUnsigned())
  393. target.outputUDecimal(self, thisSize, getDecimalPrecision(), queryXPath(field));
  394. else
  395. target.outputDecimal(self, thisSize, getDecimalPrecision(), queryXPath(field));
  396. return thisSize;
  397. }
  398. //-------------------------------------------------------------------------------------------------------------------
  399. size32_t RtlCharTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  400. {
  401. const char * str = reinterpret_cast<const char *>(self);
  402. char c;
  403. if (isEbcdic())
  404. rtlEStrToStr(1, &c, 1, str);
  405. else
  406. c = *str;
  407. target.processString(1, &c, field);
  408. return 1;
  409. }
  410. size32_t RtlCharTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  411. {
  412. const char * str = reinterpret_cast<const char *>(self);
  413. char c;
  414. if (isEbcdic())
  415. rtlEStrToStr(1, &c, 1, str);
  416. else
  417. c = *str;
  418. target.outputString(1, &c, queryXPath(field));
  419. return 1;
  420. }
  421. //-------------------------------------------------------------------------------------------------------------------
  422. size32_t RtlUnicodeTypeInfo::size(const byte * self, const byte * selfrow) const
  423. {
  424. if (isFixedSize())
  425. return length * sizeof(UChar);
  426. return sizeof(size32_t) + rtlReadUInt4(self) * sizeof(UChar);
  427. }
  428. size32_t RtlUnicodeTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  429. {
  430. const UChar * ustr = reinterpret_cast<const UChar *>(self);
  431. unsigned thisLength;
  432. unsigned thisSize;
  433. if (isFixedSize())
  434. {
  435. thisLength = length;
  436. thisSize = thisLength * sizeof(UChar);
  437. }
  438. else
  439. {
  440. ustr = reinterpret_cast<const UChar *>(self + sizeof(size32_t));
  441. thisLength = rtlReadUInt4(self);
  442. thisSize = sizeof(size32_t) + thisLength * sizeof(UChar);
  443. }
  444. target.processUnicode(thisLength, ustr, field);
  445. return thisSize;
  446. }
  447. size32_t RtlUnicodeTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  448. {
  449. const UChar * ustr = reinterpret_cast<const UChar *>(self);
  450. unsigned thisLength;
  451. unsigned thisSize;
  452. if (isFixedSize())
  453. {
  454. thisLength = length;
  455. thisSize = thisLength * sizeof(UChar);
  456. }
  457. else
  458. {
  459. ustr = reinterpret_cast<const UChar *>(self + sizeof(size32_t));
  460. thisLength = rtlReadUInt4(self);
  461. thisSize = sizeof(size32_t) + thisLength * sizeof(UChar);
  462. }
  463. target.outputUnicode(thisLength, ustr, queryXPath(field));
  464. return thisSize;
  465. }
  466. //-------------------------------------------------------------------------------------------------------------------
  467. size32_t RtlVarUnicodeTypeInfo::size(const byte * self, const byte * selfrow) const
  468. {
  469. if (isFixedSize())
  470. return (length+1) * sizeof(UChar);
  471. const UChar * ustr = reinterpret_cast<const UChar *>(self);
  472. return (rtlUnicodeStrlen(ustr)+1) * sizeof(UChar);
  473. }
  474. size32_t RtlVarUnicodeTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  475. {
  476. const UChar * ustr = reinterpret_cast<const UChar *>(self);
  477. unsigned thisLength = rtlUnicodeStrlen(ustr);
  478. unsigned thisSize;
  479. if (isFixedSize())
  480. thisSize = (length + 1) * sizeof(UChar);
  481. else
  482. thisSize = (thisLength + 1) * sizeof(UChar);
  483. target.processUnicode(thisLength, ustr, field);
  484. return thisSize;
  485. }
  486. size32_t RtlVarUnicodeTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  487. {
  488. const UChar * ustr = reinterpret_cast<const UChar *>(self);
  489. unsigned thisLength = rtlUnicodeStrlen(ustr);
  490. unsigned thisSize;
  491. if (isFixedSize())
  492. thisSize = (length + 1) * sizeof(UChar);
  493. else
  494. thisSize = (thisLength + 1) * sizeof(UChar);
  495. target.outputUnicode(thisLength, ustr, queryXPath(field));
  496. return thisSize;
  497. }
  498. //-------------------------------------------------------------------------------------------------------------------
  499. size32_t RtlUtf8TypeInfo::size(const byte * self, const byte * selfrow) const
  500. {
  501. assertex(!isFixedSize());
  502. return sizeof(size32_t) + rtlUtf8Size(rtlReadUInt4(self), self+sizeof(unsigned));
  503. }
  504. size32_t RtlUtf8TypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  505. {
  506. assertex(!isFixedSize());
  507. const char * str = reinterpret_cast<const char *>(self + sizeof(size32_t));
  508. unsigned thisLength = rtlReadUInt4(self);
  509. unsigned thisSize = sizeof(size32_t) + rtlUtf8Size(thisLength, str);
  510. target.processUtf8(thisLength, str, field);
  511. return thisSize;
  512. }
  513. size32_t RtlUtf8TypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  514. {
  515. assertex(!isFixedSize());
  516. const char * str = reinterpret_cast<const char *>(self + sizeof(size32_t));
  517. unsigned thisLength = rtlReadUInt4(self);
  518. unsigned thisSize = sizeof(size32_t) + rtlUtf8Size(thisLength, str);
  519. target.outputUtf8(thisLength, str, queryXPath(field));
  520. return thisSize;
  521. }
  522. //-------------------------------------------------------------------------------------------------------------------
  523. inline size32_t sizeFields(const RtlFieldInfo * const * cur, const byte * self, const byte * selfrow)
  524. {
  525. unsigned offset = 0;
  526. loop
  527. {
  528. const RtlFieldInfo * child = *cur;
  529. if (!child)
  530. break;
  531. offset += child->size(self+offset, selfrow);
  532. cur++;
  533. }
  534. return offset;
  535. }
  536. inline size32_t processFields(const RtlFieldInfo * const * cur, const byte * self, const byte * selfrow, IFieldProcessor & target)
  537. {
  538. unsigned offset = 0;
  539. loop
  540. {
  541. const RtlFieldInfo * child = *cur;
  542. if (!child)
  543. break;
  544. child->process(self+offset, selfrow, target);
  545. offset += child->size(self+offset, selfrow);
  546. cur++;
  547. }
  548. return offset;
  549. }
  550. inline size32_t toXMLFields(const RtlFieldInfo * const * cur, const byte * self, const byte * selfrow, IXmlWriter & target)
  551. {
  552. size32_t offset = 0;
  553. loop
  554. {
  555. const RtlFieldInfo * child = *cur;
  556. if (!child)
  557. break;
  558. size32_t size = child->toXML(self+offset, selfrow, target);
  559. offset += size;
  560. cur++;
  561. }
  562. return offset;
  563. }
  564. //-------------------------------------------------------------------------------------------------------------------
  565. size32_t RtlRecordTypeInfo::size(const byte * self, const byte * selfrow) const
  566. {
  567. return sizeFields(fields, self, self);
  568. }
  569. size32_t RtlRecordTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  570. {
  571. if (target.processBeginRow(field))
  572. {
  573. unsigned offset = processFields(fields, self, self, target);
  574. target.processEndRow(field);
  575. return offset;
  576. }
  577. return size(self, selfrow);
  578. }
  579. size32_t RtlRecordTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  580. {
  581. const char * xpath = queryXPath(field);
  582. if (*xpath)
  583. target.outputBeginNested(xpath, false);
  584. unsigned thisSize = toXMLFields(fields, self, self, target);
  585. if (*xpath)
  586. target.outputEndNested(xpath);
  587. return thisSize;
  588. }
  589. //-------------------------------------------------------------------------------------------------------------------
  590. size32_t RtlSetTypeInfo::size(const byte * self, const byte * selfrow) const
  591. {
  592. return sizeof(bool) + sizeof(size32_t) + rtlReadUInt4(self + sizeof(bool));
  593. }
  594. size32_t RtlSetTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  595. {
  596. unsigned offset = sizeof(bool) + sizeof(size32_t);
  597. unsigned max = offset + rtlReadUInt4(self + sizeof(bool));
  598. if (target.processBeginSet(field))
  599. {
  600. if (*(bool *)self)
  601. target.processSetAll(field);
  602. else
  603. {
  604. while (offset < max)
  605. {
  606. offset += child->process(self+offset, selfrow, field, target);
  607. }
  608. }
  609. target.processEndSet(field);
  610. }
  611. return max;
  612. }
  613. size32_t RtlSetTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  614. {
  615. unsigned offset = sizeof(bool) + sizeof(size32_t);
  616. unsigned max = offset + rtlReadUInt4(self + sizeof(bool));
  617. StringAttr outerTag;
  618. if (hasOuterXPath(field))
  619. {
  620. queryNestedOuterXPath(outerTag, field);
  621. target.outputBeginNested(outerTag, false);
  622. }
  623. if (*(bool *)self)
  624. target.outputSetAll();
  625. else
  626. {
  627. while (offset < max)
  628. {
  629. child->toXML(self+offset, selfrow, field, target);
  630. offset += child->size(self+offset, selfrow);
  631. }
  632. }
  633. if (outerTag)
  634. target.outputEndNested(outerTag);
  635. return max;
  636. }
  637. //-------------------------------------------------------------------------------------------------------------------
  638. size32_t RtlRowTypeInfo::size(const byte * self, const byte * selfrow) const
  639. {
  640. if (isLinkCounted())
  641. return sizeof(void *);
  642. return child->size(self, selfrow);
  643. }
  644. size32_t RtlRowTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  645. {
  646. if (isLinkCounted())
  647. {
  648. const byte * row = *(const byte * *)self;
  649. if (row)
  650. child->process(row, row, field, target);
  651. return sizeof(row);
  652. }
  653. return child->process(self, self, field, target);
  654. }
  655. size32_t RtlRowTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  656. {
  657. if (isLinkCounted())
  658. {
  659. const byte * row = *(const byte * *)self;
  660. child->toXML(row, row, field, target);
  661. return sizeof(row);
  662. }
  663. return child->toXML(self, self, field, target);
  664. }
  665. //-------------------------------------------------------------------------------------------------------------------
  666. size32_t RtlDatasetTypeInfo::size(const byte * self, const byte * selfrow) const
  667. {
  668. if (isLinkCounted())
  669. return sizeof(size32_t) + sizeof(void * *);
  670. return sizeof(size32_t) + rtlReadUInt4(self);
  671. }
  672. size32_t RtlDatasetTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  673. {
  674. if (isLinkCounted())
  675. {
  676. if (target.processBeginDataset(field))
  677. {
  678. size32_t thisCount = rtlReadUInt4(self);
  679. const byte * * rows = *reinterpret_cast<const byte * * const *>(self + sizeof(size32_t));
  680. for (unsigned i= 0; i < thisCount; i++)
  681. {
  682. const byte * row = rows[i];
  683. child->process(row, row, field, target);
  684. }
  685. target.processEndDataset(field);
  686. }
  687. return sizeof(size32_t) + sizeof(void * *);
  688. }
  689. else
  690. {
  691. unsigned offset = sizeof(size32_t);
  692. unsigned max = offset + rtlReadUInt4(self);
  693. if (target.processBeginDataset(field))
  694. {
  695. while (offset < max)
  696. {
  697. offset += child->process(self+offset, self+offset, field, target);
  698. }
  699. target.processEndDataset(field);
  700. }
  701. return max;
  702. }
  703. }
  704. size32_t RtlDatasetTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  705. {
  706. StringAttr outerTag;
  707. if (hasOuterXPath(field))
  708. {
  709. queryNestedOuterXPath(outerTag, field);
  710. target.outputBeginNested(outerTag, false);
  711. }
  712. unsigned thisSize;
  713. if (isLinkCounted())
  714. {
  715. size32_t thisCount = rtlReadUInt4(self);
  716. const byte * * rows = *reinterpret_cast<const byte * * const *>(self + sizeof(size32_t));
  717. for (unsigned i= 0; i < thisCount; i++)
  718. {
  719. const byte * row = rows[i];
  720. if (row)
  721. child->toXML(row, row, field, target);
  722. }
  723. thisSize = sizeof(size32_t) + sizeof(void * *);
  724. }
  725. else
  726. {
  727. unsigned offset = sizeof(size32_t);
  728. unsigned max = offset + rtlReadUInt4(self);
  729. while (offset < max)
  730. {
  731. child->toXML(self+offset, self+offset, field, target);
  732. offset += child->size(self+offset, self+offset);
  733. }
  734. thisSize = max;
  735. }
  736. if (outerTag)
  737. target.outputEndNested(outerTag);
  738. return thisSize;
  739. }
  740. //-------------------------------------------------------------------------------------------------------------------
  741. size32_t RtlIfBlockTypeInfo::size(const byte * self, const byte * selfrow) const
  742. {
  743. if (getCondition(selfrow))
  744. return sizeFields(fields, self, selfrow);
  745. return 0;
  746. }
  747. size32_t RtlIfBlockTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  748. {
  749. if (getCondition(selfrow))
  750. return processFields(fields, self, selfrow, target);
  751. return 0;
  752. }
  753. size32_t RtlIfBlockTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  754. {
  755. if (getCondition(selfrow))
  756. return toXMLFields(fields, self, selfrow, target);
  757. return 0;
  758. }
  759. //-------------------------------------------------------------------------------------------------------------------
  760. __int64 RtlBitfieldTypeInfo::signedValue(const byte * self) const
  761. {
  762. __int64 value = rtlReadInt(self, getBitfieldIntSize());
  763. unsigned shift = getBitfieldShift();
  764. unsigned numBits = getBitfieldNumBits();
  765. value <<= (sizeof(value) - shift - numBits);
  766. return value >> numBits;
  767. }
  768. unsigned __int64 RtlBitfieldTypeInfo::unsignedValue(const byte * self) const
  769. {
  770. unsigned __int64 value = rtlReadInt(self, getBitfieldIntSize());
  771. unsigned shift = getBitfieldShift();
  772. unsigned numBits = getBitfieldNumBits();
  773. value <<= (sizeof(value) - shift - numBits);
  774. return value >> numBits;
  775. }
  776. size32_t RtlBitfieldTypeInfo::size(const byte * self, const byte * selfrow) const
  777. {
  778. if (fieldType & RFTMislastbitfield)
  779. return getBitfieldIntSize();
  780. return 0;
  781. }
  782. size32_t RtlBitfieldTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  783. {
  784. if (isUnsigned())
  785. target.processUInt(unsignedValue(self), field);
  786. else
  787. target.processInt(signedValue(self), field);
  788. return size(self, selfrow);
  789. }
  790. size32_t RtlBitfieldTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  791. {
  792. if (isUnsigned())
  793. target.outputUInt(unsignedValue(self), queryXPath(field));
  794. else
  795. target.outputInt(signedValue(self), queryXPath(field));
  796. return size(self, selfrow);
  797. }
  798. //-------------------------------------------------------------------------------------------------------------------
  799. size32_t RtlUnimplementedTypeInfo::size(const byte * self, const byte * selfrow) const
  800. {
  801. rtlFailUnexpected();
  802. return 0;
  803. }
  804. size32_t RtlUnimplementedTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  805. {
  806. rtlFailUnexpected();
  807. return 0;
  808. }
  809. size32_t RtlUnimplementedTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  810. {
  811. rtlFailUnexpected();
  812. return 0;
  813. }
  814. //-------------------------------------------------------------------------------------------------------------------
  815. RtlFieldStrInfo::RtlFieldStrInfo(const char * _name, const char * _xpath, const RtlTypeInfo * _type)
  816. : RtlFieldInfo(rtlCreateFieldNameAtom(_name), _xpath, _type)
  817. {
  818. }
  819. /*
  820. Stack:
  821. * Change hqlhtcpp so that the correct derived classes are generated.
  822. * Test so that toXML calls the default implementaions and check that the same values are generated. (Don't if contains ifblocks/alien)
  823. * Release
  824. * Think about bitfields - how do I know it is the last bitfield, how am I going to keep track of the offsets.
  825. * What code would need to be generated for alien datatypes.
  826. * Could have alien int and alien string varieties????
  827. * What would an ecl interpreter look like (a special workunit?) helpers are interpreted? What about the graph?
  828. * Could I add associations to register user attributes - so a callback could know when they were assigned to?
  829. * Could I add ctx->noteLocation() into the generated code - so could put breakpoints on variables.
  830. * Add annotation when a member of the target dataset is updated.
  831. ctx->noteFieldAssigned(self, <field>); - does this include temporary datasets?
  832. ctx->noteAttributeX(<name>, Int|String|Unicode|
  833. ctx->noteLocation(location); - reduce locations, so only one returned per line for a non-dataset? Should it just be the first item on the line that is tagged??
  834. * Need static information about the breakpoints so debugger knows where to put valid brakpoints....
  835. * Debugger will want to know about the type of the breakpoints.
  836. * Should try and compress the location format - possibly have a table of <module.attributes>-># with breakpoint as 12:23
  837. Also need some information about which datasets, and stored variables etc. are used so they can be displayed.
  838. - Most datasets can be deduced from the parameters passed into the transform
  839. - Some are trickier e.g., the extract, could possibly define some mappings
  840. - options to disable projects/other more complex operations inline (so easier to walk through)
  841. Bitfields:
  842. - Two separate questions:
  843. i) How is the meta information generated.
  844. ii) How is it stored internally in an IHqlExpression * ?
  845. * Could store the offset in the type - either in the base type of as a qualifier.
  846. + much easier code generation.
  847. - Doesn't provie an easy indication of the last field in a bitfield (because can't really modify after the fact)
  848. - Problematic when fields are removed to merge them.
  849. * Could add a bitfield container to the record.
  850. + Makes it easier to handle the last bitfield
  851. + Matches the structure used for the cursor.
  852. - Everything needs to walk the bitfield containers similar to ifblocks.
  853. - Makes it just as tricky to merge
  854. - Harder to create the record, unless the code is implicitly handled by appendOperand().
  855. * The type of no_select could contain a modifier to indicate the offset/islast
  856. + the type of a no_select would have a 1:1 mapping with type info.
  857. - A bit complicated to calculate, especially when it isn't used much of the time
  858. => On Reflection is is probably easiest to keep the structure as it is (some comments should go in hqlexpr to avoid revisiting).
  859. * interperet bitfield offsets and "is last bitfield" dynamically
  860. + Greatly simplifies generating the meta - you can always use the field.
  861. - Requires another parameter and significant extra complexity for the uncommon case. (especially incrementing self)
  862. * Could generate from the expanded record instead of walking the record structure directly
  863. + That already knows how the bitfields are allocated, and could easily know which is the last field.
  864. - A field is no longer sufficient as key fr searching for the information.
  865. - Best would be a createFieldTypeKey(select-expr) which returns field when approriate, or modified if a bitfield. Then the pain is localised.
  866. * Output a bitfield container item into the type information
  867. + Solves the size problem
  868. - Individual bitfields still need to know their offsets, so doesn't solve the full problem.
  869. =>
  870. Change so that either use meta to generate the information, or use no_select when appropriate to fidn out the nesc. information.
  871. Probably the latter for the moment.
  872. a) Create a key function and make sure it is always used.
  873. b) Need to work out how to generate no_ifblock.
  874. - ifblock is context dependent, so need to generate as part of the parent record, and in the parent record context.
  875. */