LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
readwritetools.cpp
Go to the documentation of this file.
1// Copyright 2002-2020 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6/************************************************************************
7 *
8 * @file readwritetools.cpp
9 * @author Besancon Romaric (besanconr@zoe.cea.fr)
10 * @date Fri Nov 7 2003
11 * @version $Id$
12 * copyright Copyright (C) 2003-2020 by CEA LIST
13 *
14 ***********************************************************************/
15
16#include "readwritetools.h"
17#include <deque>
18#include <QDebug>
19#include <QFile>
20
21namespace Lima {
22namespace Common {
23namespace Misc {
24
25uint64_t readCodedInt(std::istream& file) {
26 char c = 0x1;
27 uint64_t val = 0;
28
29 while((c & 0x1) && file.good()) {
30 file.read((char*)&c, sizeof(char));
31 val = (val << 7) + ((c >> 1) & 0x7F);
32 }
33 return(val);
34}
35
36void writeCodedInt(std::ostream& file, const uint64_t number)
37{
38 //std::cerr << "writeCodedInt: input, file at: " << file.tellp() << std::endl;
39 std::deque<char> chars;
40 uint64_t n(number);
41
42 do
43 {
44 char c = (n & 0x7F) << 1;
45 chars.push_back(c);
46 n >>= 7;
47 } while (n);
48
49 //std::cerr << "writeCodedInt: nb chars to write: " << chars.size() << std::endl;
50 for (std::deque<char>::size_type i=chars.size()-1;i>0;i--)
51 {
52 chars[i] |= 0x1;
53 //std::cerr << "writeCodedInt: in for writing nb chars: " << sizeof(char) << std::endl;
54 file.put(chars[i]);
55 file.flush();
56 }
57 //std::cerr << "writeCodedInt: writing nb chars: " << sizeof(char) << std::endl;
58 file.put(chars[0]);
59 file.flush();
60
61 //std::cerr << "writeCodedInt: output, file at: " << file.tellp() << std::endl;
62}
63
64//**********************************************************************
65uint8_t readOneByteInt(std::istream& file) {
66 uint8_t c;
67 file.read((char*)&c, 1);
68 file.peek(); // Try to read next byte to force update end-of-file flag on windows
69 return c;
70}
71
72void writeOneByteInt(std::ostream& file, const uint8_t number) {
73 file.put((char)number);
74 file.flush();
75}
76
77} // end namespace
78} // end namespace
79} // end namespace
uint8_t readOneByteInt(std::istream &file)
read a integer coded on one byte in a file
uint64_t readCodedInt(std::istream &file)
read a integer coded in variable-byte format in a file
void writeOneByteInt(std::ostream &file, const uint8_t number)
write a integer coded on one byte format in a file
void writeCodedInt(std::ostream &file, const uint64_t number)
write a integer coded in variable-byte format in a file
NAUTITIA.