forked from b4winckler/macvim
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathguess.c
More file actions
205 lines (182 loc) · 6.23 KB
/
Copy pathguess.c
File metadata and controls
205 lines (182 loc) · 6.23 KB
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
203
204
205
/*
* guess.c - guessing character encoding
*
* Copyright (c) 2000-2009 Shiro Kawai <[email protected]>
*
* 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. Neither the name of the authors nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "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 COPYRIGHT
* OWNER OR CONTRIBUTORS 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.
*
* $Id: guess.c,v 1.6 2008-05-10 13:35:37 shirok Exp $
*/
#include <stdio.h>
#include <string.h>
#include "vim.h"
typedef struct guess_arc_rec {
unsigned int next; /* next state */
double score; /* score */
} guess_arc;
typedef struct guess_dfa_rec {
signed char (*states)[256];
guess_arc *arcs;
int state;
double score;
} guess_dfa;
#define DFA_INIT(st, ar) \
{ st, ar, 0, 1.0 }
#define DFA_NEXT(dfa, ch) \
do { \
int arc__; \
if (dfa.state >= 0) { \
arc__ = dfa.states[dfa.state][ch]; \
if (arc__ < 0) { \
dfa.state = -1; \
} else { \
dfa.state = dfa.arcs[arc__].next; \
dfa.score *= dfa.arcs[arc__].score; \
} \
} \
} while (0)
#define DFA_ALIVE(dfa) (dfa.state >= 0)
/* include DFA table generated by guess.scm */
#include "guess_tab.c"
static const char *guess_jp(FILE *in, const char *def)
{
int c;
guess_dfa eucj = DFA_INIT(guess_eucj_st, guess_eucj_ar);
guess_dfa sjis = DFA_INIT(guess_sjis_st, guess_sjis_ar);
guess_dfa utf8 = DFA_INIT(guess_utf8_st, guess_utf8_ar);
guess_dfa *top = NULL;
int sjis_halfwidth_alive = 1;
while ((c = fgetc(in)) != EOF) {
/* special treatment of jis escape sequence */
if (c == 0x1b) {
c = fgetc(in);
if (c == EOF)
break;
if (c == '$' || c == '(') return "iso-2022-jp";
}
if (c >= 0x80 && (c < 0xa1 || c > 0xdf))
sjis_halfwidth_alive = 0;
if (DFA_ALIVE(eucj)) {
if (!DFA_ALIVE(sjis) && !DFA_ALIVE(utf8)) return "euc-jp";
DFA_NEXT(eucj, c);
}
if (DFA_ALIVE(sjis)) {
if (!DFA_ALIVE(eucj) && !DFA_ALIVE(utf8)) return "cp932";
DFA_NEXT(sjis, c);
}
if (DFA_ALIVE(utf8)) {
if (!DFA_ALIVE(sjis) && !DFA_ALIVE(eucj)) return "utf-8";
DFA_NEXT(utf8, c);
}
if (!DFA_ALIVE(eucj) && !DFA_ALIVE(sjis) && !DFA_ALIVE(utf8)) {
/* we ran out the possibilities */
return NULL;
}
}
if (DFA_ALIVE(eucj) && DFA_ALIVE(sjis) && !DFA_ALIVE(utf8) &&
sjis_halfwidth_alive) {
/* non-ASCII chars are only cp932 half width chars */
return "cp932";
}
/* Now, we have ambigous code. Pick the highest score. If more than
one candidate tie, pick the default encoding. */
if (DFA_ALIVE(eucj)) top = &eucj;
if (DFA_ALIVE(utf8)) {
if (top) {
if (top->score <= utf8.score) top = &utf8;
} else {
top = &utf8;
}
}
if (DFA_ALIVE(sjis)) {
if (top) {
if (top->score < sjis.score) top = &sjis;
} else {
top = &sjis;
}
}
if (top == &eucj) return "euc-jp";
if (top == &utf8) return "utf-8";
if (top == &sjis) return "cp932";
return NULL;
}
static const char *guess_bom(FILE *in)
{
int c, c2, c3;
c = fgetc(in);
if (c == 0xFE || c == 0xFF) {
c2 = fgetc(in);
if (c == 0xFE && c2 == 0xFF) return "ucs-bom";
if (c == 0xFF && c2 == 0xFE) return "ucs-bom";
ungetc(c2, in);
} else if (c == 0xEF) {
c2 = fgetc(in);
c3 = fgetc(in);
if (c2 == 0xBB && c3 == 0xBF) return "ucs-bom";
ungetc(c3, in);
ungetc(c2, in);
}
ungetc(c, in);
return NULL;
}
int guess_encode(char_u** fenc, int* fenc_alloced, char_u* fname)
{
FILE *in;
const char *enc;
if (p_verbose >= 1)
{
verbose_enter();
smsg((char_u*)"guess_encode:");
smsg((char_u*)" init: fenc=%s alloced=%d fname=%s\n",
*fenc, *fenc_alloced, fname);
verbose_leave();
}
if (!fname)
return 0;
in = mch_fopen((const char *)fname, "r");
if (!in)
return 0;
enc = guess_bom(in);
if (!enc)
enc = guess_jp(in, "utf-8");
fclose(in);
if (enc)
{
if (p_verbose >= 1)
{
verbose_enter();
smsg((char_u*)" result: newenc=%s\n", enc);
verbose_leave();
}
if (*fenc_alloced)
vim_free(*fenc);
*fenc = vim_strsave((char_u*)enc);
*fenc_alloced = TRUE;
}
return 1;
}