other-licenses/7zstub/src/Common/TextConfig.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/other-licenses/7zstub/src/Common/TextConfig.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,137 @@
     1.4 +// Common/TextConfig.cpp
     1.5 +
     1.6 +#include "StdAfx.h"
     1.7 +
     1.8 +#include "Common/TextConfig.h"
     1.9 +
    1.10 +#include "Defs.h"
    1.11 +#include "Common/UTFConvert.h"
    1.12 +
    1.13 +static bool IsDelimitChar(char c)
    1.14 +{
    1.15 +  return (c == ' ' || c == 0x0A || c == 0x0D ||
    1.16 +      c == '\0' || c == '\t');
    1.17 +}
    1.18 +    
    1.19 +static AString GetIDString(const char *string, int &finishPos)
    1.20 +{
    1.21 +  AString result;
    1.22 +  for (finishPos = 0; true; finishPos++)
    1.23 +  {
    1.24 +    char c = string[finishPos];
    1.25 +    if (IsDelimitChar(c) || c == '=')
    1.26 +      return result;
    1.27 +    result += c;
    1.28 +  }
    1.29 +}
    1.30 +
    1.31 +static bool WaitNextLine(const AString &string, int &pos)
    1.32 +{
    1.33 +  for (;pos < string.Length(); pos++)
    1.34 +    if (string[pos] == 0x0A)
    1.35 +      return true;
    1.36 +  return false;
    1.37 +}
    1.38 +
    1.39 +static bool SkipSpaces(const AString &string, int &pos)
    1.40 +{
    1.41 +  for (;pos < string.Length(); pos++)
    1.42 +  {
    1.43 +    char c = string[pos];
    1.44 +    if (!IsDelimitChar(c))
    1.45 +    {
    1.46 +      if (c != ';')
    1.47 +        return true;
    1.48 +      if (!WaitNextLine(string, pos))
    1.49 +        return false;
    1.50 +    }
    1.51 +  }
    1.52 +  return false;
    1.53 +}
    1.54 +
    1.55 +bool GetTextConfig(const AString &string, CObjectVector<CTextConfigPair> &pairs)
    1.56 +{
    1.57 +  pairs.Clear();
    1.58 +  int pos = 0;
    1.59 +
    1.60 +  /////////////////////
    1.61 +  // read strings
    1.62 +
    1.63 +  while (true)
    1.64 +  {
    1.65 +    if (!SkipSpaces(string, pos))
    1.66 +      break;
    1.67 +    CTextConfigPair pair;
    1.68 +    int finishPos;
    1.69 +    AString temp = GetIDString(((const char *)string) + pos, finishPos);
    1.70 +    if (!ConvertUTF8ToUnicode(temp, pair.ID))
    1.71 +      return false;
    1.72 +    if (finishPos == 0)
    1.73 +      return false;
    1.74 +    pos += finishPos;
    1.75 +    if (!SkipSpaces(string, pos))
    1.76 +      return false;
    1.77 +    if (string[pos] != '=')
    1.78 +      return false;
    1.79 +    pos++;
    1.80 +    if (!SkipSpaces(string, pos))
    1.81 +      return false;
    1.82 +    if (string[pos] != '\"')
    1.83 +      return false;
    1.84 +    pos++;
    1.85 +    AString message;
    1.86 +    while(true)
    1.87 +    {
    1.88 +      if (pos >= string.Length())
    1.89 +        return false;
    1.90 +      char c = string[pos++];
    1.91 +      if (c == '\"')
    1.92 +        break;
    1.93 +      if (c == '\\')
    1.94 +      {
    1.95 +        char c = string[pos++];
    1.96 +        switch(c)
    1.97 +        {
    1.98 +          case 'n':
    1.99 +            message += '\n';
   1.100 +            break;
   1.101 +          case 't':
   1.102 +            message += '\t';
   1.103 +            break;
   1.104 +          case '\\':
   1.105 +            message += '\\';
   1.106 +            break;
   1.107 +          case '\"':
   1.108 +            message += '\"';
   1.109 +            break;
   1.110 +          default:
   1.111 +            message += '\\';
   1.112 +            message += c;
   1.113 +            break;
   1.114 +        }
   1.115 +      }
   1.116 +      else
   1.117 +        message += c;
   1.118 +    }
   1.119 +    if (!ConvertUTF8ToUnicode(message, pair.String))
   1.120 +      return false;
   1.121 +    pairs.Add(pair);
   1.122 +  }
   1.123 +  return true;
   1.124 +}
   1.125 +
   1.126 +int FindTextConfigItem(const CObjectVector<CTextConfigPair> &pairs, const UString &id)
   1.127 +{
   1.128 +  for (int  i = 0; i < pairs.Size(); i++)
   1.129 +    if (pairs[i].ID.Compare(id) == 0)
   1.130 +      return i;
   1.131 +  return -1;
   1.132 +}
   1.133 +
   1.134 +UString GetTextConfigValue(const CObjectVector<CTextConfigPair> &pairs, const UString &id)
   1.135 +{
   1.136 +  int index = FindTextConfigItem(pairs, id);
   1.137 +  if (index < 0)
   1.138 +    return UString();
   1.139 +  return pairs[index].String;
   1.140 +}

mercurial