Skip to content

Commit

Permalink
Merge pull request #1260 from kerautret/TableReaderAdd
Browse files Browse the repository at this point in the history
TableReader add
  • Loading branch information
dcoeurjo committed Apr 15, 2017
2 parents 8208ba8 + a878c9a commit 5ac3fd6
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 22 deletions.
5 changes: 5 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

## Changes

- *IO*
- TableReader can now read all elements contained in each line of a file
with the new method getLinesElementsFromFile().
(Bertrand Kerautret, [#1260](https://github.com/DGtal-team/DGtal/pull/1260))


## Bug Fixes

Expand Down
26 changes: 25 additions & 1 deletion src/DGtal/io/readers/TableReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace DGtal
* #include "DGtal/io/readers/TableReader.h"
* ....
* string filename= "testFile.dat";
* vector<Z2i::Point> vectPoints = TableReader<unsigned int>::getColumnElementsFromFile(filename);
* vector<unsigned int> vectPoints = TableReader<unsigned int>::getColumnElementsFromFile(filename);
* @endcode
* and you can specifying the point position:
* @code
Expand Down Expand Up @@ -113,7 +113,31 @@ namespace DGtal
getColumnElementsFromInputStream (std::istream &in,
unsigned int aPosition);

/**
* Method to import a vector where each element contains the line
* elements of a given file. Blank line or line beginning with
* "#" are skipped.
*
* @param aFilename the input file.
* @return a vector containing a vector which contains each line elements.
**/
static std::vector<std::vector< TQuantity > >
getLinesElementsFromFile (const std::string & aFilename);

/**
* Method to import a vector where each element contains the line
* elements of a given file. Blank line or line beginning with
* "#" are skipped.
*
* @param in the input file.
* @return a vector containing a vector which contains each line elements.
**/

static std::vector<std::vector< TQuantity > >
getLinesElementsFromInputStream (std::istream &in);



}; // end of class TableReader


Expand Down
95 changes: 74 additions & 21 deletions src/DGtal/io/readers/TableReader.ih
Original file line number Diff line number Diff line change
Expand Up @@ -59,36 +59,89 @@ template<typename TQuantity>
inline
std::vector< TQuantity >
DGtal::TableReader<TQuantity>::getColumnElementsFromInputStream (std::istream &in,
unsigned int aPosition)
unsigned int aPosition)
{
std::vector<TQuantity> vectResult;
std::string str;
getline(in, str );
while ( in.good() ){
if ( ( str != "" ) && ( str[ 0 ] != '#' ) ){
std::istringstream in_str( str );
std::string wordVal;
TQuantity val;
bool found = false;
unsigned int idx = 0;
while ( in_str.good()&& !found){
std::operator>>(in_str, wordVal);
std::istringstream word_str( wordVal );
word_str >> val;
bool isOK = !word_str.fail();
if (isOK && idx == aPosition) {
found=true;
vectResult.push_back(val);
while ( in.good() )
{
if ( ( str != "" ) && ( str[ 0 ] != '#' ) )
{
std::istringstream in_str( str );
std::string wordVal;
TQuantity val;
bool found = false;
unsigned int idx = 0;
while ( in_str.good()&& !found)
{
std::operator>>(in_str, wordVal);
std::istringstream word_str( wordVal );
word_str >> val;
bool isOK = !word_str.fail();
if (isOK && idx == aPosition)
{
found=true;
vectResult.push_back(val);
}
idx++;
}
}
getline(in, str );
}
return vectResult;
}


template<typename TQuantity>
inline
std::vector<std::vector<TQuantity> >
DGtal::TableReader<TQuantity>::getLinesElementsFromFile (const std::string & aFilename)
{
std::ifstream infile;
infile.open (aFilename.c_str(), std::ifstream::in);
return DGtal::TableReader<TQuantity>::getLinesElementsFromInputStream(infile);

}



template<typename TQuantity>
inline
std::vector<std::vector<TQuantity> >
DGtal::TableReader<TQuantity>::getLinesElementsFromInputStream (std::istream &in)
{
std::vector < std::vector<TQuantity> > vectResult;
std::string str;
getline(in, str );
while ( in.good() )
{
std::vector<TQuantity> aLine;
if ( ( str != "" ) && ( str[ 0 ] != '#' ) )
{
std::istringstream in_str( str );
std::string wordVal;
TQuantity val;
while ( in_str.good())
{
std::operator>>(in_str, wordVal);
std::istringstream word_str( wordVal );
word_str >> val;
if (!word_str.fail())
{
aLine.push_back(val);
}
}
vectResult.push_back(aLine);
}
idx++;
}
}
getline(in, str );
}
getline(in, str );
}
return vectResult;

}




// //
///////////////////////////////////////////////////////////////////////////////
19 changes: 19 additions & 0 deletions tests/io/readers/testTableReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ bool testNumberReader()
nb++;
trace.info() << "(" << nbok << "/" << nb << ") "<< std::endl;
trace.endBlock();



trace.beginBlock ( "Testing reading all numbers from each lines ..." );

std::vector<vector<unsigned int> > vectLineIntegers = TableReader<unsigned int>::getLinesElementsFromFile(filename);
for(unsigned int k=0;k < vectLineIntegers.size(); k++)
{
for(unsigned int l=0; l < vectLineIntegers.at(k).size(); l++)
{
trace.info() << " integer: "<< vectLineIntegers.at(k).at(l) << " " ;
}
trace.info() << endl;
}

nbok += (vectLineIntegers.at(0).at(0)==1 && vectLineIntegers.at(2).at(2)==9 && vectLineIntegers.at(3).at(2)==1) ? 1 : 0;
nb++;
trace.info() << "(" << nbok << "/" << nb << ") "<< std::endl;
trace.endBlock();


trace.beginBlock ( "Testing reading string ..." );
Expand Down

0 comments on commit 5ac3fd6

Please sign in to comment.