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

Side by Side 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, 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2013 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.
4
5 #include "webkit/fileapi/media/picasa/pmp_table_reader.h"
6
7 #include <algorithm>
8
9 #include "base/file_util.h"
10 #include "base/files/file_path.h"
11 #include "base/files/memory_mapped_file.h"
12 #include "base/logging.h"
13 #include "webkit/fileapi/media/picasa/pmp_column_reader.h"
14 #include "webkit/fileapi/media/picasa/pmp_constants.h"
15
16 namespace fileapi {
17
18 namespace {
19 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.
20 const size_t kPmpMaxFilesize = 50*1024*1024; // Maximum of 50 MB.
21 }
22
23 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.
24
25 PmpTableReader::PmpTableReader() : column_readers_(), max_row_count_(0) {
26
vandebo (ex-Chrome) 2013/03/27 00:06:19 nit: extra line.
tommycli 2013/03/27 19:34:30 Done.
27 }
28
29 PmpTableReader::~PmpTableReader() {
30 PmpTableReader::FreeAndClearContents(column_readers_);
31 }
32
33 bool PmpTableReader::ReadFromDisk(const string& table_name,
34 const base::FilePath& directory_path,
35 const std::vector<std::string>& columns) {
36 // Expect to read some columns, otherwise, a programming error.
37 DCHECK(!columns.empty());
38
39 // Directory must exist.
40 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.
41 return false;
42 }
43
44 string table_prefix = table_name + "_";
45 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.
46
47 // Look for the "%s_0".format(table_name_) file, indicating table existence.
48 base::FilePath indicator_file = directory_path.Append(table_prefix + "0");
49
50 // Expect the indicator file to exist but not be a directory.
51 if(!file_util::PathExists(indicator_file) ||
52 file_util::DirectoryExists(indicator_file)) {
53 return false;
54 }
55
56 std::vector<PmpColumnReader*> current_col_readers;
57 uint32 current_max_rows_parsed = 0;
58
59 // Construct the column readers.
60 for (std::vector<std::string>::const_iterator it = columns.begin();
61 it != columns.end(); it++) {
62 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.
63 table_prefix + *it + pmp_suffix);
64
65 // 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.
66 // copies and assignment.
67 PmpColumnReader* column_reader = new PmpColumnReader(*it);
68 current_col_readers.push_back(column_reader);
69
70 base::MemoryMappedFile file;
71 uint32 row_cnt; // row count
vandebo (ex-Chrome) 2013/03/27 00:06:19 unnecessary comment.
tommycli 2013/03/27 19:34:30 Done.
72
73 // If file is not initialized, too small, or too large (50MB), fail out.
74 if (!file.Initialize(colfile_path) ||
75 file.length() < kPmpHeaderSize ||
76 file.length() > kPmpMaxFilesize ||
77 !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.
78 PmpTableReader::FreeAndClearContents(current_col_readers);
79 return false;
80 }
81
82 current_max_rows_parsed = std::max(current_max_rows_parsed, row_cnt);
83 }
84
85 // Actually succeeded. Set member data.
86 PmpTableReader::FreeAndClearContents(column_readers_);
87 column_readers_ = current_col_readers;
88 max_row_count_ = current_max_rows_parsed;
89
90 return true;
91 }
92
93 uint32 PmpTableReader::Count() const {
94 return max_row_count_;
95 }
96
97 std::vector<const PmpColumnReader*> PmpTableReader::GetColumns() const {
98 std::vector<const PmpColumnReader*> readers;
99 std::copy(column_readers_.begin(), column_readers_.end(),
100 std::back_inserter(readers));
101 return readers;
102 }
103
104 void PmpTableReader::FreeAndClearContents(
105 std::vector<PmpColumnReader*> readers) {
106 for (std::vector<PmpColumnReader*>::iterator it = readers.begin();
107 it != readers.end(); it++) {
108 delete *it;
109 }
110 readers.clear();
111 }
112
113 } // namespace fileapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698