Jafar
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
csvFile.hpp
00001 #ifndef CSV_FILE_HPP
00002 #define CSV_FILE_HPP
00003 
00004 #include <typeinfo>
00005 #include <string>
00006 #include <sstream>
00007 #include <map>
00008 #include <fstream>
00009 #include "boost/numeric/ublas/matrix.hpp"
00010 #include "kernel/kernelException.hpp"
00011 #include "boost/date_time/posix_time/posix_time.hpp"
00012 #include "boost/tokenizer.hpp"
00013 
00014 namespace jafar {
00015   namespace kernel {
00016 
00021     class CSVFile {
00022 
00023       private:
00024 
00026       std::string m_separator;
00028       char m_commentPrefix;
00030       bool withColumnsNames;
00032       std::map<std::string, int> columnNames;
00033       typedef boost::numeric::ublas::matrix<std::string> stringMatrix;
00035       stringMatrix fileMatrix;
00036       size_t lineNumber;
00037       size_t dataLineNumber;
00038       size_t columnNumber;
00039       static void compute_matrix_size(const std::string& _file, 
00040                                       size_t& nb_lines,
00041                                       size_t& nb_columns,
00042                                       const std::string& _separator = " ",
00043                                       char _commentPrefix = '#');
00044       public:
00045 
00046       CSVFile(std::string const& separator_ = " ", char commentPrefix_ = '#', bool withColumnsNames_ = false);
00047 
00049       void readFile(std::string const& filename);
00050 
00054       void writeFile(std::string const& filename);
00057       void hasColumnsNames(const bool& _withColumnsNames);
00059       bool hasColumn(std::string const& columnName) const;
00061       size_t nbOfLines() const;
00063       size_t nbOfColumns() const;
00065       inline std::string separator() const {
00066         return m_separator;
00067       }
00069       inline char commentPrefix() const {
00070         return m_commentPrefix;
00071       }
00073       template<class T>
00074         void getItem(size_t line, size_t columnNumber, T& value) const {
00075 
00076         JFR_PRED_ERROR(line >= 0 && line < fileMatrix.size1(),
00077                        KernelException,
00078                        KernelException::CSVFILE_UNKNOWN_LINE,
00079                        "CSVFile:getItem: unknown line: " << line);
00080         
00081         JFR_PRED_ERROR(columnNumber >= 0 && columnNumber < fileMatrix.size2(),
00082                        KernelException,
00083                        KernelException::CSVFILE_UNKNOWN_COLUMN,
00084                        "CSVFile:getItem: unknown column: " << columnNumber);
00085         std::istringstream ss(fileMatrix(line, columnNumber));
00086         ss >> value;
00087 
00088         JFR_IO_STREAM(ss, 
00089                       "CSVFile::getItem: invalid value parsing:" << std::endl << 
00090                       "line: " << line << std::endl <<
00091                       "column: " << columnNumber << std::endl <<
00092                       "value: " << fileMatrix(line, columnNumber) << std::endl <<
00093                       "value-type: " << typeid(T).name());
00094       } 
00095 
00097       template<class T>
00098         void getItem(size_t line, const std::string& columnName, T& value) const {
00099         std::map<std::string,int>::const_iterator iter = columnNames.find(columnName);
00100         JFR_PRED_ERROR(iter != columnNames.end(),
00101            KernelException,
00102            KernelException::CSVFILE_UNKNOWN_COLUMN_NAME,
00103            "CSVFile:getItem: unknown column name: " << columnName);
00104 
00105         int colNbr = iter->second;
00106         getItem<T>(line, colNbr, value);
00107       }
00108 
00110       template<class T>
00111         void setItem(size_t line, size_t column, const T& value) {
00112         if (fileMatrix.size1() < line+1) {
00113           fileMatrix.resize((line+1), fileMatrix.size2(), true);
00114         }
00115         if (fileMatrix.size2() < column+1) {
00116           fileMatrix.resize(fileMatrix.size1(), (column+1), true);
00117         }
00118   std::ostringstream ss;
00119   ss << value;
00120         fileMatrix(line, column) = ss.str();
00121       }
00122 
00124       template<class T>
00125         void setItem(size_t line, const std::string& colName, const T& value) {
00126         std::map<std::string,int>::const_iterator iter = columnNames.find(colName);
00127         JFR_PRED_ERROR(iter != columnNames.end(),
00128            KernelException,
00129            KernelException::CSVFILE_UNKNOWN_COLUMN_NAME,
00130            "CSVFile:getItem: unknown column name: " << colName);
00131 
00132         int colNbr = iter->second;
00133         setItem<T>(line, colNbr, value);
00134       }
00135 
00136     }; // class CSVFile
00137 
00143     class CSVFileLoad {
00144 
00145       public:
00146 
00152       void load(std::string const& filename,
00153     std::string const& separator_ = " ", char commentPrefix_ = '#');
00154 
00155       protected:
00156 
00157       virtual ~CSVFileLoad() {}
00158 
00162       virtual void loadCSVFile(jafar::kernel::CSVFile& csvFile) = 0;
00163       
00164     }; // class CSVFileLoad
00165 
00166 
00172     class CSVFileSave {
00173 
00174       public:
00175 
00181       void save(std::string const& filename,
00182     std::string const& separator_ = " ", char commentPrefix_ = '#');
00183 
00184       protected:
00185 
00186       virtual ~CSVFileSave() {}
00187 
00191       virtual void saveCSVFile(jafar::kernel::CSVFile& csvFile) = 0;
00192 
00193     }; // class CSVFileSave
00194 
00200     class CSVFileSaveLoad : public CSVFileLoad, public CSVFileSave {
00201 
00202     }; // class CSVFileSaveLoad
00203 
00204   } // namespace kernel
00205 } // namespace jafar
00206 
00207 #endif // KERNEL_KEY_VALUE_FILE_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Wed Oct 15 2014 00:37:24 for Jafar by doxygen 1.7.6.1
LAAS-CNRS