]> Dogcows Code - chaz/openbox/blob - src/util.cc
6a6ac417a38aad58493a0809eded1aea7d10c754
[chaz/openbox] / src / util.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #ifdef HAVE_CONFIG_H
4 # include "../config.h"
5 #endif // HAVE_CONFIG_H
6
7 extern "C" {
8 #include <X11/Xatom.h>
9
10 #ifdef HAVE_STRING_H
11 #include <string.h>
12 #endif
13
14 #ifdef HAVE_STDLIB_H
15 #include <stdlib.h>
16 #endif
17
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif // HAVE_UNISTD_H
21
22 #if defined(HAVE_PROCESS_H) && defined(__EMX__)
23 # include <process.h>
24 #endif // HAVE_PROCESS_H __EMX__
25
26 #include <assert.h>
27 }
28
29 #include <algorithm>
30
31 #include "util.hh"
32
33 using std::string;
34
35 namespace ob {
36
37 string expandTilde(const string& s) {
38 if (s[0] != '~') return s;
39
40 const char* const home = getenv("HOME");
41 if (home == NULL) return s;
42
43 return string(home + s.substr(s.find('/')));
44 }
45
46
47 void bexec(const string& command, const string& displaystring) {
48 #ifndef __EMX__
49 if (! fork()) {
50 setsid();
51 int ret = putenv(const_cast<char *>(displaystring.c_str()));
52 assert(ret != -1);
53 ret = execl("/bin/sh", "/bin/sh", "-c", command.c_str(), NULL);
54 exit(ret);
55 }
56 #else // __EMX__
57 spawnlp(P_NOWAIT, "cmd.exe", "cmd.exe", "/c", command.c_str(), NULL);
58 #endif // !__EMX__
59 }
60
61
62 string textPropertyToString(Display *display, XTextProperty& text_prop) {
63 string ret;
64
65 if (text_prop.value && text_prop.nitems > 0) {
66 if (text_prop.encoding == XA_STRING) {
67 ret = (char *) text_prop.value;
68 } else {
69 text_prop.nitems = strlen((char *) text_prop.value);
70
71 char **list;
72 int num;
73 if (XmbTextPropertyToTextList(display, &text_prop,
74 &list, &num) == Success &&
75 num > 0 && *list) {
76 ret = *list;
77 XFreeStringList(list);
78 }
79 }
80 }
81
82 return ret;
83 }
84
85
86 timeval normalizeTimeval(const timeval &tm) {
87 timeval ret = tm;
88
89 while (ret.tv_usec < 0) {
90 if (ret.tv_sec > 0) {
91 --ret.tv_sec;
92 ret.tv_usec += 1000000;
93 } else {
94 ret.tv_usec = 0;
95 }
96 }
97
98 if (ret.tv_usec >= 1000000) {
99 ret.tv_sec += ret.tv_usec / 1000000;
100 ret.tv_usec %= 1000000;
101 }
102
103 if (ret.tv_sec < 0) ret.tv_sec = 0;
104
105 return ret;
106 }
107
108
109 string itostring(unsigned long i) {
110 if (i == 0)
111 return string("0");
112
113 string tmp;
114 for (; i > 0; i /= 10)
115 tmp.insert(tmp.begin(), "0123456789"[i%10]);
116 return tmp;
117 }
118
119
120 string itostring(long i) {
121 std::string tmp = itostring( (unsigned long) std::abs(i));
122 if (i < 0)
123 tmp.insert(tmp.begin(), '-');
124 return tmp;
125 }
126
127 }
128
129 #ifndef HAVE_BASENAME
130 string basename (const string& path) {
131 string::size_type slash = path.rfind('/');
132 if (slash == string::npos)
133 return path;
134 return path.substr(slash+1);
135 }
136 #endif // HAVE_BASENAME
137
This page took 0.081051 seconds and 4 git commands to generate.