From: A. Unique TensorFlower Date: Thu, 24 May 2018 23:31:48 +0000 (-0700) Subject: move wide string manipulations out of windows_file_system X-Git-Tag: upstream/v1.9.0_rc1~38^2~4^2~81 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0d35a11ccf25d31f45777dd58c1f3ec1f33806b2;p=platform%2Fupstream%2Ftensorflow.git move wide string manipulations out of windows_file_system PiperOrigin-RevId: 197974385 --- diff --git a/tensorflow/core/BUILD b/tensorflow/core/BUILD index ce68ee1..2dd8e6f 100644 --- a/tensorflow/core/BUILD +++ b/tensorflow/core/BUILD @@ -441,6 +441,7 @@ cc_library( "env.cc", "load_library.cc", ]) + tf_platform_hdrs([ + "wide_char.h", ]) + [ "platform/env.cc", "platform/file_system.cc", diff --git a/tensorflow/core/platform/env.cc b/tensorflow/core/platform/env.cc index fe7d0aa..47c59d4 100644 --- a/tensorflow/core/platform/env.cc +++ b/tensorflow/core/platform/env.cc @@ -26,7 +26,7 @@ limitations under the License. #endif #if defined(PLATFORM_WINDOWS) #include -#include "tensorflow/core/platform/windows/windows_file_system.h" +#include "tensorflow/core/platform/windows/wide_char.h" #define PATH_MAX MAX_PATH #else #include @@ -311,7 +311,7 @@ string Env::GetExecutablePath() { HMODULE hModule = GetModuleHandleW(NULL); WCHAR wc_file_path[MAX_PATH] = {0}; GetModuleFileNameW(hModule, wc_file_path, MAX_PATH); - string file_path = WindowsFileSystem::WideCharToUtf8(wc_file_path); + string file_path = WideCharToUtf8(wc_file_path); std::copy(file_path.begin(), file_path.end(), exe_path); #else CHECK_NE(-1, readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1)); diff --git a/tensorflow/core/platform/windows/env.cc b/tensorflow/core/platform/windows/env.cc index 2f54f42..68ee359 100644 --- a/tensorflow/core/platform/windows/env.cc +++ b/tensorflow/core/platform/windows/env.cc @@ -31,6 +31,7 @@ limitations under the License. #include "tensorflow/core/lib/core/error_codes.pb.h" #include "tensorflow/core/platform/load_library.h" #include "tensorflow/core/platform/logging.h" +#include "tensorflow/core/platform/windows/wide_char.h" #include "tensorflow/core/platform/windows/windows_file_system.h" #pragma comment(lib, "Shlwapi.lib") @@ -71,8 +72,8 @@ class WindowsEnv : public Env { } bool MatchPath(const string& path, const string& pattern) override { - std::wstring ws_path(WindowsFileSystem::Utf8ToWideChar(path)); - std::wstring ws_pattern(WindowsFileSystem::Utf8ToWideChar(pattern)); + std::wstring ws_path(Utf8ToWideChar(path)); + std::wstring ws_pattern(Utf8ToWideChar(pattern)); return PathMatchSpecW(ws_path.c_str(), ws_pattern.c_str()) == TRUE; } @@ -125,7 +126,7 @@ class WindowsEnv : public Env { std::string file_name = library_filename; std::replace(file_name.begin(), file_name.end(), '/', '\\'); - std::wstring ws_file_name(WindowsFileSystem::Utf8ToWideChar(file_name)); + std::wstring ws_file_name(Utf8ToWideChar(file_name)); HMODULE hModule = LoadLibraryExW(ws_file_name.c_str(), NULL, LOAD_WITH_ALTERED_SEARCH_PATH); diff --git a/tensorflow/core/platform/windows/wide_char.h b/tensorflow/core/platform/windows/wide_char.h new file mode 100644 index 0000000..1b86abc --- /dev/null +++ b/tensorflow/core/platform/windows/wide_char.h @@ -0,0 +1,46 @@ +/* Copyright 2018 Google Inc. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#ifndef TENSORFLOW_CORE_PLATFORM_WINDOWS_WIDE_CHAR_H_ +#define TENSORFLOW_CORE_PLATFORM_WINDOWS_WIDE_CHAR_H_ + +#include +#include +#include "tensorflow/core/platform/types.h" + +namespace tensorflow { + +inline std::wstring Utf8ToWideChar(const string& utf8str) { + int size_required = MultiByteToWideChar(CP_UTF8, 0, utf8str.c_str(), + (int)utf8str.size(), NULL, 0); + std::wstring ws_translated_str(size_required, 0); + MultiByteToWideChar(CP_UTF8, 0, utf8str.c_str(), (int)utf8str.size(), + &ws_translated_str[0], size_required); + return ws_translated_str; +} + +inline string WideCharToUtf8(const std::wstring& wstr) { + if (wstr.empty()) return std::string(); + int size_required = WideCharToMultiByte( + CP_UTF8, 0, wstr.c_str(), (int)wstr.size(), NULL, 0, NULL, NULL); + string utf8_translated_str(size_required, 0); + WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), (int)wstr.size(), + &utf8_translated_str[0], size_required, NULL, NULL); + return utf8_translated_str; +} + +} // namespace tensorflow + +#endif // TENSORFLOW_CORE_PLATFORM_WINDOWS_WIDE_CHAR_H_ diff --git a/tensorflow/core/platform/windows/windows_file_system.cc b/tensorflow/core/platform/windows/windows_file_system.cc index dc2efbe..9079a5c 100644 --- a/tensorflow/core/platform/windows/windows_file_system.cc +++ b/tensorflow/core/platform/windows/windows_file_system.cc @@ -32,6 +32,7 @@ limitations under the License. #include "tensorflow/core/platform/logging.h" #include "tensorflow/core/platform/posix/error.h" #include "tensorflow/core/platform/windows/error.h" +#include "tensorflow/core/platform/windows/wide_char.h" #include "tensorflow/core/platform/windows/windows_file_system.h" // TODO(mrry): Prevent this Windows.h #define from leaking out of our headers. diff --git a/tensorflow/core/platform/windows/windows_file_system.h b/tensorflow/core/platform/windows/windows_file_system.h index ba0302f..6b04720 100644 --- a/tensorflow/core/platform/windows/windows_file_system.h +++ b/tensorflow/core/platform/windows/windows_file_system.h @@ -64,25 +64,6 @@ class WindowsFileSystem : public FileSystem { Status RenameFile(const string& src, const string& target) override; string TranslateName(const string& name) const override { return name; } - - static std::wstring Utf8ToWideChar(const string& utf8str) { - int size_required = MultiByteToWideChar(CP_UTF8, 0, utf8str.c_str(), - (int)utf8str.size(), NULL, 0); - std::wstring ws_translated_str(size_required, 0); - MultiByteToWideChar(CP_UTF8, 0, utf8str.c_str(), (int)utf8str.size(), - &ws_translated_str[0], size_required); - return ws_translated_str; - } - - static string WideCharToUtf8(const std::wstring& wstr) { - if (wstr.empty()) return std::string(); - int size_required = WideCharToMultiByte( - CP_UTF8, 0, wstr.c_str(), (int)wstr.size(), NULL, 0, NULL, NULL); - string utf8_translated_str(size_required, 0); - WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), (int)wstr.size(), - &utf8_translated_str[0], size_required, NULL, NULL); - return utf8_translated_str; - } }; class LocalWinFileSystem : public WindowsFileSystem {