summaryrefslogtreecommitdiff
path: root/glcdskin/string.c
blob: a17496460e779941e002363291b5023d3eb2e79e (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * GraphLCD skin library
 *
 * string.h  -  skin string class
 *
 * This file is released under the GNU General Public License. Refer
 * to the COPYING file distributed with this package.
 *
 * based on text2skin
 *
 */

#include <syslog.h>

#include "skin.h"
#include "object.h"
#include "string.h"

namespace GLCD
{

tSkinToken::tSkinToken(void)
:   Index(-1)
{
}

tSkinToken::tSkinToken(int id, std::string n, uint32_t o, const std::string & a)
:   Id(id),
    Name(n),
    Offset(o),
    Attrib(a),
    MaxItems(-1),
    Index(-1)
{
}

bool operator< (const tSkinToken & A, const tSkinToken & B)
{
    return A.Id == B.Id
        ? A.Name == B.Name
            ? A.Attrib == B.Attrib
                ? A.Index < B.Index
                : A.Attrib < B.Attrib
            : A.Name < B.Name
        : A.Id < B.Id;
}

std::string tSkinToken::Token(const tSkinToken & Token)
{
    std::string result = (std::string) "{" + Token.Name;
    //if (Token.Attrib.length() > 0)
    //  result += ":" + Token.Attrib;
    result += "}";

    return result;
}

cSkinString::tStringList cSkinString::mStrings;

cSkinString::cSkinString(cSkinObject *Parent, bool Translate)
:   mObject(Parent),
    mSkin(Parent->Skin()),
    mTranslate(Translate)
{
    mStrings.push_back(this);
}

cSkinString::~cSkinString()
{
    tStringList::iterator it = mStrings.begin();
    for (; it != mStrings.end(); ++it) {
        if ((*it) == this) {
            mStrings.erase(it);
            break;
        }
    }
}

void cSkinString::Reparse(void)
{
    tStringList::iterator it = mStrings.begin();
    for (; it != mStrings.end(); ++it) {
        if ((*it)->mTranslate && (*it)->mText.length() > 0)
            (*it)->Parse((*it)->mOriginal, true);
    }
}


// copied from xml.c (should be valid for parsing variable names too ...)
static bool IsTokenChar(bool start, int c) {
    return isalpha(c) || c == '_' || (!start && isdigit(c));
}


bool cSkinString::Parse(const std::string & Text, bool Translate)
{
    std::string trans = Translate ? mSkin->Config().Translate(Text) : Text;
    const char * ptr, * last;
    bool inToken = false;
    bool inAttrib = false;
    int offset = 0;

    //Dprintf("parsing: %s\n", Text.c_str());
    mOriginal = Text;
    mText = "";
    mTokens.clear();

    ptr = trans.c_str();
    last = trans.c_str();
    for (; *ptr; ++ptr) {
        if (inToken && *ptr == '\\') {
            if (*(ptr + 1) == '\0') {
                syslog(LOG_ERR, "ERROR: Stray \\ in token attribute\n");
                return false;
            }

            ++ptr;
            continue;
        }
        else if (*ptr == '#') {
          if (inToken) {
              syslog(LOG_ERR, "ERROR: Unexpected '#' in token");
              return false;
          }

          mText.append(last, ptr - last);

          bool isStartChar = true;
          const char * varNameStart = ptr;
          ptr++;
          while (*ptr && IsTokenChar(isStartChar, *ptr)) {
            isStartChar = false;
            ptr++;
            offset++;
          }
          // add #VARNAME#
          mText.append(varNameStart, (ptr - varNameStart));
          mText.append("#");
          offset +=2; // adds two '#' -> fix offset
          ptr--; // we'd be at the correct position now but the for-loop does a ++ptr -> fix it by stepping back one char
          last = ptr + 1;
        }
        else if (*ptr == '{') {
            if (inToken) {
                syslog(LOG_ERR, "ERROR: Unexpected '{' in token");
                return false;
            }

            mText.append(last, ptr - last);
            last = ptr + 1;
            inToken = true;
        }
        else if (*ptr == '}' || (inToken && *ptr == ':')) {
            if (!inToken) {
                syslog(LOG_ERR, "ERROR: Unexpected '}' outside of token");
                return false;
            }

            if (inAttrib) {
                if (*ptr == ':') {
                    syslog(LOG_ERR, "ERROR: Unexpected ':' inside of token attribute");
                    return false;
                }

                int pos = -1;
                std::string attr;
                attr.assign(last, ptr - last);
                while ((pos = attr.find('\\', pos + 1)) != -1) {
                    switch (attr[pos + 1]) {
                        case 'n':
                            attr.replace(pos, 2, "\n");
                            break;

                        default:
                            attr.erase(pos, 1);
                    }
                }

                tSkinToken &lastToken = mTokens[mTokens.size() - 1];
                if (attr == "clean")
                    lastToken.Attrib = aClean;
                else if (attr == "rest")
                    lastToken.Attrib = aRest;
                else {
                    char *end;
                    int n = strtol(attr.c_str(), &end, 10);
                    //Dprintf("attr: %s, n: %d, end: |%s|\n", attr.c_str(), n, end);
                    if (end != attr.c_str() && *end == '\0') {
                        //Dprintf("a number\n");
                        lastToken.Attrib = n;
                    } else
                        lastToken.Attrib = attr;
                }

                inAttrib = false;
                inToken = false;
            } else {
                if (true)
                {
                    std::string tmp;
                    tmp.assign(last, ptr - last);
                    if (tmp != "") { // ignore empty token
                      tSkinToken token(mSkin->Config().GetTokenId(tmp), tmp, offset, "");
                      mTokens.push_back(token);
                    }
                }
                else
                {
                    syslog(LOG_ERR, "ERROR: Unexpected token {%.*s}", (int)(ptr - last), last);
                    return false;
                }

                if (*ptr == ':')
                    inAttrib = true;
                else
                    inToken = false;
            }

            last = ptr + 1;
        }
        else if (!inToken)
            ++offset;
    }

    if (inToken) {
        syslog(LOG_ERR, "ERROR: Expecting '}' in token");
        return false;
    }

    mText.append(last, ptr - last);

    if (mTranslate && !Translate && mText.length() > 0)
        Parse(Text, true);
    return true;
}

cType cSkinString::Evaluate(void) const
{
    if (mText.length() == 0 && mTokens.size() == 1)
        return mSkin->Config().GetToken(mTokens[0]);

    std::string result_raw = "";
    int offset = 0;
    for (uint32_t i = 0; i < mTokens.size(); i++) {
        result_raw.append(mText.substr(offset, mTokens[i].Offset - offset) );
        result_raw.append(mSkin->Config().GetToken(mTokens[i]));
        offset = mTokens[i].Offset;
    }
    result_raw.append(mText.c_str() + offset);

    // replace variable placeholders (#VARNAME#) with corresponding values
    std::string result_trans = "";
    size_t idxstart = 0, idxend = 0;
    size_t pos = 0;
    while ( (idxstart=result_raw.find("#", idxstart)) != std::string::npos ) {
      result_trans.append(result_raw.substr(pos, idxstart-pos));
      idxend = result_raw.find("#", idxstart+1);
      cSkinVariable * variable = mSkin->GetVariable(result_raw.substr(idxstart+1, idxend-idxstart-1));
      if (variable) {
        std::string val = (std::string) variable->Value();

        // if value of variable contains token definions: reparse value
        if (val.find("{") != std::string::npos) {
          cSkinString *result = new cSkinString(Object(), false);
          if (result->Parse(val)) {
            val = (std::string) result->Evaluate();
          }
          delete result;
        }
        result_trans.append (val);
        //   syslog(LOG_ERR, "string variable %s", trans.c_str());
      }
      idxstart = idxend+1;
      pos = idxstart;
    }
    result_trans.append(result_raw.substr(pos));
    return result_trans;
}

} // end of namespace