2 ******************************************************************************
3 * Copyright (C) 1998-2005, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 ******************************************************************************
12 #include "unicode/utypes.h"
13 #include "unicode/unistr.h"
15 #include "layout/LETypes.h"
17 #include "GUISupport.h"
18 #include "UnicodeReader.h"
20 #define BYTE(b) (((int) b) & 0xFF)
23 * Read the text from a file. The text must start with a Unicode Byte
24 * Order Mark (BOM) so that we know what order to read the bytes in.
26 const UChar *UnicodeReader::readFile(const char *fileName, GUISupport *guiSupport, int32_t &charCount)
33 char startBytes[4] = {'\xA5', '\xA5', '\xA5', '\xA5'};
34 char errorMessage[128];
36 int32_t signatureLength = 0;
38 f = fopen(fileName, "rb");
41 sprintf(errorMessage,"Couldn't open %s: %s \n", fileName, strerror(errno));
42 guiSupport->postErrorMessage(errorMessage, "Text File Error");
46 fseek(f, 0, SEEK_END);
49 fseek(f, 0, SEEK_SET);
50 fread(startBytes, sizeof(char), 4, f);
52 if (startBytes[0] == '\xFE' && startBytes[1] == '\xFF') {
55 } else if (startBytes[0] == '\xFF' && startBytes[1] == '\xFE') {
56 if (startBytes[2] == '\x00' && startBytes[3] == '\x00') {
63 } else if (startBytes[0] == '\xEF' && startBytes[1] == '\xBB' && startBytes[2] == '\xBF') {
66 } else if (startBytes[0] == '\x0E' && startBytes[1] == '\xFE' && startBytes[2] == '\xFF') {
69 } else if (startBytes[0] == '\x00' && startBytes[1] == '\x00' &&
70 startBytes[2] == '\xFE' && startBytes[3] == '\xFF') {
74 sprintf(errorMessage, "Couldn't detect the encoding of %s: (%2.2X, %2.2X, %2.2X, %2.2X)\n", fileName,
75 BYTE(startBytes[0]), BYTE(startBytes[1]), BYTE(startBytes[2]), BYTE(startBytes[3]));
76 guiSupport->postErrorMessage(errorMessage, "Text File Error");
81 fileSize -= signatureLength;
82 fseek(f, signatureLength, SEEK_SET);
83 byteBuffer = new char[fileSize];
86 sprintf(errorMessage,"Couldn't get memory for reading %s: %s \n", fileName, strerror(errno));
87 guiSupport->postErrorMessage(errorMessage, "Text File Error");
92 fread(byteBuffer, sizeof(char), fileSize, f);
94 sprintf(errorMessage,"Couldn't read %s: %s \n", fileName, strerror(errno));
95 guiSupport->postErrorMessage(errorMessage, "Text File Error");
102 UnicodeString myText(byteBuffer, fileSize, cp);
106 charCount = myText.length();
107 charBuffer = LE_NEW_ARRAY(UChar, charCount + 1);
108 if(charBuffer == 0) {
109 sprintf(errorMessage,"Couldn't get memory for reading %s: %s \n", fileName, strerror(errno));
110 guiSupport->postErrorMessage(errorMessage, "Text File Error");
114 myText.extract(0, myText.length(), charBuffer);
115 charBuffer[charCount] = 0; // NULL terminate for easier reading in the debugger