My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
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
#include "Core.h"

NAMESPACE_UPP

static void sLoadBom(Stream& in, String *t, WString *wt)
{
if(in.IsOpen()) {
String s;
if(in.GetLeft() > 3) {
word header = in.Get16();
if(header == 0xfffe || header == 0xfeff) {
int n = (int)in.GetLeft() / 2;
WStringBuffer ws(n);
ws.SetLength(in.Get(~ws, 2 * n) / 2);
if(header == 0xfffe)
EndianSwap((word *)~ws, ws.GetCount());
if(wt)
*wt = ws;
else
*t = FromUnicode(ws);
return;
}
int c = in.Get();
if(c < 0)
return;
byte *h = (byte *)&header;
if(h[0] == 0xef && h[1] == 0xbb && c == 0xbf) {
if(wt)
*wt = FromUtf8(LoadStream(in));
else
*t = ToCharset(CHARSET_DEFAULT, LoadStream(in), CHARSET_UTF8);
return;
}
s.Cat(h, 2);
s.Cat(c);
}
s.Cat(LoadStream(in));
if(wt)
*wt = ToUnicode(s, GetLNGCharset(GetSystemLNG()));
else
*t = ToCharset(CHARSET_DEFAULT, s, GetLNGCharset(GetSystemLNG()));
return;
}
return;
}

WString LoadStreamBOMW(Stream& in)
{
WString s = WString::GetVoid();
sLoadBom(in, NULL, &s);
return s;
}

String LoadStreamBOM(Stream& in)
{
String s = String::GetVoid();
sLoadBom(in, &s, NULL);
return s;
}

WString LoadFileBOMW(const char *path)
{
FileIn in(path);
return LoadStreamBOMW(in);
}

String LoadFileBOM(const char *path)
{
FileIn in(path);
return LoadStreamBOM(in);
}

bool SaveStreamBOM(Stream& out, const WString& data) {
if(!out.IsOpen() || out.IsError())
return false;
word w = 0xfeff;
out.Put(&w, 2);
out.Put(~data, 2 * data.GetLength());
out.Close();
return out.IsOK();
}

bool SaveFileBOM(const char *path, const WString& data)
{
FileOut out(path);
return SaveStreamBOM(out, data);
}

bool SaveStreamBOMUtf8(Stream& out, const String& data) {
if(!out.IsOpen() || out.IsError())
return false;
static unsigned char bom[] = {0xEF, 0xBB, 0xBF};
out.Put(bom, 3);
out.Put(ToCharset(CHARSET_UTF8, data));
out.Close();
return out.IsOK();
}

bool SaveFileBOMUtf8(const char *path, const String& data)
{
FileOut out(path);
return SaveStreamBOMUtf8(out, data);
}

END_UPP_NAMESPACE

Change log

r4169 by cxl on Nov 13, 2011   Diff
CtrlLib, ide: Support for UTF8-BOM
encoding (RM #129)
Go to: 
Project members, sign in to write a code review

Older revisions

r859 by cxl on Feb 13, 2009   Diff
BOM unicode files support, fixed
problem with scroll in frame area
All revisions of this file

File info

Size: 2195 bytes, 105 lines
Powered by Google Project Hosting