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..8d64078373308854fb2c5ad764ac51b85b958aef |
| --- /dev/null |
| +++ b/webkit/fileapi/media/picasa/pmp_test_helper.cc |
| @@ -0,0 +1,169 @@ |
| +// 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 <iterator> |
| + |
| +#include "base/file_util.h" |
| +#include "base/logging.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "webkit/fileapi/media/picasa/pmp_column_reader.h" |
| +#include "webkit/fileapi/media/picasa/pmp_constants.h" |
| + |
| +namespace picasaimport { |
| + |
| +namespace { |
| + |
| +bool 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()); |
| +} |
| + |
| +// Flatten a vector of elements into an array of bytes. |
| +template<class T> |
| +std::vector<uint8> 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> 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; |
| +} |
| + |
| +// Returns a new vector with the concatenated contents of |a| and |b|. |
| +std::vector<uint8> 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 |
| + |
| +PmpTestHelper::PmpTestHelper() { } |
| + |
| +bool PmpTestHelper::Init() { |
| + return temp_dir_.CreateUniqueTempDir(); |
| +} |
| + |
| +base::FilePath PmpTestHelper::GetTempDirPath() { |
| + DCHECK(temp_dir_.IsValid()); |
| + return temp_dir_.path(); |
| +} |
| + |
| +template<class T> |
| +bool PmpTestHelper::WriteColumnFileFromVector( |
| + std::string table_name, std::string column_name, const uint16 field_type, |
| + std::vector<T> elements_vector) { |
| + DCHECK(temp_dir_.IsValid()); |
| + |
| +#if defined(OS_WIN) |
| + base::FilePath path = temp_dir_.path().Append(UTF8ToUTF16( |
| + 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.
|
| +#else |
| + 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.
|
| + table_name + "_" + column_name + "." + picasaimport::kPmpExtension); |
| +#endif |
| + |
| + std::vector<uint8> data = PmpTestHelper::MakeHeaderAndBody( |
| + field_type, elements_vector.size(), elements_vector); |
| + |
| + return WriteToFile(path, data); |
| +} |
| + |
| +// Explicit Instantiation for all the valid types. |
| +template bool PmpTestHelper::WriteColumnFileFromVector<std::string>( |
| + std::string, std::string, const uint16, std::vector<std::string>); |
| +template bool PmpTestHelper::WriteColumnFileFromVector<uint32>( |
| + std::string, std::string, const uint16, std::vector<uint32>); |
| +template bool PmpTestHelper::WriteColumnFileFromVector<double>( |
| + std::string, std::string, const uint16, std::vector<double>); |
| +template bool PmpTestHelper::WriteColumnFileFromVector<uint8>( |
| + std::string, std::string, const uint16, std::vector<uint8>); |
| +template bool PmpTestHelper::WriteColumnFileFromVector<uint64>( |
| + std::string, std::string, const uint16, std::vector<uint64>); |
| + |
| +bool PmpTestHelper::InitColumnReaderFromBytes( |
| + PmpColumnReader* const reader, std::vector<uint8> data, uint32* rows_read) { |
| + DCHECK(temp_dir_.IsValid()); |
| + |
| + base::FilePath temp_path; |
| + |
| + if (!file_util::CreateTemporaryFileInDir(temp_dir_.path(), &temp_path) || |
| + !WriteToFile(temp_path, data)) { |
| + return false; |
| + } |
| + |
| + bool success = reader->Init(temp_path, rows_read); |
| + |
| + file_util::Delete(temp_path, true); |
| + |
| + return success; |
| + |
| +} |
| + |
| +// 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(picasaimport::kPmpHeaderSize); |
| + |
| + // Copy in magic bytes. |
| + memcpy(&header[picasaimport::kPmpMagic1Offset], &picasaimport::kPmpMagic1, |
| + sizeof(picasaimport::kPmpMagic1)); |
| + memcpy(&header[picasaimport::kPmpMagic2Offset], &picasaimport::kPmpMagic2, |
| + sizeof(picasaimport::kPmpMagic2)); |
| + memcpy(&header[picasaimport::kPmpMagic3Offset], &picasaimport::kPmpMagic3, |
| + sizeof(picasaimport::kPmpMagic3)); |
| + memcpy(&header[picasaimport::kPmpMagic4Offset], &picasaimport::kPmpMagic4, |
| + sizeof(picasaimport::kPmpMagic4)); |
| + |
| + // Copy in field type. |
| + memcpy(&header[picasaimport::kPmpFieldType1Offset], &field_type, 2); |
| + memcpy(&header[picasaimport::kPmpFieldType2Offset], &field_type, 2); |
| + |
| + // Copy in row count. |
| + memcpy(&header[picasaimport::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 CombinedVectors(PmpTestHelper::MakeHeader(field_type, row_count), |
| + 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>&); |
| + |
| +} // namespace picasaimport |