| 1 |
/* |
| 2 |
* Advanced Simulation Library <http://asl.org.il> |
| 3 |
* |
| 4 |
* Copyright 2015 Avtech Scientific <http://avtechscientific.com> |
| 5 |
* |
| 6 |
* |
| 7 |
* This file is part of Advanced Simulation Library (ASL). |
| 8 |
* |
| 9 |
* ASL is free software: you can redistribute it and/or modify it |
| 10 |
* under the terms of the GNU Affero General Public License as |
| 11 |
* published by the Free Software Foundation, version 3 of the License. |
| 12 |
* |
| 13 |
* ASL is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Affero General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Affero General Public License |
| 19 |
* along with ASL. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
|
| 24 |
#ifndef ASLABDFORMAT_H |
| 25 |
#define ASLABDFORMAT_H |
| 26 |
|
| 27 |
#include <stdlib.h> |
| 28 |
|
| 29 |
#include <iostream> |
| 30 |
#include <fstream> |
| 31 |
#include <string> |
| 32 |
#include <memory> |
| 33 |
|
| 34 |
#include <data/aslDataWrapper.h> |
| 35 |
|
| 36 |
using namespace std; |
| 37 |
|
| 38 |
namespace asl { |
| 39 |
|
| 40 |
/** |
| 41 |
\defgroup ABDFormat ASL Binary Dump (ABD) format |
| 42 |
\ingroup IO |
| 43 |
*/ |
| 44 |
|
| 45 |
/// ABD (ASL Binary Dump) file, input |
| 46 |
/** |
| 47 |
\ingroup ABDFormat |
| 48 |
*/ |
| 49 |
class ABDFileIn: public std::ifstream{ |
| 50 |
public: |
| 51 |
inline ABDFileIn(); |
| 52 |
inline ABDFileIn(string name); |
| 53 |
}; |
| 54 |
|
| 55 |
/// ABD (ASL Binary Dump) file, output |
| 56 |
/** |
| 57 |
\ingroup ABDFormat |
| 58 |
*/ |
| 59 |
class ABDFileOut: public std::ofstream{ |
| 60 |
public: |
| 61 |
inline ABDFileOut(); |
| 62 |
inline ABDFileOut(string name); |
| 63 |
}; |
| 64 |
|
| 65 |
/* /// \relates ABDFileOut |
| 66 |
template <typename T> inline ABDFileOut & operator <<(ABDFileOut & f, const T & a); |
| 67 |
/// \relates ABDFileIn |
| 68 |
template <typename T> inline ABDFileIn & operator >>(ABDFileIn & f,T & a); |
| 69 |
*/ |
| 70 |
|
| 71 |
/// \relates ABDFileOut |
| 72 |
inline ABDFileOut & operator <<(ABDFileOut & f, const int a); |
| 73 |
/// \relates ABDFileIn |
| 74 |
inline ABDFileIn & operator >>(ABDFileIn & f,int & a); |
| 75 |
/// \relates ABDFileOut |
| 76 |
inline ABDFileOut & operator <<(ABDFileOut & f, const unsigned int a); |
| 77 |
/// \relates ABDFileIn |
| 78 |
inline ABDFileIn & operator >>(ABDFileIn & f, unsigned int & a); |
| 79 |
/// \relates ABDFileOut |
| 80 |
inline ABDFileOut & operator <<(ABDFileOut & f, const float a); |
| 81 |
/// \relates ABDFileIn |
| 82 |
inline ABDFileIn & operator >>(ABDFileIn & f,float & a); |
| 83 |
/// \relates ABDFileOut |
| 84 |
inline ABDFileOut & operator <<(ABDFileOut & f, const double a); |
| 85 |
/// \relates ABDFileIn |
| 86 |
inline ABDFileIn & operator >>(ABDFileIn & f,double & a); |
| 87 |
|
| 88 |
|
| 89 |
/// \relates ABDFileOut |
| 90 |
template <typename T> inline ABDFileOut & operator <<(ABDFileOut & f,pair<T*, unsigned int> a); |
| 91 |
/// \relates ABDFileIn |
| 92 |
template <typename T> inline ABDFileIn & operator >>(ABDFileIn & f,pair<T*, unsigned int> a); |
| 93 |
|
| 94 |
/// \relates ABDFileOut |
| 95 |
inline ABDFileOut & operator <<(ABDFileOut & f, const string &a); |
| 96 |
/// \relates ABDFileIn |
| 97 |
inline ABDFileIn & operator >>(ABDFileIn & f,string &a); |
| 98 |
|
| 99 |
/// \relates ABDFileOut |
| 100 |
template <typename T> inline ABDFileOut & operator <<(ABDFileOut & f, const AVec<T> & a); |
| 101 |
/// \relates ABDFileIn |
| 102 |
template <typename T> inline ABDFileIn & operator >>(ABDFileIn & f,AVec<T> & a); |
| 103 |
|
| 104 |
class Block; |
| 105 |
|
| 106 |
/// \relates ABDFileOut |
| 107 |
ABDFileOut & operator <<(ABDFileOut & f, const Block &a); |
| 108 |
/// \relates ABDFileIn |
| 109 |
ABDFileIn & operator >>(ABDFileIn & f, Block &a); |
| 110 |
|
| 111 |
class AbstractData; |
| 112 |
|
| 113 |
/// writes data. It is assumed that the Block is written separately \relates ABDFileOut |
| 114 |
/** |
| 115 |
The function writes only the first element |
| 116 |
*/ |
| 117 |
ABDFileOut & operator <<(ABDFileOut & f, const AbstractData &a); |
| 118 |
|
| 119 |
/// reads data. It is assumed that the class has the propper size \relates ABDFileIn |
| 120 |
/** |
| 121 |
The function writes only the first element |
| 122 |
*/ |
| 123 |
ABDFileIn & operator >>(ABDFileIn & f, AbstractData &a); |
| 124 |
|
| 125 |
/// reads data. It is assumed that the class has the propper size \relates ABDFileIn |
| 126 |
/** |
| 127 |
The function reads only the first element. Additionaly it creates and |
| 128 |
stores data in the memory \p d; in case of d.get()==0 defineds whether |
| 129 |
the new d should be allocated |
| 130 |
*/ |
| 131 |
ABDFileIn & get(ABDFileIn & f, AbstractData &a, std::shared_ptr<double> d); |
| 132 |
|
| 133 |
/// writes \p data in a file with ABD (ASL Binary Dump) format |
| 134 |
/** |
| 135 |
\ingroup ABDFormat |
| 136 |
*/ |
| 137 |
void writeABD(const string &fileName, const AbstractData & data, const string & name); |
| 138 |
|
| 139 |
|
| 140 |
//------------------------IMPLEMENTATION------------------------------ |
| 141 |
|
| 142 |
ABDFileIn::ABDFileIn():ifstream() |
| 143 |
{}; |
| 144 |
ABDFileIn::ABDFileIn(string name): |
| 145 |
ifstream(name,ios::in | ios::binary) |
| 146 |
{} |
| 147 |
|
| 148 |
ABDFileOut::ABDFileOut():ofstream() |
| 149 |
{} |
| 150 |
ABDFileOut::ABDFileOut(string name): |
| 151 |
ofstream(name,ios::out | ios::binary) |
| 152 |
{} |
| 153 |
|
| 154 |
inline ABDFileOut & operator <<(ABDFileOut & f, const int a) |
| 155 |
{ |
| 156 |
f.write((char*)&a,sizeof(int)); |
| 157 |
return f; |
| 158 |
} |
| 159 |
|
| 160 |
inline ABDFileIn & operator >>(ABDFileIn & f,int & a) |
| 161 |
{ |
| 162 |
f.read((char*)&a,sizeof(int)); |
| 163 |
return f; |
| 164 |
} |
| 165 |
|
| 166 |
inline ABDFileOut & operator <<(ABDFileOut & f, const unsigned int a) |
| 167 |
{ |
| 168 |
f.write((char*)&a,sizeof(unsigned int)); |
| 169 |
return f; |
| 170 |
} |
| 171 |
|
| 172 |
inline ABDFileIn & operator >>(ABDFileIn & f, unsigned int & a) |
| 173 |
{ |
| 174 |
f.read((char*)&a,sizeof(unsigned int)); |
| 175 |
return f; |
| 176 |
} |
| 177 |
|
| 178 |
inline ABDFileOut & operator <<(ABDFileOut & f, const float a) |
| 179 |
{ |
| 180 |
f.write((char*)&a,sizeof(float)); |
| 181 |
return f; |
| 182 |
} |
| 183 |
|
| 184 |
inline ABDFileIn & operator >>(ABDFileIn & f,float & a) |
| 185 |
{ |
| 186 |
f.read((char*)&a,sizeof(float)); |
| 187 |
return f; |
| 188 |
} |
| 189 |
|
| 190 |
inline ABDFileOut & operator <<(ABDFileOut & f, const double a) |
| 191 |
{ |
| 192 |
f.write((char*)&a,sizeof(double)); |
| 193 |
return f; |
| 194 |
} |
| 195 |
|
| 196 |
inline ABDFileIn & operator >>(ABDFileIn & f,double & a) |
| 197 |
{ |
| 198 |
f.read((char*)&a,sizeof(double)); |
| 199 |
return f; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
template <typename T> inline ABDFileOut & operator <<(ABDFileOut & f,pair<T*, unsigned int> a) |
| 204 |
{ |
| 205 |
f.write((char*)a.first,sizeof(T)*a.second); |
| 206 |
return f; |
| 207 |
} |
| 208 |
template <typename T> inline ABDFileIn & operator >>(ABDFileIn & f,pair<T*, unsigned int> a) |
| 209 |
{ |
| 210 |
f.read((char*)a.first,sizeof(T)*a.second); |
| 211 |
return f; |
| 212 |
} |
| 213 |
|
| 214 |
inline ABDFileOut & operator <<(ABDFileOut & f, const string &a){ |
| 215 |
unsigned int n=a.size(); |
| 216 |
f<<n<<make_pair(&a[0],n); |
| 217 |
return f; |
| 218 |
} |
| 219 |
|
| 220 |
inline ABDFileIn & operator >>(ABDFileIn & f,string &a){ |
| 221 |
unsigned int n; |
| 222 |
f>>n; a.resize(n); |
| 223 |
f>>make_pair(a.data(),n); |
| 224 |
return f; |
| 225 |
} |
| 226 |
|
| 227 |
template <typename T> inline ABDFileOut & operator <<(ABDFileOut & f, const AVec<T> & a) |
| 228 |
{ |
| 229 |
unsigned int n(a.getSize()); |
| 230 |
f<<n<<make_pair(&(a[0]),n); |
| 231 |
return f; |
| 232 |
} |
| 233 |
|
| 234 |
template <typename T> inline ABDFileIn & operator >>(ABDFileIn & f,AVec<T> & a) |
| 235 |
{ |
| 236 |
unsigned int n(0); |
| 237 |
f>>n; |
| 238 |
a.resize(n); |
| 239 |
f>>make_pair(&(a[0]),n); |
| 240 |
return f; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
}// asl |
| 245 |
|
| 246 |
#endif //ASLVTKFORMAT_H |
| 247 |
|