blob: 07809079942bebb25624f56eebf0ea87aad8c53a [file] [log] [blame]
nnoble0c475f02014-12-05 15:37:39 -08001/*
2 *
Nicolas "Pixel" Noble1d445102016-01-27 20:49:14 +01003 * Copyright 2015-2016, Google Inc.
nnoble0c475f02014-12-05 15:37:39 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34/* Posix code for gpr snprintf support. */
35
36#include <grpc/support/port_platform.h>
37
38#ifdef GPR_WIN32
39
nnoble0c475f02014-12-05 15:37:39 -080040#include <stdarg.h>
Craig Tillerf40df232016-03-25 13:38:14 -070041#include <stdio.h>
nnoble0c475f02014-12-05 15:37:39 -080042#include <string.h>
43
44#include <grpc/support/alloc.h>
45
Nicolas Noblecfd60732015-03-18 16:27:43 -070046#include "src/core/support/string.h"
47
Craig Tillera82950e2015-09-22 12:33:20 -070048int gpr_asprintf(char **strp, const char *format, ...) {
nnoble0c475f02014-12-05 15:37:39 -080049 va_list args;
50 int ret;
51 size_t strp_buflen;
52
53 /* Determine the length. */
Craig Tillera82950e2015-09-22 12:33:20 -070054 va_start(args, format);
55 ret = _vscprintf(format, args);
56 va_end(args);
57 if (ret < 0) {
58 *strp = NULL;
59 return -1;
60 }
nnoble0c475f02014-12-05 15:37:39 -080061
62 /* Allocate a new buffer, with space for the NUL terminator. */
Craig Tillera82950e2015-09-22 12:33:20 -070063 strp_buflen = (size_t)ret + 1;
64 if ((*strp = gpr_malloc(strp_buflen)) == NULL) {
65 /* This shouldn't happen, because gpr_malloc() calls abort(). */
66 return -1;
67 }
nnoble0c475f02014-12-05 15:37:39 -080068
69 /* Print to the buffer. */
Craig Tillera82950e2015-09-22 12:33:20 -070070 va_start(args, format);
71 ret = vsnprintf_s(*strp, strp_buflen, _TRUNCATE, format, args);
72 va_end(args);
73 if ((size_t)ret == strp_buflen - 1) {
74 return ret;
75 }
nnoble0c475f02014-12-05 15:37:39 -080076
77 /* This should never happen. */
Craig Tillera82950e2015-09-22 12:33:20 -070078 gpr_free(*strp);
nnoble0c475f02014-12-05 15:37:39 -080079 *strp = NULL;
80 return -1;
81}
82
Nicolas "Pixel" Nobled6dcec52015-02-05 23:28:17 -080083#if defined UNICODE || defined _UNICODE
Craig Tiller45724b32015-09-22 10:42:19 -070084LPTSTR
Craig Tillera82950e2015-09-22 12:33:20 -070085gpr_char_to_tchar(LPCSTR input) {
Nicolas "Pixel" Nobled6dcec52015-02-05 23:28:17 -080086 LPTSTR ret;
Craig Tillera82950e2015-09-22 12:33:20 -070087 int needed = MultiByteToWideChar(CP_UTF8, 0, input, -1, NULL, 0);
Nicolas "Pixel" Noble742eac12016-01-26 22:41:19 +010088 if (needed <= 0) return NULL;
Nicolas "Pixel" Noble1d445102016-01-27 20:49:14 +010089 ret = gpr_malloc((unsigned)needed * sizeof(TCHAR));
Craig Tillera82950e2015-09-22 12:33:20 -070090 MultiByteToWideChar(CP_UTF8, 0, input, -1, ret, needed);
Nicolas "Pixel" Nobled6dcec52015-02-05 23:28:17 -080091 return ret;
92}
93
Craig Tiller45724b32015-09-22 10:42:19 -070094LPSTR
Craig Tillera82950e2015-09-22 12:33:20 -070095gpr_tchar_to_char(LPCTSTR input) {
Nicolas "Pixel" Nobled6dcec52015-02-05 23:28:17 -080096 LPSTR ret;
Craig Tillera82950e2015-09-22 12:33:20 -070097 int needed = WideCharToMultiByte(CP_UTF8, 0, input, -1, NULL, 0, NULL, NULL);
Nicolas "Pixel" Noble742eac12016-01-26 22:41:19 +010098 if (needed <= 0) return NULL;
Nicolas "Pixel" Noble1d445102016-01-27 20:49:14 +010099 ret = gpr_malloc((unsigned)needed);
Craig Tillera82950e2015-09-22 12:33:20 -0700100 WideCharToMultiByte(CP_UTF8, 0, input, -1, ret, needed, NULL, NULL);
Nicolas "Pixel" Nobled6dcec52015-02-05 23:28:17 -0800101 return ret;
102}
103#else
Craig Tillera82950e2015-09-22 12:33:20 -0700104char *gpr_tchar_to_char(LPTSTR input) { return gpr_strdup(input); }
Nicolas "Pixel" Nobled6dcec52015-02-05 23:28:17 -0800105
Craig Tillera82950e2015-09-22 12:33:20 -0700106char *gpr_char_to_tchar(LPTSTR input) { return gpr_strdup(input); }
Nicolas "Pixel" Nobled6dcec52015-02-05 23:28:17 -0800107#endif
108
Craig Tiller190d3602015-02-18 09:23:38 -0800109#endif /* GPR_WIN32 */