Welcome to mirror list, hosted at ThFree Co, Russian Federation.

text.cpp « DSUtil « src - github.com/mpc-hc/mpc-hc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3df2726cb6d2687674809b447bb71b17afcbf45a (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
/*
 * $Id$
 *
 * (C) 2003-2006 Gabest
 * (C) 2006-2012 see Authors.txt
 *
 * This file is part of MPC-HC.
 *
 * MPC-HC is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * MPC-HC 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "stdafx.h"
#include "text.h"

DWORD CharSetToCodePage(DWORD dwCharSet)
{
    if (dwCharSet == CP_UTF8) {
        return CP_UTF8;
    }
    if (dwCharSet == CP_UTF7) {
        return CP_UTF7;
    }
    CHARSETINFO cs = {0};
    ::TranslateCharsetInfo((DWORD*)dwCharSet, &cs, TCI_SRCCHARSET);
    return cs.ciACP;
}

CStringA ConvertMBCS(CStringA str, DWORD SrcCharSet, DWORD DstCharSet)
{
    WCHAR* utf16 = DNew WCHAR[str.GetLength() + 1];
    memset(utf16, 0, (str.GetLength() + 1)*sizeof(WCHAR));

    CHAR* mbcs = DNew CHAR[str.GetLength() * 6 + 1];
    memset(mbcs, 0, str.GetLength() * 6 + 1);

    int len = MultiByteToWideChar(
                  CharSetToCodePage(SrcCharSet), 0,
                  str, -1, // null terminated string
                  utf16, str.GetLength() + 1);

    len = WideCharToMultiByte(
              CharSetToCodePage(DstCharSet), 0,
              utf16, len,
              mbcs, str.GetLength() * 6,
              NULL, NULL);

    str = mbcs;

    delete [] utf16;
    delete [] mbcs;

    return str;
}

CStringA UrlEncode(CStringA str, bool fRaw)
{
    CStringA urlstr;

    for (int i = 0; i < str.GetLength(); i++) {
        CHAR c = str[i];
        if (fRaw && c == '+') {
            urlstr += "%2B";
        } else if (c > 0x20 && c < 0x7f && c != '&') {
            urlstr += c;
        } else if (c == 0x20) {
            urlstr += fRaw ? ' ' : '+';
        } else {
            CStringA tmp;
            tmp.Format("%%%02x", (BYTE)c);
            urlstr += tmp;
        }
    }

    return urlstr;
}

CStringA UrlDecode(CStringA str, bool fRaw)
{
    str.Replace("&amp;", "&");

    CHAR* s = str.GetBuffer(str.GetLength());
    CHAR* e = s + str.GetLength();
    CHAR* s1 = s;
    CHAR* s2 = s;
    while (s1 < e) {
        CHAR s11 = (s1 < e - 1) ? (__isascii(s1[1]) && isupper(s1[1]) ? tolower(s1[1]) : s1[1]) : 0;
        CHAR s12 = (s1 < e - 2) ? (__isascii(s1[2]) && isupper(s1[2]) ? tolower(s1[2]) : s1[2]) : 0;

        if (*s1 == '%' && s1 < e - 2
                && (s1[1] >= '0' && s1[1] <= '9' || s11 >= 'a' && s11 <= 'f')
                && (s1[2] >= '0' && s1[2] <= '9' || s12 >= 'a' && s12 <= 'f')) {
            s1[1] = s11;
            s1[2] = s12;
            *s2 = 0;
            if (s1[1] >= '0' && s1[1] <= '9') {
                *s2 |= s1[1] - '0';
            } else if (s1[1] >= 'a' && s1[1] <= 'f') {
                *s2 |= s1[1] - 'a' + 10;
            }
            *s2 <<= 4;
            if (s1[2] >= '0' && s1[2] <= '9') {
                *s2 |= s1[2] - '0';
            } else if (s1[2] >= 'a' && s1[2] <= 'f') {
                *s2 |= s1[2] - 'a' + 10;
            }
            s1 += 2;
        } else {
            *s2 = *s1 == '+' && !fRaw ? ' ' : *s1;
        }

        s1++;
        s2++;
    }

    str.ReleaseBuffer(int(s2 - s));

    return str;
}

CString ExtractTag(CString tag, CMapStringToString& attribs, bool& fClosing)
{
    tag.Trim();
    attribs.RemoveAll();

    fClosing = !tag.IsEmpty() ? tag[0] == '/' : false;
    tag.TrimLeft('/');

    int i = tag.Find(' ');
    if (i < 0) {
        i = tag.GetLength();
    }
    CString type = tag.Left(i).MakeLower();
    tag = tag.Mid(i).Trim();

    while ((i = tag.Find('=')) > 0) {
        CString attrib = tag.Left(i).Trim().MakeLower();
        tag = tag.Mid(i + 1);
        for (i = 0; i < tag.GetLength() && _istspace(tag[i]); i++) {
            ;
        }
        tag = i < tag.GetLength() ? tag.Mid(i) : _T("");
        if (!tag.IsEmpty() && tag[0] == '\"') {
            tag = tag.Mid(1);
            i = tag.Find('\"');
        } else {
            i = tag.Find(' ');
        }
        if (i < 0) {
            i = tag.GetLength();
        }
        CString param = tag.Left(i).Trim();
        if (!param.IsEmpty()) {
            attribs[attrib] = param;
        }
        tag = i + 1 < tag.GetLength() ? tag.Mid(i + 1) : _T("");
    }

    return type;
}

CAtlList<CString>& MakeLower(CAtlList<CString>& sl)
{
    POSITION pos = sl.GetHeadPosition();
    while (pos) {
        sl.GetNext(pos).MakeLower();
    }
    return sl;
}

CAtlList<CString>& MakeUpper(CAtlList<CString>& sl)
{
    POSITION pos = sl.GetHeadPosition();
    while (pos) {
        sl.GetNext(pos).MakeUpper();
    }
    return sl;
}