| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_table_reader.h" | 5 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_table_reader.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/threading/thread_restrictions.h" |
| 12 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_column_reader.h" | 14 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_column_reader.h" |
| 14 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_constants.h" | |
| 15 | 15 |
| 16 namespace picasaimport { | 16 namespace picasaimport { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 COMPILE_ASSERT(sizeof(double) == 8, double_must_be_8_bytes_long); | 20 COMPILE_ASSERT(sizeof(double) == 8, double_must_be_8_bytes_long); |
| 21 | 21 |
| 22 } // namespace | 22 } // namespace |
| 23 | 23 |
| 24 PmpTableReader::PmpTableReader() : column_readers_(), max_row_count_(0) { } | 24 PmpTableReader::PmpTableReader(const std::string& table_name, |
| 25 const base::FilePath& directory_path) |
| 26 : initialized_(false), |
| 27 table_name_(table_name), |
| 28 directory_path_(directory_path), |
| 29 max_row_count_(0) { |
| 30 base::ThreadRestrictions::AssertIOAllowed(); |
| 25 | 31 |
| 26 PmpTableReader::~PmpTableReader() { } | 32 if (!file_util::DirectoryExists(directory_path_)) |
| 33 return; |
| 27 | 34 |
| 28 bool PmpTableReader::Init(const std::string& table_name, | 35 std::string indicator_file_name = table_name_ + "_0"; |
| 29 const base::FilePath& directory_path, | |
| 30 const std::vector<std::string>& columns) { | |
| 31 DCHECK(!columns.empty()); | |
| 32 | 36 |
| 33 if (!column_readers_.empty()) | 37 base::FilePath indicator_file = directory_path_.Append( |
| 34 return false; | |
| 35 | |
| 36 if (!file_util::DirectoryExists(directory_path)) | |
| 37 return false; | |
| 38 | |
| 39 std::string table_prefix = table_name + "_"; | |
| 40 std::string indicator_file_name = table_prefix + "0"; | |
| 41 | |
| 42 base::FilePath indicator_file = directory_path.Append( | |
| 43 base::FilePath::FromUTF8Unsafe(indicator_file_name)); | 38 base::FilePath::FromUTF8Unsafe(indicator_file_name)); |
| 44 | 39 |
| 45 // Look for the indicator_file file, indicating table existence. | 40 // Look for the indicator_file file, indicating table existence. |
| 46 if (!file_util::PathExists(indicator_file) || | 41 initialized_ = file_util::PathExists(indicator_file) && |
| 47 file_util::DirectoryExists(indicator_file)) { | 42 !file_util::DirectoryExists(indicator_file); |
| 48 return false; | 43 } |
| 49 } | |
| 50 | 44 |
| 51 ScopedVector<PmpColumnReader> column_readers; | 45 PmpTableReader::~PmpTableReader() {} |
| 52 uint32 max_row_count = 0; | |
| 53 | 46 |
| 54 for (std::vector<std::string>::const_iterator it = columns.begin(); | 47 const PmpColumnReader* PmpTableReader::AddColumn( |
| 55 it != columns.end(); ++it) { | 48 const std::string& column_name, const PmpFieldType expected_type) { |
| 56 std::string filename = table_prefix + *it + "." + kPmpExtension; | 49 if (!initialized_) |
| 50 return NULL; |
| 57 | 51 |
| 58 base::FilePath column_file_path = directory_path.Append( | 52 std::string filename = table_name_ + "_" + column_name + "." + kPmpExtension; |
| 53 |
| 54 base::FilePath column_file_path = directory_path_.Append( |
| 59 base::FilePath::FromUTF8Unsafe(filename)); | 55 base::FilePath::FromUTF8Unsafe(filename)); |
| 56 scoped_ptr<PmpColumnReader> column_reader(new PmpColumnReader()); |
| 60 | 57 |
| 61 PmpColumnReader* column_reader = new PmpColumnReader(); | 58 uint32 row_count; |
| 62 column_readers.push_back(column_reader); | 59 if (!column_reader->Init(column_file_path, expected_type, &row_count)) |
| 60 return NULL; |
| 63 | 61 |
| 64 uint32 row_cnt; | 62 column_readers_.push_back(column_reader.release()); |
| 63 column_map_[column_name] = column_readers_.back(); |
| 65 | 64 |
| 66 if (!column_reader->Init(column_file_path, &row_cnt)) | 65 max_row_count_ = std::max(max_row_count_, row_count); |
| 67 return false; | |
| 68 | 66 |
| 69 max_row_count = std::max(max_row_count, row_cnt); | 67 return column_readers_.back(); |
| 70 } | |
| 71 | |
| 72 column_readers_ = column_readers.Pass(); | |
| 73 max_row_count_ = max_row_count; | |
| 74 | |
| 75 return true; | |
| 76 } | 68 } |
| 77 | 69 |
| 78 uint32 PmpTableReader::RowCount() const { | 70 uint32 PmpTableReader::RowCount() const { |
| 79 return max_row_count_; | 71 return max_row_count_; |
| 80 } | 72 } |
| 81 | 73 |
| 82 std::vector<const PmpColumnReader*> PmpTableReader::GetColumns() const { | 74 std::map<std::string, const PmpColumnReader*> PmpTableReader::Columns() const { |
| 83 std::vector<const PmpColumnReader*> readers( | 75 return column_map_; |
| 84 column_readers_.begin(), column_readers_.end()); | |
| 85 return readers; | |
| 86 } | 76 } |
| 87 | 77 |
| 88 } // namespace picasaimport | 78 } // namespace picasaimport |
| OLD | NEW |