fork(3) download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. #include <stdlib.h>
  6.  
  7. template <typename T> struct is_signed_int { enum { val = false }; };
  8. template <> struct is_signed_int<short> { enum { val = true}; };
  9. template <> struct is_signed_int<int> { enum { val = true}; };
  10. template <> struct is_signed_int<long> { enum { val = true}; };
  11. template <> struct is_signed_int<long long> { enum { val = true}; };
  12.  
  13. template <typename T> struct is_unsigned_int { enum { val = false }; };
  14. template <> struct is_unsigned_int<unsigned short> { enum { val = true}; };
  15. template <> struct is_unsigned_int<unsigned int> { enum { val = true}; };
  16. template <> struct is_unsigned_int<unsigned long> { enum { val = true}; };
  17. template <> struct is_unsigned_int<unsigned long long> { enum { val = true}; };
  18.  
  19. template <typename T> struct is_int {
  20. enum { val = (is_signed_int<T>::val || is_unsigned_int<T>::val) };
  21. };
  22.  
  23. struct csv_istream {
  24. std::istream &is_;
  25. csv_istream (std::istream &is) : is_(is) {}
  26. void scan_ws () const {
  27. while (is_.good()) {
  28. int c = is_.peek();
  29. if (c != ' ' && c != '\t') break;
  30. is_.get();
  31. }
  32. }
  33. void scan (std::string *s = 0) const {
  34. std::string ws;
  35. int c = is_.get();
  36. if (is_.good()) {
  37. do {
  38. if (c == ',' || c == '\n') break;
  39. if (s) {
  40. ws += c;
  41. if (c != ' ' && c != '\t') {
  42. *s += ws;
  43. ws.clear();
  44. }
  45. }
  46. c = is_.get();
  47. } while (is_.good());
  48. if (is_.eof()) is_.clear();
  49. }
  50. }
  51. template <typename T, bool> struct set_value {
  52. void operator () (std::string in, T &v) const {
  53. std::istringstream(in) >> v;
  54. }
  55. };
  56. template <typename T> struct set_value<T, true> {
  57. template <bool SIGNED> void convert (std::string in, T &v) const {
  58. if (SIGNED) v = ::strtoll(in.c_str(), 0, 0);
  59. else v = ::strtoull(in.c_str(), 0, 0);
  60. }
  61. void operator () (std::string in, T &v) const {
  62. convert<is_signed_int<T>::val>(in, v);
  63. }
  64. };
  65. template <typename T> const csv_istream & operator >> (T &v) const {
  66. std::string tmp;
  67. scan(&tmp);
  68. set_value<T, is_int<T>::val>()(tmp, v);
  69. return *this;
  70. }
  71. const csv_istream & operator >> (std::string &v) const {
  72. v.clear();
  73. scan_ws();
  74. if (is_.peek() != '"') scan(&v);
  75. else {
  76. std::string tmp;
  77. is_.get();
  78. std::getline(is_, tmp, '"');
  79. while (is_.peek() == '"') {
  80. v += tmp;
  81. v += is_.get();
  82. std::getline(is_, tmp, '"');
  83. }
  84. v += tmp;
  85. scan();
  86. }
  87. return *this;
  88. }
  89. template <typename T>
  90. const csv_istream & operator >> (T &(*manip)(T &)) const {
  91. is_ >> manip;
  92. return *this;
  93. }
  94. operator bool () const { return !is_.fail(); }
  95. };
  96.  
  97. const char input[] =
  98. "Year,Make,Model,Description,Price\n"
  99. "1997,Ford,E350,\"ac, abs, moon\",3000.00\n"
  100. "1999,Chevy,\"Venture \"\"Extended Edition\"\"\",\"\",4900.00\n"
  101. "1999,Chevy,\"Venture \"\"Extended Edition, Very Large\"\"\",\"\",5000.00\n"
  102. "1996,Jeep,Grand Cherokee,\"MUST SELL!\n\
  103. air, moon roof, loaded\",4799.00\n"
  104. ;
  105.  
  106. int main () {
  107. std::istringstream ss(input);
  108. std::string title[5];
  109. int year;
  110. std::string make, model, desc;
  111. float price;
  112. csv_istream(ss)
  113. >> title[0] >> title[1] >> title[2] >> title[3] >> title[4];
  114. while (csv_istream(ss)
  115. >> year >> make >> model >> desc >> price) {
  116. std::cout
  117. << " " << title[0] << "=[" << year << "]"
  118. << " " << title[1] << "=[" << make << "]"
  119. << " " << title[2] << "=[" << model << "]"
  120. << " " << title[3] << "=[" << desc << "]"
  121. << " " << title[4] << "=[" << price << "]"
  122. << "\n"
  123. ;
  124. }
  125. }
  126.  
Success #stdin #stdout 0.02s 2864KB
stdin
Standard input is empty
stdout
 Year=[1997] Make=[Ford] Model=[E350] Description=[ac, abs, moon] Price=[3000]
 Year=[1999] Make=[Chevy] Model=[Venture "Extended Edition"] Description=[] Price=[4900]
 Year=[1999] Make=[Chevy] Model=[Venture "Extended Edition, Very Large"] Description=[] Price=[5000]
 Year=[1996] Make=[Jeep] Model=[Grand Cherokee] Description=[MUST SELL!
air, moon roof, loaded] Price=[4799]