OOFEM  2.4
OOFEM.org - Object Oriented Finite Element Solver
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 - 2013 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 
39 namespace oofem
40 {
41 int 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(), str.size());
55  return 1;
56 }
57 
58 int 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 
65 FileDataStream :: 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 
77 {
78  fclose(this->stream);
79 }
80 
81 int FileDataStream :: read(int *data, int count)
82 {
83  return ( (int)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 
88 int FileDataStream :: read(unsigned long *data, int count)
89 {
90  return ( (int)fread(data, sizeof( unsigned long ), count, stream) == count );
91 }
92 
93 int FileDataStream :: read(long *data, int count)
94 {
95  return ( (int)fread(data, sizeof( long ), count, stream) == count );
96 }
97 
98 int FileDataStream :: read(double *data, int count)
99 {
100  return ( (int)fread(data, sizeof( double ), count, stream) == count );
101 }
102 
103 int FileDataStream :: read(char *data, int count)
104 {
105  return ( (int)fread(data, sizeof( char ), count, stream) == count );
106 }
107 
108 int FileDataStream :: read(bool &data)
109 {
110  return ( (int)fread(& data, sizeof( bool ), 1, stream) == 1 );
111 }
112 
113 int FileDataStream :: write(const int *data, int count)
114 {
115  return ( (int)fwrite(data, sizeof( int ), count, stream) == count );
116 }
117 
118 int FileDataStream :: write(const unsigned long *data, int count)
119 {
120  return ( (int)fwrite(data, sizeof( unsigned long ), count, stream) == count );
121 }
122 
123 int FileDataStream :: write(const long *data, int count)
124 {
125  return ( (int)fwrite(data, sizeof( long ), count, stream) == count );
126 }
127 
128 int FileDataStream :: write(const double *data, int count)
129 {
130  return ( (int)fwrite(data, sizeof( double ), count, stream) == count );
131 }
132 
133 int FileDataStream :: write(const char *data, int count)
134 {
135  return ( (int)fwrite(data, sizeof( char ), count, stream) == count );
136 }
137 
139 {
140  return ( (int)fwrite(& data, sizeof( bool ), 1, stream) == 1 );
141 }
142 
144 {
145  return sizeof(int)*count;
146 }
147 
149 {
150  return sizeof(double)*count;
151 }
152 
154 {
155  return sizeof(char)*count;
156 }
157 
159 {
160  return sizeof(bool)*count;
161 }
162 
164 {
165  return sizeof(int)*count;
166 }
167 
168 }
virtual int read(int *data, int count)
Reads count integer values into array pointed by data.
Definition: datastream.C:81
virtual int write(const int *data, int count)
Writes count integer values from array pointed by data.
Definition: datastream.C:113
virtual ~FileDataStream()
Destructor (will not close stream!)
Definition: datastream.C:76
virtual int givePackSizeOfInt(int count)
Definition: datastream.C:143
virtual int read(int *data, int count)=0
Reads count integer values into array pointed by data.
virtual int givePackSizeOfLong(int count)
Definition: datastream.C:163
virtual int write(const int *data, int count)=0
Writes count integer values from array pointed by data.
FileDataStream(std::string filename, bool write)
Constructor, takes associated stream pointer as parameter.
Definition: datastream.C:65
virtual int givePackSizeOfBool(int count)
Definition: datastream.C:158
virtual int givePackSizeOfDouble(int count)
Definition: datastream.C:148
virtual int givePackSizeOfChar(int count)
Definition: datastream.C:153
the oofem namespace is to define a context or scope in which all oofem names are defined.
FILE * stream
FILE pointer of associated stream.
Definition: datastream.h:148

This page is part of the OOFEM documentation. Copyright (c) 2011 Borek Patzak
Project e-mail: info@oofem.org
Generated at Tue Jan 2 2018 20:07:28 for OOFEM by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2011