Chromium Code Reviews| Index: webkit/fileapi/media/picasa/pmp_test_helper.cc |
| diff --git a/webkit/fileapi/media/picasa/pmp_test_helper.cc b/webkit/fileapi/media/picasa/pmp_test_helper.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a3a8cb8510ad41e8af8e97334e59be13c09d22fb |
| --- /dev/null |
| +++ b/webkit/fileapi/media/picasa/pmp_test_helper.cc |
| @@ -0,0 +1,100 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "webkit/fileapi/media/picasa/pmp_test_helper.h" |
| + |
| +#include "base/file_util.h" |
| +#include "webkit/fileapi/media/picasa/pmp_constants.h" |
| + |
| +namespace fileapi { |
| + |
| +// Return a vector so we don't have to worry about memory management. |
| +std::vector<uint8> PmpTestHelper::MakeHeader(const uint16 field_type, |
| + const uint32 row_count) { |
| + std::vector<uint8> header(fileapi::kPmpHeaderSize); |
| + |
| + // Copy in magic bytes. |
| + memcpy(&header[fileapi::kPmpMagic1Offset], &fileapi::kPmpMagic1, |
| + sizeof(fileapi::kPmpMagic1)); |
| + memcpy(&header[fileapi::kPmpMagic2Offset], &fileapi::kPmpMagic2, |
| + sizeof(fileapi::kPmpMagic2)); |
| + memcpy(&header[fileapi::kPmpMagic3Offset], &fileapi::kPmpMagic3, |
| + sizeof(fileapi::kPmpMagic3)); |
| + memcpy(&header[fileapi::kPmpMagic4Offset], &fileapi::kPmpMagic4, |
| + sizeof(fileapi::kPmpMagic4)); |
| + |
| + // Copy in field type. |
| + memcpy(&header[fileapi::kPmpFieldType1Offset], &field_type, 2); |
| + memcpy(&header[fileapi::kPmpFieldType2Offset], &field_type, 2); |
| + |
| + // Copy in row count. |
| + memcpy(&header[fileapi::kPmpRowCountOffset], &row_count, 4); |
| + |
| + return header; |
| +} |
| + |
| +template<class T> |
| +std::vector<uint8> PmpTestHelper::MakeHeaderAndBody( |
| + const uint16 field_type, const uint32 row_count, |
| + const std::vector<T>& elems) { |
| + return PmpTestHelper::CombinedVectors( |
| + PmpTestHelper::MakeHeader(field_type, row_count), |
| + PmpTestHelper::Flatten(elems)); |
| +} |
| + |
| +// Explicit Instantiation for all the valid types. |
| +template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<std::string>( |
| + const uint16, const uint32, const std::vector<std::string>&); |
| +template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<uint32>( |
| + const uint16, const uint32, const std::vector<uint32>&); |
| +template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<double>( |
| + const uint16, const uint32, const std::vector<double>&); |
| +template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<uint8>( |
| + const uint16, const uint32, const std::vector<uint8>&); |
| +template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<uint64>( |
| + const uint16, const uint32, const std::vector<uint64>&); |
| + |
| +bool PmpTestHelper::WriteToFile(const base::FilePath& path, |
| + std::vector<uint8> data) { |
| + // Cast for usage in WriteFile function |
| + const char* data_char = reinterpret_cast<const char*>(&data[0]); |
| + size_t bytes_written = file_util::WriteFile(path, data_char, data.size()); |
| + return (bytes_written == data.size()); |
| + |
| +} |
| + |
| +template<class T> |
|
vandebo (ex-Chrome)
2013/03/29 21:35:07
I suspect all the fancy mechanisms below are longe
tommycli
2013/04/01 22:19:18
MakeHeaderAndBody is called by a number of templat
|
| +std::vector<uint8> PmpTestHelper::Flatten(const std::vector<T>& elems) { |
| + const uint8* elems0 = reinterpret_cast<const uint8*>(&elems[0]); |
| + std::vector<uint8> data_body(elems0, elems0 + sizeof(T)*elems.size()); |
| + |
| + return data_body; |
| +} |
| + |
| +// Custom specialization for std::string. |
| +template<> |
| +std::vector<uint8> PmpTestHelper::Flatten( |
| + const std::vector<std::string>& strings) { |
| + std::vector<uint8> totalchars; |
| + |
| + for(std::vector<std::string>::const_iterator it = strings.begin(); |
| + it != strings.end(); it++) { |
| + std::copy(it->begin(), it->end(), std::back_inserter(totalchars)); |
| + totalchars.push_back('\0'); // Add the null termination too. |
| + } |
| + |
| + return totalchars; |
| +} |
| + |
| +std::vector<uint8> PmpTestHelper::CombinedVectors(const std::vector<uint8>& a, |
| + const std::vector<uint8>& b) { |
| + std::vector<uint8> total; |
| + |
| + std::copy(a.begin(), a.end(), std::back_inserter(total)); |
| + std::copy(b.begin(), b.end(), std::back_inserter(total)); |
| + |
| + return total; |
| +} |
| + |
| +} // namespace fileapi |