Add check target to the CMake build
[centerim5.git] / src / Utils.cpp
blob2982c2befa29742b6fc01728ee09fcae59a8b096
1 // Copyright (C) 2010-2015 Petr Pavlu <setup@dagobah.cz>
2 //
3 // This file is part of CenterIM.
4 //
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.
9 //
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 "Utils.h"
20 #include "Log.h"
22 #include "gettext.h"
23 #include <cerrno>
24 #include <cstdlib>
26 namespace Utils {
28 const char *getStatusIndicator(PurpleStatus *status)
30 PurpleStatusType *status_type = purple_status_get_type(status);
31 PurpleStatusPrimitive prim = purple_status_type_get_primitive(status_type);
33 switch (prim) {
34 case PURPLE_STATUS_UNSET:
35 return "[x]";
36 case PURPLE_STATUS_OFFLINE:
37 return "";
38 case PURPLE_STATUS_AVAILABLE:
39 return "[o]";
40 case PURPLE_STATUS_UNAVAILABLE:
41 return "[u]";
42 case PURPLE_STATUS_INVISIBLE:
43 return "[i]";
44 case PURPLE_STATUS_AWAY:
45 return "[a]";
46 case PURPLE_STATUS_EXTENDED_AWAY:
47 return "[A]";
48 case PURPLE_STATUS_MOBILE:
49 return "[m]";
50 case PURPLE_STATUS_TUNE:
51 return "[t]";
52 case PURPLE_STATUS_MOOD:
53 return "[d]";
54 default:
55 return "[X]";
59 char *stripAccelerator(const char *label)
61 g_assert(label != nullptr);
63 // Calculate length of the string without accelerators.
64 std::size_t len = 1;
65 for (const char *p = label; *p != '\0'; ++p)
66 if (*p != '_')
67 ++len;
69 // Allocate and populate the resulting string.
70 char *res = g_new(char, len);
71 char *o = res;
72 for (const char *p = label; *p != '\0'; ++p)
73 if (*p != '_')
74 *o++ = *p;
75 *o = '\0';
77 return res;
80 bool stringToNumber(const char *text, long min, long max, long *out)
82 g_assert(text != nullptr);
83 g_assert(out != nullptr);
85 char *endptr;
86 errno = 0;
87 *out = std::strtol(text, &endptr, 10);
88 if (*text == '\0' || *endptr != '\0') {
89 LOG->warning(_("Value is not a number."));
90 return false;
92 if (errno == ERANGE || *out < min || *out > max) {
93 *out = CLAMP(*out, min, max);
94 LOG->warning(_("Value is out of range [%ld, %ld]."), min, max);
95 return false;
97 return true;
100 } // namespace Utils
102 // vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab: