| OLD | NEW |
| (Empty) |
| 1 // Copyright 2010 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 | |
| 16 #include "omaha/goopdate/string_formatter.h" | |
| 17 #include "omaha/base/debug.h" | |
| 18 #include "omaha/base/error.h" | |
| 19 #include "omaha/base/logging.h" | |
| 20 #include "omaha/base/utils.h" | |
| 21 #include "omaha/goopdate/resource_manager.h" | |
| 22 | |
| 23 namespace omaha { | |
| 24 | |
| 25 StringFormatter::StringFormatter(const CString& language) | |
| 26 : language_(language) { | |
| 27 ASSERT1(!language.IsEmpty()); | |
| 28 } | |
| 29 | |
| 30 HRESULT StringFormatter::LoadString(int32 resource_id, CString* result) { | |
| 31 ASSERT1(result); | |
| 32 | |
| 33 HINSTANCE resource_handle = NULL; | |
| 34 HRESULT hr = ResourceManager::Instance().GetResourceDll(language_, | |
| 35 &resource_handle); | |
| 36 if (FAILED(hr)) { | |
| 37 return hr; | |
| 38 } | |
| 39 | |
| 40 const TCHAR* resource_string = NULL; | |
| 41 int string_length = ::LoadString( | |
| 42 resource_handle, | |
| 43 resource_id, | |
| 44 reinterpret_cast<TCHAR*>(&resource_string), | |
| 45 0); | |
| 46 if (string_length <= 0) { | |
| 47 return HRESULTFromLastError(); | |
| 48 } | |
| 49 ASSERT1(resource_string && *resource_string); | |
| 50 | |
| 51 // resource_string is the string starting point but not null-terminated, so | |
| 52 // explicitly copy from it for string_length characters. | |
| 53 result->SetString(resource_string, string_length); | |
| 54 | |
| 55 return S_OK; | |
| 56 } | |
| 57 | |
| 58 HRESULT StringFormatter::FormatMessage(CString* result, int32 format_id, ...) { | |
| 59 ASSERT1(result); | |
| 60 ASSERT1(format_id != 0); | |
| 61 | |
| 62 CString format_string; | |
| 63 HRESULT hr = LoadString(format_id, &format_string); | |
| 64 if (FAILED(hr)) { | |
| 65 return hr; | |
| 66 } | |
| 67 | |
| 68 va_list arguments; | |
| 69 va_start(arguments, format_id); | |
| 70 result->FormatMessageV(format_string, &arguments); | |
| 71 va_end(arguments); | |
| 72 | |
| 73 return S_OK; | |
| 74 } | |
| 75 | |
| 76 } // namespace omaha | |
| OLD | NEW |