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

Unified Diff: webkit/fileapi/media/picasa/pmp_table_reader.cc

Issue 12704024: Simple PMP reader to parse Picasa's metadata (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More formatting/ style changes. Created 7 years, 9 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
Index: webkit/fileapi/media/picasa/pmp_table_reader.cc
diff --git a/webkit/fileapi/media/picasa/pmp_table_reader.cc b/webkit/fileapi/media/picasa/pmp_table_reader.cc
new file mode 100644
index 0000000000000000000000000000000000000000..bd008acd6edbe5849b372585fb399d65c632f263
--- /dev/null
+++ b/webkit/fileapi/media/picasa/pmp_table_reader.cc
@@ -0,0 +1,113 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "webkit/fileapi/media/picasa/pmp_table_reader.h"
+
+#include <algorithm>
+
+#include "base/file_util.h"
+#include "base/files/file_path.h"
+#include "base/files/memory_mapped_file.h"
+#include "base/logging.h"
+#include "webkit/fileapi/media/picasa/pmp_column_reader.h"
+#include "webkit/fileapi/media/picasa/pmp_constants.h"
+
+namespace fileapi {
+
+namespace {
+ COMPILE_ASSERT(sizeof(double) == 8, double_must_be_8_bytes_long);
vandebo (ex-Chrome) 2013/03/27 00:06:19 no indent on namespace, blank lines before and aft
tommycli 2013/03/27 19:34:30 Done.
+ const size_t kPmpMaxFilesize = 50*1024*1024; // Maximum of 50 MB.
+}
+
+using std::string;
vandebo (ex-Chrome) 2013/03/27 00:06:19 we don't "use" anything from std.
tommycli 2013/03/27 19:34:30 Done.
+
+PmpTableReader::PmpTableReader() : column_readers_(), max_row_count_(0) {
+
vandebo (ex-Chrome) 2013/03/27 00:06:19 nit: extra line.
tommycli 2013/03/27 19:34:30 Done.
+}
+
+PmpTableReader::~PmpTableReader() {
+ PmpTableReader::FreeAndClearContents(column_readers_);
+}
+
+bool PmpTableReader::ReadFromDisk(const string& table_name,
+ const base::FilePath& directory_path,
+ const std::vector<std::string>& columns) {
+ // Expect to read some columns, otherwise, a programming error.
+ DCHECK(!columns.empty());
+
+ // Directory must exist.
+ if(!file_util::DirectoryExists(directory_path)) {
vandebo (ex-Chrome) 2013/03/27 00:06:19 nit: no {}s
tommycli 2013/03/27 19:34:30 Done.
+ return false;
+ }
+
+ string table_prefix = table_name + "_";
+ string pmp_suffix = ".pmp";
vandebo (ex-Chrome) 2013/03/27 00:06:19 Maybe this should go in the constants file?
tommycli 2013/03/27 19:34:30 Done.
+
+ // Look for the "%s_0".format(table_name_) file, indicating table existence.
+ base::FilePath indicator_file = directory_path.Append(table_prefix + "0");
+
+ // Expect the indicator file to exist but not be a directory.
+ if(!file_util::PathExists(indicator_file) ||
+ file_util::DirectoryExists(indicator_file)) {
+ return false;
+ }
+
+ std::vector<PmpColumnReader*> current_col_readers;
+ uint32 current_max_rows_parsed = 0;
+
+ // Construct the column readers.
+ for (std::vector<std::string>::const_iterator it = columns.begin();
+ it != columns.end(); it++) {
+ base::FilePath colfile_path = directory_path.Append(
vandebo (ex-Chrome) 2013/03/27 00:06:19 nit: column_file_path
tommycli 2013/03/27 19:34:30 Done.
+ table_prefix + *it + pmp_suffix);
+
+ // Constructed on the heap due to PmpColumnReader disallowing
vandebo (ex-Chrome) 2013/03/27 00:06:19 Not sure this comment is necessary.
tommycli 2013/03/27 19:34:30 Done.
+ // copies and assignment.
+ PmpColumnReader* column_reader = new PmpColumnReader(*it);
+ current_col_readers.push_back(column_reader);
+
+ base::MemoryMappedFile file;
+ uint32 row_cnt; // row count
vandebo (ex-Chrome) 2013/03/27 00:06:19 unnecessary comment.
tommycli 2013/03/27 19:34:30 Done.
+
+ // If file is not initialized, too small, or too large (50MB), fail out.
+ if (!file.Initialize(colfile_path) ||
+ file.length() < kPmpHeaderSize ||
+ file.length() > kPmpMaxFilesize ||
+ !column_reader->ReadFromMemory(&row_cnt, file.data(), file.length())) {
vandebo (ex-Chrome) 2013/03/27 00:06:19 This configuration forces a copy. If you just pas
tommycli 2013/03/27 19:34:30 OBE.
+ PmpTableReader::FreeAndClearContents(current_col_readers);
+ return false;
+ }
+
+ current_max_rows_parsed = std::max(current_max_rows_parsed, row_cnt);
+ }
+
+ // Actually succeeded. Set member data.
+ PmpTableReader::FreeAndClearContents(column_readers_);
+ column_readers_ = current_col_readers;
+ max_row_count_ = current_max_rows_parsed;
+
+ return true;
+}
+
+uint32 PmpTableReader::Count() const {
+ return max_row_count_;
+}
+
+std::vector<const PmpColumnReader*> PmpTableReader::GetColumns() const {
+ std::vector<const PmpColumnReader*> readers;
+ std::copy(column_readers_.begin(), column_readers_.end(),
+ std::back_inserter(readers));
+ return readers;
+}
+
+void PmpTableReader::FreeAndClearContents(
+ std::vector<PmpColumnReader*> readers) {
+ for (std::vector<PmpColumnReader*>::iterator it = readers.begin();
+ it != readers.end(); it++) {
+ delete *it;
+ }
+ readers.clear();
+}
+
+} // namespace fileapi

Powered by Google App Engine
This is Rietveld 408576698