EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
eve-compat.cpp
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Zhur
24  Updates: Allan (timer update to C++0x11)
25 */
26 
27 #include "eve-core.h"
28 
29 #include <time.h>
30 
31 #ifndef HAVE_WINDOWS_H
32 void Sleep( uint32 x )
33 {
34  if( 0 < x )
35  ::usleep( 1000 * x );
36 }
37 
38 
40 {
41  struct timespec ts;
42  ::clock_gettime(CLOCK_MONOTONIC, &ts);
43  return (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
44 }
45 
46 int CreateDirectory( const char* name, void* )
47 {
48  // Create the directory with mode 0755
49  return 0 == ::mkdir( name,
50  S_IRUSR | S_IWUSR | S_IXUSR
51  | S_IRGRP | S_IXGRP
52  | S_IROTH | S_IXOTH );
53 }
54 #endif /* !HAVE_WINDOWS_H */
55 
56 #ifndef HAVE_ASPRINTF
57 int asprintf( char** strp, const char* fmt, ... )
58 {
59  va_list ap;
60 
61  va_start( ap, fmt );
62  int res = ::vasprintf( strp, fmt, ap );
63  va_end( ap );
64 
65  return res;
66 }
67 #endif /* !HAVE_ASPRINTF */
68 
69 #ifndef HAVE_VASPRINTF
70 int vasprintf( char** strp, const char* fmt, va_list ap )
71 {
72  // Return code
73  int code;
74  // Size of the buffer (doubled each cycle)
75  size_t size = 0x40;
76 
77  // No buffer yet
78  *strp = NULL;
79 
80  do
81  {
82  // Reallocate the buffer
83  *strp = (char*)::realloc( *strp, size );
84 
85  // Try to print into the buffer
86  code = ::vsnprintf( *strp, size, fmt, ap );
87  // Check for truncation
88  if( size <= code )
89  // Output truncated
90  code = -1;
91 
92  // Double the size of buffer
93  size <<= 1;
94  } while( 0 > code );
95 
96  // Reallocate to save memory
97  *strp = (char*)::realloc( *strp, code + 1 );
98  // Terminate the string
99  (*strp)[code] = '\0';
100 
101  // Return the code
102  return code;
103 }
104 #endif /* !HAVE_VASPRINTF */
105 
106 std::string sprintf( const char* fmt, ... )
107 {
108  va_list ap;
109  va_start( ap, fmt );
110 
111  // The string buffer
112  std::string str;
113  // Print into the buffer
114  int code = ::vsprintf( str, fmt, ap );
115  assert( 0 <= code );
116 
117  va_end( ap );
118 
119  return str;
120 }
121 
122 std::string vsprintf( const char* fmt, va_list ap )
123 {
124  // The string buffer
125  std::string str;
126  // Print into the buffer
127  int code = ::vsprintf( str, fmt, ap );
128  assert( 0 <= code );
129 
130  return str;
131 }
132 
133 int sprintf( std::string& str, const char* fmt, ... )
134 {
135  va_list ap;
136 
137  va_start( ap, fmt );
138  int code = ::vsprintf( str, fmt, ap );
139  va_end( ap );
140 
141  return code;
142 }
143 
144 int vsprintf( std::string& str, const char* fmt, va_list ap )
145 {
146  // Return code
147  int code;
148  // Offset within the string
149  size_t offset = str.length();
150  // Size of the buffer
151  size_t size = 0x40;
152 
153  do
154  {
155  // Resize the buffer
156  str.resize( offset + size );
157 
158  // Try to print into the buffer
159  code = ::vsnprintf( &str[offset], size, fmt, ap );
160  // Check for truncation
161  if( size <= code )
162  // Output truncated
163  code = -1;
164 
165  // Double the size of the buffer
166  size <<= 1;
167  } while( 0 > code );
168 
169  // Resize to save memory
170  str.resize( offset + code );
171  // Return the code
172  return code;
173 }
174 
175 #ifndef HAVE_STRTOF
176 float strtof( const char* nptr, char** endptr )
177 {
178  // Implement using strtod
179  return (float)::strtod( nptr, endptr );
180 }
181 #endif /* !HAVE_STRTOF */
182 
183 #ifndef HAVE_LOCALTIME_R
184 tm* localtime_r( const time_t* timep, tm* result )
185 {
186 # ifdef HAVE_LOCALTIME_S
187  const errno_t err = ::localtime_s( result, timep );
188  if( 0 != err )
189  {
190  errno = err;
191  return NULL;
192  }
193 # else /* !HAVE_LOCALTIME_S */
194  /* This is quite dangerous stuff (not necessarily
195  thread-safe), but what can we do? Also,
196  there is a chance that localtime will use
197  thread-specific memory (MS's localtime does that). */
198  ::memcpy( result, ::localtime( timep ), sizeof( tm ) );
199 # endif /* !HAVE_LOCALTIME_S */
200 
201  return result;
202 }
203 #endif /* !HAVE_LOCALTIME_R */
#define vsnprintf
Definition: eve-compat.h:188
tm * localtime_r(const time_t *timep, tm *result)
Definition: eve-compat.cpp:184
std::string sprintf(const char *fmt,...)
sprintf for std::string.
Definition: eve-compat.cpp:106
int vasprintf(char **strp, const char *fmt, va_list ap)
Definition: eve-compat.cpp:70
int CreateDirectory(const char *name, void *)
Definition: eve-compat.cpp:46
uint32 GetTickCount()
Definition: eve-compat.cpp:39
int asprintf(char **strp, const char *fmt,...)
Definition: eve-compat.cpp:57
unsigned __int32 uint32
Definition: eve-compat.h:50
float strtof(const char *nptr, char **endptr)
Definition: eve-compat.cpp:176
std::string vsprintf(const char *fmt, va_list ap)
vsprintf for std::string.
Definition: eve-compat.cpp:122
void Sleep(uint32 x)
Definition: eve-compat.cpp:32