Merge branch 'master' of ssh://git.code.sf.net/p/foam-extend/foam-extend-3.2
[foam-extend-3.2.git] / src / foam / db / IOstreams / hashes / OSHA1stream.H
blob12cd2530c344014469a64c2d17bf4e6c0358128b
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation, either version 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 Class
25     Foam::OSHA1stream
27 Description
28     An output stream for calculating SHA1 digests.
30 SourceFiles
31     OSHA1stream.C
33 \*---------------------------------------------------------------------------*/
35 #ifndef OSHA1stream_H
36 #define OSHA1stream_H
38 #include "OSstream.H"
39 #include "SHA1.H"
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 namespace Foam
46 class osha1stream;
47 class OSHA1stream;
49 /*---------------------------------------------------------------------------*\
50                         Class sha1streambuf Declaration
51 \*---------------------------------------------------------------------------*/
53 //- A streambuf class for calculating SHA1 digests
54 class sha1streambuf
56     public std::streambuf
58     // Private data
60     //- This does all the work and has its own buffering
61     SHA1 sha1_;
63     friend class osha1stream;
65 public:
67     // Constructors
69         //- Construct null
70         sha1streambuf()
71         {}
73     // Member Functions
75     // Write
77         //- Process unbuffered
78         virtual std::streamsize xsputn(const char* str, std::streamsize n)
79         {
80             sha1_.append(str, n);
81             return n;
82         }
86 /*---------------------------------------------------------------------------*\
87                          Class osha1stream Declaration
88 \*---------------------------------------------------------------------------*/
90 //- A basic output stream for calculating SHA1 digests
91 class osha1stream
93     virtual public std::ios,
94     public std::ostream
96     // Private data
98     sha1streambuf sbuf_;
100 public:
102     // Constructors
104         //- Construct null
105         osha1stream()
106         :
107             std::ostream(&sbuf_)
108         {}
110     // Member Functions
112     // Access
114         //- This hides both signatures of std::basic_ios::rdbuf()
115         sha1streambuf* rdbuf()
116         {
117             return &sbuf_;
118         }
120         //- Full access to the sha1
121         SHA1& sha1()
122         {
123             return sbuf_.sha1_;
124         }
129 /*---------------------------------------------------------------------------*\
130                          Class OSHA1stream Declaration
131 \*---------------------------------------------------------------------------*/
133 //- The output stream for calculating SHA1 digests
134 class OSHA1stream
136     public OSstream
139     // Private Member Functions
141         //- Disallow default bitwise copy construct
142         OSHA1stream(const OSHA1stream&);
144         //- Disallow default bitwise assignment
145         void operator=(const OSHA1stream&);
147 public:
149     // Constructors
151         //- Construct and set stream status
152         OSHA1stream
153         (
154             streamFormat format = ASCII,
155             versionNumber version = currentVersion
156         )
157         :
158             OSstream
159             (
160                 *(new osha1stream),
161                 "OSHA1stream.sinkFile_",
162                 format,
163                 version
164             )
165         {}
168     // Destructor
170         ~OSHA1stream()
171         {
172             delete &dynamic_cast<osha1stream&>(stream());
173         }
176     // Member functions
178     // Access
180         //- Full access to the sha1
181         Foam::SHA1& sha1()
182         {
183             return dynamic_cast<osha1stream&>(stream()).sha1();
184         }
186         //- Return SHA1::Digest for the data processed until now
187         Foam::SHA1Digest digest()
188         {
189             return sha1().digest();
190         }
192     // Edit
194         //- Clear the SHA1 calculation
195         void rewind()
196         {
197             sha1().clear();
198         }
203 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
205 } // End namespace Foam
207 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
209 #endif
211 // ************************************************************************* //