Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/shared/Utils.cpp

Issue 11449051: Fix 64-bit conversion errors (Closed)
Patch Set: Created Aug. 16, 2013, 2:56 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« src/plugin/PluginClass.cpp ('K') | « src/shared/Communication.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #include <memory> 1 #include <memory>
2 #include <stdexcept> 2 #include <stdexcept>
3 #include <vector> 3 #include <vector>
4 4
5 #include <Windows.h> 5 #include <Windows.h>
6 #include <ShlObj.h> 6 #include <ShlObj.h>
7 7
8 #include "Utils.h" 8 #include "Utils.h"
9 9
10 namespace 10 namespace
11 { 11 {
12 // See http://blogs.msdn.com/b/oldnewthing/archive/2004/10/25/247180.aspx 12 // See http://blogs.msdn.com/b/oldnewthing/archive/2004/10/25/247180.aspx
13 EXTERN_C IMAGE_DOS_HEADER __ImageBase; 13 EXTERN_C IMAGE_DOS_HEADER __ImageBase;
14 14
15 std::wstring appDataPath; 15 std::wstring appDataPath;
16 16
17 } 17 }
18 18
19 bool IsWindowsVistaOrLater() 19 bool IsWindowsVistaOrLater()
20 { 20 {
21 OSVERSIONINFOEX osvi; 21 OSVERSIONINFOEX osvi;
22 ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); 22 ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
23 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); 23 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
24 GetVersionEx(reinterpret_cast<LPOSVERSIONINFO>(&osvi)); 24 GetVersionEx(reinterpret_cast<LPOSVERSIONINFO>(&osvi));
25 return osvi.dwMajorVersion >= 6; 25 return osvi.dwMajorVersion >= 6;
26 } 26 }
27 27
28 std::string ToUtf8String(const std::wstring& str) 28 std::string ToUtf8String(const std::wstring& str)
29 { 29 {
30 size_t length = str.size(); 30 int length = static_cast<int>(str.size());
31 if (length == 0) 31 if (length == 0)
32 return std::string(); 32 return std::string();
33 33
34 DWORD utf8StringLength = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), length, 0, 0, 0, 0); 34 int utf8StringLength = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), length, 0, 0, 0, 0);
35 if (utf8StringLength == 0) 35 if (utf8StringLength == 0)
36 throw std::runtime_error("Failed to determine the required buffer size"); 36 throw std::runtime_error("Failed to determine the required buffer size");
37 37
38 std::string utf8String(utf8StringLength, '\0'); 38 std::string utf8String(utf8StringLength, '\0');
39 WideCharToMultiByte(CP_UTF8, 0, str.c_str(), length, &utf8String[0], utf8Strin gLength, 0, 0); 39 WideCharToMultiByte(CP_UTF8, 0, str.c_str(), length, &utf8String[0], utf8Strin gLength, 0, 0);
40 return utf8String; 40 return utf8String;
41 } 41 }
42 42
43 std::wstring ToUtf16String(const std::string& str) 43 std::wstring ToUtf16String(const std::string& str)
44 { 44 {
45 size_t length = str.size(); 45 int length = static_cast<int>(str.size());
46 if (length == 0) 46 if (length == 0)
47 return std::wstring(); 47 return std::wstring();
48 48
49 DWORD utf16StringLength = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), length, NULL, 0); 49 int utf16StringLength = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), length, N ULL, 0);
50 if (utf16StringLength == 0) 50 if (utf16StringLength == 0)
51 throw std::runtime_error("ToUTF16String failed. Can't determine the length o f the buffer needed."); 51 throw std::runtime_error("ToUTF16String failed. Can't determine the length o f the buffer needed.");
52 52
53 std::wstring utf16String(utf16StringLength, L'\0'); 53 std::wstring utf16String(utf16StringLength, L'\0');
54 MultiByteToWideChar(CP_UTF8, 0, str.c_str(), length, &utf16String[0], utf16Str ingLength); 54 MultiByteToWideChar(CP_UTF8, 0, str.c_str(), length, &utf16String[0], utf16Str ingLength);
55 return utf16String; 55 return utf16String;
56 } 56 }
57 57
58 std::wstring GetDllDir() 58 std::wstring GetDllDir()
59 { 59 {
60 std::vector<WCHAR> path(MAX_PATH); 60 std::vector<WCHAR> path(MAX_PATH);
61 DWORD length = GetModuleFileNameW((HINSTANCE)&__ImageBase, &path[0], path.size ()); 61 int length = GetModuleFileNameW((HINSTANCE)&__ImageBase, &path[0], static_cast <DWORD>(path.size()));
62 62
63 while (length == path.size()) 63 while (length == path.size())
64 { 64 {
65 // Buffer too small, double buffer size 65 // Buffer too small, double buffer size
66 path.resize(path.size() * 2); 66 path.resize(path.size() * 2);
67 length = GetModuleFileNameW((HINSTANCE)&__ImageBase, &path[0], path.size()); 67 length = GetModuleFileNameW((HINSTANCE)&__ImageBase, &path[0], static_cast<D WORD>(path.size()));
68 } 68 }
69 69
70 try 70 try
71 { 71 {
72 if (length == 0) 72 if (length == 0)
73 throw std::runtime_error("Failed determining module path"); 73 throw std::runtime_error("Failed determining module path");
74 74
75 std::vector<WCHAR>::reverse_iterator it = std::find(path.rbegin(), path.rend (), L'\\'); 75 std::vector<WCHAR>::reverse_iterator it = std::find(path.rbegin(), path.rend (), L'\\');
76 if (it == path.rend()) 76 if (it == path.rend())
77 throw std::runtime_error("Unexpected plugin path, no backslash found"); 77 throw std::runtime_error("Unexpected plugin path, no backslash found");
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 } 112 }
113 113
114 void ReplaceString(std::wstring& input, const std::wstring placeholder, const st d::wstring replacement) 114 void ReplaceString(std::wstring& input, const std::wstring placeholder, const st d::wstring replacement)
115 { 115 {
116 size_t replaceStart = input.find(placeholder); 116 size_t replaceStart = input.find(placeholder);
117 if (replaceStart != std::string::npos) 117 if (replaceStart != std::string::npos)
118 { 118 {
119 input.replace(replaceStart, placeholder.length(), replacement); 119 input.replace(replaceStart, placeholder.length(), replacement);
120 } 120 }
121 } 121 }
OLDNEW
« src/plugin/PluginClass.cpp ('K') | « src/shared/Communication.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld