_________ __                 __
        /   _____//  |_____________ _/  |______     ____  __ __  ______
        \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
        /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ \
       /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
               \/                  \/          \//_____/            \/
    ______________________                           ______________________
                          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 #include <cstdlib>
36 #include <cstdint>
37 #include <string>
38 
39 /*----------------------------------------------------------------------------
40 -- Random
41 ----------------------------------------------------------------------------*/
42 
43 #include <cmath>
44 
45 extern unsigned SyncRandSeed;
46 extern uint32_t FileChecksums;
47 
48 extern void InitSyncRand();
49 extern int SyncRand();
50 extern int SyncRand(int max);
51 
53 extern int MyRand();
54 
55 /*----------------------------------------------------------------------------
56 -- Math
57 ----------------------------------------------------------------------------*/
58 
60 extern long isqrt(long num);
61 
62 inline int square(int v)
63 {
64  return v * v;
65 }
66 
67 template <typename T>
68 void clamp(T *value, T minValue, T maxValue)
69 {
70  Assert(minValue <= maxValue);
71 
72  if (*value < minValue) {
73  *value = minValue;
74  } else if (maxValue < *value) {
75  *value = maxValue;
76  }
77 }
78 
79 extern uint32_t fletcher32(const std::string &content);
80 
81 /*----------------------------------------------------------------------------
82 -- Strings
83 ----------------------------------------------------------------------------*/
84 
85 #include <string.h>
86 
87 #ifndef _TRUNCATE
88 #define _TRUNCATE ((size_t)-1)
89 #endif
90 
91 #ifndef HAVE_ERRNOT
92 typedef int errno_t;
93 #endif
94 
95 #ifndef HAVE_STRCPYS
96 extern errno_t strcpy_s(char *dst, size_t dstsize, const char *src);
97 #endif
98 
99 #ifndef HAVE_STRNCPYS
100 extern errno_t strncpy_s(char *dst, size_t dstsize, const char *src, size_t count);
101 #endif
102 
103 #ifndef HAVE_STRCATS
104 extern errno_t strcat_s(char *dst, size_t dstsize, const char *src);
105 #endif
106 
107 #ifndef HAVE_STRCASESTR
108 extern char *strcasestr(const char *str, const char *substr);
110 #endif // !HAVE_STRCASESTR
111 
112 #ifndef HAVE_STRNLEN
113 extern size_t strnlen(const char *str, size_t strsize);
115 #endif // !HAVE_STRNLEN
116 
117 /*----------------------------------------------------------------------------
118 -- Getopt
119 ----------------------------------------------------------------------------*/
120 
121 #ifdef HAVE_GETOPT
122 #include <unistd.h>
123 #else
124 extern char *optarg;
125 extern int optind, opterr, optopt;
126 int getopt(int argc, char *const argv[], const char *optstring);
127 #endif
128 
129 /*----------------------------------------------------------------------------
130 -- Clipboard
131 ----------------------------------------------------------------------------*/
132 
133 #include <string>
134 
135 int GetClipboard(std::string &str);
136 void SetClipboard(std::string &str);
137 
138 /*----------------------------------------------------------------------------
139 -- UTF8
140 ----------------------------------------------------------------------------*/
141 
142 int UTF8GetNext(const std::string &text, int curpos);
143 int UTF8GetPrev(const std::string &text, int curpos);
144 
145 /*----------------------------------------------------------------------------
146 -- SIMD support
147 ----------------------------------------------------------------------------*/
148 bool supportsSSE2();
149 bool supportsAVX();
150 void *aligned_malloc(size_t alignment, size_t size);
151 void aligned_free(void *block);
152 
154 
155 #endif /* __UTIL_H__ */
fletcher32
uint32_t fletcher32(const std::string &content)
Definition: util.cpp:173
supportsSSE2
bool supportsSSE2()
Definition: util.cpp:619
FileChecksums
uint32_t FileChecksums
Sync random seed value.
Definition: util.cpp:63
strncpy_s
errno_t strncpy_s(char *dst, size_t dstsize, const char *src, size_t count)
Definition: util.cpp:246
InitSyncRand
void InitSyncRand()
checksums of all loaded lua files
Definition: util.cpp:68
strcpy_s
errno_t strcpy_s(char *dst, size_t dstsize, const char *src)
Definition: util.cpp:217
supportsAVX
bool supportsAVX()
Definition: util.cpp:624
optopt
int optopt
Definition: util.h:125
aligned_free
void aligned_free(void *block)
Definition: util.cpp:642
SyncRandSeed
unsigned SyncRandSeed
Definition: util.cpp:62
aligned_malloc
void * aligned_malloc(size_t alignment, size_t size)
Definition: util.cpp:631
SyncRand
int SyncRand()
Initialize the syncron rand.
Definition: util.cpp:84
errno_t
int errno_t
Definition: util.h:92
square
int square(int v)
Definition: util.h:62
MyRand
int MyRand()
Syncron rand.
Definition: util.cpp:114
UTF8GetNext
int UTF8GetNext(const std::string &text, int curpos)
Definition: util.cpp:453
GetClipboard
int GetClipboard(std::string &str)
Definition: util.cpp:415
opterr
int opterr
Definition: util.h:125
clamp
void clamp(T *value, T minValue, T maxValue)
Definition: util.h:68
SetClipboard
void SetClipboard(std::string &str)
Definition: util.cpp:427
optind
int optind
Definition: util.cpp:349
optarg
char * optarg
Definition: util.cpp:351
isqrt
long isqrt(long num)
Compute a square root using ints.
Definition: util.cpp:133
strnlen
size_t strnlen(const char *str, size_t strsize)
determine length of a fixed-length string
Definition: util.cpp:231
getopt
int getopt(int argc, char *const argv[], const char *optstring)
Assert
#define Assert(cond)
Definition: stratagus.h:142
strcasestr
char * strcasestr(const char *str, const char *substr)
case insensitive strstr
Definition: util.cpp:306
UTF8GetPrev
int UTF8GetPrev(const std::string &text, int curpos)
Definition: util.cpp:435
strcat_s
errno_t strcat_s(char *dst, size_t dstsize, const char *src)
Definition: util.cpp:275
(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.