license changed to LGPL for libnxcl, libnxsnmp, libnxlp, libnxsl, and libnxmap
[public/netxms.git] / src / libnxsl / array.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: array.cpp
21 **
22 **/
23
24 #include "libnxsl.h"
25
26
27 //
28 // Array constructors
29 //
30
31 NXSL_Array::NXSL_Array()
32 {
33 m_refCount = 0;
34 m_size = 0;
35 m_allocated = 0;
36 m_data = NULL;
37 }
38
39 NXSL_Array::NXSL_Array(NXSL_Array *src)
40 {
41 int i;
42
43 m_refCount = 0;
44 m_size = src->m_size;
45 m_allocated = src->m_size;
46 if (m_size > 0)
47 {
48 m_data = (NXSL_ArrayElement *)malloc(sizeof(NXSL_ArrayElement) * m_size);
49 for(i = 0; i < m_size; i++)
50 {
51 m_data[i].index = src->m_data[i].index;
52 m_data[i].value = new NXSL_Value(src->m_data[i].value);
53 }
54 }
55 else
56 {
57 m_data = NULL;
58 }
59 }
60
61
62 //
63 // Array destructor
64 //
65
66 NXSL_Array::~NXSL_Array()
67 {
68 int i;
69
70 for(i = 0; i < m_size; i++)
71 delete m_data[i].value;
72 safe_free(m_data);
73 }
74
75
76 //
77 // Compare two ints
78 //
79
80 static int CompareElements(const void *p1, const void *p2)
81 {
82 return COMPARE_NUMBERS(((NXSL_ArrayElement *)p1)->index, ((NXSL_ArrayElement *)p2)->index);
83 }
84
85
86 //
87 // Get element
88 //
89
90 NXSL_Value *NXSL_Array::get(int index)
91 {
92 NXSL_ArrayElement *element, key;
93
94 key.index = index;
95 element = (NXSL_ArrayElement *)bsearch(&key, m_data, m_size, sizeof(NXSL_ArrayElement), CompareElements);
96 return (element != NULL) ? element->value : NULL;
97 }
98
99
100 //
101 // Set element
102 //
103
104 void NXSL_Array::set(int index, NXSL_Value *value)
105 {
106 NXSL_ArrayElement *element, key;
107
108 key.index = index;
109 element = (NXSL_ArrayElement *)bsearch(&key, m_data, m_size, sizeof(NXSL_ArrayElement), CompareElements);
110 if (element != NULL)
111 {
112 delete element->value;
113 element->value = value;
114 }
115 else
116 {
117 if (m_size == m_allocated)
118 {
119 m_allocated += 32;
120 m_data = (NXSL_ArrayElement *)realloc(m_data, sizeof(NXSL_ArrayElement) * m_allocated);
121 }
122 m_data[m_size].index = index;
123 m_data[m_size].value = value;
124 m_size++;
125 qsort(m_data, m_size, sizeof(NXSL_ArrayElement), CompareElements);
126 }
127 }