Use gflags ALIAS instead of ${gflags_XXX} vars
[platform/upstream/glog.git] / src / windows / port.h
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 <stdio.h>           /* read in vsnprintf decl. before redifining it */
58 #include <stdarg.h>          /* template_dictionary.cc uses va_copy */
59 #include <string.h>          /* for _strnicmp(), strerror_s() */
60 #include <time.h>            /* 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 #ifdef _MSC_VER
66
67 /* 4244: otherwise we get problems when substracting two size_t's to an int
68  * 4251: it's complaining about a private struct I've chosen not to dllexport
69  * 4355: we use this in a constructor, but we do it safely
70  * 4715: for some reason VC++ stopped realizing you can't return after abort()
71  * 4800: we know we're casting ints/char*'s to bools, and we're ok with that
72  * 4996: Yes, we're ok using "unsafe" functions like fopen() and strerror()
73  */
74 #pragma warning(disable:4244 4251 4355 4715 4800 4996)
75
76 /* file I/O */
77 #define PATH_MAX 1024
78 #define access  _access
79 #define getcwd  _getcwd
80 #define open    _open
81 #define read    _read
82 #define write   _write
83 #define lseek   _lseek
84 #define close   _close
85 #define popen   _popen
86 #define pclose  _pclose
87 #define R_OK    04           /* read-only (for access()) */
88 #define S_ISDIR(m)  (((m) & _S_IFMT) == _S_IFDIR)
89 #ifndef __MINGW32__
90 enum { STDIN_FILENO = 0, STDOUT_FILENO = 1, STDERR_FILENO = 2 };
91 #endif
92 #define S_IRUSR S_IREAD
93 #define S_IWUSR S_IWRITE
94
95 /* Not quite as lightweight as a hard-link, but more than good enough for us. */
96 #define link(oldpath, newpath)  CopyFileA(oldpath, newpath, false)
97
98 #define strcasecmp   _stricmp
99 #define strncasecmp  _strnicmp
100
101 /* In windows-land, hash<> is called hash_compare<> (from xhash.h) */
102 /* VC11 provides std::hash */
103 #if defined(_MSC_VER) && (_MSC_VER < 1700)
104 #define hash  hash_compare
105 #endif
106
107 /* Sleep is in ms, on windows */
108 #define sleep(secs)  Sleep((secs) * 1000)
109
110 /* We can't just use _vsnprintf and _snprintf as drop-in-replacements,
111  * because they don't always NUL-terminate. :-(  We also can't use the
112  * name vsnprintf, since windows defines that (but not snprintf (!)).
113  */
114 #ifndef HAVE_SNPRINTF
115 extern int GOOGLE_GLOG_DLL_DECL snprintf(char *str, size_t size,
116                                        const char *format, ...);
117 #endif
118 extern int GOOGLE_GLOG_DLL_DECL safe_vsnprintf(char *str, size_t size,
119                           const char *format, va_list ap);
120 #define vsnprintf(str, size, format, ap)  safe_vsnprintf(str, size, format, ap)
121 #ifndef va_copy
122 #define va_copy(dst, src)  (dst) = (src)
123 #endif
124
125 /* Windows doesn't support specifying the number of buckets as a
126  * hash_map constructor arg, so we leave this blank.
127  */
128 #define CTEMPLATE_SMALL_HASHTABLE
129
130 #define DEFAULT_TEMPLATE_ROOTDIR  ".."
131
132 // ----------------------------------- SYSTEM/PROCESS
133 typedef int pid_t;
134 #define getpid  _getpid
135
136 #endif  // _MSC_VER
137
138 // ----------------------------------- THREADS
139 #ifndef __MINGW32__
140 typedef DWORD pthread_t;
141 typedef DWORD pthread_key_t;
142 typedef LONG pthread_once_t;
143 enum { PTHREAD_ONCE_INIT = 0 };   // important that this be 0! for SpinLock
144 #define pthread_self  GetCurrentThreadId
145 #define pthread_equal(pthread_t_1, pthread_t_2)  ((pthread_t_1)==(pthread_t_2))
146
147 inline struct tm* localtime_r(const time_t* timep, struct tm* result) {
148   localtime_s(result, timep);
149   return result;
150 }
151 #endif
152
153 inline char* strerror_r(int errnum, char* buf, size_t buflen) {
154   strerror_s(buf, buflen, errnum);
155   return buf;
156 }
157
158 #ifndef __cplusplus
159 /* I don't see how to get inlining for C code in MSVC.  Ah well. */
160 #define inline
161 #endif
162
163 #endif  /* _WIN32 */
164
165 #endif  /* CTEMPLATE_WINDOWS_PORT_H_ */