1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_INSTALLER_MINI_INSTALLER_MINI_STRING_H_
6 #define CHROME_INSTALLER_MINI_INSTALLER_MINI_STRING_H_
9 // COMPILE_ASSERT macro borrowed from basictypes.h
11 struct CompileAssert
{};
12 #define COMPILE_ASSERT(expr, msg) \
13 typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
16 namespace mini_installer
{
18 // NOTE: Do not assume that these string functions support UTF encoding.
19 // This is fine for the purposes of the mini_installer, but you have
22 // Formats a sequence of |bytes| as hex. The |str| buffer must have room for
23 // at least 2*|size| + 1.
24 bool HexEncode(const void* bytes
, size_t size
, wchar_t* str
, size_t str_size
);
26 // Counts the number of characters in the string up to a maximum of
27 // alloc_size. The highest return value from this function can therefore be
28 // alloc_size - 1 since |alloc_size| includes the \0 terminator.
29 size_t SafeStrLen(const wchar_t* str
, size_t alloc_size
);
31 // Simple replacement for CRT string copy method that does not overflow.
32 // Returns true if the source was copied successfully otherwise returns false.
33 // Parameter src is assumed to be NULL terminated and the NULL character is
34 // copied over to string dest.
35 bool SafeStrCopy(wchar_t* dest
, size_t dest_size
, const wchar_t* src
);
37 // Simple replacement for CRT string copy method that does not overflow.
38 // Returns true if the source was copied successfully otherwise returns false.
39 // Parameter src is assumed to be NULL terminated and the NULL character is
40 // copied over to string dest. If the return value is false, the |dest|
41 // string should be the same as it was before.
42 bool SafeStrCat(wchar_t* dest
, size_t dest_size
, const wchar_t* src
);
44 // Function to check if a string (specified by str) ends with another string
45 // (specified by end_str).
46 bool StrEndsWith(const wchar_t* str
, const wchar_t* end_str
);
48 // Function to check if a string (specified by str) starts with another string
49 // (specified by start_str).
50 bool StrStartsWith(const wchar_t* str
, const wchar_t* start_str
);
52 // Case insensitive search of the first occurrence of |find| in |source|.
53 const wchar_t* SearchStringI(const wchar_t* source
, const wchar_t* find
);
55 // Searches for |tag| within |str|. Returns true if |tag| is found and is
56 // immediately followed by '-' or is at the end of the string. If |position|
57 // is non-NULL, the location of the tag is returned in |*position| on success.
58 bool FindTagInStr(const wchar_t* str
, const wchar_t* tag
,
59 const wchar_t** position
);
61 // Takes the path to file and returns a pointer to the basename component.
62 // Example input -> output:
63 // c:\full\path\to\file.ext -> file.ext
64 // file.ext -> file.ext
65 // Note: |size| is the number of characters in |path| not including the string
67 const wchar_t* GetNameFromPathExt(const wchar_t* path
, size_t size
);
68 wchar_t* GetNameFromPathExt(wchar_t* path
, size_t size
);
70 // A string class that manages a fixed size buffer on the stack.
71 // The methods in the class are based on the above string methods and the
72 // class additionally is careful about proper buffer termination.
73 template <size_t kCapacity
>
77 COMPILE_ASSERT(kCapacity
!= 0, invalid_buffer_size
);
78 buffer_
[kCapacity
] = L
'\0'; // We always reserve 1 more than asked for.
82 // We do not expose a constructor that accepts a string pointer on purpose.
83 // We expect the caller to call assign() and handle failures.
85 // Returns the number of reserved characters in this buffer, _including_
86 // the reserved char for the terminator.
87 size_t capacity() const {
95 bool assign(const wchar_t* str
) {
96 return SafeStrCopy(buffer_
, kCapacity
, str
);
99 bool append(const wchar_t* str
) {
100 return SafeStrCat(buffer_
, kCapacity
, str
);
107 size_t length() const {
108 return SafeStrLen(buffer_
, kCapacity
);
111 // Does a case insensitive search for a substring.
112 const wchar_t* findi(const wchar_t* find
) const {
113 return SearchStringI(buffer_
, find
);
116 // Case insensitive string compare.
117 int comparei(const wchar_t* str
) const {
118 return lstrcmpiW(buffer_
, str
);
121 // Case sensitive string compare.
122 int compare(const wchar_t* str
) const {
123 return lstrcmpW(buffer_
, str
);
126 // Terminates the string at the specified location.
127 // Note: this method has no effect if this object's length is less than
129 bool truncate_at(size_t location
) {
130 if (location
>= kCapacity
)
132 buffer_
[location
] = L
'\0';
137 // We reserve 1 more than what is asked for as a safeguard against
138 // off-by-one errors.
139 wchar_t buffer_
[kCapacity
+ 1];
142 StackString(const StackString
&);
143 StackString
& operator=(const StackString
&);
146 } // namespace mini_installer
148 #endif // CHROME_INSTALLER_MINI_INSTALLER_MINI_STRING_H_