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

codepage.hpp « locale « src « far2l - github.com/elfmz/far2l.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3472a1b18c74b5d0de531d9f3823733770a5405e (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
#pragma once

/*
codepage.cpp

Работа с кодовыми страницами
*/
/*
Copyright (c) 1996 Eugene Roshal
Copyright (c) 2000 Far Group
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. The name of the authors may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <type_traits> // for std::swap
#include <WinCompat.h>
#include <WinPort.h>

// Тип выбранной таблицы символов
enum CPSelectType
{
	// "Любимая" таблица символов
	CPST_FAVORITE = 1,
	// Таблица символов участвующая в поиске по всем таблицам символов
	CPST_FIND = 2
};

extern const char *FavoriteCodePagesKey;

inline bool IsUTF7(UINT CP) {return (CP==CP_UTF7); };
inline bool IsUTF8(UINT CP) {return (CP==CP_UTF8); };
inline bool IsUTF16(UINT CP) {return (CP==CP_UTF16LE || CP==CP_UTF16BE); };
inline bool IsUTF32(UINT CP) {return (CP==CP_UTF32LE || CP==CP_UTF32BE); };
inline bool IsFixedSingleCharCodePage(UINT CP) {return (!IsUTF7(CP) && !IsUTF8(CP) && !IsUTF16(CP) && !IsUTF32(CP)  ); };
inline bool IsStandard8BitCodePage(UINT CP) {return ((CP==WINPORT(GetACP)()) || CP==CP_KOI8R || (CP==WINPORT(GetOEMCP)())); };

#if (__WCHAR_MAX__ > 0xffff)
const int StandardCPCount = 3 /* DOS, ANSI, KOI */ + 2 /* UTF-32 LE, UTF-32 BE */ + 2 /* UTF-16 LE, UTF-16 BE */ + 2 /* UTF-7, UTF-8 */;
inline bool IsStandardCodePage(UINT CP) { return (CP==CP_UTF8 || CP==CP_UTF16LE || CP==CP_UTF16BE || CP==CP_UTF32LE || CP==CP_UTF32BE || IsStandard8BitCodePage(CP)); }
inline bool IsFullWideCodePage(UINT CP) { return (CP==CP_UTF32LE || CP==CP_UTF32BE); }
inline bool IsUnicodeOrUtfCodePage(UINT CP) { return (CP==CP_UTF8 || CP==CP_UTF16LE || CP==CP_UTF16BE || CP==CP_UTF32LE || CP==CP_UTF32BE); }
#else
const int StandardCPCount = 3 /* DOS, ANSI, KOI */ + 2 /* UTF-16 LE, UTF-16 BE */ + 2 /* UTF-7, UTF-8 */;
inline bool IsStandardCodePage(UINT CP) { return (CP==CP_UTF8 || CP==CP_UTF16LE || CP==CP_UTF16BE || IsStandard8BitCodePage(CP)); }
inline bool IsFullWideCodePage(UINT CP) { return (CP==CP_UTF16LE || CP==CP_UTF16BE); }
inline bool IsUnicodeOrUtfCodePage(UINT CP) { return (CP==CP_UTF8 || CP==CP_UTF16LE || CP==CP_UTF16BE); }
#endif

inline static wchar_t WideReverse(wchar_t w)
{
	union {
		uint8_t b[sizeof(wchar_t)];
		wchar_t w;
	} u;
	u.w = w;
#if (__WCHAR_MAX__ > 0xffff)
	std::swap(u.b[0], u.b[3]);
	std::swap(u.b[1], u.b[2]);
#else
	std::swap(u.b[0], u.b[1]);
#endif
	return u.w;
}

inline static void WideReverse(const wchar_t *src, wchar_t *dst, size_t l) {
	for ( ; l; --l, ++src, ++dst) {
		*dst = WideReverse(*src);
	}
}

inline static void WideReverse(wchar_t *inplace, size_t l) {
	for ( ; l; --l, ++inplace) {
		*inplace = WideReverse(*inplace);
	}
}

bool IsCodePageSupported(UINT CodePage);

UINT SelectCodePage(UINT nCurrent, bool bShowUnicode, bool bShowUTF, bool bShowUTF7 = false, bool bShowAuto = false);

UINT FillCodePagesList(HANDLE dialogHandle, UINT controlId, UINT codePage, bool allowAuto, bool allowAll);

//#define CP_DBG

#ifdef __cplusplus
extern "C" {
#endif

void cp_logger(int level, const char *cname, const char *msg, ...);

#ifdef __cplusplus
}
#endif