STK++ 1.0

STK::ImportFromCsv Class Reference

import data from a Csv File. More...

#include <STK_ImportFromCsv.h>

List of all members.

Public Member Functions

 ImportFromCsv (ReadWriteCsv const &import)
 Constructor.
virtual ~ImportFromCsv ()
 destructor.
bool run (Import::TypeImport type=Import::numeric_)
 launch the conversion from the ReadWriteCsv to a Frame.
void eraseReadWriteCsv ()
 delete the DataFrame.
String const & error () const
 delete the DataFrame.
DataFrame const * dataFrame () const
 Return a ptr on the the data read.

Protected Attributes

DataFramep_dataFrame_
 A ptr on the resulting DataFrame.
String msg_error_
 Contain the last error message.

Private Member Functions

template<class TYPE >
bool convertToTYPE (Integer const &iCol, Variable< TYPE > &col)
 convert a column of the csv in a real vector.
bool runNumeric ()
 launch the conversion from the ReadWriteCsv to a DataFrame with a numeric conversion.
bool runOnlyNumeric ()
 launch the conversion from the ReadWriteCsv to a DataFrame with only the successful numeric conversion.
bool runString ()
 launch the conversion from the ReadWriteCsv to a DataFrame with only the successful numeric conversion.

Private Attributes

ReadWriteCsv const & import_
 a constant reference on the the original ReadWriteCsv.

Detailed Description

import data from a Csv File.

A ImportFromCsv object create a DataFram from a given ReadWriteCsv object. It will try to convert the given ReadWriteCsv to the predefined type given by the user.

Definition at line 58 of file STK_ImportFromCsv.h.


Constructor & Destructor Documentation

home iovleff Developpement workspace stkpp projects DManager src STK_ImportFromCsv cpp STK::ImportFromCsv::ImportFromCsv ( ReadWriteCsv const &  import)

Constructor.

Instantiates an instance of ImportFromCvs with the readWriteCsv to import.

Parameters:
importthe ReadWriteCsv to import

Definition at line 43 of file STK_ImportFromCsv.cpp.

                            : p_dataFrame_(new DataFrame())
                            , import_(import)
{ }

STK::ImportFromCsv::~ImportFromCsv ( ) [virtual]

destructor.

Definition at line 50 of file STK_ImportFromCsv.cpp.

{ }


Member Function Documentation

bool STK::ImportFromCsv::run ( Import::TypeImport  type = Import::numeric_)

launch the conversion from the ReadWriteCsv to a Frame.

Definition at line 54 of file STK_ImportFromCsv.cpp.

References STK::Import::numeric_, STK::Import::only_numeric_, runNumeric(), runOnlyNumeric(), runString(), and STK::Import::string_.

{
  switch (type)
  {
    case Import::numeric_:
      return runNumeric();
      break;
    case Import::only_numeric_:
      return runOnlyNumeric();
      break;
    case Import::string_:
      return runString();
      break;
    default:
      return false;
      break;
  };
}

void STK::ImportFromCsv::eraseReadWriteCsv ( ) [inline]

delete the DataFrame.

The DataFrame imported is not deleted when the object ImportFromCsv is deleted. The user can call this method if needed in order to release the memory.

Definition at line 77 of file STK_ImportFromCsv.h.

References p_dataFrame_.

    {
      if (p_dataFrame_) delete p_dataFrame_;
      p_dataFrame_ = 0;
    }

String const& STK::ImportFromCsv::error ( ) const [inline]

delete the DataFrame.

Definition at line 84 of file STK_ImportFromCsv.h.

References msg_error_.

Referenced by runNumeric(), runOnlyNumeric(), and runString().

    {return msg_error_;}

DataFrame const* STK::ImportFromCsv::dataFrame ( ) const [inline]

Return a ptr on the the data read.

Definition at line 88 of file STK_ImportFromCsv.h.

References p_dataFrame_.

    { return p_dataFrame_;}

template<class TYPE >
bool STK::ImportFromCsv::convertToTYPE ( Integer const &  iCol,
Variable< TYPE > &  col 
) [inline, private]

convert a column of the csv in a real vector.

Parameters:
iColthe column to convert
colthe result stored in a vector
Returns:
true if the conversion is successful, false otherwise

Definition at line 108 of file STK_ImportFromCsv.h.

