OOFEM 3.0
Loading...
Searching...
No Matches
datastream.C
Go to the documentation of this file.
1/*
2 *
3 * ##### ##### ###### ###### ### ###
4 * ## ## ## ## ## ## ## ### ##
5 * ## ## ## ## #### #### ## # ##
6 * ## ## ## ## ## ## ## ##
7 * ## ## ## ## ## ## ## ##
8 * ##### ##### ## ###### ## ##
9 *
10 *
11 * OOFEM : Object Oriented Finite Element Code
12 *
13 * Copyright (C) 1993 - 2025 Borek Patzak
14 *
15 *
16 *
17 * Czech Technical University, Faculty of Civil Engineering,
18 * Department of Structural Mechanics, 166 29 Prague, Czech Republic
19 *
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Lesser General Public
22 * License as published by the Free Software Foundation; either
23 * version 2.1 of the License, or (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * Lesser General Public License for more details.
29 *
30 * You should have received a copy of the GNU Lesser General Public
31 * License along with this library; if not, write to the Free Software
32 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33 */
34
35#include "datastream.h"
36#include "error.h"
37#include <vector>
38
39namespace oofem
40{
41int DataStream :: read(std :: string &data)
42{
43 int n;
44 std :: vector< char >str;
45 if ( !this->read(& n, 1) ) {
46 data = "";
47 return 0;
48 }
49 str.resize(n);
50 if ( !this->read(str.data(), n) ) {
51 data = "";
52 return 0;
53 }
54 data = std::string(str.data(), n);
55 return 1;
56}
57
58int DataStream :: write(const std :: string &data)
59{
60 int n = ( int ) data.size();
61 this->write(& n, 1);
62 return this->write(data.data(), n);
63}
64
65FileDataStream :: FileDataStream(std::string filename, bool write):
66 stream(nullptr),
67 filename(std::move(filename))
68{
69 //stream.open(filename, (write ? std::ios::out : std::ios:in) | std::ios::binary );
70 this->stream = fopen(this->filename.c_str(), write ? "wb" : "rb" );
71 if ( !this->stream ) {
72 throw CantOpen(this->filename);
73 }
74}
75
76FileDataStream :: ~FileDataStream()
77{
78 fclose(this->stream);
79}
80
81int FileDataStream :: read(int *data, std::size_t count)
82{
83 return ( fread(data, sizeof( int ), count, stream) == count );
84 //this->stream.read(reinterpret_cast< char* >(data), sizeof(int)*count);
85 //return this->stream.good();
86}
87
88int FileDataStream :: read(unsigned long *data, std::size_t count)
90 return ( fread(data, sizeof( unsigned long ), count, stream) == count );
91}
92
93int FileDataStream :: read(long *data, std::size_t count)
94{
95 return ( fread(data, sizeof( long ), count, stream) == count );
96}
97
98#ifdef _WIN32
99int FileDataStream::read(std::size_t* data, std::size_t count)
100{
101 return (fread(data, sizeof(std::size_t), count, stream) == count);
102}
103#endif
104
105int FileDataStream :: read(double *data, std::size_t count)
106{
107 return ( fread(data, sizeof( double ), count, stream) == count );
108}
109
110int FileDataStream :: read(char *data, std::size_t count)
111{
112 return ( fread(data, sizeof( char ), count, stream) == count );
113}
114
115int FileDataStream :: read(bool &data)
116{
117 return ( fread(& data, sizeof( bool ), 1, stream) == 1 );
118}
119
120int FileDataStream :: write(const int *data, std::size_t count)
122 return ( fwrite(data, sizeof( int ), count, stream) == count );
123}
124
125int FileDataStream :: write(const unsigned long *data, std::size_t count)
126{
127 return ( fwrite(data, sizeof( unsigned long ), count, stream) == count );
128}
129
130#ifdef _WIN32
131int FileDataStream::write(const std::size_t* data, std::size_t count)
132{
133 return (fwrite(data, sizeof(std::size_t), count, stream) == count);
134}
135#endif
136
137int FileDataStream :: write(const long *data, std::size_t count)
138{
139 return ( fwrite(data, sizeof( long ), count, stream) == count );
140}
141
142int FileDataStream :: write(const double *data, std::size_t count)
143{
144 return ( fwrite(data, sizeof( double ), count, stream) == count );
145}
146
147int FileDataStream :: write(const char *data, std::size_t count)
148{
149 return ( fwrite(data, sizeof( char ), count, stream) == count );
150}
151
152int FileDataStream :: write(bool data)
153{
154 return ( fwrite(& data, sizeof( bool ), 1, stream) == 1 );
155}
156
157int FileDataStream :: givePackSizeOfInt(std::size_t count)
158{
159 return (int) (sizeof(int)*count);
160}
161
162int FileDataStream :: givePackSizeOfDouble(std::size_t count)
163{
164 return (int) (sizeof(double)*count);
165}
166
167int FileDataStream :: givePackSizeOfChar(std::size_t count)
168{
169 return (int) (sizeof(char)*count);
170}
171
172int FileDataStream :: givePackSizeOfBool(std::size_t count)
173{
174 return (int) (sizeof(bool)*count);
175}
176
177int FileDataStream :: givePackSizeOfLong(std::size_t count)
178{
179 return (int) (sizeof(long)*count);
180}
181
182int FileDataStream :: givePackSizeOfSizet(std::size_t count)
183{
184 return (int) (sizeof(std::size_t)*count);
185}
186
187}
virtual int read(int *data, std::size_t count)=0
Reads count integer values into array pointed by data.
virtual int write(const int *data, std::size_t count)=0
Writes count integer values from array pointed by data.
std::string filename
Filename.
Definition datastream.h:161
FILE * stream
FILE pointer of associated stream.
Definition datastream.h:159
int read(int *data, std::size_t count) override
Reads count integer values into array pointed by data.
Definition datastream.C:81
int write(const int *data, std::size_t count) override
Writes count integer values from array pointed by data.
Definition datastream.C:120

This page is part of the OOFEM-3.0 documentation. Copyright Copyright (C) 1994-2025 Borek Patzak Bořek Patzák
Project e-mail: oofem@fsv.cvut.cz
Generated at for OOFEM by doxygen 1.15.0 written by Dimitri van Heesch, © 1997-2011