Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6959)

Unified Diff: chrome/common/win_util.cc

Issue 63093: Refactoring; switch the |filter| parameter into something that is more amenab... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/common/win_util.h ('k') | chrome/renderer/render_view.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/common/win_util.cc
===================================================================
--- chrome/common/win_util.cc (revision 13572)
+++ chrome/common/win_util.cc (working copy)
@@ -219,107 +219,63 @@
return false;
}
-// Set up a filter for a Save/Open dialog, which will consist of 'file_ext'
-// file extension, 'ext_desc' as the text description of the 'file_ext' type,
-// and (optionally) the default 'All Files' view. The purpose of the filter is
-// to show only files of a particular type in a Windows Save/Open dialog box.
-// The resulting filter is stored in 'buffer', which is a vector since multiple
-// NULLs are embedded. The filters created here are:
-// 1. only files that have 'file_ext' as their extension
-// 2. all files (only added if 'include_all_files' is true)
-// Example:
-// file_ext: ".txt"
-// ext_desc: "Text Document"
-// returned (in buffer): "Text Document\0*.txt\0All Files\0*.*\0\0"
-// This is painful to build, as you will soon see.
-static void FormatFilterForExtension(const std::wstring& file_ext,
- const std::wstring& ext_desc,
- bool include_all_files,
- std::vector<wchar_t>* buffer) {
- DCHECK(buffer);
-
- // Force something reasonable to appear in the dialog box if there is no
- // description provided.
- if (file_ext.empty() || ext_desc.empty())
- include_all_files = true;
-
- size_t size;
- size_t offset = 0;
+std::wstring FormatFilterForExtensions(
+ const std::vector<std::wstring>& file_ext,
+ const std::vector<std::wstring>& ext_desc,
+ bool include_all_files) {
const std::wstring all_ext = L"*.*";
const std::wstring all_desc = l10n_util::GetString(IDS_SAVEAS_ALL_FILES);
- // Includes 2 internal NULLs + "*".
- const size_t ext_size = ext_desc.length() + file_ext.length() + 3;
- // Includes 2 internal NULLs.
- const size_t all_size = all_desc.length() + all_ext.length() + 2;
- // Includes double terminating NULL.
- const size_t buf_size = (!ext_desc.empty() ? ext_size : 0) +
- (include_all_files ? all_size : 0) + 1;
- buffer->resize(buf_size);
+ DCHECK(file_ext.size()>=ext_desc.size());
- if (!file_ext.empty() && !ext_desc.empty()) {
- // Copy in the text description ("JPEG Image") + NULL.
- size = ext_desc.length() + 1;
- memcpy(&(*buffer)[offset], ext_desc.c_str(), size * sizeof(wchar_t));
- offset += size;
+ std::wstring result;
- // Copy in the file type ("*.jpg") + NULL.
- const std::wstring wildcard_ext = L"*" + file_ext;
- size = wildcard_ext.length() + 1;
- memcpy(&(*buffer)[offset], wildcard_ext.c_str(), size * sizeof(wchar_t));
- offset += size;
- }
+ for (size_t i=0; i<file_ext.size(); ++i) {
+ std::wstring ext = file_ext[i];
+ std::wstring desc;
+ if (i<ext_desc.size())
+ desc = ext_desc[i];
- if (include_all_files) {
- // Copy in the default description ("All Files") + NULL.
- size = all_desc.length() + 1;
- memcpy(&(*buffer)[offset], all_desc.c_str(), size * sizeof(wchar_t));
- offset += size;
+ if (ext.empty()) {
+ // Force something reasonable to appear in the dialog box if there is no
+ // extension provided.
+ include_all_files = true;
+ continue;
+ }
- // Copy in the default file extension ("*.*") + NULL.
- size = all_ext.length() + 1;
- memcpy(&(*buffer)[offset], all_ext.c_str(), size * sizeof(wchar_t));
- offset += size;
+ if (desc.empty()) {
+ DCHECK(ext.find(L'.') != std::wstring::npos);
+ std::wstring first_extension = ext.substr(ext.find(L'.'));
+ size_t first_separator_index = first_extension.find(L';');
+ if (first_separator_index != std::wstring::npos)
+ first_extension = first_extension.substr(0, first_separator_index);
+ GetRegistryDescriptionFromExtension(first_extension, &desc);
+ if (desc.empty())
+ desc = L"*." + first_extension;
+ }
+
+ result.append(desc.c_str(), desc.size()+1); // Append NULL too.
+ result.append(ext.c_str(), ext.size()+1);
}
- (*buffer)[offset] = L'\0'; // Double NULL required.
-}
-
-std::wstring GetFileFilterFromPath(const std::wstring& file_name) {
- std::wstring reg_description;
- std::wstring file_ext = file_util::GetFileExtensionFromPath(file_name);
- if (!file_ext.empty()) {
- file_ext = L"." + file_ext;
- GetRegistryDescriptionFromExtension(file_ext, &reg_description);
+ if (include_all_files) {
+ result.append(all_desc.c_str(), all_desc.size()+1);
+ result.append(all_ext.c_str(), all_ext.size()+1);
}
- std::vector<wchar_t> filter;
- FormatFilterForExtension(file_ext, reg_description, true, &filter);
- return std::wstring(&filter[0], filter.size());
+ result.append(1, '\0'); // Double NULL required.
+ return result;
}
-std::wstring GetFileFilterFromExtensions(const std::wstring& extensions,
- bool include_all_files) {
- DCHECK(extensions.find(L'.') != std::wstring::npos);
- std::wstring first_extension = extensions.substr(extensions.find(L'.'));
- size_t first_separator_index = first_extension.find(L';');
- if (first_separator_index != std::wstring::npos)
- first_extension = first_extension.substr(0, first_separator_index);
-
- std::wstring description;
- GetRegistryDescriptionFromExtension(first_extension, &description);
- if (description.empty())
- description = L"*." + first_extension;
-
- std::vector<wchar_t> filter;
- FormatFilterForExtension(extensions, description, true, &filter);
- return std::wstring(&filter[0], filter.size());
-}
-
bool SaveFileAs(HWND owner,
const std::wstring& suggested_name,
std::wstring* final_name) {
- std::wstring filter = GetFileFilterFromPath(suggested_name);
+ std::wstring file_ext = file_util::GetFileExtensionFromPath(suggested_name);
+ file_ext.insert(L'.', 0);
+ std::wstring filter = FormatFilterForExtensions(
+ std::vector<std::wstring>(1, file_ext),
+ std::vector<std::wstring>(),
+ true);
unsigned index = 1;
return SaveFileAsWithFilter(owner,
suggested_name,
« no previous file with comments | « chrome/common/win_util.h ('k') | chrome/renderer/render_view.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698