Skip to content

Commit a40743b

Browse files
committed
Merge pull request #27 from dubhater/master
d2v: Get rid of ifstream
2 parents 973cc29 + e006b9c commit a40743b

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

src/core/compat.cpp

+13-10
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,29 @@
2020
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2121
*/
2222

23-
#include <fstream>
23+
#include <stdio.h>
2424
#include <string>
2525

2626
#include "compat.hpp"
2727

2828
using namespace std;
2929

30-
/* Replacement function for getline that removes any trailing \r. */
31-
istream& d2vgetline(istream& is, string& str)
30+
void d2vgetline(FILE *f, string& str)
3231
{
33-
string tmp;
34-
3532
str.clear();
3633

37-
getline(is, tmp);
34+
while (1) {
35+
int ch = fgetc(f);
3836

39-
if (tmp.size() != 0 && tmp.at(tmp.length() - 1) == 0x0D)
40-
tmp.erase(tmp.length() - 1);
37+
if (ch == EOF)
38+
break;
4139

42-
str = tmp;
40+
if (ch == '\n') {
41+
if (str[str.size() - 1] == '\r')
42+
str.erase(str.size() - 1, 1);
43+
break;
44+
}
4345

44-
return is;
46+
str += (char)ch;
47+
}
4548
}

src/core/compat.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#ifndef COMPAT_H
2424
#define COMPAT_H
2525

26-
#include <fstream>
26+
#include <stdio.h>
2727
#include <string>
2828

2929
/* Large file aware functions. */
@@ -43,6 +43,6 @@
4343

4444
using namespace std;
4545

46-
istream& d2vgetline(istream& is, string& str);
46+
void d2vgetline(FILE *f, string& str);
4747

4848
#endif

src/core/d2v.cpp

+9-6
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2121
*/
2222

23-
#include <fstream>
2423
#include <iostream>
2524
#include <string>
2625
#include <sstream>
2726
#include <vector>
2827

2928
extern "C" {
3029
#include <assert.h>
30+
#include <stdio.h>
3131
#include <stdlib.h>
3232
#include <string.h>
3333
}
@@ -86,7 +86,7 @@ void d2vfreep(d2vcontext **ctx)
8686
d2vcontext *d2vparse(const char *filename, string& err)
8787
{
8888
string line;
89-
ifstream input;
89+
FILE *input = NULL;
9090
d2vcontext *ret;
9191
int i;
9292

@@ -108,12 +108,12 @@ d2vcontext *d2vparse(const char *filename, string& err)
108108
goto fail;
109109
}
110110

111-
input.open(wide_filename);
111+
input = _wfopen(wide_filename, L"rb");
112112
#else
113-
input.open(filename);
113+
input = fopen(filename, "rb");
114114
#endif
115115

116-
if (input.fail()) {
116+
if (!input) {
117117
err = "D2V cannot be opened.";
118118
goto fail;
119119
}
@@ -278,7 +278,8 @@ d2vcontext *d2vparse(const char *filename, string& err)
278278
d2vgetline(input, line);
279279
}
280280

281-
input.close();
281+
fclose(input);
282+
input = NULL;
282283

283284
if (!ret->frames.size() || !ret->gops.size()) {
284285
err = "No frames in D2V file!";
@@ -289,5 +290,7 @@ d2vcontext *d2vparse(const char *filename, string& err)
289290

290291
fail:
291292
d2vfreep(&ret);
293+
if (input)
294+
fclose(input);
292295
return NULL;
293296
}

0 commit comments

Comments
 (0)