blob: 141f5543f9ee5bc2261520c980e9ab91e4212b52 [file] [log] [blame]
Matthew Heaney282a6752012-08-20 15:41:06 -07001// Copyright (c) 2012 The WebM project authors. All Rights Reserved.
2//
3// Use of this source code is governed by a BSD-style license
4// that can be found in the LICENSE file in the root of the source
5// tree. An additional intellectual property rights grant can be found
6// in the file PATENTS. All contributing project authors may
7// be found in the AUTHORS file in the root of the source tree.
8
9#include "./vttreader.h" // NOLINT
10
Tom Finegan327e8ab2014-01-22 14:03:40 -080011#ifdef _MSC_VER
12// Disable MSVC warnings that suggest making code non-portable.
Vignesh Venkatasubramanian5218bd22014-04-14 12:11:27 -070013#pragma warning(disable : 4996)
Tom Finegan327e8ab2014-01-22 14:03:40 -080014#endif
15
Matthew Heaney282a6752012-08-20 15:41:06 -070016namespace libwebvtt {
17
Vignesh Venkatasubramanian5218bd22014-04-14 12:11:27 -070018VttReader::VttReader() : file_(NULL) {}
Matthew Heaney282a6752012-08-20 15:41:06 -070019
Vignesh Venkatasubramanian5218bd22014-04-14 12:11:27 -070020VttReader::~VttReader() { Close(); }
Matthew Heaney282a6752012-08-20 15:41:06 -070021
22int VttReader::Open(const char* filename) {
23 if (filename == NULL || file_ != NULL)
24 return -1;
25
26 file_ = fopen(filename, "rb");
27 if (file_ == NULL)
28 return -1;
29
30 return 0; // success
31}
32
33void VttReader::Close() {
34 if (file_) {
35 fclose(file_);
36 file_ = NULL;
37 }
38}
39
40int VttReader::GetChar(char* c) {
41 if (c == NULL || file_ == NULL)
42 return -1;
43
44 const int result = fgetc(file_);
45 if (result != EOF) {
46 *c = static_cast<char>(result);
47 return 0; // success
48 }
49
50 if (ferror(file_))
51 return -1; // error
52
53 if (feof(file_))
54 return 1; // EOF
55
56 return -1; // weird
57}
58
59} // namespace libwebvtt