summaryrefslogtreecommitdiff
path: root/Protocols/SIP/strutils.h
blob: 9a67362d551619377ecb9de602373de83eb930e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* 
Copyright (C) 2009 Ricardo Pescuma Domenecci

This is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this file; see the file license.txt.  If
not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  
*/

#pragma once
#ifndef __STRUTILS_H__
#define __STRUTILS_H__


#define TcharToSip TcharToUtf8

class SipToTchar
{
public:
	SipToTchar(const pj_str_t &str) : tchar(NULL)
	{
		if (str.ptr == NULL)
			return;

		int size = MultiByteToWideChar(CP_UTF8, 0, str.ptr, str.slen, NULL, 0);
		if (size < 0)
			throw _T("Could not convert string to WCHAR");
		size++;

		WCHAR *tmp = (WCHAR *) mir_alloc(size * sizeof(WCHAR));
		if (tmp == NULL)
			throw _T("mir_alloc returned NULL");

		MultiByteToWideChar(CP_UTF8, 0, str.ptr, str.slen, tmp, size);
		tmp[size-1] = 0;

#ifdef UNICODE

		tchar = tmp;

#else

		size = WideCharToMultiByte(CP_ACP, 0, tmp, -1, NULL, 0, NULL, NULL);
		if (size <= 0)
		{
			mir_free(tmp);
			throw _T("Could not convert string to ACP");
		}

		tchar = (TCHAR *) mir_alloc(size * sizeof(char));
		if (tchar == NULL)
		{
			mir_free(tmp);
			throw _T("mir_alloc returned NULL");
		}

		WideCharToMultiByte(CP_ACP, 0, tmp, -1, tchar, size, NULL, NULL);

		mir_free(tmp);

#endif
	}

	~SipToTchar()
	{
		if (tchar != NULL)
			mir_free(tchar);
	}

	TCHAR *detach()
	{
		TCHAR *ret = tchar;
		tchar = NULL;
		return ret;
	}

	TCHAR * get() const
	{
		return tchar;
	}

	operator TCHAR *() const
	{
		return tchar;
	}

	const TCHAR & operator[](int pos) const
	{
		return tchar[pos];
	}

private:
	TCHAR *tchar;
};


static pj_str_t pj_str(const char *str)
{
	pj_str_t ret;
	pj_cstr(&ret, str);
	return ret;
}


static char * mir_pjstrdup(const pj_str_t *from)
{
	char *ret = (char *) mir_alloc(from->slen + 1);
	strncpy(ret, from->ptr, from->slen);
	ret[from->slen] = 0;
	return ret;
}


static int FirstGtZero(int n1, int n2)
{
	if (n1 > 0)
		return n1;
	return n2;
}


static TCHAR *CleanupSip(TCHAR *str)
{
	if (_tcsnicmp(_T("sip:"), str, 4) == 0)
		return &str[4];
	else
		return str;
}


static const TCHAR *CleanupSip(const TCHAR *str)
{
	if (_tcsnicmp(_T("sip:"), str, 4) == 0)
		return &str[4];
	else
		return str;
}


static void CleanupNumber(TCHAR *out, int outSize, const TCHAR *number)
{
	int pos = 0;
	int len = lstrlen(number);
	for(int i = 0; i < len && pos < outSize - 1; ++i)
	{
		TCHAR c = number[i];

		if (i == 0 && c == _T('+'))
			out[pos++] = c;
		else if (c >= _T('0') && c <= _T('9'))
			out[pos++] = c;
	}
	out[pos] = 0;
}


static void RemoveLtGt(TCHAR *str)
{
	int len = lstrlen(str);
	if (str[0] == _T('<') && str[len-1] == _T('>'))
	{
		str[len-1] = 0;
		memmove(str, &str[1], len * sizeof(TCHAR));
	}
}


static bool IsValidDTMF(TCHAR c)
{
	if (c >= _T('A') && c <= _T('D'))
		return true;
	if (c >= _T('0') && c <= _T('9'))
		return true;
	if (c == _T('#') || c == _T('*'))
		return true;

	return false;
}


static const pj_str_t pj_cstr(const char *s)
{
	pj_str_t str;
    str.ptr = (char*)s;
    str.slen = s ? (pj_ssize_t)strlen(s) : 0;
    return str;
}

#define TransportName(_T_) SipToTchar(pj_cstr(pjsip_transport_get_type_name(_T_))).get()


#endif // __STRUTILS_H__