Spreadsheet.cc 1.76 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


#include <algorithm>
#include <fstream>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include "Spreadsheet.h"
#include "Global.h"

namespace AMDiS {
  
  using namespace std;
  using namespace boost;

  using boost::lexical_cast;
  
  void Spreadsheet::write(string filename)
  {
    int nRows = static_cast<int>(data.size());
    int maxDataLength = 0;
    
    for (int i = 0; i < nRows; i++)
      maxDataLength = std::max(maxDataLength, static_cast<int>(data[i].size()));

    for (int i = 0; i < nRows; i++)
      data[i].resize(maxDataLength, 0.0);

    ofstream file;
    file.open(filename.c_str());

    for (int i = 0; i < nRows; i++) {
      for (int j = 0; j < maxDataLength; j++) {
	file << data[i][j];
	if (j + 1 < maxDataLength)
	  file << " ";
	else 
	  file << "\n";
      }
    }

    file.close();
  }


  void Spreadsheet::read(string filename)
  {
57
58
    FUNCNAME("Spreadsheet::read()");

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
    data.clear();

    string line;
    vector<string> lineSplit;

    ifstream file;
    file.open(filename.c_str());

    while (!file.eof()) {
      getline(file, line);
      split(lineSplit, line, is_any_of(" ,"));

      if (lineSplit.size() == 0)
	continue;

      if ((lineSplit.size() == 1) && (lineSplit[0] == ""))
	continue;

      if (lineSplit[0].find("#") == 0)
	continue;

      vector<double> lineData(lineSplit.size());
      for (unsigned int i = 0; i < lineSplit.size(); i++)
	lineData[i] = lexical_cast<double>(lineSplit[i]);

      data.push_back(lineData);
    }

    file.close();
  }

}