]> Dogcows Code - chaz/openbox/blob - otk/util.cc
off-by-one
[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_STDIO_H
11 #include <stdio.h>
12 #endif
13
14 #ifdef HAVE_STRING_H
15 #include <string.h>
16 #endif
17
18 #ifdef HAVE_STDLIB_H
19 #include <stdlib.h>
20 #endif
21
22 #ifdef HAVE_UNISTD_H
23 #include <unistd.h>
24 #endif // HAVE_UNISTD_H
25
26 #if defined(HAVE_PROCESS_H) && defined(__EMX__)
27 # include <process.h>
28 #endif // HAVE_PROCESS_H __EMX__
29
30 #include "../src/gettext.h"
31 #define _(str) gettext(str)
32
33 #include <assert.h>
34 }
35
36 #include <algorithm>
37
38 #include "util.hh"
39
40 using std::string;
41
42 namespace otk {
43
44 string expandTilde(const string& s) {
45 if (s[0] != '~') return s;
46
47 const char* const home = getenv("HOME");
48 if (home == NULL) return s;
49
50 return string(home + s.substr(s.find('/')));
51 }
52
53
54 void bexec(const string& command, const string& displaystring) {
55 #ifndef __EMX__
56 if (! fork()) {
57 setsid();
58 putenv(displaystring);
59 int ret = execl("/bin/sh", "/bin/sh", "-c", command.c_str(), NULL);
60 exit(ret);
61 }
62 #else // __EMX__
63 spawnlp(P_NOWAIT, "cmd.exe", "cmd.exe", "/c", command.c_str(), NULL);
64 #endif // !__EMX__
65 }
66
67
68 string itostring(unsigned long i) {
69 if (i == 0)
70 return string("0");
71
72 string tmp;
73 for (; i > 0; i /= 10)
74 tmp.insert(tmp.begin(), "0123456789"[i%10]);
75 return tmp;
76 }
77
78
79 string itostring(long i) {
80 std::string tmp = itostring( (unsigned long) std::abs(i));
81 if (i < 0)
82 tmp.insert(tmp.begin(), '-');
83 return tmp;
84 }
85
86 void putenv(const std::string &data)
87 {
88 char *c = new char[data.size() + 1];
89 std::string::size_type i, max;
90 for (i = 0, max = data.size(); i < max; ++i)
91 c[i] = data[i];
92 c[i] = 0;
93 if (::putenv(c)) {
94 printf(_("warning: couldn't set environment variable\n"));
95 perror("putenv()");
96 }
97 }
98
99 string basename (const string& path) {
100 string::size_type slash = path.rfind('/');
101 if (slash == string::npos)
102 return path;
103 return path.substr(slash+1);
104 }
105
106 }
107
This page took 0.038525 seconds and 4 git commands to generate.