summaryrefslogtreecommitdiffstats
path: root/src/libs/7zip/win/CPP/Common/MyXml.cpp
blob: 8aa9ce8cda352b9cc649c94defcbd4281aac35ff (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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// MyXml.cpp

#include "StdAfx.h"

#include "MyXml.h"

static bool IsValidChar(char c)
{
  return
    c >= 'a' && c <= 'z' ||
    c >= 'A' && c <= 'Z' ||
    c >= '0' && c <= '9' ||
    c == '-';
}

static bool IsSpaceChar(char c)
{
  return (c == ' ' || c == '\t' || c == 0x0D || c == 0x0A);
}

#define SKIP_SPACES(s, pos) while (IsSpaceChar(s[pos])) pos++;

static bool ReadProperty(const AString &s, int &pos, CXmlProp &prop)
{
  prop.Name.Empty();
  prop.Value.Empty();
  for (; pos < s.Length(); pos++)
  {
    char c = s[pos];
    if (!IsValidChar(c))
      break;
    prop.Name += c;
  }
  
  if (prop.Name.IsEmpty())
    return false;

  SKIP_SPACES(s, pos);
  if (s[pos++] != '=')
    return false;

  SKIP_SPACES(s, pos);
  if (s[pos++] != '\"')
    return false;
  
  while (pos < s.Length())
  {
    char c = s[pos++];
    if (c == '\"')
      return true;
    prop.Value += c;
  }
  return false;
}

int CXmlItem::FindProperty(const AString &propName) const
{
  for (int i = 0; i < Props.Size(); i++)
    if (Props[i].Name == propName)
      return i;
  return -1;
}

AString CXmlItem::GetPropertyValue(const AString &propName) const
{
  int index = FindProperty(propName);
  if (index >= 0)
    return Props[index].Value;
  return AString();
}

bool CXmlItem::IsTagged(const AString &tag) const
{
  return (IsTag && Name == tag);
}

int CXmlItem::FindSubTag(const AString &tag) const
{
  for (int i = 0; i < SubItems.Size(); i++)
    if (SubItems[i].IsTagged(tag))
      return i;
  return -1;
}

AString CXmlItem::GetSubString() const
{
  if (SubItems.Size() == 1)
  {
    const CXmlItem &item = SubItems[0];
    if (!item.IsTag)
      return item.Name;
  }
  return AString();
}

AString CXmlItem::GetSubStringForTag(const AString &tag) const
{
  int index = FindSubTag(tag);
  if (index >= 0)
    return SubItems[index].GetSubString();
  return AString();
}

bool CXmlItem::ParseItems(const AString &s, int &pos, int numAllowedLevels)
{
  if (numAllowedLevels == 0)
    return false;
  SubItems.Clear();
  AString finishString = "</";
  for (;;)
  {
    SKIP_SPACES(s, pos);

    if (s.Mid(pos, finishString.Length()) == finishString)
      return true;
      
    CXmlItem item;
    if (!item.ParseItem(s, pos, numAllowedLevels - 1))
      return false;
    SubItems.Add(item);
  }
}

bool CXmlItem::ParseItem(const AString &s, int &pos, int numAllowedLevels)
{
  SKIP_SPACES(s, pos);

  int pos2 = s.Find('<', pos);
  if (pos2 < 0)
    return false;
  if (pos2 != pos)
  {
    IsTag = false;
    Name += s.Mid(pos, pos2 - pos);
    pos = pos2;
    return true;
  }
  IsTag = true;

  pos++;
  SKIP_SPACES(s, pos);

  for (; pos < s.Length(); pos++)
  {
    char c = s[pos];
    if (!IsValidChar(c))
      break;
    Name += c;
  }
  if (Name.IsEmpty() || pos == s.Length())
    return false;

  int posTemp = pos;
  for (;;)
  {
    SKIP_SPACES(s, pos);
    if (s[pos] == '/')
    {
      pos++;
      // SKIP_SPACES(s, pos);
      return (s[pos++] == '>');
    }
    if (s[pos] == '>')
    {
      if (!ParseItems(s, ++pos, numAllowedLevels))
        return false;
      AString finishString = AString("</") + Name + AString(">");
      if (s.Mid(pos, finishString.Length()) != finishString)
        return false;
      pos += finishString.Length();
      return true;
    }
    if (posTemp == pos)
      return false;

    CXmlProp prop;
    if (!ReadProperty(s, pos, prop))
      return false;
    Props.Add(prop);
    posTemp = pos;
  }
}

static bool SkipHeader(const AString &s, int &pos, const AString &startString, const AString &endString)
{
  SKIP_SPACES(s, pos);
  if (s.Mid(pos, startString.Length()) == startString)
  {
    pos = s.Find(endString, pos);
    if (pos < 0)
      return false;
    pos += endString.Length();
    SKIP_SPACES(s, pos);
  }
  return true;
}

bool CXml::Parse(const AString &s)
{
  int pos = 0;
  if (!SkipHeader(s, pos, "<?xml", "?>"))
    return false;
  if (!SkipHeader(s, pos, "<!DOCTYPE", ">"))
    return false;
  if (!Root.ParseItem(s, pos, 1000))
    return false;
  SKIP_SPACES(s, pos);
  return (pos == s.Length() && Root.IsTag);
}