nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Nicolas "Pixel" Noble | 1d44510 | 2016-01-27 20:49:14 +0100 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 4 | * 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 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 40 | #include <stdarg.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 41 | #include <stdio.h> |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 42 | #include <string.h> |
| 43 | |
| 44 | #include <grpc/support/alloc.h> |
| 45 | |
Nicolas Noble | cfd6073 | 2015-03-18 16:27:43 -0700 | [diff] [blame] | 46 | #include "src/core/support/string.h" |
| 47 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 48 | int gpr_asprintf(char **strp, const char *format, ...) { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 49 | va_list args; |
| 50 | int ret; |
| 51 | size_t strp_buflen; |
| 52 | |
| 53 | /* Determine the length. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 54 | va_start(args, format); |
| 55 | ret = _vscprintf(format, args); |
| 56 | va_end(args); |
| 57 | if (ret < 0) { |
| 58 | *strp = NULL; |
| 59 | return -1; |
| 60 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 61 | |
| 62 | /* Allocate a new buffer, with space for the NUL terminator. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 63 | 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 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 68 | |
| 69 | /* Print to the buffer. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 70 | 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 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 76 | |
| 77 | /* This should never happen. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 78 | gpr_free(*strp); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 79 | *strp = NULL; |
| 80 | return -1; |
| 81 | } |
| 82 | |
Nicolas "Pixel" Noble | d6dcec5 | 2015-02-05 23:28:17 -0800 | [diff] [blame] | 83 | #if defined UNICODE || defined _UNICODE |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 84 | LPTSTR |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 85 | gpr_char_to_tchar(LPCSTR input) { |
Nicolas "Pixel" Noble | d6dcec5 | 2015-02-05 23:28:17 -0800 | [diff] [blame] | 86 | LPTSTR ret; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 87 | int needed = MultiByteToWideChar(CP_UTF8, 0, input, -1, NULL, 0); |
Nicolas "Pixel" Noble | 742eac1 | 2016-01-26 22:41:19 +0100 | [diff] [blame] | 88 | if (needed <= 0) return NULL; |
Nicolas "Pixel" Noble | 1d44510 | 2016-01-27 20:49:14 +0100 | [diff] [blame] | 89 | ret = gpr_malloc((unsigned)needed * sizeof(TCHAR)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 90 | MultiByteToWideChar(CP_UTF8, 0, input, -1, ret, needed); |
Nicolas "Pixel" Noble | d6dcec5 | 2015-02-05 23:28:17 -0800 | [diff] [blame] | 91 | return ret; |
| 92 | } |
| 93 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 94 | LPSTR |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 95 | gpr_tchar_to_char(LPCTSTR input) { |
Nicolas "Pixel" Noble | d6dcec5 | 2015-02-05 23:28:17 -0800 | [diff] [blame] | 96 | LPSTR ret; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 97 | int needed = WideCharToMultiByte(CP_UTF8, 0, input, -1, NULL, 0, NULL, NULL); |
Nicolas "Pixel" Noble | 742eac1 | 2016-01-26 22:41:19 +0100 | [diff] [blame] | 98 | if (needed <= 0) return NULL; |
Nicolas "Pixel" Noble | 1d44510 | 2016-01-27 20:49:14 +0100 | [diff] [blame] | 99 | ret = gpr_malloc((unsigned)needed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 100 | WideCharToMultiByte(CP_UTF8, 0, input, -1, ret, needed, NULL, NULL); |
Nicolas "Pixel" Noble | d6dcec5 | 2015-02-05 23:28:17 -0800 | [diff] [blame] | 101 | return ret; |
| 102 | } |
| 103 | #else |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 104 | char *gpr_tchar_to_char(LPTSTR input) { return gpr_strdup(input); } |
Nicolas "Pixel" Noble | d6dcec5 | 2015-02-05 23:28:17 -0800 | [diff] [blame] | 105 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 106 | char *gpr_char_to_tchar(LPTSTR input) { return gpr_strdup(input); } |
Nicolas "Pixel" Noble | d6dcec5 | 2015-02-05 23:28:17 -0800 | [diff] [blame] | 107 | #endif |
| 108 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 109 | #endif /* GPR_WIN32 */ |