diff --git a/src/core/compat.cpp b/src/core/compat.cpp index b701847..3f208d6 100644 --- a/src/core/compat.cpp +++ b/src/core/compat.cpp @@ -20,26 +20,29 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include +#include #include #include "compat.hpp" using namespace std; -/* Replacement function for getline that removes any trailing \r. */ -istream& d2vgetline(istream& is, string& str) +void d2vgetline(FILE *f, string& str) { - string tmp; - str.clear(); - getline(is, tmp); + while (1) { + int ch = fgetc(f); - if (tmp.size() != 0 && tmp.at(tmp.length() - 1) == 0x0D) - tmp.erase(tmp.length() - 1); + if (ch == EOF) + break; - str = tmp; + if (ch == '\n') { + if (str[str.size() - 1] == '\r') + str.erase(str.size() - 1, 1); + break; + } - return is; + str += (char)ch; + } } diff --git a/src/core/compat.hpp b/src/core/compat.hpp index 6e4098d..5a68348 100644 --- a/src/core/compat.hpp +++ b/src/core/compat.hpp @@ -23,7 +23,7 @@ #ifndef COMPAT_H #define COMPAT_H -#include +#include #include /* Large file aware functions. */ @@ -43,6 +43,6 @@ using namespace std; -istream& d2vgetline(istream& is, string& str); +void d2vgetline(FILE *f, string& str); #endif diff --git a/src/core/d2v.cpp b/src/core/d2v.cpp index f14bf5c..d2abc01 100644 --- a/src/core/d2v.cpp +++ b/src/core/d2v.cpp @@ -20,7 +20,6 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include #include #include #include @@ -28,6 +27,7 @@ extern "C" { #include +#include #include #include } @@ -86,7 +86,7 @@ void d2vfreep(d2vcontext **ctx) d2vcontext *d2vparse(const char *filename, string& err) { string line; - ifstream input; + FILE *input = NULL; d2vcontext *ret; int i; @@ -108,12 +108,12 @@ d2vcontext *d2vparse(const char *filename, string& err) goto fail; } - input.open(wide_filename); + input = _wfopen(wide_filename, L"rb"); #else - input.open(filename); + input = fopen(filename, "rb"); #endif - if (input.fail()) { + if (!input) { err = "D2V cannot be opened."; goto fail; } @@ -278,7 +278,8 @@ d2vcontext *d2vparse(const char *filename, string& err) d2vgetline(input, line); } - input.close(); + fclose(input); + input = NULL; if (!ret->frames.size() || !ret->gops.size()) { err = "No frames in D2V file!"; @@ -289,5 +290,7 @@ d2vcontext *d2vparse(const char *filename, string& err) fail: d2vfreep(&ret); + if (input) + fclose(input); return NULL; }