Matthew Heaney | 282a675 | 2012-08-20 15:41:06 -0700 | [diff] [blame] | 1 | // 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 Finegan | 327e8ab | 2014-01-22 14:03:40 -0800 | [diff] [blame] | 11 | #ifdef _MSC_VER |
| 12 | // Disable MSVC warnings that suggest making code non-portable. |
Vignesh Venkatasubramanian | 5218bd2 | 2014-04-14 12:11:27 -0700 | [diff] [blame] | 13 | #pragma warning(disable : 4996) |
Tom Finegan | 327e8ab | 2014-01-22 14:03:40 -0800 | [diff] [blame] | 14 | #endif |
| 15 | |
Matthew Heaney | 282a675 | 2012-08-20 15:41:06 -0700 | [diff] [blame] | 16 | namespace libwebvtt { |
| 17 | |
Vignesh Venkatasubramanian | 5218bd2 | 2014-04-14 12:11:27 -0700 | [diff] [blame] | 18 | VttReader::VttReader() : file_(NULL) {} |
Matthew Heaney | 282a675 | 2012-08-20 15:41:06 -0700 | [diff] [blame] | 19 | |
Vignesh Venkatasubramanian | 5218bd2 | 2014-04-14 12:11:27 -0700 | [diff] [blame] | 20 | VttReader::~VttReader() { Close(); } |
Matthew Heaney | 282a675 | 2012-08-20 15:41:06 -0700 | [diff] [blame] | 21 | |
| 22 | int 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 | |
| 33 | void VttReader::Close() { |
| 34 | if (file_) { |
| 35 | fclose(file_); |
| 36 | file_ = NULL; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | int 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 |