Skip to content

Commit

Permalink
cleanup convenience functions in Util.hh
Browse files Browse the repository at this point in the history
remove all X11 headers from Util.hh... if X11/Xutil.h is included
before Util.hh, then bt::textPropertyToString() is available.
  • Loading branch information
bradleyhughes committed Oct 22, 2003
1 parent 9f419b1 commit 8492344
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 93 deletions.
12 changes: 7 additions & 5 deletions lib/Menu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

#include "Menu.hh"
#include "Application.hh"
#include "Display.hh"
#include "Pen.hh"
#include "PixmapCache.hh"
#include "Resource.hh"

extern "C" {
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <stdio.h>
#include <assert.h>
}

#include "Menu.hh"
#include "Application.hh"
#include "Pen.hh"
#include "PixmapCache.hh"
#include "Resource.hh"

static const unsigned int arrow_width = 7;
static const unsigned int arrow_height = 7;
Expand Down
89 changes: 28 additions & 61 deletions lib/Util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,29 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

#ifdef HAVE_CONFIG_H
# include "../config.h"
#endif // HAVE_CONFIG_H
#include <X11/Xlib.h>
#include <X11/Xutil.h>

extern "C" {
#include <X11/Xatom.h>
#include "Util.hh"

#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif // HAVE_UNISTD_H
#include <X11/Xatom.h>
#include <assert.h>
#if defined(HAVE_PROCESS_H) && defined(__EMX__)
# include <process.h>
#endif // HAVE_PROCESS_H __EMX__

#include <assert.h>
}
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <algorithm>

#include "Util.hh"


#ifndef HAVE_BASENAME
std::string basename (const std::string& path) {
std::string bt::basename (const std::string& path) {
std::string::size_type slash = path.rfind('/');
if (slash == std::string::npos)
return path;
return path.substr(slash+1);
}
#endif // HAVE_BASENAME


std::string bt::expandTilde(const std::string& s) {
Expand Down Expand Up @@ -89,31 +76,6 @@ void bt::bexec(const std::string& command, const std::string& displaystring) {
}


std::string bt::textPropertyToString(Display *display,
XTextProperty& text_prop) {
std::string ret;

if (text_prop.value && text_prop.nitems > 0) {
if (text_prop.encoding == XA_STRING) {
ret = reinterpret_cast<char *>(text_prop.value);
} else {
text_prop.nitems = strlen(reinterpret_cast<char *>(text_prop.value));

char **list;
int num;
if (XmbTextPropertyToTextList(display, &text_prop,
&list, &num) == Success &&
num > 0 && *list) {
ret = *list;
XFreeStringList(list);
}
}
}

return ret;
}


std::string bt::itostring(unsigned long i) {
if (i == 0)
return std::string("0");
Expand All @@ -135,21 +97,26 @@ std::string bt::itostring(long i) {
}


std::string bt::itostring(unsigned int i) {
return bt::itostring(static_cast<unsigned long>(i));
}


std::string bt::itostring(int i) {
return bt::itostring(static_cast<long>(i));
}

std::string bt::textPropertyToString(::Display *display,
::XTextProperty& text_prop) {
std::string ret;

std::string bt::itostring(unsigned short i) {
return bt::itostring(static_cast<unsigned long>(i));
}
if (text_prop.value && text_prop.nitems > 0) {
if (text_prop.encoding == XA_STRING) {
ret = reinterpret_cast<char *>(text_prop.value);
} else {
text_prop.nitems = strlen(reinterpret_cast<char *>(text_prop.value));

char **list;
int num;
if (XmbTextPropertyToTextList(display, &text_prop,
&list, &num) == Success &&
num > 0 && *list) {
ret = *list;
XFreeStringList(list);
}
}
}

std::string bt::itostring(short i) {
return bt::itostring(static_cast<long>(i));
return ret;
}
77 changes: 50 additions & 27 deletions lib/Util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -25,54 +25,77 @@
#ifndef __Util_hh
#define __Util_hh

extern "C" {
#include <X11/Xlib.h>
#include <X11/Xutil.h>
}

#include <limits.h>
#include <string>

#ifndef HAVE_BASENAME
std::string basename(const std::string& path);
#endif
typedef struct _XDisplay Display;
typedef union _XEvent XEvent;
typedef struct _XGC *GC;

typedef unsigned long Time;

typedef unsigned long XID;
typedef XID Atom;
typedef XID Colormap;
typedef XID Cursor;
typedef XID Drawable;
typedef XID Pixmap;
typedef XID Window;

#ifndef _XLIB_H_
# define GXcopy 3
# define ClipByChildren 0
# define None 0
#endif

namespace bt {

// XXX perhaps we could just call this SENTINEL?
const unsigned int BSENTINEL = UINT_MAX;

class NoCopy {
protected:
NoCopy(void) {}
inline NoCopy(void) { }
private:
NoCopy(const NoCopy&);
NoCopy& operator=(const NoCopy&);
};

inline bool within(int x, int y, int width, int height) {
return ((x >= 0 && x <= width) && (y >= 0 && y <= height));
}

/* XXX: this needs autoconf help */
const unsigned int BSENTINEL = 65535;
class PointerAssassin {
public:
template<typename T>
inline void operator()(const T ptr) const
{ delete ptr; }
};

std::string expandTilde(const std::string& s);
inline bool within(int x, int y, int width, int height)
{ return ((x >= 0 && x <= width) && (y >= 0 && y <= height)); }

void bexec(const std::string& command, const std::string& displaystring);

std::string textPropertyToString(Display *display, XTextProperty& text_prop);
std::string basename(const std::string& path);

struct PointerAssassin {
template<typename T>
void operator()(const T ptr) const {
delete ptr;
}
};
std::string expandTilde(const std::string& s);

std::string itostring(unsigned long i);
std::string itostring(long i);
std::string itostring(unsigned int i);
std::string itostring(int i);
std::string itostring(unsigned short i);
std::string itostring(short i);

inline std::string itostring(unsigned int i)
{ return itostring(static_cast<unsigned long>(i)); }

inline std::string itostring(int i)
{ return itostring(static_cast<long>(i)); }

inline std::string itostring(unsigned short i)
{ return itostring(static_cast<unsigned long>(i)); }

inline std::string itostring(short i)
{ return itostring(static_cast<long>(i)); }

#ifdef _XUTIL_H_
std::string textPropertyToString(::Display *display,
::XTextProperty& text_prop);
#endif

} // namespace bt

Expand Down

0 comments on commit 8492344

Please sign in to comment.