mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
rawBinary.hpp
Go to the documentation of this file.
1/** \file rawBinary.hpp
2 * \author Jared R. Males (jaredmales@gmail.com)
3 * \brief Provides functions for working with raw binary files.
4 * \ingroup utils_files
5 *
6 */
7
8//***********************************************************************//
9// Copyright 2018 Jared R. Males (jaredmales@gmail.com)
10//
11// This file is part of mxlib.
12//
13// mxlib is free software: you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation, either version 3 of the License, or
16// (at your option) any later version.
17//
18// mxlib is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with mxlib. If not, see <http://www.gnu.org/licenses/>.
25//***********************************************************************//
26
27#ifndef rawBinary_hpp
28#define rawBinary_hpp
29
30#include <cstdio>
31#include <string>
32
33#include "../mxlib.hpp"
34
35namespace mx
36{
37namespace ioutils
38{
39
40/// Read an array of data from a file as raw binary.
41/** Here raw binary means no formatting or metadata.
42 *
43 * \ingroup ioutils
44 *
45 * \returns 0 on success
46 * \returns -1 on error
47 */
48template <typename T>
49int readRawBinary( T *data, ///< [out] the data pointer
50 size_t szData, ///< [in] number of elements of sizeof(T) to read
51 const std::string &fileName ///< [in] the file to read from
52)
53{
54 FILE *fout;
55
56 fout = fopen( fileName.c_str(), "rb" );
57 if( fout == 0 )
58 {
59 throw( mx::exception( errno2error_t( errno ), "Error from fopen [" + fileName + "]" ) );
60 }
61
62 int nrd = fread( data, sizeof( T ), szData, fout );
63
64 if( nrd != szData )
65 {
66 int en = errno; // get this before fclose
67 fclose( fout );
68 // Have to handle case where EOF reached but no error.
69 if( en != 0 )
70 {
71 throw( mx::exception( error_t::filererr, "Error from file [" + fileName + "]" ) );
72 }
73 else
74 {
76 "Error reading from file, did not read all elements. [" + fileName + "]" ) );
77 }
78 }
79
80 int res = fclose( fout );
81
82 if( res != 0 )
83 {
84 throw(mx::exception(errno2error_t(errno), "Error closing file [" + fileName + "]" ));
85 }
86
87 return 0;
88}
89
90/// Write an array of data to file as raw binary.
91/** Here raw binary means no formatting or metadata,
92 * just the bytes pointed to by the array are written to
93 * disk.
94 *
95 * \ingroup ioutils
96 *
97 * \returns 0 on success
98 * \returns -1 on error
99 */
100template <typename T>
101int writeRawBinary( const std::string &fileName, ///< [in] the file to write to
102 T *data, ///< [in] the data pointer
103 size_t szData ) ///< [in] number of elements of sizeof(T) to write
104{
105 FILE *fout;
106
107 fout = fopen( fileName.c_str(), "wb" );
108 if( fout == 0 )
109 {
110 throw( mx::exception( errno2error_t( errno ), "Error from fopen [" + fileName + "]" ) );
111 }
112
113 int nwr = fwrite( data, sizeof( T ), szData, fout );
114
115 if( nwr != szData )
116 {
117 int en = errno; // save before close call
118 fclose( fout );
119
120 // Have to handle case where EOF reached but no error.
121 if( en != 0 )
122 {
123 throw(mx::exception(error_t::filewerr, "Error writing to file [" + fileName + "]" ));
124 }
125 else
126 {
128 "Error writing to file, did not write all elements. [" + fileName + "]" ));
129 }
130 }
131
132 int res = fclose( fout );
133
134 if( res != 0 )
135 {
136 throw(mx::exception(error_t::filecerr,"Error closing file [" + fileName + "]" ));
137 }
138
139 return 0;
140}
141
142} // namespace ioutils
143} // namespace mx
144
145#endif // rawBinary_hpp
Augments an exception with the source file and line.
Definition exception.hpp:42
static constexpr error_t errno2error_t(const int &err)
Convert an errno code to error_t.
Definition error_t.hpp:2006
@ filererr
An error occurred while reading from a file.
@ filecerr
An error occurred while closing a file.
@ filewerr
An error occurred while writing to a file.
int writeRawBinary(const std::string &fileName, T *data, size_t szData)
Write an array of data to file as raw binary.
int readRawBinary(T *data, size_t szData, const std::string &fileName)
Read an array of data from a file as raw binary.
Definition rawBinary.hpp:49
The mxlib c++ namespace.
Definition mxlib.hpp:37