summaryrefslogtreecommitdiff
path: root/src/PYDynamicSpecialPhrase.cc
blob: d90c2495837a829ab794dc572d5c90a7b42858cd (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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* vim:set et ts=4 sts=4:
 *
 * ibus-pinyin - The Chinese PinYin engine for IBus
 *
 * Copyright (c) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 */
#include "PYDynamicSpecialPhrase.h"

namespace PY {

DynamicSpecialPhrase::~DynamicSpecialPhrase (void)
{
}

std::string
DynamicSpecialPhrase::text (void)
{
    /* get the current time */
    std::time_t rawtime;
    std::time (&rawtime);
    m_time = *std::localtime (&rawtime);

    std::string result;

    size_t pos = 0;
    size_t pnext;
    gint s = 0;
    while (s != 2) {
        switch (s) {
        case 0: // expect "${"
            pnext = m_text.find ("${", pos);
            if (pnext == m_text.npos) {
                result += m_text.substr (pos);
                s = 2;
            }
            else {
                result += m_text.substr (pos, pnext - pos);
                pos = pnext + 2;
                s = 1;
            }
            break;
        case 1: // expect "}"
            pnext = m_text.find ("}", pos);
            if (pnext == m_text.npos) {
                result += "${";
                result += m_text.substr (pos);
                s = 2;
            }
            else {
                result += variable (m_text.substr(pos, pnext - pos));
                pos = pnext + 1;
                s = 0;
            }
            break;
        default: /* should not be reached */
            g_assert_not_reached ();
        }
    }
    return result;
}

inline const std::string
DynamicSpecialPhrase::dec (gint d, const gchar *fmt)
{
    gchar string [32];
    g_snprintf (string, sizeof (string), fmt, d);
    return string;
}

inline const std::string
DynamicSpecialPhrase::year_cn (gboolean yy)
{
    static const gchar * const digits[] = {
        "〇", "一", "二", "三", "四",
        "五", "六", "七", "八", "九"
    };

    gint year = m_time.tm_year + 1900;
    gint bit = 0;
    if (yy) {
        year %= 100;
        bit = 2;
    }

    std::string result;
    while (year != 0 || bit > 0) {
        result.insert(0, digits[year % 10]);
        year /= 10;
        bit -= 1;
    }
    return result;
}

inline const std::string
DynamicSpecialPhrase::month_cn (void)
{
    static const gchar * const month_num[] = {
        "一", "二", "三", "四", "五", "六", "七", "八",
        "九", "十", "十一", "十二"
    };
    return month_num[m_time.tm_mon];
}

inline const std::string
DynamicSpecialPhrase::weekday_cn (void)
{
    static const gchar * const week_num[] = {
        "日", "一", "二", "三", "四", "五", "六"
    };
    return week_num[m_time.tm_wday];
}

inline const std::string
DynamicSpecialPhrase::hour_cn (guint i)
{
    static const gchar * const hour_num[] = {
        "零", "一", "二", "三", "四",
        "五", "六", "七", "八", "九",
        "十", "十一", "十二", "十三", "十四",
        "十五", "十六", "十七", "十八", "十九",
        "二十", "二十一", "二十二", "二十三",
    };
    return hour_num[i];
}

inline const std::string
DynamicSpecialPhrase::fullhour_cn (void)
{
    return hour_cn (m_time.tm_hour);
}

inline const std::string
DynamicSpecialPhrase::halfhour_cn (void)
{
    return hour_cn (m_time.tm_hour % 12);
}

inline const std::string
DynamicSpecialPhrase::day_cn (void)
{
    static const gchar * const day_num[] = {
        "", "一", "二", "三", "四",
        "五", "六", "七", "八", "九",
        "", "十","二十", "三十"
    };
    guint day = m_time.tm_mday;
    return std::string (day_num[day / 10 + 10]) + day_num[day % 10];
}

inline const std::string
DynamicSpecialPhrase::minsec_cn (guint i)
{
    static const gchar * const num[] = {
        "", "一", "二", "三", "四",
        "五", "六", "七", "八", "九",
        "零", "十","二十", "三十", "四十"
        "五十", "六十"
    };
    return std::string (num[i / 10 + 10]) + num[i % 10];
}

static const char * numbers [2][10] = {
    {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖",},
    {"〇", "一", "二", "三", "四", "五", "六", "七", "八", "九",},
};

struct unit_t{
    const char * unit_zh_name;  // Chinese Character
    const int digits;           // Position in string.
    const bool persist;         // Whether to force eating zero and force inserting into result string.
};

static unit_t units_simplified[] ={
    {"兆", 12, true},
    {"亿", 8, true},
    {"万", 4, true},
    {"千", 3, false},
    {"百", 2, false},
    {"十", 1, false},
    {"",   0, true},
};

static unit_t units_traditional[] ={
    {"兆", 12, true},
    {"亿", 8, true},
    {"万", 4, true},
    {"仟", 3, false},
    {"佰", 2, false},
    {"拾", 1, false},
    {"",   0, true},
};


const std::string
DynamicSpecialPhrase::simplest_cn_number(gint64 num)
{
    std::string result = "";
    if ( num == 0 )
        result = numbers[1][0];
    while (num > 0) {
        int remains = num % 10;
        num = num / 10;
        result = std::string ( numbers[1][remains] ) + result;
    }

    return result;
}

static inline const std::string
translate_to_longform(gint64 num, const char * number[10], unit_t units[])
{
    std::string result = "";
    int cur_pos = -1;
    bool eat_zero = false;

    while (num > 0) {
        int remains = num % 10;
        num = num / 10;
        cur_pos ++;
        std::string unit = "";
        int pos = cur_pos;
        size_t i = 6;
        while ( pos > 0 ) {
            for ( i = 0; i < 7; ++i) {
                pos = pos % units[i].digits;
                if ( pos == 0 )
                    break;
            }
        }

        if ( units[i].persist ) {
            result = std::string (units[i].unit_zh_name) + result;
            eat_zero = true;
        }

        if ( remains == 0){
            if ( eat_zero ) continue;

            result = std::string (number[0]) + result;
            eat_zero = true;
            continue;
        }else{
            eat_zero = false;
        }

        if (num == 0 && remains == 1 && i == 5)
            result = std::string (units[i].unit_zh_name) + result;
        else if (units[i].persist)
            result = std::string (number[remains]) + result;
        else
            result = std::string (number[remains]) + std::string (units[i].unit_zh_name) + result;
    }

    return result;
}

const std::string
DynamicSpecialPhrase::simplified_number(gint64 num)
{
    return translate_to_longform(num, numbers[1], units_simplified);
}

const std::string
DynamicSpecialPhrase::traditional_number(gint64 num)
{
    if ( 0 == num )
        return numbers[0][0];
    return translate_to_longform(num, numbers[0], units_traditional);
}

inline const std::string
DynamicSpecialPhrase::variable (const std::string &name)
{
    if (name == "year")     return dec (m_time.tm_year + 1900);
    if (name == "year_yy")  return dec ((m_time.tm_year + 1900) % 100, "%02d");
    if (name == "month")    return dec (m_time.tm_mon + 1);
    if (name == "month_mm") return dec (m_time.tm_mon + 1, "%02d");
    if (name == "day")      return dec (m_time.tm_mday);
    if (name == "day_dd")   return dec (m_time.tm_mday, "%02d");
    if (name == "weekday")  return dec (m_time.tm_wday + 1);
    if (name == "fullhour") return dec (m_time.tm_hour, "%02d");
    if (name == "falfhour") return dec (m_time.tm_hour % 12, "%02d");
    if (name == "ampm")     return m_time.tm_hour < 12 ? "AM" : "PM";
    if (name == "minute")   return dec (m_time.tm_min, "%02d");
    if (name == "second")   return dec (m_time.tm_sec, "%02d");
    if (name == "year_cn")      return year_cn ();
    if (name == "year_yy_cn")   return year_cn (TRUE);
    if (name == "month_cn")     return month_cn ();
    if (name == "day_cn")       return day_cn ();
    if (name == "weekday_cn")   return weekday_cn ();
    if (name == "fullhour_cn")  return fullhour_cn ();
    if (name == "halfhour_cn")  return halfhour_cn ();
    if (name == "ampm_cn")      return m_time.tm_hour < 12 ? "上午" : "下午";
    if (name == "minute_cn")    return minsec_cn (m_time.tm_min);
    if (name == "second_cn")    return minsec_cn (m_time.tm_sec);

    return "${" + name + "}";
}

};