Skip to content

Commit

Permalink
Merge pull request #27 from dubhater/master
Browse files Browse the repository at this point in the history
d2v: Get rid of ifstream
  • Loading branch information
dwbuiten committed Apr 28, 2016
2 parents 973cc29 + e006b9c commit a40743b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
23 changes: 13 additions & 10 deletions src/core/compat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,29 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <fstream>
#include <stdio.h>
#include <string>

#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;
}
}
4 changes: 2 additions & 2 deletions src/core/compat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#ifndef COMPAT_H
#define COMPAT_H

#include <fstream>
#include <stdio.h>
#include <string>

/* Large file aware functions. */
Expand All @@ -43,6 +43,6 @@

using namespace std;

istream& d2vgetline(istream& is, string& str);
void d2vgetline(FILE *f, string& str);

#endif
15 changes: 9 additions & 6 deletions src/core/d2v.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

extern "C" {
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
}
Expand Down Expand Up @@ -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;

Expand All @@ -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;
}
Expand Down Expand Up @@ -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!";
Expand All @@ -289,5 +290,7 @@ d2vcontext *d2vparse(const char *filename, string& err)

fail:
d2vfreep(&ret);
if (input)
fclose(input);
return NULL;
}

0 comments on commit a40743b

Please sign in to comment.