util.h (3912B)
1 // Copyright (c) 2003 - 2009 Anselm R Garbe <anselm@garbe.us> 2 // See LICENSE for license details. 3 4 #ifndef __UTIL_H 5 #define __UTIL_H 6 7 extern "C" { 8 #include <X11/keysym.h> 9 #include <X11/Xlib.h> 10 #include <sys/stat.h> 11 } 12 13 #include <list> 14 #include <string> 15 #include <set> 16 #include <map> 17 18 #include "wmi.h" 19 20 class Action; 21 class Rectangle; 22 23 typedef map<string, string, less<string> > MSettings; 24 typedef map<string, Action *, less<string> > MBindings; 25 26 /** 27 * Contains various helper methods which are missing in STL library. 28 */ 29 class Util 30 { 31 32 public: 33 34 Util(); 35 36 ~Util(); 37 38 /** Returns n'th Token (substring) of str, delimeted by delim. */ 39 static string nthToken(string str, char delim, unsigned int n); 40 41 /** Returns last token of str, delimeted by delim. */ 42 static string lastToken(string str, char delim); 43 44 static string lowerCase(string str); 45 46 /** Returns the canonical path of the given file string. */ 47 static string canonicalPath(string file); 48 49 /** Returns all dir components of the given path as new string. */ 50 static string canonicalDir(string path); 51 52 static bool containsWhitespaces(string str); 53 54 /** Returns truncated string without characters given by c or \t. */ 55 static string truncate(string str, char c); 56 57 /** Returns string with swapped characters (from -> to). */ 58 static void swapChars(string *str, char from, char to, 59 unsigned int *swapCount = 0); 60 61 62 /** Shortens this string to n characters plus "..." */ 63 static string shortenString(string str, unsigned int n); 64 65 static string encodeEscapes(string str); 66 67 static unsigned int numDigits(unsigned int number); 68 69 static unsigned long modMaskForString(string modKeys); 70 static string stringForModMask(unsigned long modMask); 71 72 static string stringForButton(unsigned int button); 73 static unsigned int buttonForString(string button); 74 75 /** Converts string into char * argument array. */ 76 static char **arguments(string command); 77 78 /** Returns timestamp of current system time. */ 79 static string timestamp(); 80 81 static unsigned int strToUInt(string str); 82 83 /** Fits client rectangle into host rectangle. */ 84 static void fitInto(Rectangle *client, Rectangle *host); 85 86 /** Rectangle <code>true</code> if the coord is within the rectangle. */ 87 static bool isPointWithinRect(unsigned int x, unsigned int y, Rectangle *rect, 88 Rectangle *offset = 0); 89 90 /** Returns direction for the given string. */ 91 static Direction dirForString(string str); 92 93 static bool exists(const char *path); 94 95 static set<string> stringSplit(string s, string delimiter); 96 97 /** removes leading blanks */ 98 static string trimL(string s); 99 100 static Direction reverseDir(Direction dir); 101 102 /** 103 * Deletes all elements of a list L, between iterator start and end 104 * with type T. 105 */ 106 template <class L, class LTI, class T> 107 static void destroy(L *l, LTI start, LTI end) { 108 for (LTI it = start; it != end; it++) { 109 T t = (T)*it; 110 delete t; 111 } 112 113 l->erase(start, end); 114 } 115 116 /** 117 * Returns <code>true</code> if the item is contained in this list. 118 */ 119 template <class L, class LTI, class T> 120 static bool contains(L *l, T *item) { 121 for (LTI it = l->begin(); it != l->end(); it++) { 122 if (item == *it) { 123 return true; 124 } 125 } 126 return false; 127 } 128 129 static string get(MSettings *settings, string key); 130 static void remove(MSettings *settings, string prefix); 131 132 static Action *get(MBindings *bindings, string key); 133 static void remove(MBindings *bindings, string prefix); 134 135 private: 136 137 /** Fits 1-dimensional c-coordinates directional into h-coordinates. */ 138 static void fitIntoDirection(int horigin, int hw, int *corigin, int *cw); 139 140 }; 141 142 #endif // __UTIL_H