~ubuntu-branches/ubuntu/lucid/lastfm/lucid

« back to all changes in this revision

Viewing changes to src/breakpad/common/windows/string_utils-inl.h

  • Committer: Bazaar Package Importer
  • Author(s): Pedro Fragoso
  • Date: 2007-12-31 09:49:54 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20071231094954-ix1amvcsj9pk61ya
Tags: 1:1.4.1.57486.dfsg-1ubuntu1
* Merge from Debian unstable (LP: #180254), remaining changes:
  - debian/rules;
    - Added dh_icons
  - Modify Maintainer value to match Debian-Maintainer-Field Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
// string_utils-inl.h: Safer string manipulation on Windows, supporting
31
 
// pre-MSVC8 environments.
32
 
 
33
 
#ifndef COMMON_WINDOWS_STRING_UTILS_INL_H__
34
 
#define COMMON_WINDOWS_STRING_UTILS_INL_H__
35
 
 
36
 
#include <stdarg.h>
37
 
#include <wchar.h>
38
 
 
39
 
#include <string>
40
 
 
41
 
// The "ll" printf format size specifier corresponding to |long long| was
42
 
// intrudced in MSVC8.  Earlier versions did not provide this size specifier,
43
 
// but "I64" can be used to print 64-bit types.  Don't use "I64" where "ll"
44
 
// is available, in the event of oddball systems where |long long| is not
45
 
// 64 bits wide.
46
 
#if _MSC_VER >= 1400  // MSVC 2005/8
47
 
#define WIN_STRING_FORMAT_LL "ll"
48
 
#else  // MSC_VER >= 1400
49
 
#define WIN_STRING_FORMAT_LL "I64"
50
 
#endif  // MSC_VER >= 1400
51
 
 
52
 
// A nonconforming version of swprintf, without the length argument, was
53
 
// included with the CRT prior to MSVC8.  Although a conforming version was
54
 
// also available via an overload, it is not reliably chosen.  _snwprintf
55
 
// behaves as a standards-confirming swprintf should, so force the use of
56
 
// _snwprintf when using older CRTs.
57
 
#if _MSC_VER < 1400  // MSVC 2005/8
58
 
#define swprintf _snwprintf
59
 
#endif  // MSC_VER < 1400
60
 
 
61
 
namespace google_breakpad {
62
 
 
63
 
using std::string;
64
 
using std::wstring;
65
 
 
66
 
class WindowsStringUtils {
67
 
 public:
68
 
  // Roughly equivalent to MSVC8's wcscpy_s, except pre-MSVC8, this does
69
 
  // not fail if source is longer than destination_size.  The destination
70
 
  // buffer is always 0-terminated.
71
 
  static void safe_wcscpy(wchar_t *destination, size_t destination_size,
72
 
                          const wchar_t *source);
73
 
 
74
 
  // Roughly equivalent to MSVC8's wcsncpy_s, except that _TRUNCATE cannot
75
 
  // be passed directly, and pre-MSVC8, this will not fail if source or count
76
 
  // are longer than destination_size.  The destination buffer is always
77
 
  // 0-terminated.
78
 
  static void safe_wcsncpy(wchar_t *destination, size_t destination_size,
79
 
                           const wchar_t *source, size_t count);
80
 
 
81
 
  // Performs multi-byte to wide character conversion on C++ strings, using
82
 
  // mbstowcs_s (MSVC8) or mbstowcs (pre-MSVC8).  Returns false on failure,
83
 
  // without setting wcs.
84
 
  static bool safe_mbstowcs(const string &mbs, wstring *wcs);
85
 
 
86
 
  // Returns the base name of a file, e.g. strips off the path.
87
 
  static wstring GetBaseName(const wstring &filename);
88
 
 
89
 
 private:
90
 
  // Disallow instantiation and other object-based operations.
91
 
  WindowsStringUtils();
92
 
  WindowsStringUtils(const WindowsStringUtils&);
93
 
  ~WindowsStringUtils();
94
 
  void operator=(const WindowsStringUtils&);
95
 
};
96
 
 
97
 
// static
98
 
inline void WindowsStringUtils::safe_wcscpy(wchar_t *destination,
99
 
                                            size_t destination_size,
100
 
                                            const wchar_t *source) {
101
 
#if _MSC_VER >= 1400  // MSVC 2005/8
102
 
  wcscpy_s(destination, destination_size, source);
103
 
#else  // _MSC_VER >= 1400
104
 
  // Pre-MSVC 2005/8 doesn't have wcscpy_s.  Simulate it with wcsncpy.
105
 
  // wcsncpy doesn't 0-terminate the destination buffer if the source string
106
 
  // is longer than size.  Ensure that the destination is 0-terminated.
107
 
  wcsncpy(destination, source, destination_size);
108
 
  if (destination && destination_size)
109
 
    destination[destination_size - 1] = 0;
110
 
#endif  // _MSC_VER >= 1400
111
 
}
112
 
 
113
 
// static
114
 
inline void WindowsStringUtils::safe_wcsncpy(wchar_t *destination,
115
 
                                             size_t destination_size,
116
 
                                             const wchar_t *source,
117
 
                                             size_t count) {
118
 
#if _MSC_VER >= 1400  // MSVC 2005/8
119
 
  wcsncpy_s(destination, destination_size, source, count);
120
 
#else  // _MSC_VER >= 1400
121
 
  // Pre-MSVC 2005/8 doesn't have wcsncpy_s.  Simulate it with wcsncpy.
122
 
  // wcsncpy doesn't 0-terminate the destination buffer if the source string
123
 
  // is longer than size.  Ensure that the destination is 0-terminated.
124
 
  if (destination_size < count)
125
 
    count = destination_size;
126
 
 
127
 
  wcsncpy(destination, source, count);
128
 
  if (destination && count)
129
 
    destination[count - 1] = 0;
130
 
#endif  // _MSC_VER >= 1400
131
 
}
132
 
 
133
 
}  // namespace google_breakpad
134
 
 
135
 
#endif  // COMMON_WINDOWS_STRING_UTILS_INL_H__