|
STK++ 1.0
|
00001 /*--------------------------------------------------------------------*/ 00002 /* Copyright (C) 2004-2007 Serge Iovleff 00003 00004 This program is free software; you can redistribute it and/or modify 00005 it under the terms of the GNU Lesser General Public License as 00006 published by the Free Software Foundation; either version 2 of the 00007 License, or (at your option) any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU Lesser General Public License for more details. 00013 00014 You should have received a copy of the GNU Lesser General Public 00015 License along with this program; if not, write to the 00016 Free Software Foundation, Inc., 00017 59 Temple Place, 00018 Suite 330, 00019 Boston, MA 02111-1307 00020 USA 00021 00022 Contact : Serge.Iovleff@stkpp.org 00023 */ 00024 00025 /* 00026 * Project: stkpp::DManager 00027 * Purpose: Implementation of the class ImportFromCsv. 00028 * Author: Serge Iovleff, serge.iovleff@stkpp.org 00029 * 00030 */ 00031 00036 #include "../include/STK_ImportFromCsv.h" 00037 #include "../../STKernel/include/STK_Exceptions.h" 00038 00039 namespace STK 00040 { 00041 00042 // Default constructor for ImportCsv 00043 ImportFromCsv::ImportFromCsv( ReadWriteCsv const& import) 00044 : p_dataFrame_(new DataFrame()) 00045 , import_(import) 00046 { } 00047 00048 00049 // virtual dtor 00050 ImportFromCsv::~ImportFromCsv() 00051 { } 00052 00053 /* launch the conversion from the ReadWriteCsv to a Frame. */ 00054 bool ImportFromCsv::run( Import::TypeImport type) 00055 { 00056 switch (type) 00057 { 00058 case Import::numeric_: 00059 return runNumeric(); 00060 break; 00061 case Import::only_numeric_: 00062 return runOnlyNumeric(); 00063 break; 00064 case Import::string_: 00065 return runString(); 00066 break; 00067 default: 00068 return false; 00069 break; 00070 }; 00071 } 00072 00073 /* launch the conversion from the ReadWriteCsv to a DataFrame. */ 00074 bool ImportFromCsv::runNumeric() 00075 { 00076 try 00077 { 00078 // for each field Try a numeric conversion 00079 for (Integer j =import_.first(); j <=import_.last(); j++) 00080 { 00081 Variable<Real>* pvReal = new Variable<Real>(); 00082 // test number of successful conversion 00083 if (convertToTYPE(j, *pvReal)) 00084 { // if no failure add variable to the dataframe 00085 p_dataFrame_->pushBackVariable(pvReal); 00086 } 00087 else 00088 { 00089 delete pvReal; // delete varReal 00090 Variable<String>* pvString = new Variable<String>(import_[j]); 00091 p_dataFrame_->pushBackVariable(pvString); 00092 } 00093 } 00094 } 00095 catch(const Exception& error) 00096 { 00097 msg_error_ = error.error(); 00098 msg_error_ += _T("\nIn ImportCsv::runNumeric()"); 00099 return false; 00100 } 00101 return true; 00102 } 00103 00105 bool ImportFromCsv::runOnlyNumeric() 00106 { 00107 try 00108 { 00109 // for each field Try a numeric conversion 00110 for (Integer j =import_.first(); j <=import_.last(); j++) 00111 { 00112 Variable<Real>* pvReal = new Variable<Real>(); 00113 // test number of successful conversion 00114 if (convertToTYPE(j, *pvReal)) 00115 { // if no failure add variable to the dataframe 00116 p_dataFrame_->pushBackVariable(pvReal); 00117 } 00118 else delete pvReal; 00119 } 00120 } 00121 catch(const Exception& error) 00122 { 00123 msg_error_ = error.error(); 00124 msg_error_ += _T("\nIn ImportCsv::runOnlyNumeric()"); 00125 return false; 00126 } 00127 return true; 00128 } 00129 00130 /* launch the conversion from the ReadWriteCsv to a DataFrame. */ 00131 bool ImportFromCsv::runString() 00132 { 00133 try 00134 { 00135 // for each field Try a numeric conversion 00136 for (Integer j =import_.first(); j <=import_.last(); j++) 00137 { 00138 Variable<String>* pvString = new Variable<String>(import_[j]); 00139 p_dataFrame_->pushBackVariable(pvString); 00140 } 00141 } 00142 catch(const Exception& error) 00143 { 00144 msg_error_ = error.error(); 00145 msg_error_ += _T("\nIn ImportCsv::runString()"); 00146 return false; 00147 } 00148 return true; 00149 } 00150 00151 00152 } // namespace STK