Add file_util.
Move file utility classes and functions from testing/test_util to common/file_util, and make them part of libwebm. Change-Id: If5b25a63b20efacc16b0fecaa8876ade4ecc4b26
This commit is contained in:
parent
87f9beae01
commit
e6a0033a8c
@ -106,6 +106,8 @@ endif ()
|
||||
|
||||
# Libwebm section.
|
||||
add_library(webm STATIC
|
||||
"${LIBWEBM_SRC_DIR}/common/file_util.cc"
|
||||
"${LIBWEBM_SRC_DIR}/common/file_util.h"
|
||||
"${LIBWEBM_SRC_DIR}/common/hdr_util.cc"
|
||||
"${LIBWEBM_SRC_DIR}/common/hdr_util.h"
|
||||
"${LIBWEBM_SRC_DIR}/mkvmuxer.cpp"
|
||||
|
@ -3,7 +3,7 @@ CXXFLAGS := -W -Wall -g -MMD -MP
|
||||
LIBWEBMA := libwebm.a
|
||||
LIBWEBMSO := libwebm.so
|
||||
WEBMOBJS := mkvparser.o mkvreader.o mkvmuxer.o mkvmuxerutil.o mkvwriter.o
|
||||
WEBMOBJS += common/hdr_util.o
|
||||
WEBMOBJS += common/file_util.o common/hdr_util.o
|
||||
OBJSA := $(WEBMOBJS:.o=_a.o)
|
||||
OBJSSO := $(WEBMOBJS:.o=_so.o)
|
||||
OBJECTS1 := sample.o
|
||||
|
64
common/file_util.cc
Normal file
64
common/file_util.cc
Normal file
@ -0,0 +1,64 @@
|
||||
// Copyright (c) 2016 The WebM project authors. All Rights Reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style license
|
||||
// that can be found in the LICENSE file in the root of the source
|
||||
// tree. An additional intellectual property rights grant can be found
|
||||
// in the file PATENTS. All contributing project authors may
|
||||
// be found in the AUTHORS file in the root of the source tree.
|
||||
#include "common/file_util.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
#ifndef _MSC_VER
|
||||
#include <unistd.h> // close()
|
||||
#endif
|
||||
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <ios>
|
||||
#include <memory>
|
||||
|
||||
namespace libwebm {
|
||||
|
||||
std::string GetTempFileName() {
|
||||
#ifndef _MSC_VER
|
||||
char temp_file_name_template[] = "libwebm_temp.XXXXXX";
|
||||
int fd = mkstemp(temp_file_name_template);
|
||||
if (fd != -1) {
|
||||
close(fd);
|
||||
return std::string(temp_file_name_template);
|
||||
}
|
||||
return std::string();
|
||||
#else
|
||||
// TODO(tomfinegan): Add the MSVC version of mkstemp() to quiet the MSVC
|
||||
// version of the security warning.
|
||||
return std::tmpnam(nullptr);
|
||||
#endif
|
||||
}
|
||||
|
||||
uint64_t GetFileSize(const std::string& file_name) {
|
||||
uint64_t file_size = 0;
|
||||
#ifndef _MSC_VER
|
||||
struct stat st;
|
||||
st.st_size = 0;
|
||||
if (stat(file_name.c_str(), &st) == 0) {
|
||||
#else
|
||||
struct _stat st;
|
||||
st.st_size = 0;
|
||||
if (_stat(file_name.c_str(), &st) == 0) {
|
||||
#endif
|
||||
file_size = st.st_size;
|
||||
}
|
||||
return file_size;
|
||||
}
|
||||
|
||||
TempFileDeleter::TempFileDeleter() { file_name_ = GetTempFileName(); }
|
||||
|
||||
TempFileDeleter::~TempFileDeleter() {
|
||||
std::ifstream file(file_name_.c_str());
|
||||
if (file.good()) {
|
||||
file.close();
|
||||
std::remove(file_name_.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace libwebm
|
41
common/file_util.h
Normal file
41
common/file_util.h
Normal file
@ -0,0 +1,41 @@
|
||||
// Copyright (c) 2016 The WebM project authors. All Rights Reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style license
|
||||
// that can be found in the LICENSE file in the root of the source
|
||||
// tree. An additional intellectual property rights grant can be found
|
||||
// in the file PATENTS. All contributing project authors may
|
||||
// be found in the AUTHORS file in the root of the source tree.
|
||||
#ifndef LIBWEBM_COMMON_FILE_UTIL_H_
|
||||
#define LIBWEBM_COMMON_FILE_UTIL_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "../mkvmuxertypes.hpp" // LIBWEBM_DISALLOW_COPY_AND_ASSIGN()
|
||||
|
||||
namespace libwebm {
|
||||
|
||||
// Returns a temporary file name.
|
||||
std::string GetTempFileName();
|
||||
|
||||
// Returns size of file specified by |file_name|, or 0 upon failure.
|
||||
uint64_t GetFileSize(const std::string& file_name);
|
||||
|
||||
// Manages life of temporary file specified at time of construction. Deletes
|
||||
// file upon destruction.
|
||||
class TempFileDeleter {
|
||||
public:
|
||||
TempFileDeleter();
|
||||
explicit TempFileDeleter(std::string file_name) : file_name_(file_name) {}
|
||||
~TempFileDeleter();
|
||||
const std::string& name() const { return file_name_; }
|
||||
|
||||
private:
|
||||
std::string file_name_;
|
||||
LIBWEBM_DISALLOW_COPY_AND_ASSIGN(TempFileDeleter);
|
||||
};
|
||||
|
||||
} // namespace libwebm
|
||||
|
||||
#endif // LIBWEBM_COMMON_FILE_UTIL_H_
|
@ -16,6 +16,8 @@
|
||||
namespace libwebm {
|
||||
|
||||
// fclose functor for wrapping FILE in std::unique_ptr.
|
||||
// TODO(tomfinegan): Move this to file_util once c++11 restrictions are
|
||||
// relaxed.
|
||||
struct FILEDeleter {
|
||||
int operator()(std::FILE* f) {
|
||||
if (f != nullptr)
|
||||
|
@ -17,6 +17,7 @@
|
||||
#include "mkvreader.hpp"
|
||||
#include "mkvwriter.hpp"
|
||||
|
||||
#include "common/file_util.h"
|
||||
#include "common/libwebm_utils.h"
|
||||
#include "testing/test_util.h"
|
||||
|
||||
|
@ -21,6 +21,7 @@
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "common/file_util.h"
|
||||
#include "common/libwebm_utils.h"
|
||||
|
||||
namespace libwebm {
|
||||
@ -60,49 +61,7 @@ bool CompareFiles(const std::string& file1, const std::string& file2) {
|
||||
return std::feof(f1.get()) && std::feof(f2.get());
|
||||
}
|
||||
|
||||
std::string GetTempFileName() {
|
||||
#ifndef _MSC_VER
|
||||
char temp_file_name_template[] = "libwebm_temp.XXXXXX";
|
||||
int fd = mkstemp(temp_file_name_template);
|
||||
if (fd != -1) {
|
||||
close(fd);
|
||||
return std::string(temp_file_name_template);
|
||||
}
|
||||
return std::string();
|
||||
#else
|
||||
// TODO(tomfinegan): Add the MSVC version of mkstemp() to quiet the MSVC
|
||||
// version of the security warning.
|
||||
return std::tmpnam(nullptr);
|
||||
#endif
|
||||
}
|
||||
|
||||
std::uint64_t GetFileSize(const std::string& file_name) {
|
||||
std::uint64_t file_size = 0;
|
||||
#ifndef _MSC_VER
|
||||
struct stat st;
|
||||
st.st_size = 0;
|
||||
if (stat(file_name.c_str(), &st) == 0) {
|
||||
#else
|
||||
struct _stat st;
|
||||
st.st_size = 0;
|
||||
if (_stat(file_name.c_str(), &st) == 0) {
|
||||
#endif
|
||||
file_size = st.st_size;
|
||||
}
|
||||
return file_size;
|
||||
}
|
||||
|
||||
TempFileDeleter::TempFileDeleter() {
|
||||
file_name_ = GetTempFileName();
|
||||
}
|
||||
|
||||
TempFileDeleter::~TempFileDeleter() {
|
||||
std::ifstream file(file_name_);
|
||||
if (file.good()) {
|
||||
file.close();
|
||||
std::remove(file_name_.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace test
|
||||
} // namespace libwebm
|
||||
|
@ -51,28 +51,7 @@ std::string GetTestFilePath(const std::string& name);
|
||||
// files match exactly, false otherwise.
|
||||
bool CompareFiles(const std::string& file1, const std::string& file2);
|
||||
|
||||
// Returns a temporary file name.
|
||||
std::string GetTempFileName();
|
||||
|
||||
// Returns size of file specified by |file_name|, or 0 upon failure.
|
||||
std::uint64_t GetFileSize(const std::string& file_name);
|
||||
|
||||
// Manages life of temporary file specified at time of construction. Deletes
|
||||
// file upon destruction.
|
||||
class TempFileDeleter {
|
||||
public:
|
||||
TempFileDeleter();
|
||||
explicit TempFileDeleter(std::string file_name) : file_name_(file_name) {}
|
||||
~TempFileDeleter();
|
||||
TempFileDeleter(const TempFileDeleter&) = delete;
|
||||
TempFileDeleter(TempFileDeleter&&) = delete;
|
||||
const std::string& name() const { return file_name_; }
|
||||
|
||||
private:
|
||||
std::string file_name_;
|
||||
};
|
||||
|
||||
} // namespace test
|
||||
} // namespace libwebm
|
||||
|
||||
#endif // LIBWEBM_TESTING_TEST_UTIL_H_
|
||||
#endif // LIBWEBM_TESTING_TEST_UTIL_H_
|
@ -14,6 +14,7 @@
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "common/file_util.h"
|
||||
#include "common/libwebm_utils.h"
|
||||
#include "testing/test_util.h"
|
||||
|
||||
@ -96,7 +97,7 @@ class Webm2PesTests : public ::testing::Test {
|
||||
void CreateAndLoadTestInput() {
|
||||
libwebm::Webm2Pes converter(input_file_name_, temp_file_name_.name());
|
||||
ASSERT_TRUE(converter.ConvertToFile());
|
||||
pes_file_size_ = libwebm::test::GetFileSize(pes_file_name());
|
||||
pes_file_size_ = libwebm::GetFileSize(pes_file_name());
|
||||
ASSERT_GT(pes_file_size_, 0);
|
||||
pes_file_data_.reserve(pes_file_size_);
|
||||
libwebm::FilePtr file = libwebm::FilePtr(
|
||||
@ -245,7 +246,7 @@ class Webm2PesTests : public ::testing::Test {
|
||||
const PesFileData& pes_file_data() const { return pes_file_data_; }
|
||||
|
||||
private:
|
||||
const libwebm::test::TempFileDeleter temp_file_name_;
|
||||
const libwebm::TempFileDeleter temp_file_name_;
|
||||
const std::string input_file_name_ =
|
||||
libwebm::test::GetTestFilePath("bbb_480p_vp9_opus_1second.webm");
|
||||
std::int64_t pes_file_size_ = 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user