Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

/otk/util.cc

Go to the documentation of this file.
00001 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
00002 
00003 #ifdef    HAVE_CONFIG_H
00004 #  include "../config.h"
00005 #endif // HAVE_CONFIG_H
00006 
00007 extern "C" {
00008 #include <X11/Xatom.h>
00009 
00010 #ifdef HAVE_STDIO_H
00011 #include <stdio.h>
00012 #endif
00013 
00014 #ifdef HAVE_STRING_H
00015 #include <string.h>
00016 #endif
00017 
00018 #ifdef HAVE_STDLIB_H
00019 #include <stdlib.h>
00020 #endif
00021 
00022 #ifdef HAVE_UNISTD_H
00023 #include <unistd.h>
00024 #endif // HAVE_UNISTD_H
00025 
00026 #if defined(HAVE_PROCESS_H) && defined(__EMX__)
00027 #  include <process.h>
00028 #endif //   HAVE_PROCESS_H             __EMX__
00029 
00030 #include "../src/gettext.h"
00031 #define _(str) gettext(str)
00032 
00033 #include <assert.h>
00034 }
00035 
00036 #include <algorithm>
00037 
00038 #include "util.hh"
00039 
00040 using std::string;
00041 
00042 namespace otk {
00043 
00044 string expandTilde(const string& s) {
00045   if (s[0] != '~') return s;
00046 
00047   const char* const home = getenv("HOME");
00048   if (home == NULL) return s;
00049 
00050   return string(home + s.substr(s.find('/')));
00051 }
00052 
00053 
00054 void bexec(const string& command, const string& displaystring) {
00055 #ifndef    __EMX__
00056   if (! fork()) {
00057     setsid();
00058     putenv(displaystring);
00059     int ret = execl("/bin/sh", "/bin/sh", "-c", command.c_str(), NULL);
00060     exit(ret);
00061   }
00062 #else //   __EMX__
00063   spawnlp(P_NOWAIT, "cmd.exe", "cmd.exe", "/c", command.c_str(), NULL);
00064 #endif // !__EMX__
00065 }
00066 
00067 
00068 string itostring(unsigned long i) {
00069   if (i == 0)
00070     return string("0");
00071   
00072   string tmp;
00073   for (; i > 0; i /= 10)
00074     tmp.insert(tmp.begin(), "0123456789"[i%10]);
00075   return tmp;
00076 }
00077 
00078 
00079 string itostring(long i) {
00080   std::string tmp = itostring( (unsigned long) std::abs(i));
00081   if (i < 0)
00082     tmp.insert(tmp.begin(), '-');
00083   return tmp;
00084 }
00085 
00086 void putenv(const std::string &data)
00087 {
00088   char *c = new char[data.size() + 1];
00089   std::string::size_type i, max;
00090   for (i = 0, max = data.size(); i < max; ++i)
00091     c[i] = data[i];
00092   c[i] = 0;
00093   if (::putenv(c)) {
00094     printf(_("warning: couldn't set environment variable\n"));
00095     perror("putenv()");
00096   }
00097 }
00098 
00099 string basename (const string& path) {
00100   string::size_type slash = path.rfind('/');
00101   if (slash == string::npos)
00102     return path;
00103   return path.substr(slash+1);
00104 }
00105 
00106 }
00107 

Generated on Tue Feb 4 22:58:57 2003 for Openbox by doxygen1.3-rc2