-
Notifications
You must be signed in to change notification settings - Fork 0
/
fn_prm.h
324 lines (263 loc) · 7.81 KB
/
fn_prm.h
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*!
Copyright 2017-2019 Maxim Noltmeer (m.noltmeer@gmail.com)
This file is part of Extern Logic Interpreter.
Extern Logic Interpreter is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Extern Logic Interpreter is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Extern Logic Interpreter. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef FN_PRM_H_INCLUDED
#define FN_PRM_H_INCLUDED
#include <stdio.h>
#include <vector>
#include <iterator>
#include <string.h>
#include <cstdlib>
#define CHARSIZE 4096 //ìàêñèìàëüíàÿ äëèíà ñòðîêîâîãî òèïà â ñêðèïòå
#define STRBUFSTACK 4096 //ìàêñ. ðàçìåð ñòðîêîâîãî áóôåðà äëÿ ýêñïîðòà ñîäåðæèìîãî ñòåêîâ
#define FNAMESIZE 32 //ìàêñèìàëüíàÿ äëèíà èìåíè ô-èè
#define PNAMESIZE 16 //ìàêñèìàëüíàÿ äëèíà èìåíè ïàðàìåòðà
#define UINT unsigned int
const wchar_t *S_NUM = L"num";
const wchar_t *S_SYM = L"sym";
const wchar_t *FARGSSEP = L","; //ðàçäåëèòåëü â îïèñàíèè ïàðàìåòðîâ ô-èè
struct EXTFUNC
{
wchar_t inname[MAXNAMELEN]; //âíóòðåííåå èìÿ
HINSTANCE exthinst; //äåñêðèïòîð âí. ëèáû
};
class PARAM
{
public:
inline PARAM(const wchar_t* p_name, const wchar_t *p_value)
{
name = p_name;
Set(p_value);
}
inline virtual ~PARAM(){};
std::wstring name; //èìÿ ïàðàìåòðà
void Set(const wchar_t *new_val){val = new_val;}
//ïðåîáðàçóåò ïàðàìåòð â integer è âîçâðàùàåò åãî
int ToInt()
{
UINT pos = val.find(L".");
if (pos != std::wstring::npos)
val.erase(pos, val.length() - pos);
return _wtoi(val.c_str());
}
//-------------------------------------------------------------------------------
//ïðåîáðàçóåò ïàðàìåòð â float è âîçâðàùàåò åãî
float ToFloat(){return _wtof(val.c_str());}
//-------------------------------------------------------------------------------
//ïðåîáðàçóåò ïàðàìåòð â ñòðîêó è âîçâðàùàåò óêàçàòåëü íà íåå
//â ñëó÷àå îøèáêè âîçâðàùàåò NULL
const wchar_t *ToStr(){return val.c_str();}
private:
std::wstring val; //çíà÷åíèå
};
//-------------------------------------------------------------------------------
class PARAMSTACK
{
public:
inline PARAMSTACK(){};
inline virtual ~PARAMSTACK(){Clear();}
//äîáàâëÿåò ïàðàìåòð â êîíåö ñòåêà
void Add(const wchar_t *name, const wchar_t *val)
{
int ind = GetParamInd(name);
if (ind < 0)
{
PARAM *new_p = new PARAM(name, val);
Stack.push_back(new_p);
}
else
{
Stack[ind]->Set(val);
}
}
//-------------------------------------------------------------------------------
//âîçâðàùàåò êîëè÷åñòâî ïàðàìåòðîâ â ñòåêå
UINT Count(){return Stack.size();}
//âîçâðàùàåò ôîðìàòèðîâàííóþ ñòðîêó ñî ñïèñêîì âñåõ ô-é
wchar_t *ShowInString()
{
static wchar_t output[STRBUFSTACK];
UINT cnt = 0;
for (UINT i = 0; i < Stack.size(); i++)
{
cnt += swprintf(output + cnt, L"[%d] %s = %s\r\n",
i,
Stack[i]->name.c_str(),
Stack[i]->ToStr());
}
return output;
}
//-------------------------------------------------------------------------------
inline PARAM *Get(const wchar_t *name)
{
int ind = GetParamInd(name);
if (ind < 0)
return NULL;
else
return Stack[ind];
}
//-------------------------------------------------------------------------------
//î÷èùàåò ñòåê ïàðàìåòðîâ
inline void Clear()
{
for (UINT i = 0; i < Stack.size(); i++)
{
delete Stack[i];
Stack[i] = NULL;
}
Stack.clear();
}
//-------------------------------------------------------------------------------
private:
std::vector<PARAM*> Stack;
//èùåò ïàðàìåòð â ñòåêå ïî èìåíè è âîçâðàùàåò èíäåêñ
//-1 - íå íàéäåíî
inline int GetParamInd(const wchar_t *name)
{
for (UINT i = 0; i < Stack.size(); i++)
{
if (0 == _wcsicmp(Stack[i]->name.c_str(), name))
return i;
}
return -1;
}
//-------------------------------------------------------------------------------
};
//-------------------------------------------------------------------------------
class FUNC
{
public:
inline FUNC(const wchar_t *name, const wchar_t *params, func_ptr fptr)
{
this->name = new wchar_t[FNAMESIZE];
this->prms_format = new wchar_t[CHARSIZE];
return_val = new wchar_t[CHARSIZE];
wcscpy(this->name, name);
this->ptr = fptr;
wcscpy(this->prms_format, params);
}
inline virtual ~FUNC()
{
delete [] name;
name = NULL;
delete [] prms_format;
prms_format = NULL;
delete [] return_val;
return_val = NULL;
ptr = NULL;
}
inline wchar_t *GetName(){return name;}
inline wchar_t *GetParams(){return prms_format;}
inline func_ptr GetPointer(){return ptr;}
//âûçûâàåò ôóíêöèþ, âîçâðàùàåò 1 â ñëó÷àå óñïåõà
inline void Call(void *e_ptr){ptr(e_ptr);}
//ïðåîáðàçóåò âîçâðàùàåìîå çíà÷åíèå ô-èè name â ñòðîêó è âîçâðàùàåò óêàçàòåëü íà íåå
//â ñëó÷àå îøèáêè âîçâðàùàåò NULL
inline wchar_t *GetResult(){return return_val;}
//óñòàíàâëèâàåò âîçâðàùàåìîå çíà÷åíèå ô-èè
inline void SetResult(const wchar_t* result){wcscpy(return_val, result);}
private:
wchar_t *name; //èìÿ ô-èè
func_ptr ptr; //óêàçàòåëü íà ô-þ èç õîñò-ïðèëîæåíèÿ èëè áèáëèîòåêè
wchar_t *prms_format; //ñòðîêà îïðåäåëÿþùàÿ èìåíà è òèï ïàðàìåòðîâ ô-è
//num val,sym val
wchar_t *return_val; //âîçâðàùàåìîå çíà÷åíèå
};
//-------------------------------------------------------------------------------
class FUNCSTACK
{
public:
inline FUNCSTACK(){};
inline virtual ~FUNCSTACK()
{
for (UINT i = 0; i < Stack.size(); i++)
{
delete Stack[i];
Stack[i] = NULL;
}
Stack.clear();
}
//âîçâðàùàåò êîëè÷åñòâî ôóíêöèé â ñòåêå
inline UINT CountFuncs(){return Stack.size();};
//âîçâðàùàåò óêàçàòåëü íà ñâîéñòâà ôóíêöèè ñ èíäåêñîì st_ind èç ñòåêà
//â ñëó÷àå îøèáêè âîçâðàùàåò NULL
inline FUNC *Get(UINT st_ind)
{
if (st_ind >= Stack.size())
return NULL;
else
return Stack[st_ind];
};
//âîçâðàùàåò óêàçàòåëü íà ñâîéñòâà ôóíêöèè name èç ñòåêà
//â ñëó÷àå îøèáêè âîçâðàùàåò NULL
inline FUNC *Get(const wchar_t *fname)
{
for (UINT i = 0; i < Stack.size(); i++)
{
if (0 == _wcsicmp(Stack[i]->GetName(), fname))
return Stack[i];
}
return NULL;
};
//äîáàâëÿåò ô-þ â ñòåê
void Add(const wchar_t *name, const wchar_t *params, func_ptr fptr)
{
if (!Get(name))
{
FUNC *fn = new FUNC(name, params, fptr);
Stack.push_back(fn);
}
}
//óäàëÿåò ô-þ èç ñòåêà
void Delete(const wchar_t *name)
{
int del_ind = GetInd(name);
if (del_ind > -1)
{
delete Stack[del_ind];
Stack[del_ind] = NULL;
Stack.erase(Stack.begin() + del_ind);
}
}
//âîçâðàùàåò ôîðìàòèðîâàííóþ ñòðîêó ñî ñïèñêîì âñåõ ô-é
wchar_t *ShowInString()
{
static wchar_t output[STRBUFSTACK];
UINT cnt = 0;
for (UINT i = 0; i < Stack.size(); i++)
{
cnt += swprintf(output + cnt, L"[%d] %s(%s), ptr = %d\r\n",
i,
Stack[i]->GetName(),
Stack[i]->GetParams(),
(int)Stack[i]->GetPointer());
}
return output;
}
private:
std::vector<FUNC*> Stack;
//èùåò ô-þ â ñòåêå, âîçâðàùàåò åå èíäåêñ
//âîçâðàùàåò -1 â ñëó÷àå íåóäà÷è
inline int GetInd(const wchar_t *fname)
{
for (UINT i = 0; i < Stack.size(); i++)
{
if (0 == _wcsicmp(Stack[i]->GetName(), fname))
return i;
}
return -1;
}
};
//-------------------------------------------------------------------------------
#endif // FN_PRM_H_INCLUDED