f0d0d7bf2fef0c762f30a1e3d09a732beb4a2266
[public/netxms.git] / src / libnxsl / library.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** NetXMS Scripting Language Interpreter
4 ** Copyright (C) 2005-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 General Public License as published by
8 ** the Free Software Foundation; either version 2 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 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: library.cpp
21 **
22 **/
23
24 #include "libnxsl.h"
25
26
27 //
28 // Constructor
29 //
30
31 NXSL_Library::NXSL_Library()
32 {
33 m_mutex = MutexCreate();
34 m_dwNumScripts = 0;
35 m_ppScriptList = NULL;
36 m_ppszNames = NULL;
37 m_pdwIdList = NULL;
38 }
39
40
41 //
42 // Destructor
43 //
44
45 NXSL_Library::~NXSL_Library()
46 {
47 DWORD i;
48
49 for(i = 0; i < m_dwNumScripts; i++)
50 {
51 delete m_ppScriptList[i];
52 free(m_ppszNames[i]);
53 }
54 safe_free(m_ppScriptList);
55 safe_free(m_ppszNames);
56 safe_free(m_pdwIdList);
57 MutexDestroy(m_mutex);
58 }
59
60
61 //
62 // Add script to list
63 //
64
65 BOOL NXSL_Library::addScript(DWORD dwId, const TCHAR *pszName, NXSL_Program *pScript)
66 {
67 DWORD i;
68
69 for(i = 0; i < m_dwNumScripts; i++)
70 if (!stricmp(m_ppszNames[i], pszName))
71 return FALSE;
72
73 m_dwNumScripts++;
74 m_ppScriptList = (NXSL_Program **)realloc(m_ppScriptList, sizeof(NXSL_Program *) * m_dwNumScripts);
75 m_ppszNames = (char **)realloc(m_ppszNames, sizeof(char *) * m_dwNumScripts);
76 m_pdwIdList = (DWORD *)realloc(m_pdwIdList, sizeof(DWORD) * m_dwNumScripts);
77 m_ppScriptList[i] = pScript;
78 m_ppszNames[i] = strdup(pszName);
79 m_pdwIdList[i] = dwId;
80 return TRUE;
81 }
82
83
84 //
85 // Delete script from list
86 //
87
88 void NXSL_Library::deleteInternal(int nIndex)
89 {
90 delete m_ppScriptList[nIndex];
91 free(m_ppszNames[nIndex]);
92 m_dwNumScripts--;
93 memmove(&m_ppScriptList[nIndex], &m_ppScriptList[nIndex + 1],
94 sizeof(NXSL_Program *) * (m_dwNumScripts - nIndex));
95 memmove(&m_ppszNames[nIndex], &m_ppszNames[nIndex + 1],
96 sizeof(char *) * (m_dwNumScripts - nIndex));
97 memmove(&m_pdwIdList[nIndex], &m_pdwIdList[nIndex + 1],
98 sizeof(DWORD) * (m_dwNumScripts - nIndex));
99 }
100
101 void NXSL_Library::deleteScript(const TCHAR *pszName)
102 {
103 DWORD i;
104
105 for(i = 0; i < m_dwNumScripts; i++)
106 if (!stricmp(m_ppszNames[i], pszName))
107 {
108 deleteInternal(i);
109 break;
110 }
111 }
112
113 void NXSL_Library::deleteScript(DWORD dwId)
114 {
115 DWORD i;
116
117 for(i = 0; i < m_dwNumScripts; i++)
118 if (m_pdwIdList[i] == dwId)
119 {
120 deleteInternal(i);
121 break;
122 }
123 }
124
125
126 //
127 // Find script by name
128 //
129
130 NXSL_Program *NXSL_Library::findScript(const TCHAR *pszName)
131 {
132 DWORD i;
133
134 for(i = 0; i < m_dwNumScripts; i++)
135 if (!stricmp(m_ppszNames[i], pszName))
136 {
137 return m_ppScriptList[i];
138 }
139 return NULL;
140 }
141
142
143 //
144 // Fill NXCP message with script data
145 //
146
147 void NXSL_Library::fillMessage(CSCPMessage *pMsg)
148 {
149 DWORD i, dwId;
150
151 pMsg->SetVariable(VID_NUM_SCRIPTS, m_dwNumScripts);
152 for(i = 0, dwId = VID_SCRIPT_LIST_BASE; i < m_dwNumScripts; i++)
153 {
154 pMsg->SetVariable(dwId++, m_pdwIdList[i]);
155 pMsg->SetVariable(dwId++, m_ppszNames[i]);
156 }
157 }