1 // Copyright (C) 2013-2015 Petr Pavlu <setup@dagobah.cz>
3 // This file is part of CenterIM.
5 // CenterIM is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
10 // CenterIM is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with CenterIM. If not, see <http://www.gnu.org/licenses/>.
18 #include "CppConsUI.h"
20 #include "ColorScheme.h"
21 #include "CoreManager.h"
22 #include "KeyConfig.h"
33 ColorScheme
*color_scheme
= nullptr;
34 CoreManager
*core_manager
= nullptr;
35 KeyConfig
*key_config
= nullptr;
37 Error::Error(ErrorCode code
, const char *string
)
38 : error_code_(code
), error_string_(nullptr)
43 Error::Error(const Error
&other
)
45 assert(other
.error_string_
!= nullptr);
47 error_code_
= other
.error_code_
;
49 std::size_t size
= std::strlen(other
.error_string_
) + 1;
50 error_string_
= new char[size
];
51 std::strcpy(error_string_
, other
.error_string_
);
54 Error
&Error::operator=(const Error
&other
)
56 assert(other
.error_string_
!= nullptr);
58 std::size_t size
= std::strlen(other
.error_string_
) + 1;
59 auto new_string
= new char[size
];
60 std::strcpy(new_string
, other
.error_string_
);
62 error_code_
= other
.error_code_
;
63 delete[] error_string_
;
64 error_string_
= new_string
;
71 delete[] error_string_
;
74 void Error::setCode(ErrorCode code
)
79 void Error::setString(const char *string
)
82 if (string
!= nullptr)
83 size
+= std::strlen(string
);
84 auto new_string
= new char[size
];
85 if (string
!= nullptr)
86 std::strcpy(new_string
, string
);
90 delete[] error_string_
;
91 error_string_
= new_string
;
94 void Error::setFormattedString(const char *format
, ...)
96 assert(format
!= nullptr);
100 va_start(args
, format
);
101 int size
= std::vsnprintf(nullptr, 0, format
, args
) + 1;
104 auto new_string
= new char[size
];
106 va_start(args
, format
);
107 std::vsprintf(new_string
, format
, args
);
110 delete[] error_string_
;
111 error_string_
= new_string
;
116 error_code_
= ERROR_NONE
;
117 delete[] error_string_
;
118 error_string_
= nullptr;
121 void initializeConsUI(AppInterface
&interface
)
123 assert(color_scheme
== nullptr);
124 assert(core_manager
== nullptr);
125 assert(key_config
== nullptr);
127 // Initialize ColorScheme and KeyConfig. These cannot fail.
128 color_scheme
= new ColorScheme
;
129 key_config
= new KeyConfig
;
131 // CoreManager depends on KeyConfig so it has to be initialized after it.
132 core_manager
= new CoreManager(interface
);
135 void finalizeConsUI()
137 assert(color_scheme
!= nullptr);
138 assert(core_manager
!= nullptr);
139 assert(key_config
!= nullptr);
141 // Destroy CoreManager, KeyConfig and ColorScheme.
143 core_manager
= nullptr;
145 key_config
= nullptr;
147 color_scheme
= nullptr;
150 ColorScheme
*getColorSchemeInstance()
152 assert(color_scheme
!= nullptr);
156 CoreManager
*getCoreManagerInstance()
158 assert(core_manager
!= nullptr);
162 KeyConfig
*getKeyConfigInstance()
164 assert(key_config
!= nullptr);
170 // Some code below is based on the GLib code.
172 // Bits Length Byte 1 Byte 2 Byte 3 Byte 4 Byte 5 Byte 6
174 // 11 2 110xxxxx 10xxxxxx
175 // 16 3 1110xxxx 10xxxxxx 10xxxxxx
176 // 21 4 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
177 // 26 5 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
178 // 31 6 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
179 UniChar
getUniChar(const char *p
)
181 assert(p
!= nullptr);
184 unsigned char c
= *p
++;
187 if ((c
& 0x80) == 0x00)
189 else if ((c
& 0xe0) == 0xc0) {
193 else if ((c
& 0xf0) == 0xe0) {
197 else if ((c
& 0xf8) == 0xf0) {
201 else if ((c
& 0xfc) == 0xf8) {
205 else if ((c
& 0xfe) == 0xfc) {
214 if ((c
& 0xc0) != 0x80)
229 int interval_compare(const void *key
, const void *elt
)
231 const UniChar uc
= *static_cast<const UniChar
*>(key
);
232 const Interval
*interval
= static_cast<const Interval
*>(elt
);
234 if (uc
< interval
->start
)
236 if (uc
> interval
->end
)
242 } // anonymous namespace
244 bool isUniCharWide(UniChar uc
)
246 static const Interval wide
[] = {
362 if (std::bsearch(&uc
, wide
, sizeof(wide
) / sizeof(wide
[0]), sizeof(wide
[0]),
369 bool isUniCharDigit(UniChar uc
)
371 // Note: this function does not behave according to the Unicode standard.
373 if (uc
> '0' && uc
< '9')
378 bool isUniCharSpace(UniChar uc
)
380 // Note: this function does not behave according to the Unicode standard.
382 if (uc
== ' ' || uc
== '\t' || uc
== '\n' || uc
== '\r' || uc
== '\f')
387 const char *getNextChar(const char *p
)
389 static const char utf8_skip_data
[256] = {
390 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x00-0x0f
391 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x10-0x1f
392 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x20-0x2f
393 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x30-0x3f
394 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x40-0x4f
395 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x50-0x5f
396 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60-0x6f
397 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x70-0x7f
398 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x80-0x8f
399 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x90-0x9f
400 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0xa0-0xaf
401 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0xb0-0xbf
402 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xc0-0xcf
403 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xd0-0xdf
404 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 0xe0-0xef
405 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 1, 1 // 0xf0-0xff
408 return p
+ utf8_skip_data
[static_cast<unsigned char>(*p
)];
411 const char *getPrevChar(const char *p
)
415 if ((*p
& 0xc0) != 0x80)
420 const char *findNextChar(const char *p
, const char *end
)
423 return getNextChar(p
);
425 while (p
+ 1 < end
) {
427 if ((*p
& 0xc0) != 0x80)
433 const char *findPrevChar(const char *start
, const char *p
)
437 if ((*p
& 0xc0) != 0x80)
445 } // namespace CppConsUI
447 // vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab: