Making the gpr_tmpfile's win32 version a bit more Windows-y.

Also adding windows helpers to convert to and from TCHAR strings.
changes/73/217573/1
Nicolas "Pixel" Noble 10 years ago committed by Nicolas "Pixel" Noble
parent 3d6fa14f1a
commit d6dcec526d
  1. 1
      build.json
  2. 1
      src/core/support/env_win32.c
  3. 51
      src/core/support/file_win32.c
  4. 29
      src/core/support/string_win32.c
  5. 49
      src/core/support/string_win32.h
  6. 1
      vsprojects/vs2013/gpr.vcxproj
  7. 3
      vsprojects/vs2013/gpr.vcxproj.filters

@ -231,6 +231,7 @@
"src/core/support/file.h", "src/core/support/file.h",
"src/core/support/murmur_hash.h", "src/core/support/murmur_hash.h",
"src/core/support/string.h", "src/core/support/string.h",
"src/core/support/string_win32.h",
"src/core/support/thd_internal.h" "src/core/support/thd_internal.h"
], ],
"src": [ "src": [

@ -39,6 +39,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h> #include <grpc/support/log.h>
char *gpr_getenv(const char *name) { char *gpr_getenv(const char *name) {

@ -35,43 +35,48 @@
#ifdef GPR_WIN32 #ifdef GPR_WIN32
#include "src/core/support/file.h"
#include <io.h> #include <io.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <tchar.h>
#include <grpc/support/alloc.h> #include <grpc/support/alloc.h>
#include <grpc/support/log.h> #include <grpc/support/log.h>
FILE *gpr_tmpfile(const char *prefix, char **tmp_filename) { #include "src/core/support/file.h"
#include "src/core/support/string_win32.h"
FILE *gpr_tmpfile(const char *prefix, char **tmp_filename_out) {
FILE *result = NULL; FILE *result = NULL;
char *template; LPTSTR template_string = NULL;
TCHAR tmp_path[MAX_PATH];
TCHAR tmp_filename[MAX_PATH];
DWORD status;
UINT success;
if (tmp_filename != NULL) *tmp_filename = NULL; if (tmp_filename_out != NULL) *tmp_filename_out = NULL;
gpr_asprintf(&template, "%s_XXXXXX", prefix); /* Convert our prefix to TCHAR. */
GPR_ASSERT(template != NULL); template_string = gpr_char_to_tchar(prefix);
GPR_ASSERT(template_string);
/* _mktemp_s can only create a maximum of 26 file names for any combination of /* Get the path to the best temporary folder available. */
base and template values which is kind of sad... We may revisit this status = GetTempPath(MAX_PATH, tmp_path);
function later to have something better... */ if (status == 0 || status > MAX_PATH) goto end;
if (_mktemp_s(template, strlen(template) + 1) != 0) {
gpr_log(LOG_ERROR, "Could not create tmp file."); /* Generate a unique filename with our template + temporary path. */
goto end; success = GetTempFileName(tmp_path, template_string, 0, tmp_filename);
} if (!success) goto end;
if (fopen_s(&result, template, "wb+") != 0) {
gpr_log(GPR_ERROR, "Could not open file %s", template); /* Open a file there. */
result = NULL; if (_tfopen_s(&result, tmp_filename, TEXT("wb+")) != 0) goto end;
goto end;
}
end: end:
if (result != NULL && tmp_filename != NULL) { if (result && tmp_filename) {
*tmp_filename = template; *tmp_filename_out = gpr_tchar_to_char(tmp_filename);
} else {
gpr_free(template);
} }
gpr_free(tmp_filename);
return result; return result;
} }

@ -37,6 +37,7 @@
#ifdef GPR_WIN32 #ifdef GPR_WIN32
#include <windows.h>
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <string.h>
@ -78,4 +79,32 @@ int gpr_asprintf(char **strp, const char *format, ...) {
return -1; return -1;
} }
#if defined UNICODE || defined _UNICODE
LPTSTR gpr_char_to_tchar(LPCSTR input) {
LPTSTR ret;
int needed = MultiByteToWideChar(CP_UTF8, 0, input, -1, NULL, 0);
if (needed == 0) return NULL;
ret = gpr_malloc(needed * sizeof(TCHAR));
MultiByteToWideChar(CP_UTF8, 0, input, -1, ret, needed);
return ret;
}
LPSTR gpr_tchar_to_char(LPCTSTR input) {
LPSTR ret;
int needed = WideCharToMultiByte(CP_UTF8, 0, input, -1, NULL, 0, NULL, NULL);
if (needed == 0) return NULL;
ret = gpr_malloc(needed);
WideCharToMultiByte(CP_UTF8, 0, input, -1, ret, needed, NULL, NULL);
return ret;
}
#else
char *gpr_tchar_to_char(LPTSTR input) {
return gpr_strdup(input);
}
char *gpr_char_to_tchar(LPTSTR input) {
return gpr_strdup(input);
}
#endif
#endif /* GPR_WIN32 */ #endif /* GPR_WIN32 */

@ -0,0 +1,49 @@
/*
*
* Copyright 2014, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef __GRPC_SUPPORT_STRING_WIN32_H__
#define __GRPC_SUPPORT_STRING_WIN32_H__
#include <grpc/support/port_platform.h>
#ifdef GPR_WIN32
#include <windows.h>
/* These allocate new strings using gpr_malloc to convert from and to utf-8. */
LPTSTR gpr_char_to_tchar(LPCSTR input);
LPSTR gpr_tchar_to_char(LPCTSTR input);
#endif /* GPR_WIN32 */
#endif /* __GRPC_SUPPORT_STRING_WIN32_H__ */

@ -100,6 +100,7 @@
<ClInclude Include="..\..\src\core\support\file.h" /> <ClInclude Include="..\..\src\core\support\file.h" />
<ClInclude Include="..\..\src\core\support\murmur_hash.h" /> <ClInclude Include="..\..\src\core\support\murmur_hash.h" />
<ClInclude Include="..\..\src\core\support\string.h" /> <ClInclude Include="..\..\src\core\support\string.h" />
<ClInclude Include="..\..\src\core\support\string_win32.h" />
<ClInclude Include="..\..\src\core\support\thd_internal.h" /> <ClInclude Include="..\..\src\core\support\thd_internal.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

@ -176,6 +176,9 @@
<ClInclude Include="..\..\src\core\support\string.h"> <ClInclude Include="..\..\src\core\support\string.h">
<Filter>src\core\support</Filter> <Filter>src\core\support</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\core\support\string_win32.h">
<Filter>src\core\support</Filter>
</ClInclude>
<ClInclude Include="..\..\src\core\support\thd_internal.h"> <ClInclude Include="..\..\src\core\support\thd_internal.h">
<Filter>src\core\support</Filter> <Filter>src\core\support</Filter>
</ClInclude> </ClInclude>

Loading…
Cancel
Save