license changed to LGPL for libnxcl, libnxsnmp, libnxlp, libnxsl, and libnxmap
[public/netxms.git] / src / libnxsl / variable.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** NetXMS Scripting Language Interpreter
4 ** Copyright (C) 2003-2010 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU Lesser General Public License as published by
8 ** the Free Software Foundation; either version 3 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU Lesser General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** File: variable.cpp
21 **
22 **/
23
24 #include "libnxsl.h"
25
26
27 //
28 // Variable constructors
29 //
30
31 NXSL_Variable::NXSL_Variable(const TCHAR *pszName)
32 {
33 m_pszName = _tcsdup(pszName);
34 m_pValue = new NXSL_Value; // Create NULL value
35 m_isConstant = false;
36 }
37
38 NXSL_Variable::NXSL_Variable(const TCHAR *pszName, NXSL_Value *pValue, bool constant)
39 {
40 m_pszName = _tcsdup(pszName);
41 m_pValue = pValue;
42 m_isConstant = constant;
43 }
44
45 NXSL_Variable::NXSL_Variable(NXSL_Variable *pSrc)
46 {
47 m_pszName = _tcsdup(pSrc->m_pszName);
48 m_pValue = new NXSL_Value(pSrc->m_pValue);
49 m_isConstant = pSrc->m_isConstant;
50 }
51
52
53 //
54 // Variable destructor
55 //
56
57 NXSL_Variable::~NXSL_Variable()
58 {
59 free(m_pszName);
60 delete m_pValue;
61 }
62
63
64 //
65 // Set variable's value
66 //
67
68 void NXSL_Variable::setValue(NXSL_Value *pValue)
69 {
70 delete m_pValue;
71 m_pValue = pValue;
72 }
73
74
75 //
76 // Variable system constructors
77 //
78
79 NXSL_VariableSystem::NXSL_VariableSystem(bool constant)
80 {
81 m_dwNumVariables = 0;
82 m_ppVariableList = NULL;
83 m_isConstant = constant;
84 }
85
86 NXSL_VariableSystem::NXSL_VariableSystem(NXSL_VariableSystem *pSrc)
87 {
88 DWORD i;
89
90 m_dwNumVariables = pSrc->m_dwNumVariables;
91 m_ppVariableList = (NXSL_Variable **)malloc(sizeof(NXSL_Variable *) * m_dwNumVariables);
92 for(i = 0; i < m_dwNumVariables; i++)
93 m_ppVariableList[i] = new NXSL_Variable(pSrc->m_ppVariableList[i]);
94 m_isConstant = pSrc->m_isConstant;
95 }
96
97
98 //
99 // Variable system destructor
100 //
101
102 NXSL_VariableSystem::~NXSL_VariableSystem()
103 {
104 DWORD i;
105
106 for(i = 0; i < m_dwNumVariables; i++)
107 delete m_ppVariableList[i];
108 safe_free(m_ppVariableList);
109 }
110
111
112 //
113 // Merge with another variable system
114 //
115
116 void NXSL_VariableSystem::merge(NXSL_VariableSystem *src)
117 {
118 DWORD i;
119
120 for(i = 0; i < src->m_dwNumVariables; i++)
121 {
122 const TCHAR *name = src->m_ppVariableList[i]->getName();
123 if (find(name) == NULL)
124 {
125 create(name, new NXSL_Value(src->m_ppVariableList[i]->getValue()));
126 }
127 }
128 }
129
130
131 //
132 // Find variable by name
133 //
134
135 NXSL_Variable *NXSL_VariableSystem::find(const TCHAR *pszName)
136 {
137 DWORD i;
138
139 for(i = 0; i < m_dwNumVariables; i++)
140 if (!_tcscmp(pszName, m_ppVariableList[i]->getName()))
141 return m_ppVariableList[i];
142 return NULL;
143 }
144
145
146 //
147 // Create variable
148 //
149
150 NXSL_Variable *NXSL_VariableSystem::create(const TCHAR *pszName, NXSL_Value *pValue)
151 {
152 NXSL_Variable *pVar;
153
154 pVar = new NXSL_Variable(pszName, (pValue != NULL) ? pValue : new NXSL_Value, m_isConstant);
155 m_ppVariableList = (NXSL_Variable **)realloc(m_ppVariableList, sizeof(NXSL_Variable *) * (m_dwNumVariables + 1));
156 m_ppVariableList[m_dwNumVariables] = pVar;
157 m_dwNumVariables++;
158 return pVar;
159 }