File: MacroManager.cpp

package info (click to toggle)
getdp 3.0.4%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 10,856 kB
  • sloc: cpp: 63,020; fortran: 13,955; yacc: 9,350; f90: 1,640; lex: 799; makefile: 55; ansic: 34; awk: 33; sh: 23
file content (122 lines) | stat: -rw-r--r-- 2,719 bytes parent folder | download
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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// GetDP - Copyright (C) 1997-2015 P. Dular, C. Geuzaine
//
// See the LICENSE.txt file for license information. Please report all
// issues on https://gitlab.onelab.info/getdp/getdp/issues

#include <stdio.h>
#include <map>
#include <stack>
#include <string>
#include "MacroManager.h"

extern std::string getdp_yystring;

class File_Position
{
 public:
  long int lineno;
  fpos_t position;
  FILE *file;
  std::string filename;
};

class MacroManagerStack
{
 public:
  std::stack<File_Position> s;
};

class MacroManagerMap
{
 public:
  std::map<std::string, File_Position> inFile;
  std::map<std::string, std::string> inString;
};

MacroManager *MacroManager::_instance = 0;

MacroManager::MacroManager()
{
  _macros = new MacroManagerMap;
  _calls = new MacroManagerStack;
}

MacroManager *MacroManager::Instance()
{
  if(!_instance) {
    _instance = new MacroManager;
  }
  return _instance;
}

void MacroManager::clear()
{
  _macros->inFile.clear();
  _macros->inString.clear();
}

int MacroManager::enterMacro(const std::string &name, FILE **f,
                             std::string &filename, long int &lno) const
{
  if(_macros->inFile.find(name) == _macros->inFile.end())
    return 0;
  File_Position fpold;
  fpold.lineno = lno;
  fpold.filename = filename;
  fpold.file = *f;
  fgetpos(fpold.file, &fpold.position);
  _calls->s.push(fpold);
  File_Position fp = (_macros->inFile)[name];
  fsetpos(fp.file, &fp.position);
  *f = fp.file;
  filename = fp.filename;
  lno = fp.lineno;
  return 1;
}

int MacroManager::leaveMacro(FILE **f, std::string &filename, long int &lno)
{
  if(!_calls->s.size())
    return 0;
  File_Position fp;
  fp = _calls->s.top();
  _calls->s.pop();
  fsetpos(fp.file, &fp.position);
  *f = fp.file;
  filename = fp.filename;
  //  lno = fp.lineno;
  // To fix: bad line number after leaving macro if not -1
  lno = fp.lineno - 1;
  return 1;
}

int MacroManager::createMacro(const std::string &name, FILE *f,
                              const std::string &filename, long int lno)
{
  if(_macros->inFile.find(name) != _macros->inFile.end())
    return 0;
  File_Position fp;
  fp.file = f;
  fp.filename = filename;
  fp.lineno = lno;
  fgetpos(fp.file, &fp.position);
  (_macros->inFile)[name] = fp;
  return 1;
}

int MacroManager::createStringMacro(const std::string &name,
                                    const std::string &value)
{
  if(_macros->inString.find(name) != _macros->inString.end())
    return 0;
  (_macros->inString)[name] = value;
  return 1;
}

int MacroManager::enterStringMacro(const std::string &name) const
{
  if(_macros->inString.find(name) == _macros->inString.end())
    return 0;
  getdp_yystring = (_macros->inString)[name];
  return 1;
}