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

Side by Side Diff: webkit/fileapi/media/picasa/pmp_test_helper.cc

Issue 12704024: Simple PMP reader to parse Picasa's metadata (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make PmpTestHelper stateful and possess the temporary directory. 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 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_test_helper.h"
6
7 #include "base/file_util.h"
8 #include "webkit/fileapi/media/picasa/pmp_column_reader.h"
9 #include "webkit/fileapi/media/picasa/pmp_constants.h"
10
11 namespace picasaimport {
12
13 PmpTestHelper::PmpTestHelper() : temp_dir_() { }
vandebo (ex-Chrome) 2013/04/02 22:17:58 You don't need temp_dir_()
tommycli 2013/04/05 16:49:24 Done.
14
15 bool PmpTestHelper::Init() {
16 return temp_dir_.CreateUniqueTempDir();
17 }
18
19 base::FilePath PmpTestHelper::GetTempDirPath() {
20 DCHECK(temp_dir_.IsValid());
21 return temp_dir_.path();
22 }
23
24 template<class T>
25 bool PmpTestHelper::WriteColumnFileFromVector(
26 std::string table_name, std::string column_name, const uint16 field_type,
27 std::vector<T> elements_vector) {
28 DCHECK(temp_dir_.IsValid());
29
30 base::FilePath path = temp_dir_.path().Append(
31 table_name + "_" + column_name + "." + picasaimport::kPmpExtension);
32
33 std::vector<uint8> data = PmpTestHelper::MakeHeaderAndBody(
34 field_type, elements_vector.size(), elements_vector);
35
36 return PmpTestHelper::WriteToFile(path, data);
37 }
38
39 // Explicit Instantiation for all the valid types.
40 template bool PmpTestHelper::WriteColumnFileFromVector<std::string>(
41 std::string, std::string, const uint16, std::vector<std::string>);
42 template bool PmpTestHelper::WriteColumnFileFromVector<uint32>(
43 std::string, std::string, const uint16, std::vector<uint32>);
44 template bool PmpTestHelper::WriteColumnFileFromVector<double>(
45 std::string, std::string, const uint16, std::vector<double>);
46 template bool PmpTestHelper::WriteColumnFileFromVector<uint8>(
47 std::string, std::string, const uint16, std::vector<uint8>);
48 template bool PmpTestHelper::WriteColumnFileFromVector<uint64>(
49 std::string, std::string, const uint16, std::vector<uint64>);
50
51 bool PmpTestHelper::InitColumnReaderFromBytes(
52 PmpColumnReader* const reader, std::vector<uint8> data, uint32* rows_read) {
53 DCHECK(temp_dir_.IsValid());
54
55 base::FilePath temp_path;
56
57 if (!file_util::CreateTemporaryFileInDir(temp_dir_.path(), &temp_path) ||
58 !PmpTestHelper::WriteToFile(temp_path, data)) {
59 return false;
60 }
61
62 bool success = reader->Init(temp_path, rows_read);
63
64 file_util::Delete(temp_path, true);
65
66 return success;
67
68 }
69
70 // Return a vector so we don't have to worry about memory management.
71 std::vector<uint8> PmpTestHelper::MakeHeader(const uint16 field_type,
72 const uint32 row_count) {
73 std::vector<uint8> header(picasaimport::kPmpHeaderSize);
74
75 // Copy in magic bytes.
76 memcpy(&header[picasaimport::kPmpMagic1Offset], &picasaimport::kPmpMagic1,
77 sizeof(picasaimport::kPmpMagic1));
78 memcpy(&header[picasaimport::kPmpMagic2Offset], &picasaimport::kPmpMagic2,
79 sizeof(picasaimport::kPmpMagic2));
80 memcpy(&header[picasaimport::kPmpMagic3Offset], &picasaimport::kPmpMagic3,
81 sizeof(picasaimport::kPmpMagic3));
82 memcpy(&header[picasaimport::kPmpMagic4Offset], &picasaimport::kPmpMagic4,
83 sizeof(picasaimport::kPmpMagic4));
84
85 // Copy in field type.
86 memcpy(&header[picasaimport::kPmpFieldType1Offset], &field_type, 2);
87 memcpy(&header[picasaimport::kPmpFieldType2Offset], &field_type, 2);
88
89 // Copy in row count.
90 memcpy(&header[picasaimport::kPmpRowCountOffset], &row_count, 4);
91
92 return header;
93 }
94
95 template<class T>
96 std::vector<uint8> PmpTestHelper::MakeHeaderAndBody(
97 const uint16 field_type, const uint32 row_count,
98 const std::vector<T>& elems) {
99 return PmpTestHelper::CombinedVectors(
100 PmpTestHelper::MakeHeader(field_type, row_count),
101 PmpTestHelper::Flatten(elems));
102 }
103
104 bool PmpTestHelper::WriteToFile(const base::FilePath& path,
105 std::vector<uint8> data) {
106 // Cast for usage in WriteFile function
107 const char* data_char = reinterpret_cast<const char*>(&data[0]);
108 size_t bytes_written = file_util::WriteFile(path, data_char, data.size());
109 return (bytes_written == data.size());
110 }
111
112 template<class T>
113 std::vector<uint8> PmpTestHelper::Flatten(const std::vector<T>& elems) {
114 const uint8* elems0 = reinterpret_cast<const uint8*>(&elems[0]);
115 std::vector<uint8> data_body(elems0, elems0 + sizeof(T)*elems.size());
116
117 return data_body;
118 }
119
120 // Custom specialization for std::string.
121 template<>
122 std::vector<uint8> PmpTestHelper::Flatten(
123 const std::vector<std::string>& strings) {
124 std::vector<uint8> totalchars;
125
126 for(std::vector<std::string>::const_iterator it = strings.begin();
127 it != strings.end(); ++it) {
128 std::copy(it->begin(), it->end(), std::back_inserter(totalchars));
129 totalchars.push_back('\0'); // Add the null termination too.
130 }
131
132 return totalchars;
133 }
134
135 std::vector<uint8> PmpTestHelper::CombinedVectors(const std::vector<uint8>& a,
136 const std::vector<uint8>& b) {
137 std::vector<uint8> total;
138
139 std::copy(a.begin(), a.end(), std::back_inserter(total));
140 std::copy(b.begin(), b.end(), std::back_inserter(total));
141
142 return total;
143 }
144
145 } // namespace picasaimport
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698