]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/layout/UnicodeReader.cpp
2 ******************************************************************************
3 * © 2016 and later: Unicode, Inc. and others. *
4 * License & terms of use: http://www.unicode.org/copyright.html#License *
5 ******************************************************************************
6 ******************************************************************************
7 * Copyright (C) 1998-2005, International Business Machines Corporation and *
8 * others. All Rights Reserved. *
9 ******************************************************************************
16 #include "unicode/utypes.h"
17 #include "unicode/unistr.h"
19 #include "layout/LETypes.h"
21 #include "GUISupport.h"
22 #include "UnicodeReader.h"
24 #define BYTE(b) (((int) b) & 0xFF)
27 * Read the text from a file. The text must start with a Unicode Byte
28 * Order Mark (BOM) so that we know what order to read the bytes in.
30 const UChar
*UnicodeReader::readFile(const char *fileName
, GUISupport
*guiSupport
, int32_t &charCount
)
37 char startBytes
[4] = {'\xA5', '\xA5', '\xA5', '\xA5'};
38 char errorMessage
[128];
40 int32_t signatureLength
= 0;
42 f
= fopen(fileName
, "rb");
45 sprintf(errorMessage
,"Couldn't open %s: %s \n", fileName
, strerror(errno
));
46 guiSupport
->postErrorMessage(errorMessage
, "Text File Error");
50 fseek(f
, 0, SEEK_END
);
53 fseek(f
, 0, SEEK_SET
);
54 fread(startBytes
, sizeof(char), 4, f
);
56 if (startBytes
[0] == '\xFE' && startBytes
[1] == '\xFF') {
59 } else if (startBytes
[0] == '\xFF' && startBytes
[1] == '\xFE') {
60 if (startBytes
[2] == '\x00' && startBytes
[3] == '\x00') {
67 } else if (startBytes
[0] == '\xEF' && startBytes
[1] == '\xBB' && startBytes
[2] == '\xBF') {
70 } else if (startBytes
[0] == '\x0E' && startBytes
[1] == '\xFE' && startBytes
[2] == '\xFF') {
73 } else if (startBytes
[0] == '\x00' && startBytes
[1] == '\x00' &&
74 startBytes
[2] == '\xFE' && startBytes
[3] == '\xFF') {
78 sprintf(errorMessage
, "Couldn't detect the encoding of %s: (%2.2X, %2.2X, %2.2X, %2.2X)\n", fileName
,
79 BYTE(startBytes
[0]), BYTE(startBytes
[1]), BYTE(startBytes
[2]), BYTE(startBytes
[3]));
80 guiSupport
->postErrorMessage(errorMessage
, "Text File Error");
85 fileSize
-= signatureLength
;
86 fseek(f
, signatureLength
, SEEK_SET
);
87 byteBuffer
= new char[fileSize
];
90 sprintf(errorMessage
,"Couldn't get memory for reading %s: %s \n", fileName
, strerror(errno
));
91 guiSupport
->postErrorMessage(errorMessage
, "Text File Error");
96 fread(byteBuffer
, sizeof(char), fileSize
, f
);
98 sprintf(errorMessage
,"Couldn't read %s: %s \n", fileName
, strerror(errno
));
99 guiSupport
->postErrorMessage(errorMessage
, "Text File Error");
106 UnicodeString
myText(byteBuffer
, fileSize
, cp
);
110 charCount
= myText
.length();
111 charBuffer
= LE_NEW_ARRAY(UChar
, charCount
+ 1);
112 if(charBuffer
== 0) {
113 sprintf(errorMessage
,"Couldn't get memory for reading %s: %s \n", fileName
, strerror(errno
));
114 guiSupport
->postErrorMessage(errorMessage
, "Text File Error");
118 myText
.extract(0, myText
.length(), charBuffer
);
119 charBuffer
[charCount
] = 0; // NULL terminate for easier reading in the debugger