subagent specific API moved to separate library from libnetxms
[public/netxms.git] / src / agent / libnxagent / tools.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2015 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU Lesser General Public License as published
7 ** by the Free Software Foundation; either version 3 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU Lesser General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** File: tools.cpp
20 **
21 **/
22
23 #include "libnxagent.h"
24
25 /**
26 * Get arguments for parameters like name(arg1,...)
27 * Returns FALSE on processing error
28 */
29 static bool AgentGetParameterArgInternal(const TCHAR *param, int index, TCHAR *arg, int maxSize)
30 {
31 const TCHAR *ptr1, *ptr2;
32 int state, currIndex, pos;
33 bool success = true;
34
35 arg[0] = 0; // Default is empty string
36 ptr1 = _tcschr(param, _T('('));
37 if (ptr1 == NULL)
38 return true; // No arguments at all
39 for(ptr2 = ptr1 + 1, currIndex = 1, state = 0, pos = 0; state != -1; ptr2++)
40 {
41 switch(state)
42 {
43 case 0: // Normal
44 switch(*ptr2)
45 {
46 case _T(')'):
47 if (currIndex == index)
48 arg[pos] = 0;
49 state = -1; // Finish processing
50 break;
51 case _T('"'):
52 state = 1; // String
53 break;
54 case _T('\''): // String, type 2
55 state = 2;
56 break;
57 case _T(','):
58 if (currIndex == index)
59 {
60 arg[pos] = 0;
61 state = -1;
62 }
63 else
64 {
65 currIndex++;
66 }
67 break;
68 case 0:
69 state = -1; // Finish processing
70 success = false; // Set error flag
71 break;
72 default:
73 if ((currIndex == index) && (pos < maxSize - 1))
74 arg[pos++] = *ptr2;
75 }
76 break;
77 case 1: // String in ""
78 switch(*ptr2)
79 {
80 case _T('"'):
81 state = 0; // Normal
82 break;
83 case 0:
84 state = -1; // Finish processing
85 success = false; // Set error flag
86 break;
87 default:
88 if ((currIndex == index) && (pos < maxSize - 1))
89 arg[pos++] = *ptr2;
90 }
91 break;
92 case 2: // String in ''
93 switch(*ptr2)
94 {
95 case _T('\''):
96 state = 0; // Normal
97 break;
98 case 0:
99 state = -1; // Finish processing
100 success = false; // Set error flag
101 break;
102 default:
103 if ((currIndex == index) && (pos < maxSize - 1))
104 arg[pos++] = *ptr2;
105 }
106 break;
107 }
108 }
109
110 if (success)
111 StrStrip(arg);
112 return success;
113 }
114
115 /**
116 * Get arguments for parameters like name(arg1,...) as multibyte string
117 * Returns FALSE on processing error
118 */
119 bool LIBNXAGENT_EXPORTABLE AgentGetParameterArgA(const TCHAR *param, int index, char *arg, int maxSize)
120 {
121 #ifdef UNICODE
122 WCHAR *temp = (WCHAR *)malloc(maxSize * sizeof(WCHAR));
123 bool success = AgentGetParameterArgInternal(param, index, temp, maxSize);
124 if (success)
125 {
126 WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR, temp, -1, arg, maxSize, NULL, NULL);
127 arg[maxSize - 1] = 0;
128 }
129 free(temp);
130 return success;
131 #else
132 return AgentGetParameterArgInternal(param, index, arg, maxSize);
133 #endif
134 }
135
136 /**
137 * Get arguments for parameters like name(arg1,...) as UNICODE string
138 * Returns FALSE on processing error
139 */
140 bool LIBNXAGENT_EXPORTABLE AgentGetParameterArgW(const TCHAR *param, int index, WCHAR *arg, int maxSize)
141 {
142 #ifdef UNICODE
143 return AgentGetParameterArgInternal(param, index, arg, maxSize);
144 #else
145 char *temp = (char *)malloc(maxSize);
146 bool success = AgentGetParameterArgInternal(param, index, temp, maxSize);
147 if (success)
148 {
149 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, temp, -1, arg, maxSize);
150 arg[maxSize - 1] = 0;
151 }
152 free(temp);
153 return success;
154 #endif
155 }