MatrixParser.h 3.06 KB
Newer Older
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
1
//-*-C++-*-
2
// Author: Michael S. Summers (ORNL)
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/** \ingroup MatrixParser */
/*@{*/

/*! \file matrixParser.h  
 *
 *  
 *
 */

#ifndef  JsonParser_MatrixParser_H
#define  JsonParser_MatrixParser_H

#include <iostream>
#include <sstream>
#include <map>
#include <vector>
#include "CharacterMapper.h"
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
20
#include "MatrixLike.h"
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
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
57
58
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

namespace JsonParser {
  
  template<typename MatrixLikeType>
  class MatrixParser: public CharacterMapper {
  public:

    typedef typename MatrixLikeType::value_type FieldType;

    MatrixLikeType& mat;
    std::wifstream& inputStream;
    int endPos;

    MatrixParser(std::wifstream& is, 
		 int end,
		 MatrixLikeType& m):
      mat(m),
      inputStream(is),
      endPos(end)
    {
      consumeMatrix();
    }
    
    //======================================================================

    bool consume(wchar_t c) {
      wchar_t nextChar(0);
      while(true) {
	nextChar = inputStream.get();
	if (nextChar == c)       return true;
	if (!inputStream.good()) return false;
	if (!isWhiteSpace(wcharToClass(nextChar)) )
	  break;
      }
      inputStream.unget();
      return false;
    }

    //======================================================================

    template<typename T>
    void consume(T& value) {
      inputStream >> value;
    }

    //======================================================================
    
    template<typename T>
    void consume(std::complex<T>& cmplx) {
      T r,i;
      if (!consume(L'['))
	throw std::logic_error("parsing error in consume(std::complex<Fieldtype> cmplx)\n  missing [\n");
      inputStream >> r;
      if (!consume(L','))
	throw std::logic_error("parsing error in consume(std::complex<Fieldtype> cmplx)\n  missing ,\n");
      inputStream >> i;
      if(!consume(L']'))
	throw std::logic_error("parsing error in consume(std::complex<Fieldtype> cmplx)\n  missing ]\n");
      cmplx = std::complex<T>(r,i);
    }
    
    //======================================================================
    
    void consumeMatrix()  {
      
      //Consume the lead bracket for the array
      if (!consume(L'['))
	throw std::logic_error("Missing [ marking the start of the array in consumeMatrix()\n");
	
      size_t datarow(0);

      while( !consume(L']')           && 
	     inputStream.good()       && 
	     inputStream.tellg() <= endPos) { // while we have not encountered the end of the array, etc.

	//Consume the lead bracket for the next row
	if(!consume(L'['))
	  throw std::logic_error("Missing [ marking the start of a row in consumeMarix()");
	
	size_t datacol(0);

	
	while(!consume(L']')) { // while we don't hit the end of the row 
	  
	  FieldType& value(mat(datarow,datacol));
	  consume(value);
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
107
          //std::cout << "mat(" << datarow << "," << datacol << ")= " << value << "\n";
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
108
109
110
111
112
113
114
	  datacol++;
	  consume(L',');

	}
	datarow++;
	consume(L',');
      }
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
115
116
      //      std::cout << "consume done\n";
      //      psimag::MatrixLike::printList(mat,std::cout);
Alvarez, Gonzalo's avatar
Alvarez, Gonzalo committed
117
118
119
120
121
122
123
124
125
126
127
128
    }
    
    //======================================================================

  };
  
} // end namespace JsonParser


/*@}*/
#endif