1/*
2 *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "webrtc/modules/remote_bitrate_estimator/test/bwe_test_fileutils.h"
12
13#ifdef WIN32
14#include <Winsock2.h>
15#else
16#include <arpa/inet.h>
17#endif
18#include <assert.h>
19
20#include "webrtc/modules/remote_bitrate_estimator/test/bwe_test_logging.h"
21#include "webrtc/system_wrappers/interface/scoped_ptr.h"
22#include "webrtc/test/testsupport/fileutils.h"
23
24namespace webrtc {
25namespace testing {
26namespace bwe {
27
28ResourceFileReader::~ResourceFileReader() {
29  if (file_ != NULL) {
30    fclose(file_);
31    file_ = NULL;
32  }
33}
34
35bool ResourceFileReader::IsAtEnd() {
36  int32_t current_pos = ftell(file_);
37  fseek(file_, 0, SEEK_END);
38  int32_t end_pos = ftell(file_);
39  fseek(file_, current_pos, SEEK_SET);
40  return current_pos == end_pos;
41}
42
43bool ResourceFileReader::Read(uint32_t* out) {
44  assert(out);
45  uint32_t tmp = 0;
46  if (fread(&tmp, 1, sizeof(uint32_t), file_) != sizeof(uint32_t)) {
47    printf("Error reading!\n");
48    return false;
49  }
50  *out = ntohl(tmp);
51  return true;
52}
53
54ResourceFileReader* ResourceFileReader::Create(const std::string& filename,
55                                               const std::string& extension) {
56  std::string filepath = webrtc::test::ResourcePath(filename, extension);
57  FILE* file = fopen(filepath.c_str(), "rb");
58  if (file == NULL) {
59    BWE_TEST_LOGGING_CONTEXT("ResourceFileReader");
60    BWE_TEST_LOGGING_LOG1("Create", "Can't read file: %s", filepath.c_str());
61    return 0;
62  } else {
63    return new ResourceFileReader(file);
64  }
65}
66
67OutputFileWriter::~OutputFileWriter() {
68  if (file_ != NULL) {
69    fclose(file_);
70    file_ = NULL;
71  }
72}
73
74bool OutputFileWriter::Write(uint32_t value) {
75  uint32_t tmp = htonl(value);
76  if (fwrite(&tmp, 1, sizeof(uint32_t), file_) != sizeof(uint32_t)) {
77    return false;
78  }
79  return true;
80}
81
82OutputFileWriter* OutputFileWriter::Create(const std::string& filename,
83                                           const std::string& extension) {
84  std::string filepath = webrtc::test::OutputPath() + filename + "." +
85      extension;
86  FILE* file = fopen(filepath.c_str(), "wb");
87  if (file == NULL) {
88    BWE_TEST_LOGGING_CONTEXT("OutputFileWriter");
89    BWE_TEST_LOGGING_LOG1("Create", "Can't write file: %s", filepath.c_str());
90    return NULL;
91  } else {
92    return new OutputFileWriter(file);
93  }
94}
95}  // namespace bwe
96}  // namespace testing
97}  // namespace webrtc
98