]> icculus.org git repositories - mikachu/openbox.git/blob - otk/util.cc
use the c++ std cheaders
[mikachu/openbox.git] / otk / util.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #include "config.h"
4
5 extern "C" {
6 #include <X11/Xatom.h>
7
8 #ifdef HAVE_UNISTD_H
9 #include <unistd.h>
10 #endif // HAVE_UNISTD_H
11
12 // this is not checked in configure anymore!!
13 //#if defined(HAVE_PROCESS_H) && defined(__EMX__)
14 //#  include <process.h>
15 //#endif //   HAVE_PROCESS_H             __EMX__
16
17 #include "../src/gettext.h"
18 #define _(str) gettext(str)
19
20 }
21
22 #include <algorithm>
23 #include <cassert>
24 #include <cstdio>
25 #include <cstring>
26 #include <cstdlib>
27
28 #include "util.hh"
29
30 using std::string;
31
32 namespace otk {
33
34 string expandTilde(const string& s) {
35   if (s[0] != '~') return s;
36
37   const char* const home = getenv("HOME");
38   if (home == NULL) return s;
39
40   return string(home + s.substr(s.find('/')));
41 }
42
43
44 void bexec(const string& command, const string& displaystring) {
45 //#ifndef    __EMX__
46   if (! fork()) {
47     setsid();
48     putenv(displaystring);
49     int ret = execl("/bin/sh", "/bin/sh", "-c", command.c_str(), NULL);
50     exit(ret);
51   }
52 //#else //   __EMX__
53 //  spawnlp(P_NOWAIT, "cmd.exe", "cmd.exe", "/c", command.c_str(), NULL);
54 //#endif // !__EMX__
55 }
56
57
58 string itostring(unsigned long i) {
59   if (i == 0)
60     return string("0");
61   
62   string tmp;
63   for (; i > 0; i /= 10)
64     tmp.insert(tmp.begin(), "0123456789"[i%10]);
65   return tmp;
66 }
67
68
69 string itostring(long i) {
70   std::string tmp = itostring( (unsigned long) std::abs(i));
71   if (i < 0)
72     tmp.insert(tmp.begin(), '-');
73   return tmp;
74 }
75
76 void putenv(const std::string &data)
77 {
78   char *c = new char[data.size() + 1];
79   std::string::size_type i, max;
80   for (i = 0, max = data.size(); i < max; ++i)
81     c[i] = data[i];
82   c[i] = 0;
83   if (::putenv(c)) {
84     printf(_("warning: couldn't set environment variable\n"));
85     perror("putenv()");
86   }
87 }
88
89 string basename (const string& path) {
90   string::size_type slash = path.rfind('/');
91   if (slash == string::npos)
92     return path;
93   return path.substr(slash+1);
94 }
95
96 }
97