source: nscp/include/charEx.h @ 5735dda

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

Merged back all (some?) of the new changes from 0.3.8

  • Property mode set to 100644
File size: 3.6 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#pragma once
22#ifdef WIN32
23#include <windows.h>
24#include <tchar.h>
25#endif
26
27namespace charEx {
28        /**
29         * Function to split a char buffer into a list<string>
30         * @param buffer A char buffer to iterate over.
31         * @param split The char to split by
32         * @return a list with strings
33         */
34        inline std::list<std::wstring> split(const wchar_t* buffer, wchar_t split) {
35                std::list<std::wstring> ret;
36                const wchar_t *start = buffer;
37                for (const wchar_t *p = buffer;*p!='\0';p++) {
38                        if (*p==split) {
39                                std::wstring str(start, p-start);
40                                ret.push_back(str);
41                                start = p+1;
42                        }
43                }
44                ret.push_back(std::wstring(start));
45                return ret;
46        }
47
48/*
49        inline char* tchar_to_char( const wchar_t* pStr, int len, int &nChars) {
50                if (pStr == NULL)
51                        throw std::exception();
52                if (len < -1)
53                        throw std::exception();
54
55                // figure out how many narrow characters we are going to get
56                nChars = WideCharToMultiByte(CP_ACP, 0, pStr, len, NULL, 0, NULL, NULL);
57                if (len == -1)
58                        --nChars;
59                if (nChars==0) {
60                        char *ret = new char[1];
61                        ret[0] = 0;
62                        return ret;
63                }
64
65                // convert the wide string to a narrow string
66                char *ret = new char[nChars+1];
67                WideCharToMultiByte(CP_ACP, 0, pStr, len, ret, nChars, NULL, NULL);
68                return ret;
69        }
70
71        inline wchar_t* char_to_tchar(const char* pStr, int len, int &nChars) {
72                if (pStr == NULL)
73                        throw std::exception();
74                if (len < -1)
75                        throw std::exception();
76
77                // figure out how many wide characters we are going to get
78                nChars = MultiByteToWideChar( CP_ACP , 0 , pStr , len , NULL , 0 );
79                if (len == -1)
80                        --nChars;
81                if (nChars == 0) {
82                        wchar_t *ret = new wchar_t[1];
83                        ret[0] = 0;
84                        return ret;
85                }
86
87                // convert the narrow string to a wide string
88                wchar_t *ret = new wchar_t[nChars+1];
89                MultiByteToWideChar(CP_ACP, 0 ,pStr ,len, const_cast<wchar_t*>(ret), nChars);
90                return ret;
91        }
92*/
93
94
95        typedef std::pair<std::wstring,wchar_t*> token;
96        inline token getToken(wchar_t *buffer, wchar_t split) {
97                if (buffer == NULL)
98                        throw std::exception();
99                wchar_t *p = wcschr(buffer, split);
100                if (!p)
101                        return token(buffer, NULL);
102                if (!p[1])
103                        return token(std::wstring(buffer, p-buffer), NULL);
104                p++;
105                return token(std::wstring(buffer, p-buffer-1), p);
106        }
107};
Note: See TracBrowser for help on using the repository browser.