CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
fileIo.hpp
1/*
2 * CoDiPack, a Code Differentiation Package
3 *
4 * Copyright (C) 2015-2024 Chair for Scientific Computing (SciComp), University of Kaiserslautern-Landau
5 * Homepage: http://www.scicomp.uni-kl.de
6 * Contact: Prof. Nicolas R. Gauger (codi@scicomp.uni-kl.de)
7 *
8 * Lead developers: Max Sagebaum, Johannes Blühdorn (SciComp, University of Kaiserslautern-Landau)
9 *
10 * This file is part of CoDiPack (http://www.scicomp.uni-kl.de/software/codi).
11 *
12 * CoDiPack is free software: you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * CoDiPack is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty
19 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * See the GNU General Public License for more details.
22 * You should have received a copy of the GNU
23 * General Public License along with CoDiPack.
24 * If not, see <http://www.gnu.org/licenses/>.
25 *
26 * For other licensing options please contact us.
27 *
28 * Authors:
29 * - SciComp, University of Kaiserslautern-Landau:
30 * - Max Sagebaum
31 * - Johannes Blühdorn
32 * - Former members:
33 * - Tim Albring
34 */
35
36#pragma once
37
38#include <errno.h>
39#include <stdio.h>
40#include <string.h>
41
42#include <iostream>
43#include <string>
44
45#include "../config.h"
46#include "macros.hpp"
47
49namespace codi {
50
52 enum struct IoError {
53 Mode,
54 Open,
55 Write,
56 Read
57 };
58
60 struct IoException {
61 public:
62
63 std::string text;
65
67 IoException(IoError id, std::string const& text, bool appendErrno) : text(text), id(id) {
68 if (appendErrno) {
69 this->text += " (Internal error: ";
70 this->text += strerror(errno);
71 this->text += ")";
72 }
73 }
74 };
75
84 struct FileIo {
85 private:
86
87 FILE* fileHandle;
88 bool writeMode;
89
90 public:
91
94 FileIo(std::string const& file, bool write) {
95 writeMode = write;
96 fileHandle = nullptr;
97
98 if (write) {
99 fileHandle = fopen(file.c_str(), "wb");
100 } else {
101 fileHandle = fopen(file.c_str(), "rb");
102 }
103
104 if (nullptr == fileHandle) {
105 throw IoException(IoError::Open, "Could not open file: " + file, true);
106 }
107 }
108
111 if (nullptr != fileHandle) {
112 fclose(fileHandle);
113 }
114 }
115
118 template<typename Data>
119 void writeData(Data const* data, size_t const length) {
120 if (writeMode) {
121 size_t s = fwrite(data, sizeof(Data), length, fileHandle);
122
123 if (s != length) {
124 throw IoException(IoError::Read, "Wrong number of bytes written.", true);
125 }
126 } else {
127 throw IoException(IoError::Mode, "Using write io handle in wrong mode.", false);
128 }
129 }
130
133 template<typename Data>
134 void readData(Data* data, size_t const length) {
135 if (!writeMode) {
136 size_t s = fread(data, sizeof(Data), length, fileHandle);
137
138 if (s != length) {
139 throw IoException(IoError::Read, "Wrong number of bytes read.", false);
140 }
141 } else {
142 throw IoException(IoError::Mode, "Using read io handle in wrong mode.", false);
143 }
144 }
145 };
146}
CoDiPack - Code Differentiation Package.
Definition: codi.hpp:90
IoError
Possible IO errors.
Definition: fileIo.hpp:52
Helper structure for writing binary data.
Definition: fileIo.hpp:84
void writeData(Data const *data, size_t const length)
Definition: fileIo.hpp:119
FileIo(std::string const &file, bool write)
Definition: fileIo.hpp:94
void readData(Data *data, size_t const length)
Definition: fileIo.hpp:134
~FileIo()
Destructor.
Definition: fileIo.hpp:110
IoException for CoDiPack.
Definition: fileIo.hpp:60
IoException(IoError id, std::string const &text, bool appendErrno)
Constructor.
Definition: fileIo.hpp:67
std::string text
Textual description.
Definition: fileIo.hpp:63
IoError id
Exception ID.
Definition: fileIo.hpp:64