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 "base/file_util.h" | |
| 8 #include "webkit/fileapi/media/picasa/pmp_constants.h" | |
| 9 | |
| 10 namespace fileapi { | |
| 11 | |
| 12 // Return a vector so we don't have to worry about memory management. | |
| 13 std::vector<uint8> PmpTestHelper::MakeHeader(const uint16 field_type, | |
| 14 const uint32 row_count) { | |
| 15 std::vector<uint8> header(fileapi::kPmpHeaderSize); | |
| 16 | |
| 17 // Copy in magic bytes. | |
| 18 memcpy(&header[fileapi::kPmpMagic1Offset], &fileapi::kPmpMagic1, | |
| 19 sizeof(fileapi::kPmpMagic1)); | |
| 20 memcpy(&header[fileapi::kPmpMagic2Offset], &fileapi::kPmpMagic2, | |
| 21 sizeof(fileapi::kPmpMagic2)); | |
| 22 memcpy(&header[fileapi::kPmpMagic3Offset], &fileapi::kPmpMagic3, | |
| 23 sizeof(fileapi::kPmpMagic3)); | |
| 24 memcpy(&header[fileapi::kPmpMagic4Offset], &fileapi::kPmpMagic4, | |
| 25 sizeof(fileapi::kPmpMagic4)); | |
| 26 | |
| 27 // Copy in field type. | |
| 28 memcpy(&header[fileapi::kPmpFieldType1Offset], &field_type, 2); | |
| 29 memcpy(&header[fileapi::kPmpFieldType2Offset], &field_type, 2); | |
| 30 | |
| 31 // Copy in row count. | |
| 32 memcpy(&header[fileapi::kPmpRowCountOffset], &row_count, 4); | |
| 33 | |
| 34 return header; | |
| 35 } | |
| 36 | |
| 37 template<class T> | |
| 38 std::vector<uint8> PmpTestHelper::MakeHeaderAndBody( | |
| 39 const uint16 field_type, const uint32 row_count, | |
| 40 const std::vector<T>& elems) { | |
| 41 return PmpTestHelper::CombinedVectors( | |
| 42 PmpTestHelper::MakeHeader(field_type, row_count), | |
| 43 PmpTestHelper::Flatten(elems)); | |
| 44 } | |
| 45 | |
| 46 // Explicit Instantiation for all the valid types. | |
| 47 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<std::string>( | |
| 48 const uint16, const uint32, const std::vector<std::string>&); | |
| 49 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<uint32>( | |
| 50 const uint16, const uint32, const std::vector<uint32>&); | |
| 51 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<double>( | |
| 52 const uint16, const uint32, const std::vector<double>&); | |
| 53 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<uint8>( | |
| 54 const uint16, const uint32, const std::vector<uint8>&); | |
| 55 template std::vector<uint8> PmpTestHelper::MakeHeaderAndBody<uint64>( | |
| 56 const uint16, const uint32, const std::vector<uint64>&); | |
| 57 | |
| 58 bool PmpTestHelper::WriteToFile(const base::FilePath& path, | |
| 59 std::vector<uint8> data) { | |
| 60 // Cast for usage in WriteFile function | |
| 61 const char* data_char = reinterpret_cast<const char*>(&data[0]); | |
| 62 size_t bytes_written = file_util::WriteFile(path, data_char, data.size()); | |
| 63 return (bytes_written == data.size()); | |
| 64 | |
| 65 } | |
| 66 | |
| 67 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
| |
| 68 std::vector<uint8> PmpTestHelper::Flatten(const std::vector<T>& elems) { | |
| 69 const uint8* elems0 = reinterpret_cast<const uint8*>(&elems[0]); | |
| 70 std::vector<uint8> data_body(elems0, elems0 + sizeof(T)*elems.size()); | |
| 71 | |
| 72 return data_body; | |
| 73 } | |
| 74 | |
| 75 // Custom specialization for std::string. | |
| 76 template<> | |
| 77 std::vector<uint8> PmpTestHelper::Flatten( | |
| 78 const std::vector<std::string>& strings) { | |
| 79 std::vector<uint8> totalchars; | |
| 80 | |
| 81 for(std::vector<std::string>::const_iterator it = strings.begin(); | |
| 82 it != strings.end(); it++) { | |
| 83 std::copy(it->begin(), it->end(), std::back_inserter(totalchars)); | |
| 84 totalchars.push_back('\0'); // Add the null termination too. | |
| 85 } | |
| 86 | |
| 87 return totalchars; | |
| 88 } | |
| 89 | |
| 90 std::vector<uint8> PmpTestHelper::CombinedVectors(const std::vector<uint8>& a, | |
| 91 const std::vector<uint8>& b) { | |
| 92 std::vector<uint8> total; | |
| 93 | |
| 94 std::copy(a.begin(), a.end(), std::back_inserter(total)); | |
| 95 std::copy(b.begin(), b.end(), std::back_inserter(total)); | |
| 96 | |
| 97 return total; | |
| 98 } | |
| 99 | |
| 100 } // namespace fileapi | |
| OLD | NEW |