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