Planeshift
|
00001 // Copyright (c) 2006, Google Inc. 00002 // All rights reserved. 00003 // 00004 // Redistribution and use in source and binary forms, with or without 00005 // modification, are permitted provided that the following conditions are 00006 // met: 00007 // 00008 // * Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // * Redistributions in binary form must reproduce the above 00011 // copyright notice, this list of conditions and the following disclaimer 00012 // in the documentation and/or other materials provided with the 00013 // distribution. 00014 // * Neither the name of Google Inc. nor the names of its 00015 // contributors may be used to endorse or promote products derived from 00016 // this software without specific prior written permission. 00017 // 00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 00030 // string_utils-inl.h: Safer string manipulation on Windows, supporting 00031 // pre-MSVC8 environments. 00032 00033 #ifndef COMMON_WINDOWS_STRING_UTILS_INL_H__ 00034 #define COMMON_WINDOWS_STRING_UTILS_INL_H__ 00035 00036 #include <stdarg.h> 00037 #include <wchar.h> 00038 00039 #include <string> 00040 00041 // The "ll" printf format size specifier corresponding to |long long| was 00042 // intrudced in MSVC8. Earlier versions did not provide this size specifier, 00043 // but "I64" can be used to print 64-bit types. Don't use "I64" where "ll" 00044 // is available, in the event of oddball systems where |long long| is not 00045 // 64 bits wide. 00046 #if _MSC_VER >= 1400 // MSVC 2005/8 00047 #define WIN_STRING_FORMAT_LL "ll" 00048 #else // MSC_VER >= 1400 00049 #define WIN_STRING_FORMAT_LL "I64" 00050 #endif // MSC_VER >= 1400 00051 00052 // A nonconforming version of swprintf, without the length argument, was 00053 // included with the CRT prior to MSVC8. Although a conforming version was 00054 // also available via an overload, it is not reliably chosen. _snwprintf 00055 // behaves as a standards-confirming swprintf should, so force the use of 00056 // _snwprintf when using older CRTs. 00057 #if _MSC_VER < 1400 // MSVC 2005/8 00058 #define swprintf _snwprintf 00059 #else 00060 // For MSVC8 and newer, swprintf_s is the recommended method. Conveniently, 00061 // it takes the same argument list as swprintf. 00062 #define swprintf swprintf_s 00063 #endif // MSC_VER < 1400 00064 00065 namespace google_breakpad { 00066 00067 using std::string; 00068 using std::wstring; 00069 00070 class WindowsStringUtils { 00071 public: 00072 // Roughly equivalent to MSVC8's wcscpy_s, except pre-MSVC8, this does 00073 // not fail if source is longer than destination_size. The destination 00074 // buffer is always 0-terminated. 00075 static void safe_wcscpy(wchar_t *destination, size_t destination_size, 00076 const wchar_t *source); 00077 00078 // Roughly equivalent to MSVC8's wcsncpy_s, except that _TRUNCATE cannot 00079 // be passed directly, and pre-MSVC8, this will not fail if source or count 00080 // are longer than destination_size. The destination buffer is always 00081 // 0-terminated. 00082 static void safe_wcsncpy(wchar_t *destination, size_t destination_size, 00083 const wchar_t *source, size_t count); 00084 00085 // Performs multi-byte to wide character conversion on C++ strings, using 00086 // mbstowcs_s (MSVC8) or mbstowcs (pre-MSVC8). Returns false on failure, 00087 // without setting wcs. 00088 static bool safe_mbstowcs(const string &mbs, wstring *wcs); 00089 00090 // The inverse of safe_mbstowcs. 00091 static bool safe_wcstombs(const wstring &wcs, string *mbs); 00092 00093 // Returns the base name of a file, e.g. strips off the path. 00094 static wstring GetBaseName(const wstring &filename); 00095 00096 private: 00097 // Disallow instantiation and other object-based operations. 00098 WindowsStringUtils(); 00099 WindowsStringUtils(const WindowsStringUtils&); 00100 ~WindowsStringUtils(); 00101 void operator=(const WindowsStringUtils&); 00102 }; 00103 00104 // static 00105 inline void WindowsStringUtils::safe_wcscpy(wchar_t *destination, 00106 size_t destination_size, 00107 const wchar_t *source) { 00108 #if _MSC_VER >= 1400 // MSVC 2005/8 00109 wcscpy_s(destination, destination_size, source); 00110 #else // _MSC_VER >= 1400 00111 // Pre-MSVC 2005/8 doesn't have wcscpy_s. Simulate it with wcsncpy. 00112 // wcsncpy doesn't 0-terminate the destination buffer if the source string 00113 // is longer than size. Ensure that the destination is 0-terminated. 00114 wcsncpy(destination, source, destination_size); 00115 if (destination && destination_size) 00116 destination[destination_size - 1] = 0; 00117 #endif // _MSC_VER >= 1400 00118 } 00119 00120 // static 00121 inline void WindowsStringUtils::safe_wcsncpy(wchar_t *destination, 00122 size_t destination_size, 00123 const wchar_t *source, 00124 size_t count) { 00125 #if _MSC_VER >= 1400 // MSVC 2005/8 00126 wcsncpy_s(destination, destination_size, source, count); 00127 #else // _MSC_VER >= 1400 00128 // Pre-MSVC 2005/8 doesn't have wcsncpy_s. Simulate it with wcsncpy. 00129 // wcsncpy doesn't 0-terminate the destination buffer if the source string 00130 // is longer than size. Ensure that the destination is 0-terminated. 00131 if (destination_size < count) 00132 count = destination_size; 00133 00134 wcsncpy(destination, source, count); 00135 if (destination && count) 00136 destination[count - 1] = 0; 00137 #endif // _MSC_VER >= 1400 00138 } 00139 00140 } // namespace google_breakpad 00141 00142 #endif // COMMON_WINDOWS_STRING_UTILS_INL_H__