IAP GITLAB

Skip to content
Snippets Groups Projects
Interface.boost.cc 3.62 KiB
Newer Older
Ralf M Ulrich's avatar
Ralf M Ulrich committed
/*
 * (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.
 */

Ralf M Ulrich's avatar
Ralf M Ulrich committed
#include <corsika_data/Interface.h>

Ralf M Ulrich's avatar
Ralf M Ulrich committed
#include <boost/iostreams/filter/bzip2.hpp>
#include <boost/iostreams/filtering_stream.hpp>
Ralf M Ulrich's avatar
Ralf M Ulrich committed
#include <fstream>
#include <iostream>
Ralf M Ulrich's avatar
Ralf M Ulrich committed
#include <string>
Ralf M Ulrich's avatar
Ralf M Ulrich committed

#include <iostream>
using namespace std;

namespace corsika_data {

  struct DataFile {
    std::ifstream file_in_;
    boost::iostreams::filtering_istream in_;
    DataFile() {}
    ~DataFile() {}
Ralf M Ulrich's avatar
Ralf M Ulrich committed
    void Open(const char* name) {
Ralf M Ulrich's avatar
Ralf M Ulrich committed
      if (file_in_.is_open()) {
Ralf M Ulrich's avatar
Ralf M Ulrich committed
        std::cout << "DataFile is still open! Closing it now..." << std::endl;
Ralf M Ulrich's avatar
Ralf M Ulrich committed
        file_in_.close();
Ralf M Ulrich's avatar
Ralf M Ulrich committed
      }
Ralf M Ulrich's avatar
Ralf M Ulrich committed
      std::string s(name);
Ralf M Ulrich's avatar
Ralf M Ulrich committed
      auto i1 = s.find_first_not_of(" \t\r\n"); // trimm right
      auto i2 = s.find_first_of(" \t\r\n", i1); // trimm left
Ralf M Ulrich's avatar
Ralf M Ulrich committed
      std::string trimmed(s.substr(i1, i2 - i1));
Ralf M Ulrich's avatar
Ralf M Ulrich committed
      file_in_.open(trimmed, ios_base::in | ios_base::binary);
      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);
      }
ralfulrich's avatar
ralfulrich committed
      if (trimmed.rfind(".bz2") == trimmed.length() - 4)
        in_.push(boost::iostreams::bzip2_decompressor());
Ralf M Ulrich's avatar
Ralf M Ulrich committed
      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));
        }
      }
Ralf M Ulrich's avatar
Ralf M Ulrich committed
    }
    double ReadNextNumber() {
      double data;
      in_ >> data;
      if (!in_.good()) { throw std::runtime_error(std::string("Cannot ReadNextNumber")); }
Ralf M Ulrich's avatar
Ralf M Ulrich committed
      return data;
    }
    int ReadNextText(std::string& data) {
      std::getline(in_, data, ' ');
      if (!in_.good()) { throw std::runtime_error(std::string("Cannot ReadNextText")); }
Ralf M Ulrich's avatar
Ralf M Ulrich committed
      return 0;
    }
  };
Ralf M Ulrich's avatar
Ralf M Ulrich committed

Ralf M Ulrich's avatar
Ralf M Ulrich committed
  DataFile global_DataFile;
Ralf M Ulrich's avatar
Ralf M Ulrich committed

Ralf M Ulrich's avatar
Ralf M Ulrich committed
  void CorDataOpenFile(const std::string& name) { global_DataFile.Open(name.c_str()); }
Ralf M Ulrich's avatar
Ralf M Ulrich committed
  void CorDataFillArray(double* data, const int& length) {
    global_DataFile.FillArray(data, length);
  }
Ralf M Ulrich's avatar
Ralf M Ulrich committed
  void CorDataCloseFile() { global_DataFile.Close(); }
  double CorDataNextNumber() { return global_DataFile.ReadNextNumber(); }
ralfulrich's avatar
ralfulrich committed
  void CorDataNextText(std::string& data) { global_DataFile.ReadNextText(data); }
Ralf M Ulrich's avatar
Ralf M Ulrich committed
  void CorDataNextText(char* data, const int maxlength) {
Ralf M Ulrich's avatar
Ralf M Ulrich committed
    std::string STR;
Ralf M Ulrich's avatar
Ralf M Ulrich committed
    CorDataNextText(STR);
ralfulrich's avatar
ralfulrich committed
    for (int i = 0; i < maxlength && i < (int)STR.size(); ++i) data[i] = STR[i];
Ralf M Ulrich's avatar
Ralf M Ulrich committed
  }
ralfulrich's avatar
ralfulrich committed
  bool CorDataCanDeCompress() { return true; }
ralfulrich's avatar
ralfulrich committed

Ralf M Ulrich's avatar
Ralf M Ulrich committed
  extern "C" {
  void cordataopenfile_(const char* name, const int stringlength) {
    // make a 'real' string out of fortran one (if \0 is missing)
ralfulrich's avatar
ralfulrich committed
    char* buffer = new char[stringlength + 1];
    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);
ralfulrich's avatar
ralfulrich committed
    delete[] buffer;
ralfulrich's avatar
ralfulrich committed
  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; }
Ralf M Ulrich's avatar
Ralf M Ulrich committed
  }
Ralf M Ulrich's avatar
Ralf M Ulrich committed
} // namespace corsika_data