Planeshift
psutil.h
Go to the documentation of this file.
1 /*
2  * Author: Andrew Robberts
3  *
4  * Copyright (C) 2004 Atomic Blue (info@planeshift.it, http://www.atomicblue.org)
5  *
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation (version 2 of the License)
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */
19 
20 #ifndef PSUTIL_H
21 #define PSUTIL_H
22 
23 #include <cstypes.h>
24 #include <csutil/csstring.h>
25 
30 // Colour support. Only supported by pawsMessageTextBox for now.
31 // 1 is a control character which should be filtered out of user text input.
32 // The code is a fixed-size of 8 bytes.
33 // byte 1 is 033 which is the control character
34 // 2-3 are the R value in hex
35 // 4-5 are the G value in hex
36 // 6-7 are the B value in hex
37 // 8 is the text size in decimal
38 
39 // Shortcut color/size codes
40 // Safe for use with tolower, toupper, UTF-8
41 #define REDCODE "\033ff00000"
42 #define GREENCODE "\03300ff000"
43 #define BLUECODE "\0330000ff0"
44 #define WHITECODE "\033ffffff0"
45 #define DEFAULTCODE "\0330000000"
46 #define ESCAPECODE '\033'
47 #define LENGTHCODE 8
48 
49 class psColours
50 {
51 public:
52  static bool ParseColour(const char* str, int& r, int& g, int& b, int& size)
53  {
54  if(strlen(str) < LENGTHCODE || str[0] != '\033')
55  return false;
56  str++;
57  r = DecodeHex(str, 2);
58  if(r == -1)
59  return false;
60  str += 2;
61  g = DecodeHex(str, 2);
62  if(g == -1)
63  return false;
64  str += 2;
65  b = DecodeHex(str, 2);
66  if(b == -1)
67  return false;
68  str += 2;
69  size = DecodeHex(str, 1);
70  if(size == -1)
71  return false;
72  str++;
73  return true;
74  }
75 
76  // Decode a hex string, can accept both uppercase and lowercase.
77  // -1 indicates an error occurred.
78  static int DecodeHex(const char* str, int len)
79  {
80  int count = 0;
81  for(int i = 0; i < len; i++)
82  {
83  if(str[i] >= 'a' && str[i] <= 'f')
84  count += (str[i] - 'a') + 10;
85  else if(str[i] >= '0' && str[i] <= '9')
86  count += (str[i] - '0');
87  else if(str[i] >= 'A' && str[i] <= 'F')
88  count += (str[i] - 'A') + 10;
89  else return -1;
90  count *= 16;
91  }
92  return count;
93  }
94 };
95 
99 void GetTimeOfDay(csString& string);
100 
101 struct psPoint
102 {
103  int x;
104  int y;
105 
106  psPoint ():x(0), y(0) { }
107 
109  psPoint (int iX, int iY):x(iX), y(iY) { }
110 };
111 
112 class ScopedTimer; // Forward declaration
113 
122 {
123 public:
124  virtual void ScopedTimerCallback(const ScopedTimer* timer) = 0;
125  virtual ~ScopedTimerCB() {}
126 };
127 
128 
137 {
138  csTicks start;
139  csTicks timeUsed;
140  csTicks limit;
141  ScopedTimerCB* callback;
142  csString comment;
143 
144 public:
145 
151  ScopedTimer(csTicks limit, const char * format, ... );
152 
158  ScopedTimer(csTicks limit, ScopedTimerCB* callback );
159 
163  ~ScopedTimer();
164 
168  csTicks TimeUsed() const;
169 };
170 
175 float psGetRandom();
176 
181 uint32 psGetRandom(uint32 limit);
182 
183 
184 
187 #endif
Check how long time it take to process a scope.
Definition: psutil.h:136
Callback function for ScopedTimers.
Definition: psutil.h:121
static int DecodeHex(const char *str, int len)
Definition: psutil.h:78
psPoint()
Definition: psutil.h:106
#define LENGTHCODE
Definition: psutil.h:47
float psGetRandom()
Returns a random number.
void GetTimeOfDay(csString &string)
Get the time of day in GMT.
int x
Definition: psutil.h:103
virtual ~ScopedTimerCB()
Definition: psutil.h:125
static bool ParseColour(const char *str, int &r, int &g, int &b, int &size)
Definition: psutil.h:52
psPoint(int iX, int iY)
Constructor: initialize the object with given values.
Definition: psutil.h:109
int y
Definition: psutil.h:104