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
|
// Common/Lang.cpp
#include "StdAfx.h"
#include "Lang.h"
#include "StringToInt.h"
#include "UTFConvert.h"
#include "../Windows/FileIO.h"
void CLang::Clear() throw()
{
_ids.Clear();
_offsets.Clear();
Comments.Clear();
delete []_text;
_text = NULL;
}
static const char * const kLangSignature = ";!@Lang2@!UTF-8!\n";
bool CLang::OpenFromString(const AString &s2)
{
UString su;
if (!ConvertUTF8ToUnicode(s2, su))
return false;
if (su.IsEmpty())
return false;
const wchar_t *s = su;
const wchar_t *sLim = s + su.Len();
if (*s == 0xFEFF)
s++;
for (const char *p = kLangSignature;; s++)
{
const Byte c = (Byte)(*p++);
if (c == 0)
break;
if (*s != c)
return false;
}
wchar_t *text = new wchar_t[(size_t)(sLim - s) + 1];
_text = text;
UString comment;
Int32 id = -1024;
unsigned pos = 0;
while (s != sLim)
{
const unsigned start = pos;
do
{
wchar_t c = *s++;
if (c == '\n')
break;
if (c == '\\')
{
if (s == sLim)
return false;
c = *s++;
switch (c)
{
case '\n': return false;
case 'n': c = '\n'; break;
case 't': c = '\t'; break;
case '\\': /* c = '\\'; */ break;
default: text[pos++] = L'\\'; break;
}
}
text[pos++] = c;
}
while (s != sLim);
{
unsigned j = start;
for (; j < pos; j++)
if (text[j] != ' ' && text[j] != '\t')
break;
if (j == pos)
{
id++;
pos = start;
continue;
}
}
// start != pos
text[pos++] = 0;
if (text[start] == ';')
{
comment = text + start;
comment.TrimRight();
if (comment.Len() != 1)
Comments.Add(comment);
id++;
pos = start;
continue;
}
const wchar_t *end;
const UInt32 id32 = ConvertStringToUInt32(text + start, &end);
if (*end == 0)
{
if (id32 > ((UInt32)1 << 30) || (Int32)id32 < id)
return false;
id = (Int32)id32;
pos = start;
continue;
}
if (id < 0)
return false;
_ids.Add((UInt32)id++);
_offsets.Add(start);
}
return true;
}
bool CLang::Open(CFSTR fileName, const char *id)
{
Clear();
NWindows::NFile::NIO::CInFile file;
if (!file.Open(fileName))
return false;
UInt64 length;
if (!file.GetLength(length))
return false;
if (length > (1 << 20))
return false;
AString s;
const unsigned len = (unsigned)length;
char *p = s.GetBuf(len);
size_t processed;
if (!file.ReadFull(p, len, processed))
return false;
file.Close();
if (len != processed)
return false;
char *p2 = p;
for (unsigned i = 0; i < len; i++)
{
const char c = p[i];
if (c == 0)
break;
if (c != 0x0D)
*p2++ = c;
}
*p2 = 0;
s.ReleaseBuf_SetLen((unsigned)(p2 - p));
if (OpenFromString(s))
{
const wchar_t *name = Get(0);
if (name && StringsAreEqual_Ascii(name, id))
return true;
}
Clear();
return false;
}
const wchar_t *CLang::Get(UInt32 id) const throw()
{
const int index = _ids.FindInSorted(id);
if (index < 0)
return NULL;
return _text + (size_t)_offsets[(unsigned)index];
}
|