glog/src/windows/port.h
Go to the documentation of this file.
1 /* Copyright (c) 2008, Google Inc.
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  * * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * ---
31  * Author: Craig Silverstein
32  * Copied from google-perftools and modified by Shinichiro Hamaji
33  *
34  * These are some portability typedefs and defines to make it a bit
35  * easier to compile this code under VC++.
36  *
37  * Several of these are taken from glib:
38  * http://developer.gnome.org/doc/API/glib/glib-windows-compatability-functions.html
39  */
40 
41 #ifndef CTEMPLATE_WINDOWS_PORT_H_
42 #define CTEMPLATE_WINDOWS_PORT_H_
43 
44 #include "config.h"
45 
46 #ifdef _WIN32
47 
48 #ifndef WIN32_LEAN_AND_MEAN
49 #define WIN32_LEAN_AND_MEAN /* We always want minimal includes */
50 #endif
51 
52 #include <windows.h>
53 #include <winsock.h> /* for gethostname */
54 #include <io.h> /* because we so often use open/close/etc */
55 #include <direct.h> /* for _getcwd() */
56 #include <process.h> /* for _getpid() */
57 #include <cstdarg> /* template_dictionary.cc uses va_copy */
58 #include <cstdio> /* read in vsnprintf decl. before redifining it */
59 #include <cstring> /* for _strnicmp(), strerror_s() */
60 #include <ctime> /* for localtime_s() */
61 /* Note: the C++ #includes are all together at the bottom. This file is
62  * used by both C and C++ code, so we put all the C++ together.
63  */
64 
65 #include <glog/logging.h>
66 
67 #ifdef _MSC_VER
68 
69 /* 4244: otherwise we get problems when substracting two size_t's to an int
70  * 4251: it's complaining about a private struct I've chosen not to dllexport
71  * 4355: we use this in a constructor, but we do it safely
72  * 4715: for some reason VC++ stopped realizing you can't return after abort()
73  * 4800: we know we're casting ints/char*'s to bools, and we're ok with that
74  * 4996: Yes, we're ok using "unsafe" functions like fopen() and strerror()
75  * 4312: Converting uint32_t to a pointer when testing %p
76  * 4267: also subtracting two size_t to int
77  * 4722: Destructor never returns due to abort()
78  */
79 #pragma warning(disable:4244 4251 4355 4715 4800 4996 4267 4312 4722)
80 
81 /* file I/O */
82 #define PATH_MAX 1024
83 #define access _access
84 #define getcwd _getcwd
85 #define open _open
86 #define read _read
87 #define write _write
88 #define lseek _lseek
89 #define close _close
90 #define popen _popen
91 #define pclose _pclose
92 #define R_OK 04 /* read-only (for access()) */
93 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
94 
95 #define O_WRONLY _O_WRONLY
96 #define O_CREAT _O_CREAT
97 #define O_EXCL _O_EXCL
98 
99 #ifndef __MINGW32__
100 enum { STDIN_FILENO = 0, STDOUT_FILENO = 1, STDERR_FILENO = 2 };
101 #endif
102 #define S_IRUSR S_IREAD
103 #define S_IWUSR S_IWRITE
104 
105 /* Not quite as lightweight as a hard-link, but more than good enough for us. */
106 #define link(oldpath, newpath) CopyFileA(oldpath, newpath, false)
107 
108 #define strcasecmp _stricmp
109 #define strncasecmp _strnicmp
110 
111 /* In windows-land, hash<> is called hash_compare<> (from xhash.h) */
112 /* VC11 provides std::hash */
113 #if defined(_MSC_VER) && (_MSC_VER < 1700)
114 #define hash hash_compare
115 #endif
116 
117 /* Sleep is in ms, on windows */
118 #define sleep(secs) Sleep((secs) * 1000)
119 
120 /* We can't just use _vsnprintf and _snprintf as drop-in-replacements,
121  * because they don't always NUL-terminate. :-( We also can't use the
122  * name vsnprintf, since windows defines that (but not snprintf (!)).
123  */
124 #ifndef HAVE_SNPRINTF
125 extern int GLOG_EXPORT snprintf(char* str, size_t size, const char* format,
126  ...);
127 #endif
128 extern int GLOG_EXPORT safe_vsnprintf(char* str, size_t size,
129  const char* format, va_list ap);
130 #define vsnprintf(str, size, format, ap) safe_vsnprintf(str, size, format, ap)
131 #ifndef va_copy
132 #define va_copy(dst, src) (dst) = (src)
133 #endif
134 
135 /* Windows doesn't support specifying the number of buckets as a
136  * hash_map constructor arg, so we leave this blank.
137  */
138 #define CTEMPLATE_SMALL_HASHTABLE
139 
140 #define DEFAULT_TEMPLATE_ROOTDIR ".."
141 
142 // ----------------------------------- SYSTEM/PROCESS
143 typedef int pid_t;
144 #define getpid _getpid
145 
146 #endif // _MSC_VER
147 
148 // ----------------------------------- THREADS
149 #if defined(HAVE_PTHREAD)
150 # include <pthread.h>
151 #else // no PTHREAD
152 typedef DWORD pthread_t;
153 typedef DWORD pthread_key_t;
154 typedef LONG pthread_once_t;
155 enum { PTHREAD_ONCE_INIT = 0 }; // important that this be 0! for SpinLock
156 #define pthread_self GetCurrentThreadId
157 #define pthread_equal(pthread_t_1, pthread_t_2) ((pthread_t_1)==(pthread_t_2))
158 #endif // HAVE_PTHREAD
159 
160 #ifndef HAVE_LOCALTIME_R
161 extern GLOG_EXPORT struct tm* localtime_r(const time_t* timep,
162  struct tm* result);
163 #endif // not HAVE_LOCALTIME_R
164 
165 #ifndef HAVE_GMTIME_R
166 extern GLOG_EXPORT struct tm* gmtime_r(const time_t* timep, struct tm* result);
167 #endif // not HAVE_GMTIME_R
168 
169 inline char* strerror_r(int errnum, char* buf, size_t buflen) {
170  strerror_s(buf, buflen, errnum);
171  return buf;
172 }
173 
174 #ifndef __cplusplus
175 /* I don't see how to get inlining for C code in MSVC. Ah well. */
176 #define inline
177 #endif
178 
179 #endif /* _WIN32 */
180 
181 #endif /* CTEMPLATE_WINDOWS_PORT_H_ */
localtime_r
struct tm * localtime_r(const time_t *timep, struct tm *result)
Definition: port.cc:52
safe_vsnprintf
int safe_vsnprintf(char *str, size_t size, const char *format, va_list ap)
Definition: port.cc:44
gmtime_r
struct tm * gmtime_r(const time_t *timep, struct tm *result)
Definition: port.cc:58
io.h
GLOG_EXPORT
#define GLOG_EXPORT
Definition: glog/src/googletest.h:76
snprintf
int snprintf(char *str, size_t size, const char *format,...)
Definition: port.cc:64
format
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:2773
update_failure_list.str
str
Definition: update_failure_list.py:41
buf
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:4175
size
GLsizeiptr size
Definition: glcorearb.h:2943


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:57