00001 #ifndef tableconv_h
00002 #define tableconv_h
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "sets.h"
00016 #include "executor.h"
00017 #include "bufstringset.h"
00018 #include <iostream>
00019
00020 namespace Table
00021 {
00022
00023 mClass ImportHandler
00024 {
00025 public:
00026 ImportHandler( std::istream& strm )
00027 : strm_(strm)
00028 , colpos_(0) {}
00029
00030 enum State { Error, InCol, EndCol, EndRow };
00031
00032 virtual State add(char) = 0;
00033 const char* getCol() const { return col_.buf(); }
00034 const char* errMsg() const { return col_.buf(); }
00035
00036 virtual void newRow() {}
00037 virtual void newCol() { *col_.buf() = '\0';
00038 colpos_ = 0; }
00039
00040 inline char readNewChar() const
00041 {
00042 char c = strm_.peek();
00043 strm_.ignore( 1 );
00044 return c;
00045 }
00046 inline bool atEnd() const { return strm_.eof(); }
00047
00048 protected:
00049
00050 std::istream& strm_;
00051 BufferString col_;
00052 int colpos_;
00053
00054 void addToCol(char);
00055
00056 };
00057
00058
00059 mClass ExportHandler
00060 {
00061 public:
00062 ExportHandler( std::ostream& strm )
00063 : strm_(strm) {}
00064
00065 virtual const char* putRow(const BufferStringSet&) = 0;
00066
00067 virtual bool init();
00068 virtual void finish();
00069
00070 static bool isNumber(const char*);
00071
00072 BufferString prepend_;
00074 BufferString append_;
00076
00077 protected:
00078
00079 std::ostream& strm_;
00080
00081 inline const char* getStrmMsg() const
00082 { return strm_.good() ? 0 : "Error writing to output"; }
00083
00084 };
00085
00086
00087
00088 mClass Converter : public Executor
00089 {
00090 public:
00091 Converter( ImportHandler& i, ExportHandler& o )
00092 : Executor("Data import")
00093 , imphndlr_(i), exphndlr_(o)
00094 , rowsdone_(0), selcolnr_(-1)
00095 , msg_("Importing") {}
00096
00097 TypeSet<int> selcols_;
00098 BufferString msg_;
00099
00100 virtual int nextStep();
00101 const char* message() const { return msg_.buf(); }
00102 const char* nrDoneText() const { return "Records read"; }
00103 od_int64 nrDone() const { return rowsdone_; }
00104
00105 struct RowManipulator
00106 {
00107 virtual bool accept(BufferStringSet&) const = 0;
00109 };
00110 void setManipulator( const RowManipulator* m )
00111 { manipulators_.erase(); addManipulator(m); }
00112 void addManipulator( const RowManipulator* m )
00113 { manipulators_ += m; }
00114
00115 protected:
00116
00117 ImportHandler& imphndlr_;
00118 ExportHandler& exphndlr_;
00119 BufferStringSet row_;
00120 ObjectSet<const RowManipulator> manipulators_;
00121
00122 int colnr_;
00123 int selcolnr_;
00124 int rowsdone_;
00125
00126 bool handleImpState(ImportHandler::State);
00127 inline bool colSel() const
00128 { return selcols_.isEmpty()
00129 || selcols_.indexOf(colnr_) > -1; }
00130 };
00131
00132 };
00133
00134
00135 #endif