PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/libcoopy_core/include/coopy/CsvSheet.h

https://github.com/paulfitz/coopy
C Header | 256 lines | 159 code | 36 blank | 61 comment | 10 complexity | c8530d48f9a91f235267fa9d4edc7ad8 MD5 | raw file
  1. #ifndef SSFOSSIL_CSVSHEET
  2. #define SSFOSSIL_CSVSHEET
  3. #include <coopy/TypedSheet.h>
  4. #include <vector>
  5. #include <string>
  6. namespace coopy {
  7. namespace store {
  8. class CsvSheet;
  9. class CsvSheetSchema;
  10. }
  11. }
  12. class coopy::store::CsvSheet : public EscapedTypedSheet<std::string> {
  13. private:
  14. std::vector<pairCellType> rec;
  15. int th, tw;
  16. bool valid;
  17. SheetStyle style;
  18. bool strict;
  19. Poly<SheetSchema> pSchema;
  20. std::string sheetName;
  21. public:
  22. using EscapedTypedSheet<std::string>::resize;
  23. using EscapedTypedSheet<std::string>::insertRow;
  24. CsvSheet() {
  25. tw = th = 0;
  26. valid = true;
  27. strict = true;
  28. }
  29. virtual SheetSchema *getSchema() const {
  30. return pSchema.getContent();
  31. }
  32. void setSchema(Poly<SheetSchema> pSchema) {
  33. this->pSchema = pSchema;
  34. }
  35. const SheetStyle& getStyle() {
  36. return style;
  37. }
  38. void setStyle(const SheetStyle& style) {
  39. this->style = style;
  40. }
  41. bool setStrict(bool strict) {
  42. this->strict = strict;
  43. return strict;
  44. }
  45. bool isStrict() {
  46. return strict;
  47. }
  48. // Deprecated, should migrate to deleteRow
  49. bool removeRow(int index) {
  50. s.arr.erase(s.arr.begin()+index);
  51. if (s.arr.size()<(size_t)s.h) {
  52. th = s.h = s.arr.size();
  53. return true;
  54. }
  55. return false;
  56. }
  57. /*
  58. // Deprecated
  59. bool insertRow(int index, int width = -1) {
  60. std::vector<pairCellType> rec;
  61. if (width == -1) { width = s.w; }
  62. for (int i=0; i<width; i++) {
  63. rec.push_back(pairCellType("",true));
  64. }
  65. if (index == -1 || index>=height()) {
  66. s.arr.push_back(rec);
  67. } else {
  68. s.arr.insert(s.arr.begin()+index,rec);
  69. }
  70. th = s.h = s.arr.size();
  71. tw = s.w = rec.size();
  72. return true;
  73. }
  74. */
  75. void addField(const char *s, bool escaped);
  76. void addField(const coopy::store::SheetCell& c) {
  77. addField(c.text.c_str(),c.escaped);
  78. }
  79. void addField(const char *s, int len, bool escaped) {
  80. std::string str(s,len);
  81. str += "\0";
  82. addField(str.c_str(),escaped);
  83. }
  84. void addRecord();
  85. void addRow(CsvSheet& alt, int row) {
  86. for (int i=0; i<alt.width(); i++) {
  87. const pairCellType& p = alt.pcell(i,row);
  88. addField(p.first.c_str(),p.second);
  89. }
  90. addRecord();
  91. }
  92. void clear() {
  93. tw = th = 0;
  94. s.w = s.h = 0;
  95. s.arr.clear();
  96. rec.clear();
  97. valid = true;
  98. }
  99. bool isValid() const {
  100. return valid;
  101. }
  102. virtual std::string cellString(int x, int y) const {
  103. if (valid) { return cell(x,y); }
  104. if (s.arr[y].size()>x) {
  105. return cell(x,y);
  106. }
  107. return "";
  108. }
  109. virtual std::string cellString(int x, int y, bool& escaped) const {
  110. if (valid) {
  111. const pairCellType& c = pcell(x,y);
  112. escaped = c.second;
  113. return c.first;
  114. }
  115. if (s.arr[y].size()>x) {
  116. const pairCellType& c = pcell(x,y);
  117. escaped = c.second;
  118. return c.first;
  119. }
  120. // for CSV-friendliness, return "" instead of NULL
  121. // for cells that did not get appended. This "feature"
  122. // is only used by the patch generator.
  123. escaped = false;
  124. return "";
  125. }
  126. virtual bool cellString(int x, int y, const std::string& str) {
  127. cell(x,y) = str;
  128. return true;
  129. }
  130. virtual bool cellString(int x, int y, const std::string& str,
  131. bool escaped) {
  132. pairCellType& p = pcell(x,y);
  133. p.first = str;
  134. p.second = escaped;
  135. return true;
  136. }
  137. const CsvSheet& copy(const CsvSheet& alt) {
  138. s.arr = alt.s.arr;
  139. s.h = alt.s.h;
  140. s.w = alt.s.w;
  141. return *this;
  142. }
  143. const CsvSheet& copy(const DataSheet& alt) {
  144. clear();
  145. for (int i=0; i<alt.height(); i++) {
  146. for (int j=0; j<alt.width(); j++) {
  147. addField(alt.cellSummary(j,i));
  148. }
  149. addRecord();
  150. }
  151. return *this;
  152. }
  153. virtual bool canResize() { return true; }
  154. virtual bool resize(int w, int h) {
  155. clear();
  156. SheetCell cell;
  157. for (int i=0; i<h; i++) {
  158. for (int j=0; j<w; j++) {
  159. addField(cell);
  160. }
  161. addRecord();
  162. }
  163. s.w = w;
  164. s.h = h;
  165. return true;
  166. }
  167. bool setWidth(int w) {
  168. s.w = w;
  169. return true;
  170. }
  171. virtual std::string getDescription() const {
  172. return "csv";
  173. }
  174. virtual bool hasSheetName() const {
  175. return sheetName!="";
  176. }
  177. void setSheetName(const char *name) {
  178. sheetName = name;
  179. }
  180. };
  181. /*
  182. class coopy::store::CsvSheetSchema : public SheetSchema {
  183. public:
  184. CsvSheet *sheet;
  185. std::string name;
  186. int row;
  187. CsvSheetSchema(CsvSheet *sheet, const std::string& name, int row) :
  188. sheet(sheet), name(name), row(row)
  189. {
  190. row = 0;
  191. }
  192. virtual int headerHeight() const {
  193. return row;
  194. }
  195. virtual std::string getSheetName() const {
  196. return name;
  197. }
  198. virtual ColumnInfo getColumnInfo(int x) const {
  199. if (row==-1) return ColumnInfo();
  200. return ColumnInfo(sheet.cellString(x,row),ColumnType());
  201. }
  202. virtual int getColumnCount() const {
  203. return sheet->width();
  204. }
  205. virtual bool providesPrimaryKeys() const {
  206. return false;
  207. }
  208. virtual bool isGuess() const {
  209. return false;
  210. }
  211. };
  212. */
  213. #endif