summaryrefslogtreecommitdiff
path: root/storage/mroonga/vendor/groonga/include/groonga/portability.h
blob: faee70b9c814cbf327cbe8235caed5d34f0ebf03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
  Copyright(C) 2015-2017 Brazil

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1335  USA
*/

#pragma once

#ifdef WIN32
# ifdef __cplusplus
#  define grn_memcpy(dest, src, n) ::memcpy_s((dest), (n), (src), (n))
# else /* __cplusplus */
#  define grn_memcpy(dest, src, n) memcpy_s((dest), (n), (src), (n))
# endif /* __cplusplus */
#else /* WIN32 */
# ifdef __cplusplus
#  define grn_memcpy(dest, src, n) std::memcpy((dest), (src), (n))
# else /* __cplusplus */
#  define grn_memcpy(dest, src, n) memcpy((dest), (src), (n))
# endif /* __cplusplus */
#endif /* WIN32 */

#ifdef WIN32
# define grn_memmove(dest, src, n) memmove_s((dest), (n), (src), (n))
#else /* WIN32 */
# define grn_memmove(dest, src, n) memmove((dest), (src), (n))
#endif /* WIN32 */

#define GRN_ENV_BUFFER_SIZE 1024

#ifdef WIN32
# define grn_getenv(name, dest, dest_size) do {                         \
    char *dest_ = (dest);                                               \
    size_t dest_size_ = (dest_size);                                    \
    if (dest_size_ > 0) {                                               \
      DWORD env_size;                                                   \
      env_size = GetEnvironmentVariableA((name), dest_, dest_size_);    \
      if (env_size == 0 || env_size > dest_size_) {                     \
        dest_[0] = '\0';                                                \
      }                                                                 \
    }                                                                   \
  } while (0)
#else /* WIN32 */
# define grn_getenv(name, dest, dest_size) do {         \
    const char *env_value = getenv((name));             \
    char *dest_ = (dest);                               \
    size_t dest_size_ = (dest_size);                    \
    if (dest_size_ > 0) {                               \
      if (env_value) {                                  \
        strncpy(dest_, env_value, dest_size_ - 1);      \
      } else {                                          \
        dest_[0] = '\0';                                \
      }                                                 \
    }                                                   \
  } while (0)
#endif /* WIN32 */

#ifdef WIN32
# define grn_fopen(name, mode) _fsopen((name), (mode), _SH_DENYNO)
#else /* WIN32 */
# define grn_fopen(name, mode) fopen((name), (mode))
#endif /* WIN32 */

#ifdef WIN32
# define grn_strdup_raw(string) _strdup((string))
#else /* WIN32 */
# define grn_strdup_raw(string) strdup((string))
#endif /* WIN32 */

#ifdef WIN32
# define grn_unlink(filename) _unlink((filename))
#else /* WIN32 */
# define grn_unlink(filename) unlink((filename))
#endif /* WIN32 */

#ifdef WIN32
# define grn_strncat(dest, dest_size, src, n)   \
  strncat_s((dest), (dest_size), (src), (n))
#else /* WIN32 */
# define grn_strncat(dest, dest_size, src, n)   \
  strncat((dest), (src), (n))
#endif /* WIN32 */

#ifdef WIN32
# define grn_strcpy(dest, dest_size, src)       \
  strcpy_s((dest), (dest_size), (src))
#else /* WIN32 */
# define grn_strcpy(dest, dest_size, src)       \
  strcpy((dest), (src))
#endif /* WIN32 */

#ifdef WIN32
# define grn_strncpy(dest, dest_size, src, n)   \
  strncpy_s((dest), (dest_size), (src), (n))
#else /* WIN32 */
# define grn_strncpy(dest, dest_size, src, n)   \
  strncpy((dest), (src), (n))
#endif /* WIN32 */

#ifdef WIN32
# define grn_strcat(dest, dest_size, src)       \
  strcat_s((dest), (dest_size), (src))
#else /* WIN32 */
# define grn_strcat(dest, dest_size, src)       \
  strcat((dest), (src))
#endif /* WIN32 */

#ifdef WIN32
# define grn_strcasecmp(string1, string2)       \
  _stricmp((string1), (string2))
#else /* WIN32 */
# define grn_strcasecmp(string1, string2)       \
  strcasecmp((string1), (string2))
#endif /* WIN32 */

#ifdef WIN32
# define grn_strncasecmp(string1, string2, n)   \
  _strnicmp((string1), (string2), (n))
#else /* WIN32 */
# define grn_strncasecmp(string1, string2, n)   \
  strncasecmp((string1), (string2), (n))
#endif /* WIN32 */

#ifdef WIN32
# define grn_snprintf(dest, dest_size, n, ...) do {             \
    _snprintf_s((dest), (dest_size), (n) - 1, __VA_ARGS__);     \
  } while (GRN_FALSE)
#else /* WIN32 */
# define grn_snprintf(dest, dest_size, n, ...)  \
  snprintf((dest), (n), __VA_ARGS__)
#endif /* WIN32 */

#ifdef WIN32
# define grn_vsnprintf(dest, dest_size, format, args) do {      \
    vsnprintf((dest), (dest_size), (format), (args));           \
    (dest)[(dest_size) - 1] = '\0';                             \
  } while (GRN_FALSE)
#else /* WIN32 */
# define grn_vsnprintf(dest, dest_size, format, args)           \
  vsnprintf((dest), (dest_size), (format), (args))
#endif /* WIN32 */

#ifdef WIN32
# define grn_write(fd, buf, count) _write((fd), (buf), (count))
#else /* WIN32 */
# define grn_write(fd, buf, count) write((fd), (buf), (count))
#endif /* WIN32 */

#ifdef WIN32
# define grn_read(fd, buf, count) _read((fd), (buf), (count))
#else /* WIN32 */
# define grn_read(fd, buf, count) read((fd), (buf), (count))
#endif /* WIN32 */

#ifdef WIN32
# define GRN_OPEN_CREATE_MODE (_S_IREAD | _S_IWRITE)
# define GRN_OPEN_FLAG_BINARY _O_BINARY
# define grn_open(fd, pathname, flags)                                  \
  _sopen_s(&(fd), (pathname), (flags), _SH_DENYNO, GRN_OPEN_CREATE_MODE)
#else /* WIN32 */
# define GRN_OPEN_CREATE_MODE (S_IRUSR | S_IWUSR | S_IRGRP)
# define GRN_OPEN_FLAG_BINARY 0
# define grn_open(fd, pathname, flags)                          \
  (fd) = open((pathname), (flags), GRN_OPEN_CREATE_MODE)
#endif /* WIN32 */

#ifdef WIN32
# define grn_close(fd) _close((fd))
#else /* WIN32 */
# define grn_close(fd) close((fd))
#endif /* WIN32 */

#ifdef WIN32
# define grn_fileno(stream) _fileno((stream))
#else /* WIN32 */
# define grn_fileno(stream) fileno((stream))
#endif /* WIN32 */

#ifdef WIN32
# define grn_isatty(stream) _isatty((stream))
#else /* WIN32 */
# define grn_isatty(stream) isatty((stream))
#endif /* WIN32 */

#ifdef WIN32
# define grn_getpid() _getpid()
#else /* WIN32 */
# define grn_getpid() getpid()
#endif /* WIN32 */