rtlfield.cpp 33 KB

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