forked from VincentWei/mgncs
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
win-funcs.c
140 lines (120 loc) · 3.52 KB
/
win-funcs.c
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
#ifdef WIN32
#include <windows.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <io.h>
#define LOWCASE(ch) (((ch)>='A' && (ch)<='Z')?(ch)-'A'+'a':(ch))
static int mystrcasecmp(const char* s1, const char* s2)
{
if(s1 == NULL && s2 == NULL)
return 0;
if(s1 == NULL)
return -1;
if(s2 == NULL)
return 1;
while(*s1 && *s2 && LOWCASE(*s1) == LOWCASE(*s2)){
s1 ++;
s2 ++;
}
if(!*s1 && !*s2)
return 0;
if(!*s1)
return -1;
if(!*s2)
return 1;
if(LOWCASE(*s1) > LOWCASE(*s2))
return 1;
return -1;
}
void win_truncate_file(FILE *fp, int size)
{
int fd = _fileno(fp);
HANDLE hFile = (HANDLE)_get_osfhandle(fd);
fseek(fp, size, SEEK_SET);
SetEndOfFile(hFile);
}
//////////////////////////////////////////////////////
//convert
static int get_codepage(const char* charset)
{
if(charset == NULL)
return CP_ACP;
if(mystrcasecmp(charset,"utf-8") == 0)
return CP_UTF8;
else if(mystrcasecmp(charset, "unicode") == 0)
return -1; //-1 is unicode
return CP_ACP;
}
static int Utf8ToUnicode(const char* in_words, char* out_words, int in_len, int out_len)
{
return MultiByteToWideChar(CP_UTF8, MB_PRECOMPOSED,(LPCTSTR)in_words, in_len, (LPWSTR)out_words, out_len);
}
static int UnicodeToUtf8(const char* in_words, char* out_words, int in_len, int out_len)
{
return WideCharToMultiByte(CP_UTF8, MB_PRECOMPOSED, (LPCWSTR)in_words, in_len, (LPTSTR)out_words, out_len,NULL,NULL);
}
static int AcpToUtf8(const char* in_words, char* out_words, int in_len, int out_len)
{
wchar_t szBuffer[1024];
int len = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,(LPCTSTR)in_words, in_len, (LPCTSTR)szBuffer, out_len);
if(len <= 0)
return 0;
return WideCharToMultiByte(CP_UTF8, MB_PRECOMPOSED, (LPCWSTR)szBuffer, len, (LPTSTR)out_words, out_len,NULL,NULL);
}
static int Utf8ToAcp(const char* in_words, char* out_words, int in_len, int out_len)
{
wchar_t szBuffer[1024];
int len = MultiByteToWideChar(CP_UTF8, MB_PRECOMPOSED,(LPCTSTR)in_words, in_len, (LPCTSTR)szBuffer, out_len);
if(len <= 0)
return 0;
return WideCharToMultiByte(CP_ACP, MB_PRECOMPOSED, (LPCWSTR)szBuffer, len, (LPTSTR)out_words, out_len,NULL,NULL);
}
static int AcpToUnicode(const char* in_words, char* out_words, int in_len, int out_len)
{
return MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,(LPCTSTR)in_words, in_len, (LPCTSTR)out_words, out_len);
}
static int UnicodeToAcp(const char* in_words, char* out_words, int in_len, int out_len)
{
return WideCharToMultiByte(CP_ACP, MB_PRECOMPOSED, (LPCWSTR)in_words, in_len, (LPTSTR)out_words, out_len,NULL,NULL);
}
static int no_trans(const char* in_words, char* out_words, int in_len, int out_len)
{
int len = (in_len > out_len ? out_len : in_len) -1;
strncpy(out_words, in_words, len);
out_words[len] = 0;
return len;
}
void* win_get_convert_cb(const char* from_charset, const char* to_charset)
{
int cp_from, cp_to;
cp_from = get_codepage(from_charset);
cp_to = get_codepage(to_charset);
if(cp_from == cp_to)
return no_trans;
if(cp_from == CP_UTF8)
{
if(cp_to == CP_ACP)
return Utf8ToAcp;
else if(cp_to == -1) //unicode
return Utf8ToUnicode;
}
else if(cp_from == -1)//unicode
{
if(cp_to == CP_ACP)
return UnicodeToAcp;
else if(cp_to == CP_UTF8)
return UnicodeToUtf8;
}
else if(cp_from == CP_ACP)
{
if(cp_to == CP_UTF8)
return AcpToUtf8;
else if(cp_to == -1)
return AcpToUnicode;
}
return no_trans;
}
#endif