~vcs-imports/bibletime/trunk

« back to all changes in this revision

Viewing changes to bibletime/backend/btstringmgr.cpp

  • Committer: mgruner
  • Date: 2007-05-08 15:51:07 UTC
  • Revision ID: vcs-imports@canonical.com-20070508155107-0rj7jdmm5ivf8685
-imported source and data files to new svn module
-this is where KDE4-based development will take place

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// C++ Implementation: btstringmgr
 
3
//
 
4
// Description:
 
5
//
 
6
//
 
7
// Author: The BibleTime team <info@bibletime.info>, (C) 2004
 
8
//
 
9
// Copyright: See COPYING file that comes with this distribution
 
10
//
 
11
//
 
12
 
 
13
#include "btstringmgr.h"
 
14
 
 
15
//System includes
 
16
#include <ctype.h>
 
17
 
 
18
char* BTStringMgr::upperUTF8(char* text, unsigned int maxlen) const {
 
19
        const int max = (maxlen>0) ? maxlen : strlen(text);
 
20
 
 
21
        if (isUtf8(text)) {
 
22
                strncpy(text, (const char*)QString::fromUtf8(text).upper().utf8(), max);
 
23
 
 
24
                return text;
 
25
        }
 
26
        else {
 
27
                char* ret = text;
 
28
 
 
29
                while (*text) {
 
30
                        *text = toupper(*text);
 
31
                        text++;
 
32
                }
 
33
 
 
34
                return ret;
 
35
        }
 
36
 
 
37
        return text;
 
38
}
 
39
 
 
40
char* BTStringMgr::upperLatin1(char* text, unsigned int max) const {
 
41
        char* ret = text;
 
42
 
 
43
        while (*text) {
 
44
                *text = toupper(*text);
 
45
                text++;
 
46
        }
 
47
 
 
48
        return ret;
 
49
}
 
50
 
 
51
bool BTStringMgr::supportsUnicode() const {
 
52
        return true;
 
53
}
 
54
 
 
55
const bool BTStringMgr::isUtf8(const char *buf) const {
 
56
        int i, n;
 
57
        register unsigned char c;
 
58
        bool gotone = false;
 
59
 
 
60
        #define F 0   /* character never appears in text */
 
61
        #define T 1   /* character appears in plain ASCII text */
 
62
        #define I 2   /* character appears in ISO-8859 text */
 
63
        #define X 3   /* character appears in non-ISO extended ASCII (Mac, IBM PC) */
 
64
 
 
65
        static const unsigned char text_chars[256] = {
 
66
                                /*                  BEL BS HT LF    FF CR    */
 
67
                                F, F, F, F, F, F, F, T, T, T, T, F, T, T, F, F,  /* 0x0X */
 
68
                                /*                              ESC          */
 
69
                                F, F, F, F, F, F, F, F, F, F, F, T, F, F, F, F,  /* 0x1X */
 
70
                                T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0x2X */
 
71
                                T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0x3X */
 
72
                                T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0x4X */
 
73
                                T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0x5X */
 
74
                                T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0x6X */
 
75
                                T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, F,  /* 0x7X */
 
76
                                /*            NEL                            */
 
77
                                X, X, X, X, X, T, X, X, X, X, X, X, X, X, X, X,  /* 0x8X */
 
78
                                X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X,  /* 0x9X */
 
79
                                I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I,  /* 0xaX */
 
80
                                I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I,  /* 0xbX */
 
81
                                I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I,  /* 0xcX */
 
82
                                I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I,  /* 0xdX */
 
83
                                I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I,  /* 0xeX */
 
84
                                I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I   /* 0xfX */
 
85
                        };
 
86
 
 
87
        /* *ulen = 0; */
 
88
 
 
89
        for (i = 0; (c = buf[i]); i++) {
 
90
                if ((c & 0x80) == 0) {        /* 0xxxxxxx is plain ASCII */
 
91
                        /*
 
92
                         * Even if the whole file is valid UTF-8 sequences,
 
93
                         * still reject it if it uses weird control characters.
 
94
                         */
 
95
 
 
96
                        if (text_chars[c] != T)
 
97
                                return false;
 
98
 
 
99
                }
 
100
                else if ((c & 0x40) == 0) { /* 10xxxxxx never 1st byte */
 
101
                        return false;
 
102
                }
 
103
                else {                           /* 11xxxxxx begins UTF-8 */
 
104
                        int following;
 
105
 
 
106
                        if ((c & 0x20) == 0) {             /* 110xxxxx */
 
107
                                following = 1;
 
108
                        }
 
109
                        else if ((c & 0x10) == 0) {      /* 1110xxxx */
 
110
                                following = 2;
 
111
                        }
 
112
                        else if ((c & 0x08) == 0) {      /* 11110xxx */
 
113
                                following = 3;
 
114
                        }
 
115
                        else if ((c & 0x04) == 0) {      /* 111110xx */
 
116
                                following = 4;
 
117
                        }
 
118
                        else if ((c & 0x02) == 0) {      /* 1111110x */
 
119
                                following = 5;
 
120
                        }
 
121
                        else
 
122
                                return false;
 
123
 
 
124
                        for (n = 0; n < following; n++) {
 
125
                                i++;
 
126
 
 
127
                                if (!(c = buf[i]))
 
128
                                        goto done;
 
129
 
 
130
                                if ((c & 0x80) == 0 || (c & 0x40))
 
131
                                        return false;
 
132
                        }
 
133
 
 
134
                        gotone = true;
 
135
                }
 
136
        }
 
137
 
 
138
done:
 
139
        return gotone;   /* don't claim it's UTF-8 if it's all 7-bit */
 
140
}
 
141
 
 
142
#undef F
 
143
#undef T
 
144
#undef I
 
145
#undef X