]> Dogcows Code - chaz/openbox/blob - otk/util.cc
39ea0f6cc7a85a9326e74a9588ee801dad4ac10e
[chaz/openbox] / 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 <assert.h>
27 }
28
29 #include <algorithm>
30
31 #include "util.hh"
32
33 using std::string;
34
35 namespace otk {
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 printf("tilde: getenv(DISPLAY)=%s\n", getenv("DISPLAY"));
43
44 return string(home + s.substr(s.find('/')));
45 }
46
47
48 void bexec(const string& command, const string& displaystring) {
49 #ifndef __EMX__
50 if (! fork()) {
51 setsid();
52 int ret = putenv(const_cast<char *>(displaystring.c_str()));
53 assert(ret != -1);
54 ret = execl("/bin/sh", "/bin/sh", "-c", command.c_str(), NULL);
55 exit(ret);
56 }
57 #else // __EMX__
58 spawnlp(P_NOWAIT, "cmd.exe", "cmd.exe", "/c", command.c_str(), NULL);
59 #endif // !__EMX__
60 }
61
62
63 string textPropertyToString(Display *display, XTextProperty& text_prop) {
64 string ret;
65
66 if (text_prop.value && text_prop.nitems > 0) {
67 if (text_prop.encoding == XA_STRING) {
68 ret = (char *) text_prop.value;
69 } else {
70 text_prop.nitems = strlen((char *) text_prop.value);
71
72 char **list;
73 int num;
74 if (XmbTextPropertyToTextList(display, &text_prop,
75 &list, &num) == Success &&
76 num > 0 && *list) {
77 ret = *list;
78 XFreeStringList(list);
79 }
80 }
81 }
82
83 return ret;
84 }
85
86
87 string itostring(unsigned long i) {
88 if (i == 0)
89 return string("0");
90
91 string tmp;
92 for (; i > 0; i /= 10)
93 tmp.insert(tmp.begin(), "0123456789"[i%10]);
94 return tmp;
95 }
96
97
98 string itostring(long i) {
99 std::string tmp = itostring( (unsigned long) std::abs(i));
100 if (i < 0)
101 tmp.insert(tmp.begin(), '-');
102 return tmp;
103 }
104
105 string basename (const string& path) {
106 string::size_type slash = path.rfind('/');
107 if (slash == string::npos)
108 return path;
109 return path.substr(slash+1);
110 }
111
112 }
113
This page took 0.038806 seconds and 4 git commands to generate.