Add string_util_win.
Utility functions for wstring to string conversion on Windows.
Change-Id: I107d35beb9539a0df327fede11608f5289916250
diff --git a/encoder/CMakeLists.txt b/encoder/CMakeLists.txt
index 38b03d0..dec2a2c 100644
--- a/encoder/CMakeLists.txt
+++ b/encoder/CMakeLists.txt
@@ -159,6 +159,8 @@
win/media_source_dshow.h
win/media_type_dshow.cc
win/media_type_dshow.h
+ win/string_util_win.cc
+ win/string_util_win.h
win/video_sink_filter.cc
win/video_sink_filter.h
win/webm_guids.cc
diff --git a/encoder/win/string_util_win.cc b/encoder/win/string_util_win.cc
new file mode 100644
index 0000000..31cc1e0
--- /dev/null
+++ b/encoder/win/string_util_win.cc
@@ -0,0 +1,45 @@
+// Copyright (c) 2015 The WebM project authors. All Rights Reserved.
+//
+// Use of this source code is governed by a BSD-style license
+// that can be found in the LICENSE file in the root of the source
+// tree. An additional intellectual property rights grant can be found
+// in the file PATENTS. All contributing project authors may
+// be found in the AUTHORS file in the root of the source tree.
+#include "encoder/win/string_util_win.h"
+
+#include <memory>
+#include <sstream>
+
+#include "glog/logging.h"
+
+namespace webmlive {
+
+// Converts a std::string to std::wstring.
+std::wstring StringToWString(const std::string& str) {
+ std::wostringstream wstr;
+ wstr << str.c_str();
+ return wstr.str();
+}
+
+// Converts |wstr| to a multi-byte string and returns result std::string.
+std::string WStringToString(const std::wstring& wstr) {
+ // Conversion buffer for |wcstombs| calls.
+ const size_t buf_size = wstr.length() + 1;
+ std::unique_ptr<char[]> temp_str(
+ new (std::nothrow) char[buf_size]); // NOLINT
+ if (!temp_str) {
+ LOG(ERROR) << "can't convert wstring of length=" << wstr.length();
+ return "";
+ }
+ memset(temp_str.get(), 0, buf_size);
+ size_t num_converted = 0;
+ if (wcstombs_s(&num_converted, temp_str.get(), buf_size, wstr.c_str(),
+ wstr.length() * sizeof(wchar_t))) {
+ LOG(ERROR) << "conversion failed.";
+ return "";
+ }
+ std::string str = temp_str.get();
+ return str;
+}
+
+} // namespace webmlive
\ No newline at end of file
diff --git a/encoder/win/string_util_win.h b/encoder/win/string_util_win.h
new file mode 100644
index 0000000..8418c4b
--- /dev/null
+++ b/encoder/win/string_util_win.h
@@ -0,0 +1,25 @@
+// Copyright (c) 2015 The WebM project authors. All Rights Reserved.
+//
+// Use of this source code is governed by a BSD-style license
+// that can be found in the LICENSE file in the root of the source
+// tree. An additional intellectual property rights grant can be found
+// in the file PATENTS. All contributing project authors may
+// be found in the AUTHORS file in the root of the source tree.
+#ifndef WEBMLIVE_ENCODER_WIN_STRING_UTIL_WIN_H_
+#define WEBMLIVE_ENCODER_WIN_STRING_UTIL_WIN_H_
+
+#include <string>
+
+namespace webmlive {
+
+// String utility functions. All return an empty string upon error.
+
+// Converts and returns the contents of a std::string as a std::wstring.
+std::wstring StringToWString(const std::string& str);
+
+// Converts and returns the contents of a std::wstring as a std::string.
+std::string WStringToString(const std::wstring& wstr);
+
+} // namespace webmlive
+
+#endif // WEBMLIVE_ENCODER_WIN_STRING_UTIL_WIN_H_
\ No newline at end of file