Newer
Older
/*
* (c) Copyright 2020 CORSIKA Project, corsika-project@lists.kit.edu
*
* 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 <boost/iostreams/filter/bzip2.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <iostream>
using namespace std;
namespace corsika_data {
struct DataFile {
std::ifstream file_in_;
boost::iostreams::filtering_istream in_;
DataFile() {}
~DataFile() {}
std::cout << "DataFile is still open! Closing it now..." << std::endl;
auto i1 = s.find_first_not_of(" \t\r\n"); // trimm right
auto i2 = s.find_first_of(" \t\r\n", i1); // trimm left
if (!file_in_ || !file_in_.good()) {
std::cerr << std::string("Cannot open file: \'") + trimmed << "\'" << std::endl;
throw std::runtime_error(std::string("Cannot open file: ") + trimmed);
}
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];
if (!in_.good()) {
throw std::runtime_error(std::string("Cannot FillArray i=") +
std::to_string(i));
}
}
}
double ReadNextNumber() {
double data;
in_ >> data;
if (!in_.good()) { throw std::runtime_error(std::string("Cannot ReadNextNumber")); }
return data;
}
int ReadNextText(std::string& data) {
std::getline(in_, data, ' ');
if (!in_.good()) { throw std::runtime_error(std::string("Cannot ReadNextText")); }
void CorDataOpenFile(const std::string& name) { global_DataFile.Open(name.c_str()); }
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) {
for (int i = 0; i < maxlength && i < (int)STR.size(); ++i) data[i] = STR[i];
void cordataopenfile_(const char* name, const int stringlength) {
// make a 'real' string out of fortran one (if \0 is missing)
for (int i = 0; i < stringlength; ++i) buffer[i] = name[i];
// if not already terminated by 0, add it
if (buffer[stringlength - 1] != '\0') buffer[stringlength] = '\0';
CorDataOpenFile(buffer);
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; }