-
ralfulrich authoredralfulrich authored
Interface.boost.cc 2.86 KiB
/*
* (c) Copyright 2020 CORSIKA Project, corsika-project@lists.kit.edu
*
* See file AUTHORS for a list of contributors.
*
* This software is distributed under the terms of the GNU General Public
* Licence version 3 (GPL Version 3). See file LICENSE for a full version of
* the license.
*/
#include <corsika_data/Interface.h>
#include <boost/iostreams/filter/bzip2.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <fstream>
#include <iostream>
#include <string>
#include <iostream>
using namespace std;
namespace corsika_data {
struct DataFile {
std::ifstream file_in_;
boost::iostreams::filtering_istream in_;
DataFile() {}
~DataFile() {}
void Open(const char* name) {
if (file_in_.is_open()) {
std::cout << "DataFile is still open! Closing it now..." << std::endl;
file_in_.close();
}
std::string s(name);
auto i1 = s.find_first_not_of(" \t\r\n"); // trimm right
auto i2 = s.find_first_of(" \t\r\n", i1); // trimm left
std::string trimmed(s.substr(i1, i2 - i1));
file_in_.open(trimmed, ios_base::in | ios_base::binary);
in_.reset();
if (trimmed.rfind(".bz2") == trimmed.length() - 4)
in_.push(boost::iostreams::bzip2_decompressor());
in_.push(file_in_);
}
void Close() { file_in_.close(); }
void FillArray(double* data, const int& length) {
for (int i = 0; i < length; i++) in_ >> data[i];
}
double ReadNextNumber() {
double data;
in_ >> data;
return data;
}
int ReadNextText(std::string& data) {
std::getline(in_, data, ' ');
return 0;
}
};
DataFile global_DataFile;
void CorDataOpenFile(const std::string& name) { global_DataFile.Open(name.c_str()); }
// void CorDataOpenFile(const char* name) { global_DataFile.Open(name); }
void CorDataFillArray(double* data, const int& length) {
global_DataFile.FillArray(data, length);
}
void CorDataCloseFile() { global_DataFile.Close(); }
double CorDataNextNumber() { return global_DataFile.ReadNextNumber(); }
void CorDataNextText(std::string& data) { global_DataFile.ReadNextText(data); }
void CorDataNextText(char* data, const int maxlength) {
std::string STR;
CorDataNextText(STR);
for (int i = 0; i < maxlength && i < (int)STR.size(); ++i) data[i] = STR[i];
}
bool CorDataCanDeCompress() { return true; }
extern "C" {
void cordataopenfile_(const char* name) { CorDataOpenFile(name); }
void cordatafillarray_(double* data, const int& length) {
global_DataFile.FillArray(data, length);
}
double cordatanextnumber_() { return global_DataFile.ReadNextNumber(); }
void cordatanexttext_(char* data, const int maxlength) {
return CorDataNextText(data, maxlength);
}
void cordataclosefile_() { global_DataFile.Close(); }
void cordatacandecompress_(int& v) { v=1; }
}
} // namespace corsika_data