Skip to content

Commit c274ecd

Browse files
committed
Merge branch 'master' into file-read-mode-fix
Conflicts: vlsv_reader.cpp
2 parents 7b6b04f + 0126ad1 commit c274ecd

File tree

2 files changed

+44
-40
lines changed

2 files changed

+44
-40
lines changed

portable_file_io.cpp

+35-34
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
* for some of the functions defined in Linux header unistd.h. In Windows
33
* systems define preprocessor macro "WINDOWS".
44
*
5-
* Copyright 2015 Arto Sandroos
5+
* Copyright 2015-2016 Arto Sandroos
6+
* Copyright 2016 Finnish Meteorological Institute
67
*
78
* This program is free software: you can redistribute it and/or modify
89
* it under the terms of the GNU Lesser General Public License as published by
@@ -16,37 +17,37 @@
1617
*
1718
* You should have received a copy of the GNU Lesser General Public License
1819
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19-
*/
20-
21-
#include <cstdlib>
22-
23-
#ifdef WINDOWS
20+
*/
21+
22+
#include <cstdlib>
23+
24+
#ifdef WINDOWS
2425
#include <direct.h>
25-
#include <io.h>
26-
#else
27-
#include <unistd.h>
28-
#endif
29-
30-
#include "portable_file_io.h"
31-
32-
using namespace std;
33-
34-
namespace fileio {
35-
36-
int chdir(const char* path) {
37-
#ifdef WINDOWS
38-
return _chdir(path);
39-
#else
40-
return chdir(path);
41-
#endif
42-
}
43-
44-
char* getcwd(char* buf, size_t size) {
45-
#ifdef WINDOWS
46-
return _getcwd(buf,size);
47-
#else
48-
return getcwd(buf,size);
49-
#endif
50-
}
51-
52-
} // namespace fileio
26+
#include <io.h>
27+
#else
28+
#include <unistd.h>
29+
#endif
30+
31+
#include "portable_file_io.h"
32+
33+
using namespace std;
34+
35+
namespace fileio {
36+
37+
int chdir(const char* path) {
38+
#ifdef WINDOWS
39+
return _chdir(path);
40+
#else
41+
return ::chdir(path);
42+
#endif
43+
}
44+
45+
char* getcwd(char* buf, size_t size) {
46+
#ifdef WINDOWS
47+
return _getcwd(buf,size);
48+
#else
49+
return ::getcwd(buf,size);
50+
#endif
51+
}
52+
53+
} // namespace fileio

vlsv_reader.cpp

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/** This file is part of VLSV file format.
22
*
3-
* Copyright 2011-2013 Finnish Meteorological Institute
3+
* Copyright 2011-2015 Finnish Meteorological Institute
4+
* Copyright 2016 Arto Sandroos
45
*
56
* This program is free software: you can redistribute it and/or modify
67
* it under the terms of the GNU Lesser General Public License as published by
@@ -155,7 +156,7 @@ namespace vlsv {
155156
#endif
156157
return false;
157158
}
158-
159+
159160
// If given filename includes path, chdir into that path:
160161
string fnameWithoutPath;
161162
string pathName;
@@ -168,13 +169,14 @@ namespace vlsv {
168169
} else {
169170
pathName = fname.substr(0,position);
170171
fnameWithoutPath = fname.substr(position+1);
172+
// Chdir to path containing input file and attempt to open the file,
173+
// then chdir back to current working directory.
174+
// Chdir returns zero value if it succeeds
175+
// Not done if the string is empty as chdir fials in that case.
176+
if (fileio::chdir(pathName.c_str()) != 0) success = false;
171177
}
172178
}
173179

174-
// Chdir to path containing input file and attempt to open the file,
175-
// then chdir back to current working directory.
176-
// Chdir returns zero value if it succeeds
177-
if (fileio::chdir(pathName.c_str()) != 0) success = false;
178180
filein.open(fnameWithoutPath.c_str(), fstream::in | fstream::binary);
179181
if (fileio::chdir(cwd) != 0) success = false;
180182

@@ -185,6 +187,7 @@ namespace vlsv {
185187
filein.close();
186188
success = false;
187189
}
190+
188191
if (success == false) {
189192
#ifndef NDEBUG
190193
cerr << "vlsv::Reader ERROR: File '" << fnameWithoutPath << "' could not be opened!" << endl;

0 commit comments

Comments
 (0)