References import_, STK::Variable< TYPE >::importString(), and STK::IContainer1D::size().

Referenced by runNumeric(), and runOnlyNumeric().

    {
      // get dimensions of the variable
      // try to ConvertType strings to TYPE
      Integer nSuccess = col.importString(import_[iCol]);
      return (nSuccess == col.size());
    }

bool STK::ImportFromCsv::runNumeric ( ) [private]

launch the conversion from the ReadWriteCsv to a DataFrame with a numeric conversion.

Definition at line 74 of file STK_ImportFromCsv.cpp.

References _T, convertToTYPE(), error(), STK::ReadWriteCsv::first(), import_, STK::ReadWriteCsv::last(), msg_error_, p_dataFrame_, and STK::DataFrame::pushBackVariable().

Referenced by run().

{
  try
  {
    // for each field Try a numeric conversion
    for (Integer j =import_.first(); j <=import_.last(); j++)
    {
      Variable<Real>* pvReal = new Variable<Real>();
      // test number of successful conversion
      if (convertToTYPE(j, *pvReal))
      {  // if no failure add variable to the dataframe
         p_dataFrame_->pushBackVariable(pvReal);
      }
      else
      {
        delete pvReal; // delete varReal
        Variable<String>* pvString = new Variable<String>(import_[j]);
        p_dataFrame_->pushBackVariable(pvString);
      }
    }
  }
  catch(const std::exception& error)
  {
    msg_error_  = error.what();
    msg_error_ += _T("\nIn ImportCsv::runNumeric()");
    return false;
  }
  return true;
}

bool STK::ImportFromCsv::runOnlyNumeric ( ) [private]

launch the conversion from the ReadWriteCsv to a DataFrame with only the successful numeric conversion.

launch the conversion from the ReadWriteCsv to a Frame.

Definition at line 105 of file STK_ImportFromCsv.cpp.

References _T, convertToTYPE(), error(), STK::ReadWriteCsv::first(), import_, STK::ReadWriteCsv::last(), msg_error_, p_dataFrame_, and STK::DataFrame::pushBackVariable().

Referenced by run().

{
  try
  {
    // for each field Try a numeric conversion
    for (Integer j =import_.first(); j <=import_.last(); j++)
    {
      Variable<Real>* pvReal = new Variable<Real>();
      // test number of successful conversion
      if (convertToTYPE(j, *pvReal))
      {  // if no failure add variable to the dataframe
         p_dataFrame_->pushBackVariable(pvReal);
      }
      else delete pvReal;
    }
  }
  catch(const std::exception& error)
  {
    msg_error_  = error.what();
    msg_error_ += _T("\nIn ImportCsv::runOnlyNumeric()");
    return false;
  }
  return true;
}

bool STK::ImportFromCsv::runString ( ) [private]

launch the conversion from the ReadWriteCsv to a DataFrame with only the successful numeric conversion.

Definition at line 131 of file STK_ImportFromCsv.cpp.

References _T, error(), STK::ReadWriteCsv::first(), import_, STK::ReadWriteCsv::last(), msg_error_, p_dataFrame_, and STK::DataFrame::pushBackVariable().

Referenced by run().

{
  try
  {
    // for each field Try a numeric conversion
    for (Integer j =import_.first(); j <=import_.last(); j++)
    {
      Variable<String>* pvString = new Variable<String>(import_[j]);
      p_dataFrame_->pushBackVariable(pvString);
    }
  }
  catch(const std::exception& error)
  {
    msg_error_ = error.what();
    msg_error_ += _T("\nIn ImportCsv::runString()");
    return false;
  }
  return true;
}


Member Data Documentation

A ptr on the resulting DataFrame.

Definition at line 93 of file STK_ImportFromCsv.h.

Referenced by dataFrame(), eraseReadWriteCsv(), runNumeric(), runOnlyNumeric(), and runString().

String STK::ImportFromCsv::msg_error_ [mutable, protected]

Contain the last error message.

Definition at line 96 of file STK_ImportFromCsv.h.

Referenced by error(), runNumeric(), runOnlyNumeric(), and runString().

a constant reference on the the original ReadWriteCsv.

Definition at line 100 of file STK_ImportFromCsv.h.

Referenced by convertToTYPE(), runNumeric(), runOnlyNumeric(), and runString().


The documentation for this class was generated from the following files: