librostlab  1.0.20
 All Classes Namespaces Files Functions Variables Typedefs
rostlab_stdio.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 Laszlo Kajan, Technical University of Munich, Germany
3 
4  This file is part of librostlab.
5 
6  librostlab is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #ifndef ROSTLAB_STDIO
20 #define ROSTLAB_STDIO 1
21 
22 #include <stdint.h>
23 #include <stdio.h>
24 
25 namespace rostlab {
26 
27 template<typename _Tp>
28 inline void fwrite( const _Tp& __v, FILE* __out ){ if( ::fwrite( &__v, sizeof(__v), 1, __out ) != 1 ) throw rostlab::runtime_error( strerror(errno) ); }
29 template<typename _Tp, typename _Alloc>
30 inline void fwrite( const vector<_Tp,_Alloc>& __v, FILE* __out )
31 {
32  if( __v.size() > 0xffff ){ ostringstream s; s << __v.size() << " is out of range (0-65535)"; throw rostlab::range_error( s.str() ); }
33  fwrite<uint16_t>( __v.size(), __out );
34  if( ::fwrite( __v.data(), sizeof(typename vector<_Tp,_Alloc>::value_type), __v.size(), __out ) != __v.size() ) throw rostlab::runtime_error( strerror(errno) );
35 }
36 template<>
37 inline void fwrite<string>( const string& __str, FILE* __out )
38 {
39  fwrite<uint16_t>( __str.length(), __out );
40  if( !__str.length() ) return;
41  if( ::fwrite( __str.c_str(), sizeof( string::value_type ), __str.length(), __out ) != __str.length() ) throw rostlab::runtime_error( strerror(errno) );
42 }
43 inline void fwrite( const char* __c, FILE* __out ) { fwrite( string(__c), __out ); }
44 
45 template<typename _Tp>
46 inline void fread( _Tp& __v, FILE* __in )
47 {
48  if( ::fread( &__v, sizeof(__v), 1, __in ) != 1 ) throw rostlab::runtime_error( strerror(errno) );
49 }
50 template<typename _Tp, typename _Alloc>
51 inline void fread( vector<_Tp, _Alloc>& __v, FILE* __in )
52 {
53  uint16_t size; fread( size, __in);
54  __v = vector<_Tp, _Alloc>(size);
55  if( ::fread( __v.data(), sizeof(typename vector<_Tp, _Alloc>::value_type), __v.size(), __in ) != __v.size() ) throw rostlab::runtime_error( strerror(errno) );
56 }
57 template<>
58 inline void fread<string>( string& __v, FILE* __in )
59 {
60  uint16_t strlen; fread( strlen, __in);
61  if( !strlen ){ __v = string(); return; }
62 
63  string::value_type buf[ sizeof( string::value_type ) * strlen ];
64  size_t managed_to_read = ::fread( buf, sizeof( string::value_type ), strlen, __in );
65  if( managed_to_read != strlen ) throw rostlab::runtime_error( strerror(errno) );
66  __v = string( buf, managed_to_read );
67 }
68 // This must come last - uses the others.
69 template<typename _Tp>
70 inline _Tp fread( FILE* __in ){ _Tp ret; fread( ret, __in ); return ret; }
71 
72 } // namespace rostlab
73 
74 #endif // ROSTLAB_STDIO
75 // vim:et:ts=4:ai: