]> icculus.org git repositories - mikachu/openbox.git/blob - otk/util.cc
otk::Timer-ng!! thanks ManMower for this shizznit!
[mikachu/openbox.git] / otk / 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 "gettext.h"
27 #define _(str) gettext(str)
28
29 #include <assert.h>
30 }
31
32 #include <algorithm>
33
34 #include "util.hh"
35
36 using std::string;
37
38 namespace otk {
39
40 string expandTilde(const string& s) {
41   if (s[0] != '~') return s;
42
43   const char* const home = getenv("HOME");
44   if (home == NULL) return s;
45
46   return string(home + s.substr(s.find('/')));
47 }
48
49
50 void bexec(const string& command, const string& displaystring) {
51 #ifndef    __EMX__
52   if (! fork()) {
53     setsid();
54     putenv(displaystring);
55     int ret = execl("/bin/sh", "/bin/sh", "-c", command.c_str(), NULL);
56     exit(ret);
57   }
58 #else //   __EMX__
59   spawnlp(P_NOWAIT, "cmd.exe", "cmd.exe", "/c", command.c_str(), NULL);
60 #endif // !__EMX__
61 }
62
63
64 string textPropertyToString(Display *display, XTextProperty& text_prop) {
65   string ret;
66
67   if (text_prop.value && text_prop.nitems > 0) {
68     if (text_prop.encoding == XA_STRING) {
69       ret = (char *) text_prop.value;
70     } else {
71       text_prop.nitems = strlen((char *) text_prop.value);
72
73       char **list;
74       int num;
75       if (XmbTextPropertyToTextList(display, &text_prop,
76                                     &list, &num) == Success &&
77           num > 0 && *list) {
78         ret = *list;
79         XFreeStringList(list);
80       }
81     }
82   }
83
84   return ret;
85 }
86
87
88 string itostring(unsigned long i) {
89   if (i == 0)
90     return string("0");
91   
92   string tmp;
93   for (; i > 0; i /= 10)
94     tmp.insert(tmp.begin(), "0123456789"[i%10]);
95   return tmp;
96 }
97
98
99 string itostring(long i) {
100   std::string tmp = itostring( (unsigned long) std::abs(i));
101   if (i < 0)
102     tmp.insert(tmp.begin(), '-');
103   return tmp;
104 }
105
106 void putenv(const std::string &data)
107 {
108   char *c = new char[data.size() + 1];
109   std::string::size_type i, max;
110   for (i = 0, max = data.size(); i < max; ++i)
111     c[i] = data[i];
112   c[i] = 0;
113   if (::putenv(c)) {
114     printf(_("warning: couldn't set environment variable\n"));
115     perror("putenv()");
116   }
117 }
118
119 string basename (const string& path) {
120   string::size_type slash = path.rfind('/');
121   if (slash == string::npos)
122     return path;
123   return path.substr(slash+1);
124 }
125
126 }
127