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

Side by Side Diff: chrome/browser/media_galleries/fileapi/picasa/pmp_test_helper.cc

Issue 13529028: PicasaAlbumTableReader for Media Galleries API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@0005-picasa-import-pmp-reader
Patch Set: Update gypi file to exclude test on android. 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
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_test_helper.h" 5 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_test_helper.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <iterator> 8 #include <iterator>
9 9
10 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
13 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_column_reader.h" 13 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_column_reader.h"
14 14
15 namespace picasaimport { 15 namespace picasaimport {
16 16
17 namespace { 17 namespace {
18 18
19 bool WriteToFile(const base::FilePath& path, std::vector<uint8> data) { 19 bool WriteToFile(const base::FilePath& path, std::vector<uint8> data) {
20 // Cast for usage in WriteFile function 20 // Cast for usage in WriteFile function
21 const char* data_char = reinterpret_cast<const char*>(&data[0]); 21 const char* data_char = reinterpret_cast<const char*>(&data[0]);
22 size_t bytes_written = file_util::WriteFile(path, data_char, data.size()); 22 size_t bytes_written = file_util::WriteFile(path, data_char, data.size());
23 return (bytes_written == data.size()); 23 return (bytes_written == data.size());
24 } 24 }
25 25
26 // Flatten a vector of elements into an array of bytes. 26 // Flatten a vector of elements into an array of bytes.
27 template<class T> 27 template<class T>
28 std::vector<uint8> Flatten(const std::vector<T>& elems) { 28 std::vector<uint8> Flatten(const std::vector<T>& elems) {
29 const uint8* elems0 = reinterpret_cast<const uint8*>(&elems[0]); 29 const uint8* elems0 = reinterpret_cast<const uint8*>(&elems[0]);
30 std::vector<uint8> data_body(elems0, elems0 + sizeof(T)*elems.size()); 30 std::vector<uint8> data_body(elems0, elems0 + sizeof(T) * elems.size());
31 31
32 return data_body; 32 return data_body;
33 } 33 }
34 34
35 // Custom specialization for std::string. 35 // Custom specialization for std::string.
36 template<> 36 template<>
37 std::vector<uint8> Flatten(const std::vector<std::string>& strings) { 37 std::vector<uint8> Flatten(const std::vector<std::string>& strings) {
38 std::vector<uint8> totalchars; 38 std::vector<uint8> totalchars;
39 39
40 for (std::vector<std::string>::const_iterator it = strings.begin(); 40 for (std::vector<std::string>::const_iterator it = strings.begin();
41 it != strings.end(); ++it) { 41 it != strings.end(); ++it) {
42 std::copy(it->begin(), it->end(), std::back_inserter(totalchars)); 42 std::copy(it->begin(), it->end(), std::back_inserter(totalchars));
43 totalchars.push_back('\0'); // Add the null termination too. 43 totalchars.push_back('\0'); // Add the null termination too.
44 } 44 }
45 45
46 return totalchars; 46 return totalchars;
47 } 47 }
48 48
49 // Returns a new vector with the concatenated contents of |a| and |b|. 49 // Returns a new vector with the concatenated contents of |a| and |b|.
50 std::vector<uint8> CombinedVectors(const std::vector<uint8>& a, 50 std::vector<uint8> CombinedVectors(const std::vector<uint8>& a,
51 const std::vector<uint8>& b) { 51 const std::vector<uint8>& b) {
52 std::vector<uint8> total; 52 std::vector<uint8> total;
53 53
54 std::copy(a.begin(), a.end(), std::back_inserter(total)); 54 std::copy(a.begin(), a.end(), std::back_inserter(total));
55 std::copy(b.begin(), b.end(), std::back_inserter(total)); 55 std::copy(b.begin(), b.end(), std::back_inserter(total));
56 56
57 return total; 57 return total;
58 } 58 }
59 59
60 } // namespace 60 } // namespace
61 61
62 PmpTestHelper::PmpTestHelper() { } 62 PmpTestHelper::PmpTestHelper(const std::string& table_name)
63 : table_name_(table_name) {
64 }
63 65
64 bool PmpTestHelper::Init() { 66 bool PmpTestHelper::Init() {
65 return temp_dir_.CreateUniqueTempDir(); 67 if (!temp_dir_.CreateUniqueTempDir() || !temp_dir_.IsValid())
68 return false;
69
70 base::FilePath indicator_path = temp_dir_.path().Append(
71 base::FilePath::FromUTF8Unsafe(table_name_ + "_0"));
72
73 return file_util::WriteFile(indicator_path, NULL, 0) == 0;
66 } 74 }
67 75
68 base::FilePath PmpTestHelper::GetTempDirPath() { 76 base::FilePath PmpTestHelper::GetTempDirPath() {
69 DCHECK(temp_dir_.IsValid()); 77 DCHECK(temp_dir_.IsValid());
70 return temp_dir_.path(); 78 return temp_dir_.path();
71 } 79 }
72 80
73 template<class T> 81 template<class T>
74 bool PmpTestHelper::WriteColumnFileFromVector( 82 bool PmpTestHelper::WriteColumnFileFromVector(
75 const std::string& table_name, const std::string& column_name, 83 const std::string& column_name, const PmpFieldType field_type,
76 const PmpFieldType field_type, const std::vector<T>& elements_vector) { 84 const std::vector<T>& elements_vector) {
77 DCHECK(temp_dir_.IsValid()); 85 DCHECK(temp_dir_.IsValid());
78 86
79 std::string file_name = table_name + "_" + column_name + "." + kPmpExtension; 87 std::string file_name = table_name_ + "_" + column_name + "." + kPmpExtension;
80 88
81 base::FilePath path = temp_dir_.path().Append( 89 base::FilePath path = temp_dir_.path().Append(
82 base::FilePath::FromUTF8Unsafe(file_name)); 90 base::FilePath::FromUTF8Unsafe(file_name));
83 91
84 std::vector<uint8> data = PmpTestHelper::MakeHeaderAndBody( 92 std::vector<uint8> data = PmpTestHelper::MakeHeaderAndBody(
85 field_type, elements_vector.size(), elements_vector); 93 field_type, elements_vector.size(), elements_vector);
86 94
87 return WriteToFile(path, data); 95 return WriteToFile(path, data);
88 } 96 }
89 97
90 // Explicit Instantiation for all the valid types. 98 // Explicit Instantiation for all the valid types.
91 template bool PmpTestHelper::WriteColumnFileFromVector<std::string>( 99 template bool PmpTestHelper::WriteColumnFileFromVector<std::string>(
92 const std::string&, const std::string&, const PmpFieldType, 100 const std::string&, const PmpFieldType, const std::vector<std::string>&);
93 const std::vector<std::string>&);
94 template bool PmpTestHelper::WriteColumnFileFromVector<uint32>( 101 template bool PmpTestHelper::WriteColumnFileFromVector<uint32>(
95 const std::string&, const std::string&, const PmpFieldType, 102 const std::string&, const PmpFieldType, const std::vector<uint32>&);
96 const std::vector<uint32>&);
97 template bool PmpTestHelper::WriteColumnFileFromVector<double>( 103 template bool PmpTestHelper::WriteColumnFileFromVector<double>(
98 const std::string&, const std::string&, const PmpFieldType, 104 const std::string&, const PmpFieldType, const std::vector<double>&);
99 const std::vector<double>&);
100 template bool PmpTestHelper::WriteColumnFileFromVector<uint8>( 105 template bool PmpTestHelper::WriteColumnFileFromVector<uint8>(
101 const std::string&, const std::string&, const PmpFieldType, 106 const std::string&, const PmpFieldType, const std::vector<uint8>&);
102 const std::vector<uint8>&);
103 template bool PmpTestHelper::WriteColumnFileFromVector<uint64>( 107 template bool PmpTestHelper::WriteColumnFileFromVector<uint64>(
104 const std::string&, const std::string&, const PmpFieldType, 108 const std::string&, const PmpFieldType, const std::vector<uint64>&);
105 const std::vector<uint64>&);
106 109
107 bool PmpTestHelper::InitColumnReaderFromBytes( 110 bool PmpTestHelper::InitColumnReaderFromBytes(PmpColumnReader* const reader,
108 PmpColumnReader* const reader, const std::vector<uint8>& data, 111 const std::vector<uint8>& data,
109 uint32* rows_read) { 112 const PmpFieldType expected_type,
113 uint32* rows_read) {
110 DCHECK(temp_dir_.IsValid()); 114 DCHECK(temp_dir_.IsValid());
111 115
112 base::FilePath temp_path; 116 base::FilePath temp_path;
113 117
114 if (!file_util::CreateTemporaryFileInDir(temp_dir_.path(), &temp_path) || 118 if (!file_util::CreateTemporaryFileInDir(temp_dir_.path(), &temp_path)
115 !WriteToFile(temp_path, data)) { 119 || !WriteToFile(temp_path, data)) {
116 return false; 120 return false;
117 } 121 }
118 122
119 bool success = reader->Init(temp_path, rows_read); 123 bool success = reader->Init(temp_path, expected_type, rows_read);
120 124
121 file_util::Delete(temp_path, true); 125 file_util::Delete(temp_path, true);
122 126
123 return success; 127 return success;
124 128
125 } 129 }
126 130
127 // Return a vector so we don't have to worry about memory management. 131 // Return a vector so we don't have to worry about memory management.
128 std::vector<uint8> PmpTestHelper::MakeHeader(const PmpFieldType field_type, 132 std::vector<uint8> PmpTestHelper::MakeHeader(const PmpFieldType field_type,
129 const uint32 row_count) { 133 const uint32 row_count) {
130 std::vector<uint8> header(picasaimport::kPmpHeaderSize); 134 std::vector<uint8> header(picasaimport::kPmpHeaderSize);
131 135
132 // Copy in magic bytes. 136 // Copy in magic bytes.
133 memcpy(&header[picasaimport::kPmpMagic1Offset], &picasaimport::kPmpMagic1, 137 memcpy(&header[picasaimport::kPmpMagic1Offset], &picasaimport::kPmpMagic1,
134 sizeof(picasaimport::kPmpMagic1)); 138 sizeof(picasaimport::kPmpMagic1));
135 memcpy(&header[picasaimport::kPmpMagic2Offset], &picasaimport::kPmpMagic2, 139 memcpy(&header[picasaimport::kPmpMagic2Offset], &picasaimport::kPmpMagic2,
136 sizeof(picasaimport::kPmpMagic2)); 140 sizeof(picasaimport::kPmpMagic2));
137 memcpy(&header[picasaimport::kPmpMagic3Offset], &picasaimport::kPmpMagic3, 141 memcpy(&header[picasaimport::kPmpMagic3Offset], &picasaimport::kPmpMagic3,
138 sizeof(picasaimport::kPmpMagic3)); 142 sizeof(picasaimport::kPmpMagic3));
139 memcpy(&header[picasaimport::kPmpMagic4Offset], &picasaimport::kPmpMagic4, 143 memcpy(&header[picasaimport::kPmpMagic4Offset], &picasaimport::kPmpMagic4,
140 sizeof(picasaimport::kPmpMagic4)); 144 sizeof(picasaimport::kPmpMagic4));
141 145
142 // Copy in field type. 146 // Copy in field type.
143 memcpy(&header[picasaimport::kPmpFieldType1Offset], &field_type, 2); 147 uint16 field_type_short = static_cast<uint16>(field_type);
144 memcpy(&header[picasaimport::kPmpFieldType2Offset], &field_type, 2); 148 memcpy(&header[picasaimport::kPmpFieldType1Offset], &field_type_short,
149 sizeof(uint16));
150 memcpy(&header[picasaimport::kPmpFieldType2Offset], &field_type_short,
151 sizeof(uint16));
145 152
146 // Copy in row count. 153 // Copy in row count.
147 memcpy(&header[picasaimport::kPmpRowCountOffset], &row_count, 4); 154 memcpy(&header[picasaimport::kPmpRowCountOffset], &row_count, sizeof(uint32));
148 155
149 return header; 156 return header;
150 } 157 }
151 158
152 template<class T> 159 template<class T>
153 std::vector<uint8> PmpTestHelper::MakeHeaderAndBody( 160 std::vector<uint8> PmpTestHelper::MakeHeaderAndBody(
154 const PmpFieldType field_type, const uint32 row_count, 161 const PmpFieldType field_type, const uint32 row_count,
155 const std::vector<T>& elems) { 162 const std::vector<T>& elems) {
156 return CombinedVectors(PmpTestHelper::MakeHeader(field_type, row_count), 163 return CombinedVectors(PmpTestHelper::MakeHeader(field_type, row_count),
157 Flatten(elems)); 164 Flatten(elems));
158 } 165 }
159 166
160 // Explicit Instantiation for all the valid types. 167 // Explicit Instantiation for all the valid types.
161 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<std::string>( 168 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<std::string>(
162 const PmpFieldType, const uint32, const std::vector<std::string>&); 169 const PmpFieldType, const uint32, const std::vector<std::string>&);
163 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<uint32>( 170 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<uint32>(
164 const PmpFieldType, const uint32, const std::vector<uint32>&); 171 const PmpFieldType, const uint32, const std::vector<uint32>&);
165 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<double>( 172 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<double>(
166 const PmpFieldType, const uint32, const std::vector<double>&); 173 const PmpFieldType, const uint32, const std::vector<double>&);
167 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<uint8>( 174 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<uint8>(
168 const PmpFieldType, const uint32, const std::vector<uint8>&); 175 const PmpFieldType, const uint32, const std::vector<uint8>&);
169 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<uint64>( 176 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<uint64>(
170 const PmpFieldType, const uint32, const std::vector<uint64>&); 177 const PmpFieldType, const uint32, const std::vector<uint64>&);
171 178
172 } // namespace picasaimport 179 } // namespace picasaimport
OLDNEW
« no previous file with comments | « chrome/browser/media_galleries/fileapi/picasa/pmp_test_helper.h ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698