UFO: Alien Invasion
Loading...
Searching...
No Matches
shared.h
Go to the documentation of this file.
1
4
5/*
6All original material Copyright (C) 2002-2025 UFO: Alien Invasion.
7
8Copyright (C) 1997-2001 Id Software, Inc.
9
10This program is free software; you can redistribute it and/or
11modify it under the terms of the GNU General Public License
12as published by the Free Software Foundation; either version 2
13of the License, or (at your option) any later version.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18
19See the GNU General Public License for more details.
20
21You should have received a copy of the GNU General Public License
22along with this program; if not, write to the Free Software
23Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24*/
25
26#pragma once
27
28#ifdef HAVE_CONFIG_H
29# ifdef ANDROID
31# else
32# include "../../config.h"
33# endif
34#endif
35
36#define MAX_VAR 64
37
38#include <errno.h>
39#include <assert.h>
40#include <math.h>
41#include <stdio.h>
42#include <stdarg.h>
43#include <string.h>
44#include <stdlib.h>
45#include <time.h>
46#include <ctype.h>
47#include <limits.h>
48#include <stddef.h>
49#include "ufotypes.h" /* needed for Com_sprintf */
50#include <algorithm>
51
52#include "sharedptr.h"
53#include "autoptr.h"
54#include "cxx.h"
55
56/* to support the gnuc __attribute__ command */
57#if defined __ICC || !defined __GNUC__
58# define __attribute__(x) /*NOTHING*/
59#endif
60
61#if defined(__GNUC__)
62#define UFO_DEPRECATED __attribute__ ((deprecated))
63#else
64#define UFO_DEPRECATED
65#endif
66
67#ifdef _WIN32
68# ifndef snprintf
69# define snprintf _snprintf
70# endif
71# define EXPORT
72# define IMPORT
73#else
74# define EXPORT
75# define IMPORT
76#endif
77
78#if !defined __cplusplus && (!defined __STDC_VERSION__ || __STDC_VERSION__ < 199901L)
79/* if we are using ansi - the compiler doesn't know about inline */
80# if defined __GNUC__
81# define inline __inline__
82# elif defined _MSVC
83# define inline __inline
84# else
85# define inline
86# endif
87#endif
88
89#define STRINGIFY(x) #x
90#define DOUBLEQUOTE(x) STRINGIFY(x)
91
92const char* Com_SkipPath(const char* pathname);
93void Com_ReplaceFilename(const char* fileName, const char* name, char* path, size_t size);
94void Com_StripExtension(const char* in, char* out, size_t size);
95void Com_FilePath(const char* in, char* out, size_t size);
96const char* Com_GetExtension(const char* path);
97void Com_DefaultExtension(char* path, size_t len, const char* extension);
98int Com_Filter(const char* pattern, const char* text);
99char* Com_Trim(char* s);
100char* Com_ConvertToASCII7(char* s);
101char* Com_Chop(char* s);
102bool Com_IsValidName(const char* input);
103
105#define lengthof(x) (sizeof(x) / sizeof(*(x)))
106#define endof(x) ((x) + lengthof((x)))
107#define CASSERT(x) extern int ASSERT_COMPILE[((x) != 0) * 2 - 1]
108
109const char* va(const char* format, ...) __attribute__((format(__printf__, 1, 2)));
110int Q_FloatSort(const void* float1, const void* float2);
111int Q_StringSort(const void* string1, const void* string2) __attribute__((nonnull));
112
113unsigned int Com_HashKey(const char* name, int hashsize);
114void Com_MakeTimestamp(char* ts, const size_t tslen);
115bool Com_sprintf(char* dest, size_t size, const char* fmt, ...) __attribute__((format(__printf__, 3, 4)));
116
118#if defined(__MINGW32_VERSION) && defined(__STRICT_ANSI__)
119/* function exists but are not defined */
120_CRTIMP char* __cdecl strdup (const char*) __MINGW_ATTRIB_MALLOC;
121_CRTIMP int __cdecl _stricmp (const char*, const char*);
122_CRTIMP int __cdecl _strnicmp (const char*, const char*, size_t);
123#define strncasecmp _strnicmp
124#endif
125
126/* portable case sensitive compare */
127#if defined(_WIN32)
128# define Q_strcasecmp(a, b) _stricmp((a), (b))
129# define Q_strncasecmp(s1, s2, n) _strnicmp((s1), (s2), (n))
130#else
131# define Q_strcasecmp(a, b) strcasecmp((a), (b))
132# define Q_strncasecmp(s1, s2, n) strncasecmp((s1), (s2), (n))
133#endif
134
135#define Q_strcaseeq(a, b) (Q_strcasecmp(a, b) == 0)
136#define Q_streq(a, b) (strcmp(a, b) == 0)
137#define Q_strneq(a, b, n) (strncmp(a, b, n) == 0)
138inline bool Q_strnull (const char* string) {
139 return string == nullptr || string[0] == '\0';
140}
141#define Q_strvalid(string) !Q_strnull(string)
142
143#ifndef DEBUG
144void Q_strncpyz(char* dest, const char* src, size_t destsize) __attribute__((nonnull));
145#else
146#define Q_strncpyz(string1,string2,length) Q_strncpyzDebug( string1, string2, length, __FILE__, __LINE__ )
147void Q_strncpyzDebug(char* dest, const char* src, size_t destsize, const char* file, int line) __attribute__((nonnull));
148#endif
149
150int Q_vsnprintf(char* str, size_t size, const char* format, va_list ap);
151void Q_strcat(char* dest, size_t destsize, const char* src, ...) __attribute__((nonnull, format(__printf__, 3, 4)));
152char* Q_strlwr(char* str) __attribute__((nonnull));
153const char* Q_stristr(const char* str, const char* substr) __attribute__((nonnull));
154
167bool Q_strreplace(const char* source, const char* pattern, const char* replace, char* dest, size_t destsize);
168
171char const* Q_strstart(char const* str, char const* start) __attribute__((nonnull));
172
173void Com_Printf(const char* fmt, ...) __attribute__((format(__printf__, 1, 2)));
174void Com_DPrintf(int level, const char* msg, ...) __attribute__((format(__printf__, 2, 3)));
175void Com_Error(int code, const char* fmt, ...) __attribute__((noreturn, format(__printf__, 2, 3)));
176
177#define OBJSET(obj, val) (memset(&(obj), (val), sizeof(obj)))
178#define OBJZERO(obj) OBJSET((obj), 0)
179
180#ifdef NDEBUG
181#define UFO_assert(condition, ...)
182#else
183void UFO_assert(bool condition, const char* fmt, ...) __attribute__((format(__printf__, 2, 3)));
184#endif
void Com_DPrintf(int level, const char *fmt,...)
A Com_Printf that only shows up if the "developer" cvar is set.
Definition common.cpp:440
void Com_Error(int code, const char *fmt,...)
Definition common.cpp:459
void Com_Printf(const char *const fmt,...)
Definition common.cpp:428
#define __attribute__(x)
Definition cxx.h:37
level_locals_t level
Definition g_main.cpp:38
voidpf void uLong size
Definition ioapi.h:42
void format(__printf__, 1, 2)))
QGL_EXTERN GLuint GLchar GLuint * len
Definition r_gl.h:99
QGL_EXTERN GLenum GLuint * dest
Definition r_gl.h:101
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition r_gl.h:110
void Com_StripExtension(const char *in, char *out, size_t size)
Removes the file extension from a filename.
Definition shared.cpp:259
const char * va(const char *format,...) __attribute__((format(__printf__
void Q_strncpyz(char *dest, const char *src, size_t destsize) __attribute__((nonnull))
Safe strncpy that ensures a trailing zero.
Definition shared.cpp:457
char * Com_Chop(char *s)
Removed trailing whitespaces.
Definition shared.cpp:48
void Com_ReplaceFilename(const char *fileName, const char *name, char *path, size_t size)
Replaces the filename from one path with another one.
Definition shared.cpp:239
bool Q_strnull(const char *string)
Definition shared.h:138
bool Com_IsValidName(const char *input)
Checks whether the given input string is allowed to be used as a user-given name string for aircraft,...
Definition shared.cpp:612
const char * Com_SkipPath(const char *pathname)
Returns just the filename from a given path.
Definition shared.cpp:37
void Com_DefaultExtension(char *path, size_t len, const char *extension)
Sets a default extension if there is none.
Definition shared.cpp:297
void Com_FilePath(const char *in, char *out, size_t size)
Returns the path up to, but not including the last /.
Definition shared.cpp:319
int Q_vsnprintf(char *str, size_t size, const char *format, va_list ap)
Safe (null terminating) vsnprintf implementation.
Definition shared.cpp:535
const char * Com_GetExtension(const char *path)
Definition shared.cpp:282
#define __attribute__(x)
Definition shared.h:58
char * Com_Trim(char *s)
Removed leading and trailing whitespaces.
Definition shared.cpp:65
char * Com_ConvertToASCII7(char *s)
Remove high character values and only keep ascii. This can be used to print utf-8 characters to the c...
Definition shared.cpp:82
void UFO_assert(bool condition, const char *fmt,...) __attribute__((format(__printf__
int Com_Filter(const char *pattern, const char *text)
Match the pattern PATTERN against the string TEXT;.
Definition shared.cpp:145
void Q_strcat(char *dest, size_t destsize, const char *src,...) __attribute__((nonnull
char * Q_strlwr(char *str)
Converts a string to lowercase.
Definition shared.cpp:438
unsigned int Com_HashKey(const char *name, int hashsize)
returns hash key for a string
Definition shared.cpp:336
int Q_StringSort(const void *string1, const void *string2)
Compare two strings.
Definition shared.cpp:385
void Com_MakeTimestamp(char *ts, const size_t tslen)
Creates a timestamp with date and time at the specified location.
Definition shared.cpp:352
char const * Q_strstart(char const *str, char const *start)
Matches the start of a string.
Definition shared.cpp:587
const char * Q_stristr(const char *str, const char *substr)
Checks in case insensitive manner whether str contains substr.
Definition shared.cpp:563
bool Q_strreplace(const char *source, const char *pattern, const char *replace, char *dest, size_t destsize)
Replaces the first occurence of the given pattern in the source string with the given replace string.
Definition shared.cpp:596
bool Com_sprintf(char *dest, size_t size, const char *fmt,...)
copies formatted string with buffer-size checking
Definition shared.cpp:494
int Q_FloatSort(const void *float1, const void *float2)
Compare two floats.
Definition shared.cpp:372
Cross-platform type definitions.