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

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 #include <cstdint>
44 #include <string>
45 
46 /*----------------------------------------------------------------------------
47 -- Random
48 ----------------------------------------------------------------------------*/
49 
50 #include <cmath>
51 
52 extern unsigned SyncRandSeed;
53 extern uint32_t FileChecksums;
54 
55 extern void InitSyncRand();
56 extern int SyncRand();
57 extern int SyncRand(int max);
58 
60 extern int MyRand();
61 
62 /*----------------------------------------------------------------------------
63 -- Math
64 ----------------------------------------------------------------------------*/
65 
67 extern long isqrt(long num);
68 
69 inline int square(int v)
70 {
71  return v * v;
72 }
73 
74 template <typename T>
75 void clamp(T *value, T minValue, T maxValue)
76 {
77  Assert(minValue <= maxValue);
78 
79  if (*value < minValue) {
80  *value = minValue;
81  } else if (maxValue < *value) {
82  *value = maxValue;
83  }
84 }
85 
86 extern uint32_t fletcher32(const std::string &content);
87 
88 /*----------------------------------------------------------------------------
89 -- Strings
90 ----------------------------------------------------------------------------*/
91 
92 #include <string.h>
93 
94 #ifndef _TRUNCATE
95 #define _TRUNCATE ((size_t)-1)
96 #endif
97 
98 #ifndef HAVE_ERRNOT
99 typedef int errno_t;
100 #endif
101 
102 #ifndef HAVE_STRCPYS
103 extern errno_t strcpy_s(char *dst, size_t dstsize, const char *src);
104 #endif
105 
106 #ifndef HAVE_STRNCPYS
107 extern errno_t strncpy_s(char *dst, size_t dstsize, const char *src, size_t count);
108 #endif
109 
110 #ifndef HAVE_STRCATS
111 extern errno_t strcat_s(char *dst, size_t dstsize, const char *src);
112 #endif
113 
114 #ifndef HAVE_STRCASESTR
115 extern char *strcasestr(const char *str, const char *substr);
117 #endif // !HAVE_STRCASESTR
118 
119 #ifndef HAVE_STRNLEN
120 extern size_t strnlen(const char *str, size_t strsize);
122 #endif // !HAVE_STRNLEN
123 
124 /*----------------------------------------------------------------------------
125 -- Getopt
126 ----------------------------------------------------------------------------*/
127 
128 #ifdef HAVE_GETOPT
129 #include <unistd.h>
130 #else
131 extern char *optarg;
132 extern int optind, opterr, optopt;
133 int getopt(int argc, char *const argv[], const char *optstring);
134 #endif
135 
136 /*----------------------------------------------------------------------------
137 -- Clipboard
138 ----------------------------------------------------------------------------*/
139 
140 #include <string>
141 
142 int GetClipboard(std::string &str);
143 
144 /*----------------------------------------------------------------------------
145 -- UTF8
146 ----------------------------------------------------------------------------*/
147 
148 int UTF8GetNext(const std::string &text, int curpos);
149 int UTF8GetPrev(const std::string &text, int curpos);
150 
152 
153 #endif /* __UTIL_H__ */
void InitSyncRand()
checksums of all loaded lua files
Definition: util.cpp:65
int optind
Definition: util.cpp:317
int UTF8GetPrev(const std::string &text, int curpos)
Definition: util.cpp:478
long isqrt(long num)
Compute a square root using ints.
Definition: util.cpp:118
errno_t strcpy_s(char *dst, size_t dstsize, const char *src)
Definition: util.cpp:185
errno_t strcat_s(char *dst, size_t dstsize, const char *src)
Definition: util.cpp:243
int square(int v)
Definition: util.h:69
void clamp(T *value, T minValue, T maxValue)
Definition: util.h:75
int optopt
Definition: util.cpp:318
int opterr
Definition: util.cpp:316
uint32_t fletcher32(const std::string &content)
Definition: util.cpp:157
int errno_t
Definition: util.h:99
#define Assert(cond)
Definition: stratagus.h:119
char * strcasestr(const char *str, const char *substr)
case insensitive strstr
Definition: util.cpp:274
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:214
size_t strnlen(const char *str, size_t strsize)
determine length of a fixed-length string
Definition: util.cpp:199
int SyncRand()
Initialize the syncron rand.
Definition: util.cpp:76
unsigned SyncRandSeed
Definition: util.cpp:59
int GetClipboard(std::string &str)
Definition: util.cpp:383
int MyRand()
Syncron rand.
Definition: util.cpp:99
uint32_t FileChecksums
Sync random seed value.
Definition: util.cpp:60
char * optarg
Definition: util.cpp:319
int UTF8GetNext(const std::string &text, int curpos)
Definition: util.cpp:496
(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.