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

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 // Common/TextConfig.cpp
michael@0 2
michael@0 3 #include "StdAfx.h"
michael@0 4
michael@0 5 #include "Common/TextConfig.h"
michael@0 6
michael@0 7 #include "Defs.h"
michael@0 8 #include "Common/UTFConvert.h"
michael@0 9
michael@0 10 static bool IsDelimitChar(char c)
michael@0 11 {
michael@0 12 return (c == ' ' || c == 0x0A || c == 0x0D ||
michael@0 13 c == '\0' || c == '\t');
michael@0 14 }
michael@0 15
michael@0 16 static AString GetIDString(const char *string, int &finishPos)
michael@0 17 {
michael@0 18 AString result;
michael@0 19 for (finishPos = 0; true; finishPos++)
michael@0 20 {
michael@0 21 char c = string[finishPos];
michael@0 22 if (IsDelimitChar(c) || c == '=')
michael@0 23 return result;
michael@0 24 result += c;
michael@0 25 }
michael@0 26 }
michael@0 27
michael@0 28 static bool WaitNextLine(const AString &string, int &pos)
michael@0 29 {
michael@0 30 for (;pos < string.Length(); pos++)
michael@0 31 if (string[pos] == 0x0A)
michael@0 32 return true;
michael@0 33 return false;
michael@0 34 }
michael@0 35
michael@0 36 static bool SkipSpaces(const AString &string, int &pos)
michael@0 37 {
michael@0 38 for (;pos < string.Length(); pos++)
michael@0 39 {
michael@0 40 char c = string[pos];
michael@0 41 if (!IsDelimitChar(c))
michael@0 42 {
michael@0 43 if (c != ';')
michael@0 44 return true;
michael@0 45 if (!WaitNextLine(string, pos))
michael@0 46 return false;
michael@0 47 }
michael@0 48 }
michael@0 49 return false;
michael@0 50 }
michael@0 51
michael@0 52 bool GetTextConfig(const AString &string, CObjectVector<CTextConfigPair> &pairs)
michael@0 53 {
michael@0 54 pairs.Clear();
michael@0 55 int pos = 0;
michael@0 56
michael@0 57 /////////////////////
michael@0 58 // read strings
michael@0 59
michael@0 60 while (true)
michael@0 61 {
michael@0 62 if (!SkipSpaces(string, pos))
michael@0 63 break;
michael@0 64 CTextConfigPair pair;
michael@0 65 int finishPos;
michael@0 66 AString temp = GetIDString(((const char *)string) + pos, finishPos);
michael@0 67 if (!ConvertUTF8ToUnicode(temp, pair.ID))
michael@0 68 return false;
michael@0 69 if (finishPos == 0)
michael@0 70 return false;
michael@0 71 pos += finishPos;
michael@0 72 if (!SkipSpaces(string, pos))
michael@0 73 return false;
michael@0 74 if (string[pos] != '=')
michael@0 75 return false;
michael@0 76 pos++;
michael@0 77 if (!SkipSpaces(string, pos))
michael@0 78 return false;
michael@0 79 if (string[pos] != '\"')
michael@0 80 return false;
michael@0 81 pos++;
michael@0 82 AString message;
michael@0 83 while(true)
michael@0 84 {
michael@0 85 if (pos >= string.Length())
michael@0 86 return false;
michael@0 87 char c = string[pos++];
michael@0 88 if (c == '\"')
michael@0 89 break;
michael@0 90 if (c == '\\')
michael@0 91 {
michael@0 92 char c = string[pos++];
michael@0 93 switch(c)
michael@0 94 {
michael@0 95 case 'n':
michael@0 96 message += '\n';
michael@0 97 break;
michael@0 98 case 't':
michael@0 99 message += '\t';
michael@0 100 break;
michael@0 101 case '\\':
michael@0 102 message += '\\';
michael@0 103 break;
michael@0 104 case '\"':
michael@0 105 message += '\"';
michael@0 106 break;
michael@0 107 default:
michael@0 108 message += '\\';
michael@0 109 message += c;
michael@0 110 break;
michael@0 111 }
michael@0 112 }
michael@0 113 else
michael@0 114 message += c;
michael@0 115 }
michael@0 116 if (!ConvertUTF8ToUnicode(message, pair.String))
michael@0 117 return false;
michael@0 118 pairs.Add(pair);
michael@0 119 }
michael@0 120 return true;
michael@0 121 }
michael@0 122
michael@0 123 int FindTextConfigItem(const CObjectVector<CTextConfigPair> &pairs, const UString &id)
michael@0 124 {
michael@0 125 for (int i = 0; i < pairs.Size(); i++)
michael@0 126 if (pairs[i].ID.Compare(id) == 0)
michael@0 127 return i;
michael@0 128 return -1;
michael@0 129 }
michael@0 130
michael@0 131 UString GetTextConfigValue(const CObjectVector<CTextConfigPair> &pairs, const UString &id)
michael@0 132 {
michael@0 133 int index = FindTextConfigItem(pairs, id);
michael@0 134 if (index < 0)
michael@0 135 return UString();
michael@0 136 return pairs[index].String;
michael@0 137 }

mercurial