File: FeatureParser.h

package info (click to toggle)
graphite2 1.3.14-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,588 kB
  • sloc: cpp: 14,738; cs: 1,998; python: 1,737; ansic: 1,673; perl: 184; xml: 123; sh: 104; makefile: 62
file content (176 lines) | stat: -rw-r--r-- 5,782 bytes parent folder | download | duplicates (4)
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
/*  GRAPHITE2 LICENSING

    Copyright 2010, SIL International
    All rights reserved.

    This library is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published
    by the Free Software Foundation; either version 2.1 of License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should also have received a copy of the GNU Lesser General Public
    License along with this library in the file named "LICENSE".
    If not, write to the Free Software Foundation, 51 Franklin Street,
    Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
    internet at http://www.fsf.org/licenses/lgpl.html.
*/
#pragma once

#include <cstring>
#include <cstdlib>

union FeatID
{
    unsigned char uChar[4];
    unsigned int uId;
};

typedef struct
{
    FeatID m_id;
    union {
        signed short m_sValue;
        unsigned short m_uValue;
    };
} FeatureSetting;

class FeatureParser
{
public:
    FeatureParser(const char * features)
        : m_settings(NULL), m_featureCount(0)
    {
        if (!features)  return;
        const char * pLang = NULL;
        m_lang.uId = 0;
        size_t featuresLength = strlen(features);
        if (featuresLength == 0)
            return;
        m_featureCount = 1;
        if (features && (pLang = strstr(features, "lang=")))
        {
            pLang += 5;
            size_t i = 0;
            while ((i < 4) && (*pLang != '0') && (*pLang != '&'))
            {
                m_lang.uChar[i] = *pLang;
                ++pLang;
                ++i;
            }
            m_lang.uId = swap32(m_lang.uId);
            m_featureCount = 0;
        }
        // count features
        for (size_t i = 0; i < featuresLength; i++)
        {
            if (features[i] == ',') m_featureCount++;
        }
        m_settings = new FeatureSetting[m_featureCount];
        if (!m_settings)
        {
            m_featureCount = 0;
            return;
        }
        //featureList = gr_face_featureval_for_lang(face, lang.uId);
        const char * name = features;
        const char * valueText = NULL;
        size_t nameLength = 0;
        int value = 0;
        FeatID featId;
        //const gr_feature_ref* ref = NULL;
        size_t featIndex = 0;
        featId.uId = 0;
        for (size_t i = 0; i < featuresLength; i++)
        {
            switch (features[i])
            {
                case ',':
                case '&':
                    value = atoi(valueText);
                    if (m_settings)
                    {
                        //gr_fref_set_feature_value(ref, value, featureList);
                        m_settings[featIndex].m_sValue = value;
                        //ref = NULL;
                    }
                    valueText = NULL;
                    name = features + i + 1;
                    nameLength = 0;
                    featId.uId = 0;
                    ++featIndex;
                    break;
                case '=':
                    if (nameLength <= 4 && (*name < '0' || *name > '9'))
                    {
                        featId.uId = swap32(featId.uId);
                        //ref = gr_face_find_fref(face, featId.uId);
                    }
                    else
                    {
                        featId.uId = atoi(name);
                        //ref = gr_face_find_fref(face, featId.uId);
                    }
                    m_settings[featIndex].m_id.uId = featId.uId;
                    valueText = features + i + 1;
                    name = NULL;
                    break;
                default:
                    if (valueText == NULL)
                    {
                        if (nameLength < 4 && features[i] >= 0x20 && features[i] <= 0x7F)
                        {
                            featId.uChar[nameLength++] = features[i];
                        }
                    }
                    break;
            }
            if (featIndex < m_featureCount && valueText)
            {
                value = atoi(valueText);
                m_settings[featIndex].m_sValue = value;
            }
        }
    }
    ~FeatureParser()
    {
        delete[] m_settings;
        m_settings = NULL;
    }
    int swap32(int x)
    {
        return (((x & 0xff) << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | ((x & 0xff000000) >> 24));
    }
    unsigned int featureId(size_t i) const { return m_settings[i].m_id.uId; }
    unsigned int featureIdBE(size_t i) const { return m_settings[i].m_id.uId; }
    signed short featureSValue(size_t i) const { return m_settings[i].m_sValue; }
    signed short featureUValue(size_t i) const { return m_settings[i].m_uValue; }
    size_t featureCount() const { return m_featureCount; }
    unsigned int langId() const { return m_lang.uId; }
    unsigned int otLang() const {
        FeatID otId;
        static const int lowerCaseOffset = 'a' - 'A';
        otId.uId = m_lang.uId;
        if (m_lang.uId > 0)
        {
            for (size_t i = 0; i < 4; i++)
            {
                // convert to upper case
                if (m_lang.uChar[i] >= 'a' && m_lang.uChar[i] <= 'z')
                    otId.uChar[i] -= lowerCaseOffset;
                // space, not zero pad
                if (m_lang.uChar[i] == 0)
                    otId.uChar[i] = ' ';
            }
        }
        return otId.uId;
    }
private:
    FeatID m_lang;
    FeatureSetting * m_settings;
    size_t m_featureCount;
};