OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
StdCompiler.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 #include "C4Include.h"
17 #include "lib/StdCompiler.h"
18 
19 // *** StdCompiler
20 
21 void StdCompiler::Warn(const char *szWarning, ...)
22 {
23  // Got warning callback?
24  if (!pWarnCB) return;
25  // Format message
26  va_list args; va_start(args, szWarning);
27  StdStrBuf Msg; Msg.FormatV(szWarning, args);
28  // do callback
29  (*pWarnCB)(pWarnData, getPosition().getData(), Msg.getData());
30 }
31 
33 {
34  switch (eSep)
35  {
36  case SEP_SEP: return ',';
37  case SEP_SEP2: return ';';
38  case SEP_SET: return '=';
39  case SEP_PART: return '.';
40  case SEP_PART2: return ':';
41  case SEP_PLUS: return '+';
42  case SEP_START: return '(';
43  case SEP_END: return ')';
44  case SEP_START2: return '[';
45  case SEP_END2: return ']';
46  case SEP_VLINE: return '|';
47  case SEP_DOLLAR: return '$';
48  default: assert(!"Unhandled Separator value");
49  }
50  return ' ';
51 }
52 
54 {
55  switch (eType)
56  {
57  case RCT_Escaped: return c == '"' || !c || c == '\n' || c == '\r';
58  case RCT_All: return !c || c == '\n' || c == '\r';
59  // '-' is needed for Layers in Scenario.txt (C4NameList) and other Material-Texture combinations
60  case RCT_Idtf: case RCT_IdtfAllowEmpty: case RCT_ID: return !isalnum((unsigned char)c) && c != '_' && c != '-';
61  }
62  // unreachable
63  return true;
64 }
65 
66 // *** StdCompilerBinWrite
67 
68 void StdCompilerBinWrite::DWord(int32_t &rInt) { WriteValue(rInt); }
69 void StdCompilerBinWrite::DWord(uint32_t &rInt) { WriteValue(rInt); }
70 void StdCompilerBinWrite::Word(int16_t &rShort) { WriteValue(rShort); }
71 void StdCompilerBinWrite::Word(uint16_t &rShort) { WriteValue(rShort); }
72 void StdCompilerBinWrite::Byte(int8_t &rByte) { WriteValue(rByte); }
73 void StdCompilerBinWrite::Byte(uint8_t &rByte) { WriteValue(rByte); }
74 void StdCompilerBinWrite::Boolean(bool &rBool) { WriteValue(rBool); }
75 void StdCompilerBinWrite::Character(char &rChar) { WriteValue(rChar); }
76 void StdCompilerBinWrite::String(char *szString, size_t iMaxLength, RawCompileType eType)
77 {
78  WriteData(szString, strlen(szString) + 1);
79 }
80 void StdCompilerBinWrite::String(char **pszString, RawCompileType eType)
81 {
82  if (*pszString)
83  WriteData(*pszString, strlen(*pszString) + 1);
84  else
85  WriteValue('\0');
86 }
87 
88 void StdCompilerBinWrite::String(std::string &str, RawCompileType type)
89 {
90  WriteData(str.c_str(), str.size() + 1);
91 }
92 
93 template <class T>
94 void StdCompilerBinWrite::WriteValue(const T &rValue)
95 {
96  // Copy data
97  if (fSecondPass)
98  *getMBufPtr<T>(Buf, iPos) = rValue;
99  iPos += sizeof(rValue);
100 }
101 
102 void StdCompilerBinWrite::WriteData(const void *pData, size_t iSize)
103 {
104  // Copy data
105  if (fSecondPass)
106  Buf.Write(pData, iSize, iPos);
107  iPos += iSize;
108 }
109 
110 void StdCompilerBinWrite::Raw(void *pData, size_t iSize, RawCompileType eType)
111 {
112  // Copy data
113  if (fSecondPass)
114  Buf.Write(pData, iSize, iPos);
115  iPos += iSize;
116 }
117 
119 {
120  fSecondPass = false; iPos = 0;
121 }
122 
124 {
125  Buf.New(iPos);
126  fSecondPass = true; iPos = 0;
127 }
128 
129 // *** StdCompilerBinRead
130 
131 void StdCompilerBinRead::DWord(int32_t &rInt) { ReadValue(rInt); }
132 void StdCompilerBinRead::DWord(uint32_t &rInt) { ReadValue(rInt); }
133 void StdCompilerBinRead::Word(int16_t &rShort) { ReadValue(rShort); }
134 void StdCompilerBinRead::Word(uint16_t &rShort) { ReadValue(rShort); }
135 void StdCompilerBinRead::Byte(int8_t &rByte) { ReadValue(rByte); }
136 void StdCompilerBinRead::Byte(uint8_t &rByte) { ReadValue(rByte); }
137 void StdCompilerBinRead::Boolean(bool &rBool) { ReadValue(rBool); }
138 void StdCompilerBinRead::Character(char &rChar) { ReadValue(rChar); }
139 void StdCompilerBinRead::String(char *szString, size_t iMaxLength, RawCompileType eType)
140 {
141  // At least one byte data needed
142  if (iPos >= Buf.getSize())
143  { excEOF(); return; }
144  // Copy until no data left
145  char *pPos = szString;
146  while ((*pPos++ = *getBufPtr<char>(Buf, iPos++)))
147  if (iPos >= Buf.getSize())
148  { excEOF(); return; }
149  else if (pPos > szString + iMaxLength)
150  { excCorrupt("string too long"); return; }
151 }
152 
153 void StdCompilerBinRead::String(char **pszString, RawCompileType eType)
154 {
155  // At least one byte data needed
156  if (iPos >= Buf.getSize())
157  { excEOF(); return; }
158  int iStart = iPos;
159  // Search string end
160  while (*getBufPtr<char>(Buf, iPos++))
161  if (iPos >= Buf.getSize())
162  { excEOF(); return; }
163  // Allocate and copy data
164  *pszString = (char *) malloc(iPos - iStart);
165  memcpy(*pszString, Buf.getPtr(iStart), iPos - iStart);
166 }
167 
168 void StdCompilerBinRead::String(std::string &str, RawCompileType type)
169 {
170  // At least one byte data needed
171  if (iPos >= Buf.getSize())
172  {
173  excEOF(); return;
174  }
175  int iStart = iPos;
176  // Search string end
177  while (*getBufPtr<char>(Buf, iPos++))
178  if (iPos >= Buf.getSize())
179  {
180  excEOF(); return;
181  }
182  // Copy data
183  str.assign(getBufPtr<char>(Buf, iStart), getBufPtr<char>(Buf, iPos));
184 }
185 
186 void StdCompilerBinRead::Raw(void *pData, size_t iSize, RawCompileType eType)
187 {
188  if (iPos + iSize > Buf.getSize())
189  { excEOF(); return; }
190  // Copy data
191  memcpy(pData, Buf.getPtr(iPos), iSize);
192  iPos += iSize;
193 }
194 
196 {
197  return FormatString("byte %ld", static_cast<unsigned long>(iPos));
198 }
199 
200 template <class T>
201 inline void StdCompilerBinRead::ReadValue(T &rValue)
202 {
203  // Pufferüberhang prüfen
204  if (iPos + sizeof(T) > Buf.getSize())
205  { excEOF(); return; }
206  // Kopieren
207  rValue = *getBufPtr<T>(Buf, iPos);
208  iPos += sizeof(T);
209 }
210 
212 {
213  iPos = 0;
214 }
215 
216 // *** StdCompilerINIWrite
217 
218 bool StdCompilerINIWrite::Name(const char *szName)
219 {
220  // Sub-Namesections exist, so it's a section. Write name if not already done so.
221  if (fPutName) PutName(true);
222  // Push struct
223  Naming *pnNaming = new Naming;
224  pnNaming->Name.Copy(szName);
225  pnNaming->Parent = pNaming;
226  pNaming = pnNaming;
227  iDepth++;
228  // Done
229  fPutName = true; fInSection = false;
230  return true;
231 }
232 
234 {
235  // Append newline
236  if (!fPutName && !fInSection)
237  Buf.Append("\r\n");
238  fPutName = false;
239  // Note this makes it impossible to distinguish an empty name section from
240  // a non-existing name section.
241 
242  // Pop
243  assert(iDepth);
244  Naming *poNaming = pNaming;
245  pNaming = poNaming->Parent;
246  delete poNaming;
247  iDepth--;
248  // We're inside a section now
249  fInSection = true;
250 }
251 
253 {
254  if (fInSection)
255  {
256  // Re-put section name
257  PutName(true);
258  }
259  else
260  {
261  PrepareForValue();
263  }
264  return true;
265 }
266 
267 void StdCompilerINIWrite::DWord(int32_t &rInt)
268 {
269  PrepareForValue();
270  Buf.AppendFormat("%d", rInt);
271 }
272 void StdCompilerINIWrite::DWord(uint32_t &rInt)
273 {
274  PrepareForValue();
275  Buf.AppendFormat("%u", rInt);
276 }
277 void StdCompilerINIWrite::Word(int16_t &rInt)
278 {
279  PrepareForValue();
280  Buf.AppendFormat("%d", rInt);
281 }
282 void StdCompilerINIWrite::Word(uint16_t &rInt)
283 {
284  PrepareForValue();
285  Buf.AppendFormat("%u", rInt);
286 }
287 void StdCompilerINIWrite::Byte(int8_t &rByte)
288 {
289  PrepareForValue();
290  Buf.AppendFormat("%d", rByte);
291 }
292 void StdCompilerINIWrite::Byte(uint8_t &rInt)
293 {
294  PrepareForValue();
295  Buf.AppendFormat("%u", rInt);
296 }
298 {
299  PrepareForValue();
300  Buf.Append(rBool ? "true" : "false");
301 }
303 {
304  PrepareForValue();
305  Buf.AppendFormat("%c", rChar);
306 }
307 
308 void StdCompilerINIWrite::String(char *szString, size_t iMaxLength, RawCompileType eType)
309 {
310  PrepareForValue();
311  switch (eType)
312  {
313  case RCT_Escaped:
314  WriteEscaped(szString, szString + strlen(szString));
315  break;
316  case RCT_All:
317  case RCT_Idtf:
318  case RCT_IdtfAllowEmpty:
319  case RCT_ID:
320  Buf.Append(szString);
321  }
322 }
323 
324 void StdCompilerINIWrite::String(char **pszString, RawCompileType eType)
325 {
326  char cNull = '\0';
327  String(*pszString ? *pszString : &cNull, 0, eType);
328 }
329 
330 void StdCompilerINIWrite::Raw(void *pData, size_t iSize, RawCompileType eType)
331 {
332  switch (eType)
333  {
334  case RCT_Escaped:
335  WriteEscaped(reinterpret_cast<char *>(pData), reinterpret_cast<char *>(pData) + iSize);
336  break;
337  case RCT_All:
338  case RCT_Idtf:
339  case RCT_IdtfAllowEmpty:
340  case RCT_ID:
341  Buf.Append(reinterpret_cast<char *>(pData), iSize);
342  }
343 }
344 
345 void StdCompilerINIWrite::String(std::string &str, RawCompileType type)
346 {
347  Raw(&str[0], str.size(), type);
348 }
349 
351 {
352  pNaming = nullptr;
353  fPutName = false;
354  iDepth = 0;
355  fInSection = false;
356  Buf.Clear();
357 }
358 
360 {
361  // Ensure all namings were closed properly
362  assert(!iDepth);
363 }
364 
366 {
367  // Put name (value-type), if not already done so
368  if (fPutName) PutName(false);
369  // No data allowed inside of sections
370  assert(!fInSection);
371  // No values allowed on top-level - must be contained in at least one section
372  assert(iDepth > 1);
373 }
374 
375 void StdCompilerINIWrite::WriteEscaped(const char *szString, const char *pEnd)
376 {
377  Buf.AppendChar('"');
378  // Try to write chunks as huge as possible of "normal" chars.
379  // Note this excludes '\0', so the standard Append() can be used.
380  const char *pStart, *pPos; pStart = pPos = szString;
381  bool fLastNumEscape = false; // catch "\1""1", which must become "\1\61"
382  for (; pPos < pEnd; pPos++)
383  if (!isprint((unsigned char)(unsigned char) *pPos) || *pPos == '\\' || *pPos == '"' || (fLastNumEscape && isdigit((unsigned char)*pPos)))
384  {
385  // Write everything up to this point
386  if (pPos - pStart) Buf.Append(pStart, pPos - pStart);
387  // Escape
388  fLastNumEscape = false;
389  switch (*pPos)
390  {
391  case '\a': Buf.Append(R"(\a)"); break;
392  case '\b': Buf.Append(R"(\b)"); break;
393  case '\f': Buf.Append(R"(\f)"); break;
394  case '\n': Buf.Append(R"(\n)"); break;
395  case '\r': Buf.Append(R"(\r)"); break;
396  case '\t': Buf.Append(R"(\t)"); break;
397  case '\v': Buf.Append(R"(\v)"); break;
398  case '\"': Buf.Append(R"(\")"); break;
399  case '\\': Buf.Append(R"(\\)"); break;
400  default:
401  Buf.AppendFormat(R"(\%o)", *reinterpret_cast<const unsigned char *>(pPos));
402  fLastNumEscape = true;
403  }
404  // Set pointer
405  pStart = pPos + 1;
406  }
407  else
408  fLastNumEscape = false;
409  // Write the rest
410  if (pEnd - pStart) Buf.Append(pStart, pEnd - pStart);
411  Buf.AppendChar('"');
412 }
413 
415 {
416  // Do not indent level 1 (level 0 values aren't allowed - see above)
417  int iIndent = iDepth - 1;
418  // Sections are indented more, even though they belong to this level
419  if (!fSection) iIndent--;
420  // Do indention
421  if (iIndent <= 0) return;
422  Buf.AppendChars(' ', iIndent * 2);
423 }
424 
425 void StdCompilerINIWrite::PutName(bool fSection)
426 {
427  if (fSection && Buf.getLength())
428  Buf.Append("\r\n");
429  WriteIndent(fSection);
430  // Put name
431  if (fSection)
432  Buf.AppendFormat("[%s]\r\n", pNaming->Name.getData());
433  else
434  Buf.AppendFormat("%s=", pNaming->Name.getData());
435  // Set flag
436  fPutName = false;
437 }
438 
439 // *** StdCompilerINIRead
440 
442 
444 {
445  FreeNameTree();
446 }
447 
448 // Naming
449 bool StdCompilerINIRead::Name(const char *szName)
450 {
451  // Increase depth
452  iDepth++;
453  // Parent category virtual?
454  if (iDepth - 1 > iRealDepth)
455  return false;
456  // Name must be alphanumerical and non-empty (force it)
457  if (!isalpha((unsigned char)*szName))
458  { assert(false); return false; }
459  for (const char *p = szName + 1; *p; p++)
460  // C4Update needs Name**...
461  if (!isalnum((unsigned char)*p) && *p != ' ' && *p != '_' && *p != '*')
462  { assert(false); return false; }
463  // Search name
464  NameNode *pNode;
465  for (pNode = pName->FirstChild; pNode; pNode = pNode->NextChild)
466  if (pNode->Pos && pNode->Name == szName)
467  break;
468  // Not found?
469  if (!pNode)
470  {
471  NotFoundName = szName;
472  return false;
473  }
474  // Save tree position, indicate success
475  pName = pNode;
476  pPos = pName->Pos;
477  pReenter = nullptr;
478  iRealDepth++;
479  return true;
480 }
482 {
483  assert(iDepth > 0);
484  if (iRealDepth == iDepth)
485  {
486  // Remove childs
487  for (NameNode *pNode = pName->FirstChild, *pNext; pNode; pNode = pNext)
488  {
489  // Report unused entries
490  if (pNode->Pos && !fBreak)
491  Warn(R"(Unexpected %s "%s"!)", pNode->Section ? "section" : "value", pNode->Name.getData());
492  // delete node
493  pNext = pNode->NextChild;
494  delete pNode;
495  }
496  // Remove name so it won't be found again
497  NameNode *pParent = pName->Parent;
500  delete pName;
501  // Go up
502  pName = pParent;
503  iRealDepth--;
504  }
505  // Decrease depth
506  iDepth--;
507  // This is the middle of nowhere
508  pPos = nullptr; pReenter = nullptr;
509 }
510 
511 bool StdCompilerINIRead::FollowName(const char *szName)
512 {
513  // Current naming virtual?
514  if (iDepth > iRealDepth)
515  return false;
516  // Next section must be the one
517  if (!pName->NextChild || pName->NextChild->Name != szName)
518  {
519  // End current naming
520  NameEnd();
521  // Go into virtual naming
522  iDepth++;
523  return false;
524  }
525  // End current naming
526  NameEnd();
527  // Start new one
528  Name(szName);
529  // Done
530  return true;
531 }
532 
533 // Separators
535 {
536  if (iDepth > iRealDepth) return false;
537  // In section?
538  if (pName->Section)
539  {
540  // Store current name, search another section with the same name
541  StdStrBuf CurrName = pName->Name;
542  NameEnd();
543  return Name(CurrName.getData());
544  }
545  // Position saved back from separator mismatch?
546  if (pReenter) { pPos = pReenter; pReenter = nullptr; }
547  // Nothing to read?
548  if (!pPos) return false;
549  // Read (while skipping over whitespace)
550  SkipWhitespace();
551  // Separator mismatch? Let all read attempts fail until the correct separator is found or the naming ends.
552  if (*pPos != SeparatorToChar(eSep)) { pReenter = pPos; pPos = nullptr; return false; }
553  // Go over separator, success
554  pPos++;
555  return true;
556 }
557 
559 {
560  // Position saved back from separator mismatch?
561  if (pReenter) { pPos = pReenter; pReenter = nullptr; }
562 }
563 
564 int StdCompilerINIRead::NameCount(const char *szName)
565 {
566  // not in virtual naming
567  if (iDepth > iRealDepth || !pName) return 0;
568  // count within current name
569  int iCount = 0;
570  NameNode *pNode;
571  for (pNode = pName->FirstChild; pNode; pNode = pNode->NextChild)
572  // if no name is given, all valid subsections are counted
573  if (pNode->Pos && (!szName || pNode->Name == szName))
574  ++iCount;
575  return iCount;
576 }
577 
578 const char *StdCompilerINIRead::GetNameByIndex(size_t idx) const
579 {
580  // not in virtual naming
581  if (iDepth > iRealDepth || !pName) return nullptr;
582  // count within current name
583  NameNode *pNode;
584  for (pNode = pName->FirstChild; pNode; pNode = pNode->NextChild)
585  // all valid subsections are counted
586  if (pNode->Pos)
587  if (!idx--)
588  return pNode->Name.getData();
589  // index out of range
590  return nullptr;
591 }
592 
593 // Various data readers
594 void StdCompilerINIRead::DWord(int32_t &rInt)
595 {
596  rInt = ReadNum();
597 }
598 void StdCompilerINIRead::DWord(uint32_t &rInt)
599 {
600  rInt = ReadUNum();
601 }
602 void StdCompilerINIRead::Word(int16_t &rShort)
603 {
604  const int MIN = -(1 << 15), MAX = (1 << 15) - 1;
605  int iNum = ReadNum();
606  if (iNum < MIN || iNum > MAX)
607  Warn("number out of range (%d to %d): %d ", MIN, MAX, iNum);
608  rShort = Clamp(iNum, MIN, MAX);
609 }
610 void StdCompilerINIRead::Word(uint16_t &rShort)
611 {
612  const unsigned int MIN = 0, MAX = (1 << 16) - 1;
613  unsigned int iNum = ReadUNum();
614  if (iNum > MAX)
615  Warn("number out of range (%u to %u): %u ", MIN, MAX, iNum);
616  rShort = Clamp(iNum, MIN, MAX);
617 }
618 void StdCompilerINIRead::Byte(int8_t &rByte)
619 {
620  const int MIN = -(1 << 7), MAX = (1 << 7) - 1;
621  int iNum = ReadNum();
622  if (iNum < MIN || iNum > MAX)
623  Warn("number out of range (%d to %d): %d ", MIN, MAX, iNum);
624  rByte = Clamp(iNum, MIN, MAX);
625 }
626 void StdCompilerINIRead::Byte(uint8_t &rByte)
627 {
628  const unsigned int MIN = 0, MAX = (1 << 8) - 1;
629  unsigned int iNum = ReadUNum();
630  if (iNum > MAX)
631  Warn("number out of range (%u to %u): %u ", MIN, MAX, iNum);
632  rByte = Clamp(iNum, MIN, MAX);
633 }
635 {
636  if (!pPos) { notFound("Boolean"); return; }
637  if (*pPos == '1' && !isdigit((unsigned char)*(pPos+1)))
638  { rBool = true; pPos ++; }
639  else if (*pPos == '0' && !isdigit((unsigned char)*(pPos+1)))
640  { rBool = false; pPos ++; }
641  else if (SEqual2(pPos, "true"))
642  { rBool = true; pPos += 4; }
643  else if (SEqual2(pPos, "false"))
644  { rBool = false; pPos += 5; }
645  else
646  { notFound("Boolean"); return; }
647 }
649 {
650  if (!pPos || !isalpha((unsigned char)*pPos))
651  { notFound("Character"); return; }
652  rChar = *pPos++;
653 }
654 void StdCompilerINIRead::String(char *szString, size_t iMaxLength, RawCompileType eType)
655 {
656  // Read data
657  StdBuf Buf = ReadString(iMaxLength, eType, true);
658  // Copy
659  SCopy(getBufPtr<char>(Buf), szString, iMaxLength);
660 }
661 void StdCompilerINIRead::String(char **pszString, RawCompileType eType)
662 {
663  // Get length
664  size_t iLength = GetStringLength(eType);
665  // Read data
666  StdBuf Buf = ReadString(iLength, eType, true);
667  // Set
668  *pszString = reinterpret_cast<char *>(Buf.GrabPointer());
669 }
670 void StdCompilerINIRead::String(std::string &str, RawCompileType type)
671 {
672  // Get length
673  size_t iLength = GetStringLength(type);
674  // Read data
675  StdBuf Buf = ReadString(iLength, type, true);
676  str = getBufPtr<char>(Buf);
677 }
678 void StdCompilerINIRead::Raw(void *pData, size_t iSize, RawCompileType eType)
679 {
680  // Read data
681  StdBuf Buf = ReadString(iSize, eType, false);
682  // Correct size?
683  if (Buf.getSize() != iSize)
684  Warn("got %u bytes raw data, but %u bytes expected!", Buf.getSize(), iSize);
685  // Copy
686  MemCopy(Buf.getData(), pData, iSize);
687 }
688 
689 uint32_t StdCompilerINIRead::getLineNumberOfPos(const char *pos) const
690 {
691  // Figure out quickly whether we already know which line this is
692  auto entry = std::lower_bound(lineBreaks.begin(), lineBreaks.end(), pos);
693  if (entry != lineBreaks.end())
694  {
695  return std::distance(lineBreaks.begin(), entry) + 1;
696  }
697  // Otherwise search through the buffer until we find out, filling the
698  // cache in the process
699  const char *cursor = Buf.getData();
700  if (!lineBreaks.empty())
701  cursor = *(lineBreaks.end() - 1) + 1;
702  for (;;)
703  {
704  if (*cursor == '\0' || *cursor == '\n')
705  {
706  lineBreaks.push_back(cursor);
707 
708  // If we're at the end of the file or have found the line break
709  // past the requested position, we're done for now
710  if (*cursor == '\0' || pos < cursor)
711  {
712  break;
713  }
714  }
715  ++cursor;
716  }
717  return std::distance(lineBreaks.begin(),
718  std::lower_bound(lineBreaks.begin(), lineBreaks.end(), pos)) + 1;
719 }
720 
722 {
723  if (pPos)
724  return FormatString("line %d", getLineNumberOfPos(pPos));
725  else if (iDepth == iRealDepth)
726  return FormatString(pName->Section ? R"(section "%s", after line %d)" : R"(value "%s", line %d)", pName->Name.getData(), getLineNumberOfPos(pName->Pos));
727  else if (iRealDepth)
728  return FormatString(R"(missing value/section "%s" inside section "%s" (line %d))", NotFoundName.getData(), pName->Name.getData(), getLineNumberOfPos(pName->Pos));
729  else
730  return FormatString(R"(missing value/section "%s")", NotFoundName.getData());
731 }
732 
734 {
735  // Already running? This may happen if someone confuses Compile with Value.
736  assert(!iDepth && !iRealDepth && !pNameRoot);
737  // Create tree
738  CreateNameTree();
739  // Start must be inside a section
740  iDepth = iRealDepth = 0;
741  pPos = nullptr; pReenter = nullptr;
742 }
744 {
745  assert(!iDepth && !iRealDepth);
746  FreeNameTree();
747 }
748 
750 {
751  FreeNameTree();
752  // Create root node
753  pName = pNameRoot = new NameNode();
754  // No input? Stop
755  if (!Buf) return;
756  // Start scanning
757  pPos = Buf.getPtr(0);
758  while (*pPos)
759  {
760  // Go over whitespace
761  int iIndent = 0;
762  while (*pPos == ' ' || *pPos == '\t')
763  { pPos++; iIndent++; }
764  // Name/Section?
765  bool fSection = *pPos == '[' && isalpha((unsigned char)*(pPos+1));
766  if (fSection || isalpha((unsigned char)*pPos))
767  {
768  // Treat values as if they had more indention
769  // (so they become children of sections on the same level)
770  if (!fSection) iIndent++; else pPos++;
771  // Go up in tree structure if there is less indention
772  while (pName->Parent && pName->Indent >= iIndent)
773  pName = pName->Parent;
774  // Copy name
775  StdStrBuf Name;
776  while (isalnum((unsigned char)*pPos) || *pPos == ' ' || *pPos == '_')
777  Name.AppendChar(*pPos++);
778  while (*pPos == ' ' || *pPos == '\t') pPos++;
779  if ( *pPos != (fSection ? ']' : '=') )
780  // Warn, ignore
781  Warn(isprint((unsigned char)*pPos) ? "Unexpected character ('%c'): %s ignored" : "Unexpected character ('0x%02x'): %s ignored", unsigned(*pPos), fSection ? "section" : "value");
782  else
783  {
784  pPos++;
785  // Create new node
786  NameNode *pPrev = pName->LastChild;
787  pName =
788  pName->LastChild =
790  new NameNode(pName);
791  pName->PrevChild = pPrev;
792  pName->Name.Take(std::move(Name));
793  pName->Pos = pPos;
794  pName->Indent = iIndent;
795  pName->Section = fSection;
796  // Values don't have children (even if the indention looks like it)
797  if (!fSection)
798  pName = pName->Parent;
799  }
800  }
801  // Skip line
802  while (*pPos && (*pPos != '\n' && *pPos != '\r'))
803  pPos++;
804  while (*pPos == '\n' || *pPos == '\r')
805  pPos++;
806  }
807  // Set pointer back
808  pName = pNameRoot;
809 }
810 
812 {
813  // free all nodes
815  pName = pNameRoot = nullptr;
816 }
817 
819 {
820  NameNode *pNode = pDelNode;
821  while (pNode)
822  {
823  if (pNode->FirstChild)
824  pNode = pNode->FirstChild;
825  else
826  {
827  NameNode *pDelete = pNode;
828  if (pDelete == pDelNode) { delete pDelete; break; }
829  if (pNode->NextChild)
830  pNode = pNode->NextChild;
831  else
832  {
833  pNode = pNode->Parent;
834  if (pNode) pNode->FirstChild = nullptr;
835  }
836  delete pDelete;
837  }
838  }
839 }
840 
842 {
843  while (*pPos == ' ' || *pPos == '\t')
844  pPos++;
845 }
846 
848 {
849  while (*pPos == '+' || *pPos == '-' || isdigit((unsigned char)*pPos))
850  pPos++;
851 }
852 
854 {
855  if (!pPos)
856  { notFound("Number"); return 0; }
857  // Skip whitespace
858  SkipWhitespace();
859  // Read number. If this breaks, Günther is to blame!
860  const char *pnPos = pPos;
861  long iNum = strtol(pPos, const_cast<char **>(&pnPos), 10);
862  // Could not read?
863  if (!iNum && pnPos == pPos)
864  { notFound("Number"); return 0; }
865  // Get over it
866  pPos = pnPos;
867  return iNum;
868 }
869 
871 {
872  if (!pPos)
873  { notFound("Number"); return 0; }
874  // Skip whitespace
875  SkipWhitespace();
876  // Read number. If this breaks, Günther is to blame!
877  const char *pnPos = pPos;
878  unsigned long iNum = strtoul(pPos, const_cast<char **>(&pnPos), 10);
879  // Could not read?
880  if (!iNum && pnPos == pPos)
881  { notFound("Number"); return 0; }
882  // Get over it
883  pPos = pnPos;
884  return iNum;
885 }
886 
888 {
889  // Excpect valid position
890  if (!pPos)
891  { notFound("String"); return 0; }
892  // Skip whitespace
893  SkipWhitespace();
894  // Save position
895  const char *pStart = pPos;
896  // Escaped? Go over '"'
897  if (eRawType == RCT_Escaped && *pPos++ != '"')
898  { notFound("Escaped string"); return 0; }
899  // Search end of string
900  size_t iLength = 0;
901  while (!TestStringEnd(eRawType))
902  {
903  // Read a character (we're just counting atm)
904  if (eRawType == RCT_Escaped)
905  ReadEscapedChar();
906  else
907  pPos++;
908  // Count it
909  iLength++;
910  }
911  // Reset position, return the length
912  pPos = pStart;
913  return iLength;
914 }
915 
916 StdBuf StdCompilerINIRead::ReadString(size_t iLength, RawCompileType eRawType, bool fAppendNull)
917 {
918  // Excpect valid position
919  if (!pPos)
920  { notFound("String"); return StdBuf(); }
921  // Skip whitespace
922  SkipWhitespace();
923  // Escaped? Go over '"'
924  if (eRawType == RCT_Escaped && *pPos++ != '"')
925  { notFound("Escaped string"); return StdBuf(); }
926  // Create buffer
927  StdBuf OutBuf; OutBuf.New(iLength + (fAppendNull ? sizeof('\0') : 0));
928  // Read
929  char *pOut = getMBufPtr<char>(OutBuf);
930  while (iLength && !TestStringEnd(eRawType))
931  {
932  // Read a character
933  if (eRawType == RCT_Escaped)
934  *pOut++ = ReadEscapedChar();
935  else
936  *pOut++ = *pPos++;
937  // Count it
938  iLength--;
939  }
940  // Escaped: Go over '"'
941  if (eRawType == RCT_Escaped)
942  {
943  while (*pPos != '"')
944  {
945  if (!*pPos || *pPos == '\n' || *pPos == '\r')
946  {
947  Warn("string not terminated!");
948  pPos--;
949  break;
950  }
951  pPos++;
952  }
953  pPos++;
954  }
955  // Nothing read? Identifiers need to be non-empty
956  if (pOut == OutBuf.getData() && (eRawType == RCT_Idtf || eRawType == RCT_ID))
957  { notFound("String"); return StdBuf(); }
958  // Append null
959  if (fAppendNull)
960  *pOut = '\0';
961  // Shrink, if less characters were read
962  OutBuf.Shrink(iLength);
963  // Done
964  return OutBuf;
965 }
966 
968 {
969  // Catch some no-noes like \0, \n etc.
970  if (*pPos >= 0 && iscntrl((unsigned char)*pPos))
971  {
972  Warn("Nonprintable character found in string: %02x", static_cast<unsigned char>(*pPos));
973  return *pPos;
974  }
975  // Not escaped? Just return it
976  if (*pPos != '\\') return *pPos++;
977  // What type of escape?
978  switch (*++pPos)
979  {
980  case 'a': pPos++; return '\a';
981  case 'b': pPos++; return '\b';
982  case 'f': pPos++; return '\f';
983  case 'n': pPos++; return '\n';
984  case 'r': pPos++; return '\r';
985  case 't': pPos++; return '\t';
986  case 'v': pPos++; return '\v';
987  case '\'': pPos++; return '\'';
988  case '"': pPos++; return '"';
989  case '\\': pPos++; return '\\';
990  case '?': pPos++; return '?';
991  case 'x':
992  // Treat '\x' as 'x' - damn special cases
993  if (!isxdigit((unsigned char)*++pPos))
994  return 'x';
995  else
996  {
997  // Read everything that looks like it might be hexadecimal - MSVC does it this way, so do not sue me.
998  int iCode = 0;
999  do
1000  { iCode = iCode * 16 + (isdigit((unsigned char)*pPos) ? *pPos - '0' : *pPos - 'a' + 10); pPos++; }
1001  while (isxdigit((unsigned char)*pPos));
1002  // Done. Don't bother to check the range (we aren't doing anything mission-critical here, are we?)
1003  return char(iCode);
1004  }
1005  default:
1006  // Not octal? Let it pass through.
1007  if (!isdigit((unsigned char)*pPos) || *pPos >= '8')
1008  return *pPos++;
1009  else
1010  {
1011  // Read it the octal way.
1012  int iCode = 0;
1013  do
1014  { iCode = iCode * 8 + (*pPos - '0'); pPos++;}
1015  while (isdigit((unsigned char)*pPos) && *pPos < '8');
1016  // Done. See above.
1017  return char(iCode);
1018  }
1019  }
1020  // unreachable
1021  assert (false);
1022 }
1023 
1024 void StdCompilerINIRead::notFound(const char *szWhat)
1025 {
1026  excNotFound("%s expected", szWhat);
1027 }
1028 
1029 void StdCompilerWarnCallback(void *pData, const char *szPosition, const char *szError)
1030 {
1031  const char *szName = reinterpret_cast<const char *>(pData);
1032  if (!szPosition || !*szPosition)
1033  DebugLogF("WARNING: %s (in %s)", szError, szName);
1034  else
1035  DebugLogF("WARNING: %s (in %s, %s)", szError, szPosition, szName);
1036 }
const char * getData() const
Definition: StdBuf.h:442
void WriteData(const void *pData, size_t iSize)
void WriteEscaped(const char *szString, const char *pEnd)
bool Name(const char *szName) override
const void * getData() const
Definition: StdBuf.h:99
Definition: StdBuf.h:29
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:130
void Boolean(bool &rBool) override
void Boolean(bool &rBool) override
void String(char *szString, size_t iMaxLength, RawCompileType eType=RCT_Escaped) override
void Character(char &rChar) override
void excCorrupt(const char *szMessage,...)
Definition: StdCompiler.h:249
void WriteIndent(bool fSectionName)
void Boolean(bool &rBool) override
Definition: StdCompiler.cpp:74
void Clear()
Definition: StdBuf.h:466
void StdCompilerWarnCallback(void *pData, const char *szPosition, const char *szError)
StdBuf ReadString(size_t iLength, RawCompileType eTyped, bool fAppendNull=true)
StdStrBuf getPosition() const override
void Word(int16_t &rShort) override
void Word(int16_t &rShort) override
void * GrabPointer()
Definition: StdBuf.h:133
void excEOF(const char *szMessage="EOF",...)
Definition: StdCompiler.h:243
const char * pPos
Definition: StdCompiler.h:701
void String(char *szString, size_t iMaxLength, RawCompileType eType=RCT_Escaped) override
Definition: StdCompiler.cpp:76
virtual StdStrBuf getPosition() const
Definition: StdCompiler.h:151
void Byte(int8_t &rByte) override
Definition: StdCompiler.cpp:72
void Byte(int8_t &rByte) override
void AppendChars(char cChar, size_t iCnt)
Definition: StdBuf.h:582
size_t getSize() const
Definition: StdBuf.h:101
void NameEnd(bool fBreak=false) override
T Clamp(T bval, T lbound, T rbound)
Definition: Standard.h:44
void String(char *szString, size_t iMaxLength, RawCompileType eType=RCT_Escaped) override
void AppendFormat(const char *szFmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: StdBuf.cpp:190
void Begin() override
void AppendChar(char cChar)
Definition: StdBuf.h:588
NameNode * pNameRoot
Definition: StdCompiler.h:692
bool Name(const char *szName) override
static char SeparatorToChar(Sep eSep)
Definition: StdCompiler.cpp:32
const char * GetNameByIndex(size_t idx) const override
void WriteValue(const T &rValue)
Definition: StdCompiler.cpp:94
bool DebugLogF(const char *strMessage...)
Definition: C4Log.cpp:278
void Raw(void *pData, size_t iSize, RawCompileType eType=RCT_Escaped) override
~StdCompilerINIRead() override
void End() override
void DWord(int32_t &rInt) override
Definition: StdCompiler.cpp:68
void Character(char &rChar) override
void FreeNameNode(NameNode *pNode)
StdCopyStrBuf NotFoundName
Definition: StdCompiler.h:707
void Take(char *pnData)
Definition: StdBuf.h:457
void Append(const char *pnData, size_t iChars)
Definition: StdBuf.h:519
unsigned long ReadUNum()
void Raw(void *pData, size_t iSize, RawCompileType eType=RCT_Escaped) override
static bool IsStringEnd(char c, RawCompileType eType)
Definition: StdCompiler.cpp:53
void Boolean(bool &rBool) override
void Shrink(size_t iShrink)
Definition: StdBuf.h:180
void BeginSecond() override
void excNotFound(const char *szMessage,...)
Definition: StdCompiler.h:233
void MemCopy(const void *lpMem1, void *lpMem2, size_t dwSize)
Definition: Standard.h:65
void Character(char &rChar) override
void Begin() override
void Byte(int8_t &rByte) override
void New(size_t inSize)
Definition: StdBuf.h:146
void Write(const void *pnData, size_t inSize, size_t iAt=0)
Definition: StdBuf.h:153
void notFound(const char *szWhat)
void Byte(int8_t &rByte) override
void DWord(int32_t &rInt) override
void Begin() override
void Warn(const char *szWarning,...)
Definition: StdCompiler.cpp:21
void Word(int16_t &rShort) override
Definition: StdCompiler.cpp:70
void NoSeparator() override
void Begin() override
bool Separator(Sep eSep) override
void End() override
void String(char *szString, size_t iMaxLength, RawCompileType eType=RCT_Escaped) override
void PutName(bool fSection)
bool SEqual2(const char *szStr1, const char *szStr2)
Definition: Standard.cpp:176
void FormatV(const char *szFmt, va_list args)
Definition: StdBuf.cpp:182
void Raw(void *pData, size_t iSize, RawCompileType eType=RCT_Escaped) override
const char * getPtr(size_t i) const
Definition: StdBuf.h:448
const void * getPtr(size_t i) const
Definition: StdBuf.h:104
bool Separator(Sep eSep) override
size_t getLength() const
Definition: StdBuf.h:445
StdStrBuf getPosition() const override
void DWord(int32_t &rInt) override
bool TestStringEnd(RawCompileType eType)
Definition: StdCompiler.h:722
bool FollowName(const char *szName) override
void Word(int16_t &rShort) override
void Copy()
Definition: StdBuf.h:467
NameNode * pName
Definition: StdCompiler.h:692
void DWord(int32_t &rInt) override
size_t GetStringLength(RawCompileType eTyped)
void NameEnd(bool fBreak=false) override
void Raw(void *pData, size_t iSize, RawCompileType eType=RCT_Escaped) override
int NameCount(const char *szName=nullptr) override
void ReadValue(T &rValue)
int iSize
Definition: TstC4NetIO.cpp:32
const char * pReenter
Definition: StdCompiler.h:704
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:270
void Character(char &rChar) override
Definition: StdCompiler.cpp:75