Removed braces for file upload schedule and make it internal
[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, bool inBrackets)
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(!inBrackets) //If no brackets is used set beginning of the sting as a parameter beginning
38 ptr1 = param;
39 if (ptr1 == NULL)
40 return true; // No arguments at all
41 for(ptr2 = ptr1 + 1, currIndex = 1, state = 0, pos = 0; state != -1; ptr2++)
42 {
43 switch(state)
44 {
45 case 0: // Normal
46 switch(*ptr2)
47 {
48 case _T(')'):
49 if (currIndex == index)
50 arg[pos] = 0;
51 state = -1; // Finish processing
52 break;
53 case _T('"'):
54 state = 1; // String
55 break;
56 case _T('\''): // String, type 2
57 state = 2;
58 break;
59 case _T(','):
60 if (currIndex == index)
61 {
62 arg[pos] = 0;
63 state = -1;
64 }
65 else
66 {
67 currIndex++;
68 }
69 break;
70 case 0:
71 state = -1; // Finish processing
72 if(!inBrackets) //No error flag if parameters were given without braces
73 success = false; // Set error flag
74 break;
75 default:
76 if ((currIndex == index) && (pos < maxSize - 1))
77 arg[pos++] = *ptr2;
78 }
79 break;
80 case 1: // String in ""
81 switch(*ptr2)
82 {
83 case _T('"'):
84 state = 0; // Normal
85 break;
86 case 0:
87 state = -1; // Finish processing
88 success = false; // Set error flag
89 break;
90 default:
91 if ((currIndex == index) && (pos < maxSize - 1))
92 arg[pos++] = *ptr2;
93 }
94 break;
95 case 2: // String in ''
96 switch(*ptr2)
97 {
98 case _T('\''):
99 state = 0; // Normal
100 break;
101 case 0:
102 state = -1; // Finish processing
103 success = false; // Set error flag
104 break;
105 default:
106 if ((currIndex == index) && (pos < maxSize - 1))
107 arg[pos++] = *ptr2;
108 }
109 break;
110 }
111 }
112
113 if (success)
114 StrStrip(arg);
115 return success;
116 }
117
118 /**
119 * Get arguments for parameters like name(arg1,...) as multibyte string
120 * Returns FALSE on processing error
121 */
122 bool LIBNXAGENT_EXPORTABLE AgentGetParameterArgA(const TCHAR *param, int index, char *arg, int maxSize, bool inBrackets)
123 {
124 #ifdef UNICODE
125 WCHAR *temp = (WCHAR *)malloc(maxSize * sizeof(WCHAR));
126 bool success = AgentGetParameterArgInternal(param, index, temp, maxSize, inBrackets);
127 if (success)
128 {
129 WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR, temp, -1, arg, maxSize, NULL, NULL);
130 arg[maxSize - 1] = 0;
131 }
132 free(temp);
133 return success;
134 #else
135 return AgentGetParameterArgInternal(param, index, arg, maxSize, inBrackets);
136 #endif
137 }
138
139 /**
140 * Get arguments for parameters like name(arg1,...) as UNICODE string
141 * Returns FALSE on processing error
142 */
143 bool LIBNXAGENT_EXPORTABLE AgentGetParameterArgW(const TCHAR *param, int index, WCHAR *arg, int maxSize, bool inBrackets)
144 {
145 #ifdef UNICODE
146 return AgentGetParameterArgInternal(param, index, arg, maxSize, inBrackets);
147 #else
148 char *temp = (char *)malloc(maxSize);
149 bool success = AgentGetParameterArgInternal(param, index, temp, maxSize, inBrackets);
150 if (success)
151 {
152 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, temp, -1, arg, maxSize);
153 arg[maxSize - 1] = 0;
154 }
155 free(temp);
156 return success;
157 #endif
158 }