_________ __                 __
        /   _____//  |_____________ _/  |______     ____  __ __  ______
        \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
        /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ \
       /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
               \/                  \/          \//_____/            \/
    ______________________                           ______________________
                          T H E   W A R   B E G I N S
                   Stratagus - A free fantasy real time strategy game engine

util.h
Go to the documentation of this file.
1 // _________ __ __
2 // / _____// |_____________ _/ |______ ____ __ __ ______
3 // \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
4 // / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
5 // /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
6 // \/ \/ \//_____/ \/
7 // ______________________ ______________________
8 // T H E W A R B E G I N S
9 // Stratagus - A free fantasy real time strategy game engine
10 //
12 //
13 // (c) Copyright 1998-2006 by Lutz Sammer and Jimmy Salmon
14 //
15 // This program is free software; you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation; only version 2 of the License.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 // 02111-1307, USA.
28 //
29 
30 #ifndef __UTIL_H__
31 #define __UTIL_H__
32 
34 
35 #ifdef USE_WIN32
36 #undef NOUSER
37 #ifndef _WIN32_WINNT
38 #define _WIN32_WINNT 0x0400
39 #endif
40 #endif
41 
42 #include <cstdlib>
43 
44 /*----------------------------------------------------------------------------
45 -- Random
46 ----------------------------------------------------------------------------*/
47 
48 #include <cmath>
49 
50 extern unsigned SyncRandSeed;
51 
52 extern void InitSyncRand();
53 extern int SyncRand();
54 extern int SyncRand(int max);
55 
57 extern int MyRand();
58 
59 /*----------------------------------------------------------------------------
60 -- Math
61 ----------------------------------------------------------------------------*/
62 
64 extern long isqrt(long num);
65 
66 inline int square(int v)
67 {
68  return v * v;
69 }
70 
71 template <typename T>
72 void clamp(T *value, T minValue, T maxValue)
73 {
74  Assert(minValue <= maxValue);
75 
76  if (*value < minValue) {
77  *value = minValue;
78  } else if (maxValue < *value) {
79  *value = maxValue;
80  }
81 }
82 
83 
84 /*----------------------------------------------------------------------------
85 -- Strings
86 ----------------------------------------------------------------------------*/
87 
88 #include <string.h>
89 
90 #ifndef _TRUNCATE
91 #define _TRUNCATE ((size_t)-1)
92 #endif
93 
94 #ifndef HAVE_ERRNOT
95 typedef int errno_t;
96 #endif
97 
98 #ifndef HAVE_STRCPYS
99 extern errno_t strcpy_s(char *dst, size_t dstsize, const char *src);
100 #endif
101 
102 #ifndef HAVE_STRNCPYS
103 extern errno_t strncpy_s(char *dst, size_t dstsize, const char *src, size_t count);
104 #endif
105 
106 #ifndef HAVE_STRCATS
107 extern errno_t strcat_s(char *dst, size_t dstsize, const char *src);
108 #endif
109 
110 #ifndef HAVE_STRCASESTR
111 extern char *strcasestr(const char *str, const char *substr);
113 #endif // !HAVE_STRCASESTR
114 
115 #ifndef HAVE_STRNLEN
116 extern size_t strnlen(const char *str, size_t strsize);
118 #endif // !HAVE_STRNLEN
119 
120 /*----------------------------------------------------------------------------
121 -- Getopt
122 ----------------------------------------------------------------------------*/
123 
124 #ifdef HAVE_GETOPT
125 #include <unistd.h>
126 #else
127 extern char *optarg;
128 extern int optind, opterr, optopt;
129 int getopt(int argc, char *const argv[], const char *optstring);
130 #endif
131 
132 /*----------------------------------------------------------------------------
133 -- Clipboard
134 ----------------------------------------------------------------------------*/
135 
136 #include <string>
137 
138 int GetClipboard(std::string &str);
139 
140 /*----------------------------------------------------------------------------
141 -- UTF8
142 ----------------------------------------------------------------------------*/
143 
144 int UTF8GetNext(const std::string &text, int curpos);
145 int UTF8GetPrev(const std::string &text, int curpos);
146 
148 
149 #endif /* __UTIL_H__ */
void InitSyncRand()
Sync random seed value.
Definition: util.cpp:64
int optind
Definition: util.cpp:293
int UTF8GetPrev(const std::string &text, int curpos)
Definition: util.cpp:454
long isqrt(long num)
Compute a square root using ints.
Definition: util.cpp:117
errno_t strcpy_s(char *dst, size_t dstsize, const char *src)
Definition: util.cpp:161
errno_t strcat_s(char *dst, size_t dstsize, const char *src)
Definition: util.cpp:219
int square(int v)
Definition: util.h:66
void clamp(T *value, T minValue, T maxValue)
Definition: util.h:72
int optopt
Definition: util.cpp:294
int opterr
Definition: util.cpp:292
int errno_t
Definition: util.h:95
#define Assert(cond)
Definition: stratagus.h:120
char * strcasestr(const char *str, const char *substr)
case insensitive strstr
Definition: util.cpp:250
int getopt(int argc, char *const argv[], const char *optstring)
errno_t strncpy_s(char *dst, size_t dstsize, const char *src, size_t count)
Definition: util.cpp:190
size_t strnlen(const char *str, size_t strsize)
determine length of a fixed-length string
Definition: util.cpp:175
int SyncRand()
Initialize the syncron rand.
Definition: util.cpp:75
unsigned SyncRandSeed
Definition: util.cpp:59
int GetClipboard(std::string &str)
Definition: util.cpp:359
int MyRand()
Syncron rand.
Definition: util.cpp:98
char * optarg
Definition: util.cpp:295
int UTF8GetNext(const std::string &text, int curpos)
Definition: util.cpp:472
(C) Copyright 1998-2012 by The Stratagus Project under the GNU General Public License.
All trademarks and copyrights on this page are owned by their respective owners.