| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name util.h - General utilities. */ |
| 11 |
// |
| 12 |
// (c) Copyright 1998-2008 by Lutz Sammer and Jimmy Salmon |
| 13 |
// |
| 14 |
// This program is free software; you can redistribute it and/or modify |
| 15 |
// it under the terms of the GNU General Public License as published by |
| 16 |
// the Free Software Foundation; only version 2 of the License. |
| 17 |
// |
| 18 |
// This program is distributed in the hope that it will be useful, |
| 19 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
// GNU General Public License for more details. |
| 22 |
// |
| 23 |
// You should have received a copy of the GNU General Public License |
| 24 |
// along with this program; if not, write to the Free Software |
| 25 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 26 |
// 02111-1307, USA. |
| 27 |
|
| 28 |
#ifndef __UTIL_H__ |
| 29 |
#define __UTIL_H__ |
| 30 |
|
| 31 |
//@{ |
| 32 |
|
| 33 |
/*---------------------------------------------------------------------------- |
| 34 |
-- Function attributes |
| 35 |
----------------------------------------------------------------------------*/ |
| 36 |
|
| 37 |
// Most *.cpp files include "stratagus.h", which includes "util.h". |
| 38 |
// The guichan *.cpp files do not include "stratagus.h", but some of them |
| 39 |
// do include "util.h". Because we want to use GCC_ATTR in the sprintf_s |
| 40 |
// declaration in "util.h", define GCC_ATTR here. |
| 41 |
|
| 42 |
#ifdef __GNUC__ |
| 43 |
# define GCC_ATTR(list) __attribute__(list) |
| 44 |
#else |
| 45 |
# define GCC_ATTR(list) |
| 46 |
#endif |
| 47 |
|
| 48 |
/*---------------------------------------------------------------------------- |
| 49 |
-- Random |
| 50 |
----------------------------------------------------------------------------*/ |
| 51 |
|
| 52 |
extern unsigned SyncRandSeed; /// Sync random seed value |
| 53 |
|
| 54 |
extern void InitSyncRand(void); /// Initialize the syncron rand |
| 55 |
extern int SyncRand(void); /// Syncron rand |
| 56 |
extern int SyncRand(int max); /// Syncron rand |
| 57 |
|
| 58 |
/*---------------------------------------------------------------------------- |
| 59 |
-- Math |
| 60 |
----------------------------------------------------------------------------*/ |
| 61 |
|
| 62 |
/// rand only used on this computer. |
| 63 |
#define MyRand() rand() |
| 64 |
|
| 65 |
/// Compute a square root using ints |
| 66 |
extern long isqrt(long num); |
| 67 |
|
| 68 |
/*---------------------------------------------------------------------------- |
| 69 |
-- Strings |
| 70 |
----------------------------------------------------------------------------*/ |
| 71 |
|
| 72 |
#include <string.h> |
| 73 |
|
| 74 |
#if !defined(_MSC_VER) || _MSC_VER < 1400 |
| 75 |
#define _TRUNCATE ((size_t)-1) |
| 76 |
extern unsigned int strcpy_s(char *dst, size_t dstsize, const char *src); |
| 77 |
extern unsigned int strncpy_s(char *dst, size_t dstsize, const char *src, size_t count); |
| 78 |
extern unsigned int strcat_s(char *dst, size_t dstsize, const char *src); |
| 79 |
extern int sprintf_s(char *dest, size_t destSize, const char *format, ...) GCC_ATTR((format(printf, 3, 4))); |
| 80 |
#endif |
| 81 |
|
| 82 |
inline char *new_strdup(const char *str) |
| 83 |
{ |
| 84 |
size_t len = strlen(str) + 1; |
| 85 |
char *newstr = new char[len]; |
| 86 |
strcpy_s(newstr, len, str); |
| 87 |
return newstr; |
| 88 |
} |
| 89 |
|
| 90 |
/// strdup + strcat |
| 91 |
extern char *strdcat(const char *l, const char *r); |
| 92 |
/// strdup + strcat + strcat |
| 93 |
extern char *strdcat3(const char *l, const char *m, const char *r); |
| 94 |
|
| 95 |
#ifndef HAVE_STRCASESTR |
| 96 |
/// case insensitive strstr |
| 97 |
extern char *strcasestr(const char *str, const char *substr); |
| 98 |
#endif // !HAVE_STRCASESTR |
| 99 |
|
| 100 |
#ifndef HAVE_STRNLEN |
| 101 |
/// determine length of a fixed-length string |
| 102 |
extern size_t strnlen(const char *str, size_t strsize); |
| 103 |
#endif // !HAVE_STRNLEN |
| 104 |
|
| 105 |
/*---------------------------------------------------------------------------- |
| 106 |
-- Clipboard |
| 107 |
----------------------------------------------------------------------------*/ |
| 108 |
|
| 109 |
#include <string> |
| 110 |
|
| 111 |
int GetClipboard(std::string &str); |
| 112 |
|
| 113 |
/*---------------------------------------------------------------------------- |
| 114 |
-- UTF8 |
| 115 |
----------------------------------------------------------------------------*/ |
| 116 |
|
| 117 |
int UTF8GetNext(const std::string &text, int curpos); |
| 118 |
int UTF8GetPrev(const std::string &text, int curpos); |
| 119 |
|
| 120 |
//@} |
| 121 |
|
| 122 |
#endif /* __UTIL_H__ */ |