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
|
// Gmsh - Copyright (C) 1997-2021 C. Geuzaine, J.-F. Remacle
//
// See the LICENSE.txt file for license information. Please report all
// issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
#include <map>
#include <stack>
#include <string>
#include "FunctionManager.h"
class File_Position
{
public:
int lineno;
fpos_t position;
FILE *file;
std::string filename;
};
class FunctionManagerStack
{
public:
std::stack<File_Position> s;
};
class FunctionManagerMap
{
public:
std::map<std::string, File_Position> m;
};
FunctionManager *FunctionManager::instance = nullptr;
FunctionManager::FunctionManager()
{
functions = new FunctionManagerMap;
calls = new FunctionManagerStack;
}
FunctionManager *FunctionManager::Instance()
{
if(!instance) {
instance = new FunctionManager;
}
return instance;
}
void FunctionManager::clear()
{
functions->m.clear();
}
int FunctionManager::enterFunction(const std::string &name, FILE **f,
std::string &filename, int &lno) const
{
if(functions->m.find(name) == functions->m.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 = (functions->m)[name];
fsetpos(fp.file, &fp.position);
*f = fp.file;
filename = fp.filename;
lno = fp.lineno;
return 1;
}
int FunctionManager::leaveFunction(FILE **f, std::string &filename, 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;
return 1;
}
int FunctionManager::createFunction(const std::string &name, FILE *f,
const std::string &filename, int lno)
{
if(functions->m.find(name) != functions->m.end())
return 0;
File_Position fp;
fp.file = f;
fp.filename = filename;
fp.lineno = lno;
fgetpos(fp.file, &fp.position);
(functions->m)[name] = fp;
return 1;
}
|