forked from keizi666/charu3
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
text.cpp
154 lines (141 loc) · 4.78 KB
/
text.cpp
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
/*----------------------------------------------------------
文字列処理クラス
2002/11/20 (c)keizi
----------------------------------------------------------*/
#include "stdafx.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#define new DEBUG_NEW
#endif
#include "text.h"
//---------------------------------------------------
//関数名 awk(char *strSource,int nAwk,char cSplit)
//機能 指定文字区切りで文字列を切り出す
//---------------------------------------------------
TCHAR* Text::Awk(TCHAR* strSource, TCHAR* strRet, int nSize, int nAwk, TCHAR cSplit /* = 0x20*/)
{
bool isSpace = false;
TCHAR* szStart = NULL;
if (nSize < 1 || nAwk < 1 || *strSource == NULL) return strSource;
while (*strSource != NULL) {
if (!Text::IsSJIS(strSource)) {
if (!isSpace) {
if (*strSource == cSplit) {//スプリッタが見つかった
isSpace = true;
}
}
else if (isSpace) {
if (*strSource != cSplit) {//スプリッタじゃ無くなった
nAwk--;//カウンタを減らす
isSpace = false;
}
}
}
else if (isSpace) {
nAwk--;//カウンタを減らす
isSpace = false;
}
if (nAwk == 1) {
if (!szStart) szStart = strSource - 1;
if (*strSource == 0x0a || *strSource == 0x0d || *strSource == cSplit ||
*strSource == 0x1a || *strSource == NULL) {
break;
}
if (nSize <= 1) break;
*strRet = *strSource;
if (Text::IsSJIS(strSource)) {
strSource++;//ポインタを進める
strRet++;
*strRet = *strSource;
}
nSize--;
strRet++;
}
else if (Text::IsSJIS(strSource)) strSource++;//ポインタを進める
strSource++;//ポインタを進める
}
*strRet = (TCHAR)NULL;
return szStart;
}
//---------------------------------------------------
//関数名 isSJIS(char *szSource)
//機能 S-JISかどうかを判別
//---------------------------------------------------
bool Text::IsSJIS(TCHAR* szSource)
{
bool isRet = false;
#ifdef _UNICODE
return false;
#endif
if (*szSource == NULL) return isRet;
if (((unsigned char)*szSource >= 0x81 && (unsigned char)*szSource <= 0x9f) ||
((unsigned char)*szSource >= 0xe0 && (unsigned char)*szSource <= 0xef))
if (((unsigned char)*(szSource + 1) >= 0x40 && (unsigned char)*(szSource + 1) <= 0x7e) ||
((unsigned char)*szSource >= 0x80 && (unsigned char)*szSource <= 0xfc))
isRet = true;
return isRet;
}
//---------------------------------------------------
//関数名 compressionSpace(TCHAR *szSource)
//機能 複数のスペースを一つに纏める
//---------------------------------------------------
int Text::CompressionSpace(TCHAR* szSource)
{
int i;
for (i = 0; *szSource; i++, szSource++) {
if (*szSource == _T(' ')) {
TCHAR* szSpaceEnd;
szSpaceEnd = szSource;
int j;
for (j = 0; *szSpaceEnd && *szSpaceEnd == _T(' '); j++, szSpaceEnd++);
Text::MoveForward(szSpaceEnd, j - 1);
szSource++;
}
}
return i;
}
//---------------------------------------------------
//関数名 moveForward(TCHAR *szMovePos,int nMove)
//機能 文字列を前に移動(メモリ範囲に注意)
//---------------------------------------------------
int Text::MoveForward(TCHAR* szMovePos, int nMove)
{
//文字列移動
int i;
for (i = 0; *szMovePos; i++, szMovePos++) {
*(szMovePos - nMove) = *szMovePos;
}
*(szMovePos - nMove) = NULL;
return i;
}
//---------------------------------------------------
//関数名 ConvertWcharToCString(wchar_t *szUnicodeBuff)
//機能 ワイド文字をMBCSにしてCStringに入れる
//---------------------------------------------------
CString Text::ConvertWcharToCString(wchar_t* szUnicodeBuff)
{
char* szMbcsBuff;
CString strRet;
int nDataSize = ::WideCharToMultiByte(CP_ACP, 0, szUnicodeBuff, -1, NULL, 0, NULL, NULL);
szMbcsBuff = new char[nDataSize];
if (szMbcsBuff) {
::WideCharToMultiByte(CP_ACP, 0, szUnicodeBuff, -1, szMbcsBuff, nDataSize, "", NULL);
strRet = szMbcsBuff;
delete[] szMbcsBuff;
}
return strRet;
}
CStringA Text::ConvertUnicodeToUTF8(const CStringW& uni)
{
if (uni.IsEmpty()) return "";
CStringA utf8;
int cc = 0;
// get length (cc) of the new multibyte string excluding the \0 terminator first
if ((cc = WideCharToMultiByte(CP_UTF8, 0, uni, -1, NULL, 0, 0, 0) - 1) > 0) {
char* buf = utf8.GetBuffer(cc);
if (buf) WideCharToMultiByte(CP_UTF8, 0, uni, -1, buf, cc, 0, 0);
utf8.ReleaseBuffer();
}
return utf8;
}