source: nscp/include/arrayBuffer.cpp @ d66ccee

0.4.00.4.10.4.2
Last change on this file since d66ccee was d66ccee, checked in by Michael Medin <michael@…>, 2 years ago

Tweaks to make building faster

  • Property mode set to 100644
File size: 7.8 KB
Line 
1/**************************************************************************
2*   Copyright (C) 2004-2007 by Michael Medin <michael@medin.name>         *
3*                                                                         *
4*   This code is part of NSClient++ - http://trac.nakednuns.org/nscp      *
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                         *
18*   Free Software Foundation, Inc.,                                       *
19*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20***************************************************************************/
21#include <arrayBuffer.h>
22
23
24/**
25 * Make a list out of a array of char arrays (arguments type)
26 * @param argLen Length of argument array
27 * @param *argument[] Argument array
28 * @return Argument wrapped as a list
29 */
30array_buffer::arrayList array_buffer::arrayBuffer2list(const unsigned int argLen, wchar_t *argument[]) {
31        arrayList ret;
32        int i=0;
33        for (unsigned int i=0;i<argLen;i++) {
34                std::wstring s = argument[i];
35                ret.push_back(s);
36        }
37        return ret;
38}
39/**
40 * Make a list out of a array of char arrays (arguments type)
41 * @param argLen Length of argument array
42 * @param *argument[] Argument array
43 * @return Argument wrapped as a list
44 */
45array_buffer::arrayVector array_buffer::arrayBuffer2vector(const unsigned int argLen, wchar_t *argument[]) {
46        arrayVector ret;
47        int i=0;
48        for (unsigned int i=0;i<argLen;i++) {
49                std::wstring s = argument[i];
50                ret.push_back(s);
51        }
52        return ret;
53}
54/**
55* Create an arrayBuffer from a list.
56* This is the reverse of arrayBuffer2list.
57* <b>Notice</b> it is up to the caller to free the memory allocated in the returned buffer.
58*
59* @param lst A list to convert.
60* @param &argLen Write the length to this argument.
61* @return A pointer that is managed by the caller.
62*/
63array_buffer::arrayBuffer array_buffer::list2arrayBuffer(const arrayList lst, unsigned int &argLen) {
64        argLen = static_cast<unsigned int>(lst.size());
65        arrayBuffer arrayBuffer = new arrayBufferItem[argLen];
66        arrayList::const_iterator it = lst.begin();
67        int i;
68        for (i=0;it!=lst.end();++it,i++) {
69                std::wstring::size_type alen = (*it).size();
70                arrayBuffer[i] = new wchar_t[alen+2];
71                wcsncpy(arrayBuffer[i], (*it).c_str(), alen+1);
72        }
73        if (i != argLen)
74                throw ArrayBufferException("Invalid length!");
75        return arrayBuffer;
76}
77/**
78* Creates an empty arrayBuffer (only used to allow consistency)
79* @param &argLen [OUT] The length (items) of the arrayBuffer
80* @return The arrayBuffer
81*/
82array_buffer::arrayBuffer array_buffer::createEmptyArrayBuffer(unsigned int &argLen) {
83        argLen = 0;
84        arrayBuffer arrayBuffer = new arrayBufferItem[0];
85        return arrayBuffer;
86}
87/**
88* Creates an arrayBuffer with N-elements
89* @param &argLen [IN OUT] The length (items) of the arrayBuffer
90* @return The arrayBuffer
91*/
92array_buffer::arrayBuffer array_buffer::createArrayBuffer(unsigned int &argLen) {
93        arrayBuffer arrayBuffer = new arrayBufferItem[argLen];
94        for (unsigned int i=0;i<argLen;i++) {
95                arrayBuffer[i] = NULL;
96        }
97        return arrayBuffer;
98}
99/**
100* Joins an arrayBuffer back into a string
101* @param **argument The ArrayBuffer
102* @param argLen The length of the ArrayBuffer
103* @param join The char to use as separators when joining
104* @return The joined arrayBuffer
105*/
106std::wstring array_buffer::arrayBuffer2string(arrayBuffer argument, const unsigned int argLen, std::wstring join) {
107        std::wstring ret;
108        for (unsigned int i=0;i<argLen;i++) {
109                if (argument[i] != NULL) {
110                        ret += argument[i];
111                        if (i != argLen-1)
112                                ret += join;
113                }
114        }
115        return ret;
116}
117/**
118* Split a string into elements as a newly created arrayBuffer
119* @param buffer The CharArray to split along
120* @param splitChar The char to use as splitter
121* @param &argLen [OUT] The length of the Array
122* @return The arrayBuffer
123*/
124array_buffer::arrayBuffer array_buffer::split2arrayBuffer(const wchar_t* buffer, wchar_t splitChar, unsigned int &argLen) {
125        if (!buffer)
126                throw ArrayBufferException("Invalid buffer specified!");
127        argLen = 0;
128        const wchar_t *p = buffer;
129        if (!p[0]) {
130                return createEmptyArrayBuffer(argLen);
131        }
132        while (*p) {
133                if (*p == splitChar)
134                        argLen++;
135                p++;
136        }
137        argLen++;
138        wchar_t **arrayBuffer = new wchar_t*[argLen];
139        p = buffer;
140        for (unsigned int i=0;i<argLen;i++) {
141                const wchar_t *q = wcschr(p, (i<argLen-1)?splitChar:0);
142                unsigned int len = static_cast<int>(q-p);
143                arrayBuffer[i] = new wchar_t[len+1];
144                wcsncpy(arrayBuffer[i], p, len);
145                arrayBuffer[i][len] = 0;
146                p = ++q;
147        }
148        return arrayBuffer;
149}
150
151
152void array_buffer::set(arrayBuffer arrayBuffer, const unsigned int argLen, const unsigned int position, std::wstring argument) {
153        if (position >= argLen)
154                throw ArrayBufferException("position is outside the buffer");
155        delete [] arrayBuffer[position];
156        size_t len = argument.length();
157        arrayBuffer[position] = new wchar_t[len+2];
158        wcsncpy(arrayBuffer[position], argument.c_str(), len);
159        arrayBuffer[position][len] = 0;
160}
161
162/**
163 * Split a string into elements as a newly created arrayBuffer
164 * @param inBuf The CharArray to split along
165 * @param splitChar The char to use as splitter
166 * @param &argLen [OUT] The length of the Array
167 * @param escape [IN] Set to true to try to escape ":s ie. //token1 "token2 with space" token3//
168 * @return The arrayBuffer
169 */
170array_buffer::arrayBuffer array_buffer::split2arrayBuffer(const std::wstring inBuf, wchar_t splitChar, unsigned int &argLen, bool escape) {
171        if (inBuf.empty())
172                return createEmptyArrayBuffer(argLen);
173
174        std::wstring::size_type p1 = 0;
175        std::wstring::size_type p2 = 0;
176        std::list<std::wstring> token_list;
177        while (p1 != std::wstring::npos && p2 != std::wstring::npos) {
178                if (escape && inBuf[p1] == '\"') {
179                        p2 = inBuf.find('\"', p1+1);
180                        if (p2 != std::wstring::npos)
181                                p2 = inBuf.find(splitChar, p2);
182                } else {
183                        p2 = inBuf.find(splitChar, p1);
184                }
185                if (p2 == std::wstring::npos)
186                        p2 = inBuf.size();
187                if (p1 == p2 && p1 != inBuf.size()) {
188                        p1++;
189                        continue;
190                }
191                // p1 = start of "this token"
192                // p2 = end of "this token" (next split char)
193
194                if (p2<=p1)
195                        throw ArrayBufferException("Invalid position");
196                std::wstring token = inBuf.substr(p1,p2-p1);
197                if (escape && token[0] == '\"')
198                        token = token.substr(1);
199                if (escape && token[token.size()-1] == '\"')
200                        token = token.substr(0, token.size()-1);
201
202
203                token_list.push_back(token);
204                if (p2 < inBuf.size())
205                        p2++;
206                if (p2 == inBuf.size())
207                        p2 = std::wstring::npos;
208                p1 = p2;
209        }
210        arrayBuffer arrayBuffer = new arrayBufferItem[token_list.size()];
211        argLen=0;
212        for (std::list<std::wstring>::const_iterator cit=token_list.begin();cit!=token_list.end();++cit) {
213                size_t len = (*cit).size();
214                wchar_t* token = new wchar_t[len+1];
215                wcsncpy(token, (*cit).c_str(), len);
216                arrayBuffer[argLen++] = token;
217        }
218        token_list.clear();
219        return arrayBuffer;
220}
221
222/**
223* Destroy an arrayBuffer.
224* The buffer should have been created with list2arrayBuffer.
225*
226* @param **argument
227* @param argLen
228*/
229void array_buffer::destroyArrayBuffer(arrayBuffer argument, const unsigned int argLen) {
230        for (unsigned int i=0;i<argLen;i++) {
231                delete [] argument[i];
232        }
233        delete [] argument;
234}
235
236
Note: See TracBrowser for help on using the repository browser.