source: nscp/include/charEx.h @ 7f9c823

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

First attempt at serious boostification.
NOTICE! This is NOT a complete edition, it build and runs but many features are disabled and/or broken.
But we have working, sockets and mutexes and conversion functions from boost inside now and more to come...
Also started to build with CMake since it works better then boost.build

  • Property mode set to 100644
File size: 4.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#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#ifdef _DEBUG
108        inline void test_getToken(wchar_t* in1, wchar_t in2, std::wstring out1, wchar_t * out2) {
109                token t = getToken(in1, in2);
110                std::wcout << _T("charEx::test_getToken(") << in1 << _T(", ") << in2 << _T(") : ");
111                if (t.first == out1)  {
112                        if ((t.second == NULL) && (out2 == NULL))
113                                std::wcout << _T("Succeeded") << std::endl;
114                        else if (t.second == NULL)
115                                std::wcout << _T("Failed [NULL=") << out2 << _T("]") << std::endl;
116                        else if (out2 == NULL)
117                                std::wcout << _T("Failed [") << t.second << _T("=NULL]") << std::endl;
118                        else if (wcscmp(t.second, out2) == 0)
119                                std::wcout << _T("Succeeded") << std::endl;
120                        else
121                                std::wcout << _T("Failed") << std::endl;
122                } else
123                        std::wcout << _T("Failed [") << out1 << _T("=") << t.first << _T("]") << std::endl;
124        }
125        inline void run_test_getToken() {
126                test_getToken(_T(""), '&', _T(""), NULL);
127                test_getToken(_T("&"), '&', _T(""), NULL);
128                test_getToken(_T("&&"), '&', _T(""), _T("&"));
129                test_getToken(_T("foo"), '&', _T("foo"), NULL);
130                test_getToken(_T("foo&"), '&', _T("foo"), NULL);
131                test_getToken(_T("foo&bar"), '&', _T("foo"), _T("bar"));
132                test_getToken(_T("foo&bar&test"), '&', _T("foo"), _T("bar&test"));
133        }
134#endif
135
136};
Note: See TracBrowser for help on using the repository browser.