PageRenderTime 31ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/xbmc/dbwrappers/qry_dat.cpp

https://gitlab.com/sloshedpuppie/LetsGoRetro
C++ | 745 lines | 653 code | 57 blank | 35 comment | 27 complexity | 58dfc4b82318467b8d0df28bbbdd29e3 MD5 | raw file
  1. /**********************************************************************
  2. * Copyright (c) 2004, Leo Seib, Hannover
  3. *
  4. * Project: C++ Dynamic Library
  5. * Module: FieldValue class realisation file
  6. * Author: Leo Seib E-Mail: leoseib@web.de
  7. * Begin: 5/04/2002
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. **********************************************************************/
  28. /**********************************************************************
  29. * 2005-03-29 - Minor modifications to allow get_asBool to function on
  30. * on string values that are 1 or 0
  31. **********************************************************************/
  32. #include "qry_dat.h"
  33. #include "system.h" // for PRId64
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #ifndef __GNUC__
  37. #pragma warning (disable:4800)
  38. #pragma warning (disable:4715)
  39. #endif
  40. namespace dbiplus {
  41. //Constructors
  42. field_value::field_value()
  43. {
  44. field_type = ft_String;
  45. is_null = false;
  46. }
  47. field_value::field_value(const char *s):
  48. str_value(s)
  49. {
  50. field_type = ft_String;
  51. is_null = false;
  52. }
  53. field_value::field_value(const bool b) {
  54. bool_value = b;
  55. field_type = ft_Boolean;
  56. is_null = false;
  57. }
  58. field_value::field_value(const char c) {
  59. char_value = c;
  60. field_type = ft_Char;
  61. is_null = false;
  62. }
  63. field_value::field_value(const short s) {
  64. short_value = s;
  65. field_type = ft_Short;
  66. is_null = false;
  67. }
  68. field_value::field_value(const unsigned short us) {
  69. ushort_value = us;
  70. field_type = ft_UShort;
  71. is_null = false;
  72. }
  73. field_value::field_value(const int i) {
  74. int_value = i;
  75. field_type = ft_Int;
  76. is_null = false;
  77. }
  78. field_value::field_value(const unsigned int ui) {
  79. uint_value = ui;
  80. field_type = ft_UInt;
  81. is_null = false;
  82. }
  83. field_value::field_value(const float f) {
  84. float_value = f;
  85. field_type = ft_Float;
  86. is_null = false;
  87. }
  88. field_value::field_value(const double d) {
  89. double_value = d;
  90. field_type = ft_Double;
  91. is_null = false;
  92. }
  93. field_value::field_value(const int64_t i) {
  94. int64_value = i;
  95. field_type = ft_Int64;
  96. is_null = false;
  97. }
  98. field_value::field_value (const field_value & fv) {
  99. switch (fv.get_fType()) {
  100. case ft_String: {
  101. set_asString(fv.get_asString());
  102. break;
  103. }
  104. case ft_Boolean:{
  105. set_asBool(fv.get_asBool());
  106. break;
  107. }
  108. case ft_Char: {
  109. set_asChar(fv.get_asChar());
  110. break;
  111. }
  112. case ft_Short: {
  113. set_asShort(fv.get_asShort());
  114. break;
  115. }
  116. case ft_UShort: {
  117. set_asUShort(fv.get_asUShort());
  118. break;
  119. }
  120. case ft_Int: {
  121. set_asInt(fv.get_asInt());
  122. break;
  123. }
  124. case ft_UInt: {
  125. set_asUInt(fv.get_asUInt());
  126. break;
  127. }
  128. case ft_Float: {
  129. set_asFloat(fv.get_asFloat());
  130. break;
  131. }
  132. case ft_Double: {
  133. set_asDouble(fv.get_asDouble());
  134. break;
  135. }
  136. case ft_Int64: {
  137. set_asInt64(fv.get_asInt64());
  138. break;
  139. }
  140. default:
  141. break;
  142. }
  143. is_null = fv.get_isNull();
  144. }
  145. //empty destructor
  146. field_value::~field_value(){
  147. }
  148. //Conversations functions
  149. std::string field_value::get_asString() const {
  150. std::string tmp;
  151. switch (field_type) {
  152. case ft_String: {
  153. tmp = str_value;
  154. return tmp;
  155. }
  156. case ft_Boolean:{
  157. if (bool_value)
  158. return tmp = "True";
  159. else
  160. return tmp = "False";
  161. }
  162. case ft_Char: {
  163. return tmp = char_value;
  164. }
  165. case ft_Short: {
  166. char t[10];
  167. sprintf(t,"%i",short_value);
  168. return tmp = t;
  169. }
  170. case ft_UShort: {
  171. char t[10];
  172. sprintf(t,"%i",ushort_value);
  173. return tmp = t;
  174. }
  175. case ft_Int: {
  176. char t[12];
  177. sprintf(t,"%d",int_value);
  178. return tmp = t;
  179. }
  180. case ft_UInt: {
  181. char t[12];
  182. sprintf(t,"%u",uint_value);
  183. return tmp = t;
  184. }
  185. case ft_Float: {
  186. char t[16];
  187. sprintf(t,"%f",float_value);
  188. return tmp = t;
  189. }
  190. case ft_Double: {
  191. char t[32];
  192. sprintf(t,"%f",double_value);
  193. return tmp = t;
  194. }
  195. case ft_Int64: {
  196. char t[23];
  197. sprintf(t,"%" PRId64,int64_value);
  198. return tmp = t;
  199. }
  200. default:
  201. return tmp = "";
  202. }
  203. }
  204. bool field_value::get_asBool() const {
  205. switch (field_type) {
  206. case ft_String: {
  207. if (str_value == "True" || str_value == "true" || str_value == "1")
  208. return true;
  209. else
  210. return false;
  211. }
  212. case ft_Boolean:{
  213. return bool_value;
  214. }
  215. case ft_Char: {
  216. if (char_value == 'T' || char_value == 't')
  217. return true;
  218. else
  219. return false;
  220. }
  221. case ft_Short: {
  222. return (bool)short_value;
  223. }
  224. case ft_UShort: {
  225. return (bool)ushort_value;
  226. }
  227. case ft_Int: {
  228. return (bool)int_value;
  229. }
  230. case ft_UInt: {
  231. return (bool)uint_value;
  232. }
  233. case ft_Float: {
  234. return (bool)float_value;
  235. }
  236. case ft_Double: {
  237. return (bool)double_value;
  238. }
  239. case ft_Int64: {
  240. return (bool)int64_value;
  241. }
  242. default:
  243. return false;
  244. }
  245. }
  246. char field_value::get_asChar() const {
  247. switch (field_type) {
  248. case ft_String: {
  249. return str_value[0];
  250. }
  251. case ft_Boolean:{
  252. if (bool_value)
  253. return 'T';
  254. else
  255. return 'F';
  256. }
  257. case ft_Char: {
  258. return char_value;
  259. }
  260. case ft_Short: {
  261. char t[10];
  262. sprintf(t,"%i",short_value);
  263. return t[0];
  264. }
  265. case ft_UShort: {
  266. char t[10];
  267. sprintf(t,"%i",ushort_value);
  268. return t[0];
  269. }
  270. case ft_Int: {
  271. char t[12];
  272. sprintf(t,"%d",int_value);
  273. return t[0];
  274. }
  275. case ft_UInt: {
  276. char t[12];
  277. sprintf(t,"%u",uint_value);
  278. return t[0];
  279. }
  280. case ft_Float: {
  281. char t[16];
  282. sprintf(t,"%f",float_value);
  283. return t[0];
  284. }
  285. case ft_Double: {
  286. char t[32];
  287. sprintf(t,"%f",double_value);
  288. return t[0];
  289. }
  290. case ft_Int64: {
  291. char t[24];
  292. sprintf(t,"%" PRId64,int64_value);
  293. return t[0];
  294. }
  295. default:
  296. return '\0';
  297. }
  298. }
  299. short field_value::get_asShort() const {
  300. switch (field_type) {
  301. case ft_String: {
  302. return (short)atoi(str_value.c_str());
  303. }
  304. case ft_Boolean:{
  305. return (short)bool_value;
  306. }
  307. case ft_Char: {
  308. return (short)char_value;
  309. }
  310. case ft_Short: {
  311. return short_value;
  312. }
  313. case ft_UShort: {
  314. return (short)ushort_value;
  315. }
  316. case ft_Int: {
  317. return (short)int_value;
  318. }
  319. case ft_UInt: {
  320. return (short)uint_value;
  321. }
  322. case ft_Float: {
  323. return (short)float_value;
  324. }
  325. case ft_Double: {
  326. return (short)double_value;
  327. }
  328. case ft_Int64: {
  329. return (short)int64_value;
  330. }
  331. default:
  332. return 0;
  333. }
  334. }
  335. unsigned short field_value::get_asUShort() const {
  336. switch (field_type) {
  337. case ft_String: {
  338. return (unsigned short)atoi(str_value.c_str());
  339. }
  340. case ft_Boolean:{
  341. return (unsigned short)bool_value;
  342. }
  343. case ft_Char: {
  344. return (unsigned short)char_value;
  345. }
  346. case ft_Short: {
  347. return (unsigned short)short_value;
  348. }
  349. case ft_UShort: {
  350. return ushort_value;
  351. }
  352. case ft_Int: {
  353. return (unsigned short)int_value;
  354. }
  355. case ft_UInt: {
  356. return (unsigned short)uint_value;
  357. }
  358. case ft_Float: {
  359. return (unsigned short)float_value;
  360. }
  361. case ft_Double: {
  362. return (unsigned short)double_value;
  363. }
  364. case ft_Int64: {
  365. return (unsigned short)int64_value;
  366. }
  367. default:
  368. return 0;
  369. }
  370. }
  371. int field_value::get_asInt() const {
  372. switch (field_type) {
  373. case ft_String: {
  374. return (int)atoi(str_value.c_str());
  375. }
  376. case ft_Boolean:{
  377. return (int)bool_value;
  378. }
  379. case ft_Char: {
  380. return (int)char_value;
  381. }
  382. case ft_Short: {
  383. return (int)short_value;
  384. }
  385. case ft_UShort: {
  386. return (int)ushort_value;
  387. }
  388. case ft_Int: {
  389. return int_value;
  390. }
  391. case ft_UInt: {
  392. return (int)uint_value;
  393. }
  394. case ft_Float: {
  395. return (int)float_value;
  396. }
  397. case ft_Double: {
  398. return (int)double_value;
  399. }
  400. case ft_Int64: {
  401. return (int)int64_value;
  402. }
  403. default:
  404. return 0;
  405. }
  406. }
  407. unsigned int field_value::get_asUInt() const {
  408. switch (field_type) {
  409. case ft_String: {
  410. return (unsigned int)atoi(str_value.c_str());
  411. }
  412. case ft_Boolean:{
  413. return (unsigned int)bool_value;
  414. }
  415. case ft_Char: {
  416. return (unsigned int)char_value;
  417. }
  418. case ft_Short: {
  419. return (unsigned int)short_value;
  420. }
  421. case ft_UShort: {
  422. return (unsigned int)ushort_value;
  423. }
  424. case ft_Int: {
  425. return (unsigned int)int_value;
  426. }
  427. case ft_UInt: {
  428. return uint_value;
  429. }
  430. case ft_Float: {
  431. return (unsigned int)float_value;
  432. }
  433. case ft_Double: {
  434. return (unsigned int)double_value;
  435. }
  436. case ft_Int64: {
  437. return (unsigned int)int64_value;
  438. }
  439. default:
  440. return 0;
  441. }
  442. }
  443. float field_value::get_asFloat() const {
  444. switch (field_type) {
  445. case ft_String: {
  446. return (float)atof(str_value.c_str());
  447. }
  448. case ft_Boolean:{
  449. return (float)bool_value;
  450. }
  451. case ft_Char: {
  452. return (float)char_value;
  453. }
  454. case ft_Short: {
  455. return (float)short_value;
  456. }
  457. case ft_UShort: {
  458. return (float)ushort_value;
  459. }
  460. case ft_Int: {
  461. return (float)int_value;
  462. }
  463. case ft_UInt: {
  464. return (float)uint_value;
  465. }
  466. case ft_Float: {
  467. return float_value;
  468. }
  469. case ft_Double: {
  470. return (float)double_value;
  471. }
  472. case ft_Int64: {
  473. return (float)int64_value;
  474. }
  475. default:
  476. return 0.0;
  477. }
  478. }
  479. double field_value::get_asDouble() const {
  480. switch (field_type) {
  481. case ft_String: {
  482. return atof(str_value.c_str());
  483. }
  484. case ft_Boolean:{
  485. return (double)bool_value;
  486. }
  487. case ft_Char: {
  488. return (double)char_value;
  489. }
  490. case ft_Short: {
  491. return (double)short_value;
  492. }
  493. case ft_UShort: {
  494. return (double)ushort_value;
  495. }
  496. case ft_Int: {
  497. return (double)int_value;
  498. }
  499. case ft_UInt: {
  500. return (double)uint_value;
  501. }
  502. case ft_Float: {
  503. return (double)float_value;
  504. }
  505. case ft_Double: {
  506. return (double)double_value;
  507. }
  508. case ft_Int64: {
  509. return (double)int64_value;
  510. }
  511. default:
  512. return 0.0;
  513. }
  514. }
  515. int64_t field_value::get_asInt64() const {
  516. switch (field_type) {
  517. case ft_String: {
  518. return _atoi64(str_value.c_str());
  519. }
  520. case ft_Boolean:{
  521. return (int64_t)bool_value;
  522. }
  523. case ft_Char: {
  524. return (int64_t)char_value;
  525. }
  526. case ft_Short: {
  527. return (int64_t)short_value;
  528. }
  529. case ft_UShort: {
  530. return (int64_t)ushort_value;
  531. }
  532. case ft_Int: {
  533. return (int64_t)int_value;
  534. }
  535. case ft_UInt: {
  536. return (int64_t)uint_value;
  537. }
  538. case ft_Float: {
  539. return (int64_t)float_value;
  540. }
  541. case ft_Double: {
  542. return (int64_t)double_value;
  543. }
  544. case ft_Int64: {
  545. return int64_value;
  546. }
  547. default:
  548. return 0;
  549. }
  550. }
  551. field_value& field_value::operator= (const field_value & fv) {
  552. if ( this == &fv ) return *this;
  553. is_null = fv.get_isNull();
  554. switch (fv.get_fType()) {
  555. case ft_String: {
  556. set_asString(fv.get_asString());
  557. return *this;
  558. break;
  559. }
  560. case ft_Boolean:{
  561. set_asBool(fv.get_asBool());
  562. return *this;
  563. break;
  564. }
  565. case ft_Char: {
  566. set_asChar(fv.get_asChar());
  567. return *this;
  568. break;
  569. }
  570. case ft_Short: {
  571. set_asShort(fv.get_asShort());
  572. return *this;
  573. break;
  574. }
  575. case ft_UShort: {
  576. set_asUShort(fv.get_asUShort());
  577. return *this;
  578. break;
  579. }
  580. case ft_Int: {
  581. set_asInt(fv.get_asInt());
  582. return *this;
  583. break;
  584. }
  585. case ft_UInt: {
  586. set_asUInt(fv.get_asUInt());
  587. return *this;
  588. break;
  589. }
  590. case ft_Float: {
  591. set_asFloat(fv.get_asFloat());
  592. return *this;
  593. break;
  594. }
  595. case ft_Double: {
  596. set_asDouble(fv.get_asDouble());
  597. return *this;
  598. break;
  599. }
  600. case ft_Int64: {
  601. set_asInt64(fv.get_asInt64());
  602. return *this;
  603. break;
  604. }
  605. default:
  606. return *this;
  607. }
  608. }
  609. //Set functions
  610. void field_value::set_asString(const char *s) {
  611. str_value = s;
  612. field_type = ft_String;}
  613. void field_value::set_asString(const std::string & s) {
  614. str_value = s;
  615. field_type = ft_String;}
  616. void field_value::set_asBool(const bool b) {
  617. bool_value = b;
  618. field_type = ft_Boolean;}
  619. void field_value::set_asChar(const char c) {
  620. char_value = c;
  621. field_type = ft_Char;}
  622. void field_value::set_asShort(const short s) {
  623. short_value = s;
  624. field_type = ft_Short;}
  625. void field_value::set_asUShort(const unsigned short us) {
  626. ushort_value = us;
  627. field_type = ft_UShort;
  628. }
  629. void field_value::set_asInt(const int i) {
  630. int_value = i;
  631. field_type = ft_Int;
  632. }
  633. void field_value::set_asUInt(const unsigned int ui) {
  634. int_value = ui;
  635. field_type = ft_UInt;
  636. }
  637. void field_value::set_asFloat(const float f) {
  638. float_value = f;
  639. field_type = ft_Float;}
  640. void field_value::set_asDouble(const double d) {
  641. double_value = d;
  642. field_type = ft_Double;}
  643. void field_value::set_asInt64(const int64_t i) {
  644. int64_value = i;
  645. field_type = ft_Int64;}
  646. fType field_value::get_field_type() {
  647. return field_type;}
  648. std::string field_value::gft() {
  649. std::string tmp;
  650. switch (field_type) {
  651. case ft_String: {
  652. tmp.assign("string");
  653. return tmp;
  654. }
  655. case ft_Boolean:{
  656. tmp.assign("bool");
  657. return tmp;
  658. }
  659. case ft_Char: {
  660. tmp.assign("char");
  661. return tmp;
  662. }
  663. case ft_Short: {
  664. tmp.assign("short");
  665. return tmp;
  666. }
  667. case ft_Int: {
  668. tmp.assign("int");
  669. return tmp;
  670. }
  671. case ft_Float: {
  672. tmp.assign("float");
  673. return tmp;
  674. }
  675. case ft_Double: {
  676. tmp.assign("double");
  677. return tmp;
  678. }
  679. case ft_Int64: {
  680. tmp.assign("int64");
  681. return tmp;
  682. }
  683. default:
  684. break;
  685. }
  686. return tmp;
  687. }
  688. } //namespace