Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Lang.cpp « Common « CPP - github.com/kornelski/7z.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 803209cf6a26278a81dda52f0c20cc6b8d56488d (plain)
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
// Common/Lang.cpp

#include "StdAfx.h"

#include "Lang.h"
#include "TextConfig.h"

#include "StdInStream.h"
#include "UTFConvert.h"
#include "Defs.h"

/*
static UInt32 HexStringToNumber(const char *string, int &finishPos)
{
  UInt32 number = 0;
  for (finishPos = 0; finishPos < 8; finishPos++)
  {
    char c = string[finishPos];
    int a;
    if (c >= '0' && c <= '9')
      a = c - '0';
    else if (c >= 'A' && c <= 'F')
      a = 10 + c - 'A';
    else if (c >= 'a' && c <= 'f')
      a = 10 + c - 'a';
    else
      return number;
    number *= 0x10;
    number += a;
  }
  return number;
}
*/
static bool HexStringToNumber(const UString &string, UInt32 &aResultValue)
{
  aResultValue = 0;
  if (string.IsEmpty())
    return false;
  for (int i = 0; i < string.Length(); i++)
  {
    wchar_t c = string[i];
    int a;
    if (c >= L'0' && c <= L'9')
      a = c - L'0';
    else if (c >= L'A' && c <= L'F')
      a = 10 + c - L'A';
    else if (c >= L'a' && c <= L'f')
      a = 10 + c - L'a';
    else
      return false;
    aResultValue *= 0x10;
    aResultValue += a;
  }
  return true;
}


static bool WaitNextLine(const AString &string, int &pos)
{
  for (;pos < string.Length(); pos++)
    if (string[pos] == 0x0A)
      return true;
  return false;
}

static int CompareLangItems(void *const *elem1, void *const *elem2, void *)
{
  const CLangPair &langPair1 = *(*((const CLangPair **)elem1));
  const CLangPair &langPair2 = *(*((const CLangPair **)elem2));
  return MyCompare(langPair1.Value, langPair2.Value);
}

bool CLang::Open(LPCTSTR fileName)
{
  _langPairs.Clear();
  CStdInStream file;
  if (!file.Open(fileName))
    return false;
  AString string;
  file.ReadToString(string);
  file.Close();
  int pos = 0;
  if (string.Length() >= 3)
  {
    if (Byte(string[0]) == 0xEF && Byte(string[1]) == 0xBB && Byte(string[2]) == 0xBF)
      pos += 3;
  }

  /////////////////////
  // read header

  AString stringID = ";!@Lang@!UTF-8!";
  if (string.Mid(pos, stringID.Length()) != stringID)
    return false;
  pos += stringID.Length();
  
  if (!WaitNextLine(string, pos))
    return false;

  CObjectVector<CTextConfigPair> pairs;
  if (!GetTextConfig(string.Mid(pos),  pairs))
    return false;

  _langPairs.Reserve(_langPairs.Size());
  for (int i = 0; i < pairs.Size(); i++)
  {
    CTextConfigPair textConfigPair = pairs[i];
    CLangPair langPair;
    if (!HexStringToNumber(textConfigPair.ID, langPair.Value))
      return false;
    langPair.String = textConfigPair.String;
    _langPairs.Add(langPair);
  }
  _langPairs.Sort(CompareLangItems, NULL);
  return true;
}

int CLang::FindItem(UInt32 value) const
{
  int left = 0, right = _langPairs.Size(); 
  while (left != right)
  {
    UInt32 mid = (left + right) / 2;
    UInt32 midValue = _langPairs[mid].Value;
    if (value == midValue)
      return mid;
    if (value < midValue)
      right = mid;
    else
      left = mid + 1;
  }
  return -1;
}

bool CLang::GetMessage(UInt32 value, UString &message) const
{
  int index =  FindItem(value);
  if (index < 0)
    return false;
  message = _langPairs[index].String;
  return true;
}