Chromium Code Reviews| 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 <iterator> | |
| 8 | |
| 9 #include "base/file_util.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/utf_string_conversions.h" | |
| 12 #include "webkit/fileapi/media/picasa/pmp_column_reader.h" | |
| 13 #include "webkit/fileapi/media/picasa/pmp_constants.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 std::string table_name, std::string column_name, const uint16 field_type, | |
| 76 std::vector<T> elements_vector) { | |
| 77 DCHECK(temp_dir_.IsValid()); | |
| 78 | |
| 79 #if defined(OS_WIN) | |
| 80 base::FilePath path = temp_dir_.path().Append(UTF8ToUTF16( | |
| 81 table_name + "_" + column_name + "." + picasaimport::kPmpExtension)); | |
|
vandebo (ex-Chrome)
2013/04/05 18:52:47
You're in the picassaimport namespace here.
tommycli
2013/04/05 20:06:44
Done.
| |
| 82 #else | |
| 83 base::FilePath path = temp_dir_.path().Append( | |
|
vandebo (ex-Chrome)
2013/04/05 18:52:47
You may want to reorganize this a bit to reduce th
tommycli
2013/04/05 20:06:44
Done.
| |
| 84 table_name + "_" + column_name + "." + picasaimport::kPmpExtension); | |
| 85 #endif | |
| 86 | |
| 87 std::vector<uint8> data = PmpTestHelper::MakeHeaderAndBody( | |
| 88 field_type, elements_vector.size(), elements_vector); | |
| 89 | |
| 90 return WriteToFile(path, data); | |
| 91 } | |
| 92 | |
| 93 // Explicit Instantiation for all the valid types. | |
| 94 template bool PmpTestHelper::WriteColumnFileFromVector<std::string>( | |
| 95 std::string, std::string, const uint16, std::vector<std::string>); | |
| 96 template bool PmpTestHelper::WriteColumnFileFromVector<uint32>( | |
| 97 std::string, std::string, const uint16, std::vector<uint32>); | |
| 98 template bool PmpTestHelper::WriteColumnFileFromVector<double>( | |
| 99 std::string, std::string, const uint16, std::vector<double>); | |
| 100 template bool PmpTestHelper::WriteColumnFileFromVector<uint8>( | |
| 101 std::string, std::string, const uint16, std::vector<uint8>); | |
| 102 template bool PmpTestHelper::WriteColumnFileFromVector<uint64>( | |
| 103 std::string, std::string, const uint16, std::vector<uint64>); | |
| 104 | |
| 105 bool PmpTestHelper::InitColumnReaderFromBytes( | |
| 106 PmpColumnReader* const reader, std::vector<uint8> data, uint32* rows_read) { | |
| 107 DCHECK(temp_dir_.IsValid()); | |
| 108 | |
| 109 base::FilePath temp_path; | |
| 110 | |
| 111 if (!file_util::CreateTemporaryFileInDir(temp_dir_.path(), &temp_path) || | |
| 112 !WriteToFile(temp_path, data)) { | |
| 113 return false; | |
| 114 } | |
| 115 | |
| 116 bool success = reader->Init(temp_path, rows_read); | |
| 117 | |
| 118 file_util::Delete(temp_path, true); | |
| 119 | |
| 120 return success; | |
| 121 | |
| 122 } | |
| 123 | |
| 124 // Return a vector so we don't have to worry about memory management. | |
| 125 std::vector<uint8> PmpTestHelper::MakeHeader(const uint16 field_type, | |
| 126 const uint32 row_count) { | |
| 127 std::vector<uint8> header(picasaimport::kPmpHeaderSize); | |
| 128 | |
| 129 // Copy in magic bytes. | |
| 130 memcpy(&header[picasaimport::kPmpMagic1Offset], &picasaimport::kPmpMagic1, | |
| 131 sizeof(picasaimport::kPmpMagic1)); | |
| 132 memcpy(&header[picasaimport::kPmpMagic2Offset], &picasaimport::kPmpMagic2, | |
| 133 sizeof(picasaimport::kPmpMagic2)); | |
| 134 memcpy(&header[picasaimport::kPmpMagic3Offset], &picasaimport::kPmpMagic3, | |
| 135 sizeof(picasaimport::kPmpMagic3)); | |
| 136 memcpy(&header[picasaimport::kPmpMagic4Offset], &picasaimport::kPmpMagic4, | |
| 137 sizeof(picasaimport::kPmpMagic4)); | |
| 138 | |
| 139 // Copy in field type. | |
| 140 memcpy(&header[picasaimport::kPmpFieldType1Offset], &field_type, 2); | |
| 141 memcpy(&header[picasaimport::kPmpFieldType2Offset], &field_type, 2); | |
| 142 | |
| 143 // Copy in row count. | |
| 144 memcpy(&header[picasaimport::kPmpRowCountOffset], &row_count, 4); | |
| 145 | |
| 146 return header; | |
| 147 } | |
| 148 | |
| 149 template<class T> | |
| 150 std::vector<uint8> PmpTestHelper::MakeHeaderAndBody( | |
| 151 const uint16 field_type, const uint32 row_count, | |
| 152 const std::vector<T>& elems) { | |
| 153 return CombinedVectors(PmpTestHelper::MakeHeader(field_type, row_count), | |
| 154 Flatten(elems)); | |
| 155 } | |
| 156 | |
| 157 // Explicit Instantiation for all the valid types. | |
| 158 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<std::string>( | |
| 159 const uint16, const uint32, const std::vector<std::string>&); | |
| 160 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<uint32>( | |
| 161 const uint16, const uint32, const std::vector<uint32>&); | |
| 162 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<double>( | |
| 163 const uint16, const uint32, const std::vector<double>&); | |
| 164 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<uint8>( | |
| 165 const uint16, const uint32, const std::vector<uint8>&); | |
| 166 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<uint64>( | |
| 167 const uint16, const uint32, const std::vector<uint64>&); | |
| 168 | |
| 169 } // namespace picasaimport | |
| OLD | NEW |