Loading...
Searching...
No Matches
GenericIO.cpp
1/*---------------------------------------------------------------------------*\
2 *
3 * bitpit
4 *
5 * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6 *
7 * -------------------------------------------------------------------------
8 * License
9 * This file is part of bitpit.
10 *
11 * bitpit is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License v3 (LGPL)
13 * as published by the Free Software Foundation.
14 *
15 * bitpit is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with bitpit. If not, see <http://www.gnu.org/licenses/>.
22 *
23\*---------------------------------------------------------------------------*/
24
25#include "GenericIO.hpp"
26
27namespace bitpit {
28
29namespace genericIO {
30
36template<>
37void flushASCII(std::fstream &str, const uint8_t &data)
38{
39 std::ios::fmtflags streamFlags(str.flags());
40
41 str << std::setprecision(8) << std::scientific;
42 str << unsigned(data) << " ";
43
44 str.flags(streamFlags);
45}
46
61void copyUntilEOFInString(std::fstream &str, char *&buffer, int &length)
62{
63 std::fstream::pos_type position_insert, position_eof;
64
65 // Ask for actual position and end-of-file positions
66 position_insert = str.tellg();
67 str.seekg(0, std::ios::end);
68 position_eof = str.tellg();
69
70 // Evaluate the length on file. This is a prediction of the maximum
71 // number of charactes readable in the current file slot.
72 length = static_cast<int>(position_eof - position_insert);
73
74 // Instantiate a char vector for reading purposes
75 std::vector<char> trybuf(length);
76
77 // Clear and get again the stream to the initial insert position.
78 str.clear();
79 str.seekg(position_insert);
80
81 // Read data
82 str.read(trybuf.data(), length);
83
84 // Resize the trybuf with the effective valid character read.
85 //
86 // This number can be less than length, especially while reading OS-Windows
87 // files.
88 trybuf.resize(str.gcount());
89 length = trybuf.size();
90
91 // Allocate your real buffer and copy effective data in it.
92 buffer = new char[trybuf.size()];
93 std::copy(trybuf.begin(), trybuf.end(), buffer);
94
95 // Clear the stream and get the read stream input to the initial position.
96 str.clear();
97 str.seekg(position_insert);
98}
99
100}
101
102}
void copyUntilEOFInString(std::fstream &str, char *&buffer, int &length)
Definition GenericIO.cpp:61
void flushASCII(std::fstream &str, const uint8_t &data)
Definition GenericIO.cpp:37
--- layout: doxygen_footer ---