OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006, Google Inc. | |
2 // All rights reserved. | |
3 // | |
4 // Redistribution and use in source and binary forms, with or without | |
5 // modification, are permitted provided that the following conditions are | |
6 // met: | |
7 // | |
8 // * Redistributions of source code must retain the above copyright | |
9 // notice, this list of conditions and the following disclaimer. | |
10 // * Redistributions in binary form must reproduce the above | |
11 // copyright notice, this list of conditions and the following disclaimer | |
12 // in the documentation and/or other materials provided with the | |
13 // distribution. | |
14 // * Neither the name of Google Inc. nor the names of its | |
15 // contributors may be used to endorse or promote products derived from | |
16 // this software without specific prior written permission. | |
17 // | |
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 | |
30 #include <cassert> | |
31 | |
32 #include "common/windows/string_utils-inl.h" | |
33 | |
34 namespace google_breakpad { | |
35 | |
36 // static | |
37 wstring WindowsStringUtils::GetBaseName(const wstring &filename) { | |
38 wstring base_name(filename); | |
39 size_t slash_pos = base_name.find_last_of(L"/\\"); | |
40 if (slash_pos != wstring::npos) { | |
41 base_name.erase(0, slash_pos + 1); | |
42 } | |
43 return base_name; | |
44 } | |
45 | |
46 // static | |
47 bool WindowsStringUtils::safe_mbstowcs(const string &mbs, wstring *wcs) { | |
48 assert(wcs); | |
49 | |
50 // First, determine the length of the destination buffer. | |
51 size_t wcs_length; | |
52 | |
53 #if _MSC_VER >= 1400 // MSVC 2005/8 | |
54 errno_t err; | |
55 if ((err = mbstowcs_s(&wcs_length, NULL, 0, mbs.c_str(), _TRUNCATE)) != 0) { | |
56 return false; | |
57 } | |
58 #else // _MSC_VER >= 1400 | |
59 if ((wcs_length = mbstowcs(NULL, mbs.c_str(), mbs.length())) < 0) { | |
60 return false; | |
61 } | |
62 | |
63 // Leave space for the 0-terminator. | |
64 ++wcs_length; | |
65 #endif // _MSC_VER >= 1400 | |
66 | |
67 // TODO(mmentovai): move scoped_ptr into common and use it for wcs_c. | |
68 wchar_t *wcs_c = new wchar_t[wcs_length]; | |
69 | |
70 // Now, convert. | |
71 #if _MSC_VER >= 1400 // MSVC 2005/8 | |
72 if ((err = mbstowcs_s(NULL, wcs_c, wcs_length, mbs.c_str(), | |
73 _TRUNCATE)) != 0) { | |
74 delete[] wcs_c; | |
75 return false; | |
76 } | |
77 #else // _MSC_VER >= 1400 | |
78 if (mbstowcs(wcs_c, mbs.c_str(), mbs.length()) < 0) { | |
79 delete[] wcs_c; | |
80 return false; | |
81 } | |
82 | |
83 // Ensure presence of 0-terminator. | |
84 wcs_c[wcs_length - 1] = '\0'; | |
85 #endif // _MSC_VER >= 1400 | |
86 | |
87 *wcs = wcs_c; | |
88 delete[] wcs_c; | |
89 return true; | |
90 } | |
91 | |
92 } // namespace google_breakpad | |
OLD | NEW |