URL:
https://svn.lrde.epita.fr/svn/oln/trunk/milena
ChangeLog:
2009-05-22 Fabien Freling <fabien.freling(a)lrde.epita.fr>
Implement load() for filling an array from a plot.
* mln/io/plot/load.hh: Load a plot into an float array.
* tests/io/plot/load.cc: New test file for plot loading.
---
mln/io/plot/load.hh | 33 +++++++++++++++++++++++++--------
tests/io/plot/load.cc | 17 +++++++++++++++++
2 files changed, 42 insertions(+), 8 deletions(-)
Index: trunk/milena/tests/io/plot/load.cc
===================================================================
--- trunk/milena/tests/io/plot/load.cc (revision 0)
+++ trunk/milena/tests/io/plot/load.cc (revision 3865)
@@ -0,0 +1,17 @@
+#include <mln/io/plot/load.hh>
+#include <mln/util/array.hh>
+
+
+using namespace mln;
+
+int main(int argc, char* argv[])
+{
+ util::array<float> arr;
+
+ io::plot::load(arr, argv[1]);
+
+ for (unsigned i = 0; i < arr.nelements(); ++i)
+ std::cout << arr[i] << std::endl;
+
+ return 0;
+}
Index: trunk/milena/mln/io/plot/load.hh
===================================================================
--- trunk/milena/mln/io/plot/load.hh (revision 3864)
+++ trunk/milena/mln/io/plot/load.hh (revision 3865)
@@ -35,6 +35,7 @@
# include <iostream>
# include <fstream>
+# include <cstring>
# include <mln/core/image/image1d.hh>
# include <mln/metal/equal.hh>
# include <mln/util/array.hh>
@@ -58,9 +59,9 @@
* \param[in] start_value The start index value of the plot
* (optional).
*/
- template <typename I>
+ /*template <typename I>
void load(image1d<I>& ima,
- const std::string& filename);
+ const std::string& filename);*/
/*! Load a Milena array from a plot file.
*
@@ -77,7 +78,7 @@
# ifndef MLN_INCLUDE_ONLY
- template <typename I>
+ /*template <typename I>
inline
void load(image1d<I>& ima, const std::string& filename)
{
@@ -88,7 +89,7 @@
file_out << start_value + i << ", " << ima.at_(i) <<
std::endl;
trace::exiting("mln::io::plot::load");
- }
+ }*/
template <typename I>
inline
@@ -96,11 +97,27 @@
{
trace::entering("mln::io::plot::load");
- std::ifstream file_out(filename.c_str());
- while (!oef)
+ arr.clear();
+ std::ifstream file_in(filename.c_str());
+
+ int MAX_LENGTH = 100;
+ char line[MAX_LENGTH];
+ char delims[] = " ";
+ char *result = NULL;
+ char *tmp_str = NULL;
+
+ while (file_in.getline(line, MAX_LENGTH))
+ {
+ if (strlen(line) > 0 && line[0] != '#')
+ {
+ tmp_str = strtok(line, delims);
+ while (tmp_str != NULL)
{
- if (line is comment)
- continue;
+ result = tmp_str;
+ tmp_str = strtok(NULL, delims);
+ }
+ arr.append(atof(result)); // FIXME: type
+ }
}
trace::exiting("mln::io::plot::load");