xoutput.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "xconfig.h"
4 #include "xerror.h"
5 #include "xfmt.h"
6 #include <fstream>
7 
8 namespace nmx {
9 
14 class Output
15 {
16 public:
17  inline static const Format latex{ "table", "&", "\\\\\n", "tex" };
18  inline static const Format plot{ "plot", ",", "\n", "csv" };
19  inline static const Format terminal{ "terminal", "\t", "\n", "out" };
20  inline static const Format csv{ "csv", ",", "\n", "csv" };
21 
29  static std::ofstream get_stream(const std::string &fname) {
30  std::ofstream _ofs(fname.c_str());
31  if (!_ofs) {
32  std::string _msg = "error open file:" + fname;
33  throw std::runtime_error(nmx_msg_txt(_msg));
34  }
35  return _ofs;
36  }
37 
46  static std::ofstream get_stream(const std::string &dirname, const std::string &fname) {
47  std::stringstream _stream;
49  << "/" << dirname //
50  << "/" << fname; //
51  return get_stream(_stream.str());
52  }
53 
63  static std::ofstream get_stream(const std::string &dirname, const Format &fmt) {
64  std::stringstream _stream;
66  << "/" << dirname //
67  << "/" << fmt.name() //
68  << "." << fmt.ext(); //
69  std::ofstream ofs = get_stream(_stream.str());
70  fmt.set_defaults(ofs);
71  return ofs;
72  }
73 
84  template<class T>
85  static std::ofstream get_stream(const std::string &dirname, const Format &fmt, const T &id) {
86  std::stringstream _stream;
88  << "/" << dirname //
89  << "/" << fmt.name() //
90  << "-" << id << "." << fmt.ext();
91 
92  //erzeuge Ausgabestrom
93  std::ofstream ofs = get_stream(_stream.str());
94  fmt.set_defaults(ofs);
95  return ofs;
96  }
97 
98 private:
102  inline static Format _array = Output::csv;
103 
104 public:
112  friend std::ostream &operator<<(std::ostream &os, const Format &fmt);
113 
118  inline static const Format &array() { return _array; }
119 };
120 
128 inline std::ostream &operator<<(std::ostream &os, const Format &fmt) {
129  Output::_array = fmt;
130  return os;
131 }
132 
133 } // namespace nmx
friend std::ostream & operator<<(std::ostream &os, const Format &fmt)
operator << überladener Ausgabeoperator internes Formatierungsobjekt wird gesetzt.
Definition: xoutput.h:128
static const Format csv
Definition: xoutput.h:20
static const Format & array()
array lese internes Formatierungsobjekt
Definition: xoutput.h:118
static std::ofstream get_stream(const std::string &dirname, const Format &fmt)
get_output_stream Öffnen einer Ausgabedatei in einem Unterverzeichnis. Falls die Datei nicht geöffnet...
Definition: xoutput.h:63
static const Format terminal
Definition: xoutput.h:19
const std::string & ext() const
Definition: xfmt.h:142
static std::ofstream get_stream(const std::string &fname)
get_output_stream Öffnen einer Ausgabedatei falls die Datei nicht geöffnet werden kann wird ein Fehle...
Definition: xoutput.h:29
The Output struct Hilfsklasse zur Erzeugung von Ausgabeströmen zum Schreiben in Dateien.
Definition: xoutput.h:14
static std::ofstream get_stream(const std::string &dirname, const Format &fmt, const T &id)
get_output_stream Öffnen einer Ausgabedatei in einem Unterverzeichnis. Falls die Datei nicht geöffnet...
Definition: xoutput.h:85
void set_defaults(std::ostream &ofs) const
set_defaults Formatierung der Zahlen
Definition: xfmt.h:133
#define nmx_msg_txt(msg)
Definition: xerror.h:119
const std::string dirname
Definition: x006.h:19
The Format class Formatierte Ausgabe von Arrays.
Definition: xfmt.h:10
static const std::string data_directory
Definition: xconfig.h:23
const std::string & name() const
Definition: xfmt.h:141
static std::ofstream get_stream(const std::string &dirname, const std::string &fname)
get_output_stream Öffnen einer Ausgabedatei in einem Unterverzeichnis, Falls die Datei nicht geöffnet...
Definition: xoutput.h:46
static const Format latex
Definition: xoutput.h:17
static const Format plot
Definition: xoutput.h:18