1 /* Copyright (c) 2007, 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  *
33  * These are some portability typedefs and defines to make it a bit
34  * easier to compile this code under VC++.
35  *
36  * Several of these are taken from glib:
37  *    http://developer.gnome.org/doc/API/glib/glib-windows-compatability-functions.html
38  */
39 
40 #ifndef CTEMPLATE_WINDOWS_PORT_H_
41 #define CTEMPLATE_WINDOWS_PORT_H_
42 
43 #include "windows/config.h"
44 #ifdef _WIN32
45 
46 #define USING_PORT_CC
47 
48 #define WIN32_LEAN_AND_MEAN  /* We always want minimal includes */
49 #include <windows.h>
50 #include <io.h>              /* because we so often use open/close/etc */
51 #include <direct.h>          /* for _getcwd() */
52 #include <sys/utime.h>       /* for _utime() */
53 #include <stdio.h>           /* read in vsnprintf decl. before redifining it */
54 #include <stdarg.h>          /* template_dictionary.cc uses va_copy */
55 #include <string.h>          /* for _strnicmp */
56 /* Note: the C++ #includes are all together at the bottom.  This file is
57  * used by both C and C++ code, so we put all the C++ together.
58  */
59 
60 /* 4244: otherwise we get problems when substracting two size_t's to an int
61  * 4251: it's complaining about a private struct I've chosen not to dllexport
62  * 4355: we use this in a constructor, but we do it safely
63  * 4715: for some reason VC++ stopped realizing you can't return after abort()
64  * 4800: we know we're casting ints/char*'s to bools, and we're ok with that
65  * 4996: Yes, we're ok using "unsafe" functions like fopen() and strerror()
66  */
67 #ifdef _MSC_VER
68 #pragma warning(disable:4244 4251 4355 4715 4800 4996)
69 #endif
70 
71 /* file I/O */
72 #define PATH_MAX 1024
73 #define access  _access
74 #define getcwd  _getcwd
75 #define open    _open
76 #define read    _read
77 #define write   _write
78 #define lseek   _lseek
79 #define close   _close
80 #define popen   _popen
81 #define pclose  _pclose
82 #ifndef R_OK   /* mingw defines this, for instance */
83 #define R_OK    04           /* read-only (for access()) */
84 #endif
85 #define S_ISDIR(m)  (((m) & _S_IFMT) == _S_IFDIR)
86 
87 #define utime    _utime
88 #define utimbuf  _utimbuf
89 
90 /* Not quite as lightweight as a hard-link, but more than good enough for us. */
91 #define link(oldpath, newpath)  (!CopyFileA(oldpath, newpath, false))
92 
93 #define strcasecmp   _stricmp
94 #define strncasecmp  _strnicmp
95 
96 /* Sleep is in ms, on windows */
97 #define sleep(secs)  Sleep((secs) * 1000)
98 
99 /* We can't just use _vsnprintf and _snprintf as drop-in-replacements,
100  * because they don't always NUL-terminate. :-(  We also can't use the
101  * name vsnprintf, since windows defines that (but not snprintf (!)).
102  */
103 #if !defined(__MINGW32__) && !defined(__MINGW64__) && (!defined(_MSC_VER) || _MSC_VER < 1900) /* mingw already defines */
104 extern CTEMPLATE_DLL_DECL int snprintf(char *str, size_t size,
105                                        const char *format, ...);
106 extern int CTEMPLATE_DLL_DECL safe_vsnprintf(char *str, size_t size,
107                                              const char *format, va_list ap);
108 #define vsnprintf(str, size, format, ap)  safe_vsnprintf(str, size, format, ap)
109 #define va_copy(dst, src)  (dst) = (src)
110 #endif  /* #if !defined(__MINGW32__) && !defined(__MINGW64__) */
111 
112 /* Windows doesn't support specifying the number of buckets as a
113  * hash_map constructor arg, so we leave this blank.
114  */
115 #define CTEMPLATE_SMALL_HASHTABLE
116 
117 #define DEFAULT_TEMPLATE_ROOTDIR  ".."
118 
119 
120 /* These are functions we have to override because they're O/S-specific */
121 #ifdef __cplusplus
122 #include <string>
123 #include <vector>
124 
125 namespace ctemplate {
126 extern CTEMPLATE_DLL_DECL std::string TmpFile(const char* basename);
127 void CTEMPLATE_DLL_DECL CreateOrCleanTestDir(const std::string& dirname);
128 }
129 void CTEMPLATE_DLL_DECL GetNamelist(const char* testdata_dir,
130                                     std::vector<std::string>* namelist);
131 #endif  /* __cplusplus */
132 
133 #ifndef __cplusplus
134 /* I don't see how to get inlining for C code in MSVC.  Ah well. */
135 #define inline
136 #endif
137 
138 #endif  /* _WIN32 */
139 
140 #endif  /* CTEMPLATE_WINDOWS_PORT_H_ */
141