aboutsummaryrefslogtreecommitdiff
path: root/src/common/windows/string_utils.cc
diff options
context:
space:
mode:
authormmentovai <mmentovai@4c0a9323-5329-0410-9bdc-e9ce6186880e>2007-01-18 21:13:14 +0000
committermmentovai <mmentovai@4c0a9323-5329-0410-9bdc-e9ce6186880e>2007-01-18 21:13:14 +0000
commit50e299b00e803f085b34847dfe5232b471d84197 (patch)
tree7cfbba04057ac1234f603e8aaf3a528b91ce4584 /src/common/windows/string_utils.cc
parentBe lenient when reading CodeView records of unknown types (#110). r=bryner (diff)
downloadbreakpad-50e299b00e803f085b34847dfe5232b471d84197.tar.xz
Library to handle SymSrv integration (#111). r=bryner
http://groups.google.com/group/airbag-dev/browse_thread/thread/b40e66d1d57e61b5 git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@105 4c0a9323-5329-0410-9bdc-e9ce6186880e
Diffstat (limited to 'src/common/windows/string_utils.cc')
-rw-r--r--src/common/windows/string_utils.cc48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/common/windows/string_utils.cc b/src/common/windows/string_utils.cc
index ec2073ef..3d24caa9 100644
--- a/src/common/windows/string_utils.cc
+++ b/src/common/windows/string_utils.cc
@@ -27,6 +27,8 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#include <cassert>
+
#include "common/windows/string_utils-inl.h"
namespace google_airbag {
@@ -41,4 +43,50 @@ wstring WindowsStringUtils::GetBaseName(const wstring &filename) {
return base_name;
}
+// static
+bool WindowsStringUtils::safe_mbstowcs(const string &mbs, wstring *wcs) {
+ assert(wcs);
+
+ // First, determine the length of the destination buffer.
+ size_t wcs_length;
+
+#if _MSC_VER >= 1400 // MSVC 2005/8
+ errno_t err;
+ if ((err = mbstowcs_s(&wcs_length, NULL, 0, mbs.c_str(), _TRUNCATE)) != 0) {
+ return false;
+ }
+#else // _MSC_VER >= 1400
+ if ((wcs_length = mbstowcs(NULL, mbs.c_str(), mbs.length())) < 0) {
+ return false;
+ }
+
+ // Leave space for the 0-terminator.
+ ++wcs_length;
+#endif // _MSC_VER >= 1400
+
+ // TODO(mmentovai): move scoped_ptr into common and use it for wcs_c.
+ wchar_t *wcs_c = new wchar_t[wcs_length];
+
+ // Now, convert.
+#if _MSC_VER >= 1400 // MSVC 2005/8
+ if ((err = mbstowcs_s(NULL, wcs_c, wcs_length, mbs.c_str(),
+ _TRUNCATE)) != 0) {
+ delete[] wcs_c;
+ return false;
+ }
+#else // _MSC_VER >= 1400
+ if (mbstowcs(wcs_c, mbs.c_str(), mbs.length()) < 0) {
+ delete[] wcs_c;
+ return false;
+ }
+
+ // Ensure presence of 0-terminator.
+ wcs_c[wcs_length - 1] = '\0';
+#endif // _MSC_VER >= 1400
+
+ *wcs = wcs_c;
+ delete[] wcs_c;
+ return true;
+}
+
} // namespace google_airbag