| 1 |
/* |
| 2 |
* (C) Copyright 2013 ECMWF. |
| 3 |
* |
| 4 |
* This software is licensed under the terms of the Apache Licence Version 2.0 |
| 5 |
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 |
* In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 |
* granted to it by virtue of its status as an intergovernmental organisation |
| 8 |
* nor does it submit to any jurisdiction. |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <cstring> |
| 12 |
#include <fstream> |
| 13 |
#include <sstream> |
| 14 |
|
| 15 |
#include "eckit/filesystem/PathName.h" |
| 16 |
#include "eckit/runtime/Main.h" |
| 17 |
|
| 18 |
#include "atlas/runtime/Exception.h" |
| 19 |
#include "atlas_f/internals/atlas_read_file.h" |
| 20 |
|
| 21 |
namespace atlas { |
| 22 |
|
| 23 |
void read_file(const eckit::PathName& p, std::ostream& out) { |
| 24 |
if (p.exists()) { |
| 25 |
std::ifstream in; |
| 26 |
in.open(p.asString().c_str()); |
| 27 |
if (!in) { |
| 28 |
throw_CantOpenFile(p.asString(), Here()); |
| 29 |
} |
| 30 |
else { |
| 31 |
out << in.rdbuf(); |
| 32 |
in.close(); |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
} // namespace atlas |
| 38 |
|
| 39 |
extern "C" { |
| 40 |
|
| 41 |
//----------------------------------------------------------------------------- |
| 42 |
|
| 43 |
int atlas__read_file(const char* path, char*& content, int& size) { |
| 44 |
// eckit::FileReadPolicy p = |
| 45 |
// eckit::Main::instance().behavior().fileReadPolicy(); |
| 46 |
|
| 47 |
// std::stringstream ss; |
| 48 |
|
| 49 |
// if( read( p, path, ss ) ) |
| 50 |
// { |
| 51 |
// std::string s = ss.str(); |
| 52 |
// size = s.size()+1; |
| 53 |
// content = new char[size]; |
| 54 |
// strcpy(content,s.c_str()); |
| 55 |
// return true; |
| 56 |
// } |
| 57 |
|
| 58 |
std::stringstream ss; |
| 59 |
atlas::read_file(path, ss); |
| 60 |
std::string s = ss.str(); |
| 61 |
size = static_cast<int>(s.size()); |
| 62 |
content = new char[size + 1]; |
| 63 |
std::strncpy(content, s.c_str(), size + 1); |
| 64 |
return true; |
| 65 |
} |
| 66 |
|
| 67 |
//----------------------------------------------------------------------------- |
| 68 |
} |