int64IO.C 3.52 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2017-2019 OpenCFD Ltd.
OpenFOAM bot's avatar
OpenFOAM bot committed
6
7
8
     \\/     M anipulation  |
-------------------------------------------------------------------------------
                            | Copyright (C) 2014-2016 OpenFOAM Foundation
9
10
11
12
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

13
14
15
16
    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
17
18
19
20
21
22
23

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
24
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
25
26
27

\*---------------------------------------------------------------------------*/

28
#include "int64.H"
29
30
#include "error.H"
#include "parsing.H"
31
#include "IOstreams.H"
32
#include <cinttypes>
33

34
// * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
35

36
int64_t Foam::readInt64(const char* buf)
37
{
38
39
40
41
42
43
    char *endptr = nullptr;
    errno = 0;
    const intmax_t parsed = ::strtoimax(buf, &endptr, 10);

    const int64_t val = int64_t(parsed);

44
45
46
47
48
49
    const parsing::errorType err =
    (
        (parsed < INT64_MIN || parsed > INT64_MAX)
      ? parsing::errorType::RANGE
      : parsing::checkConversion(buf, endptr)
    );
50
51
52
53
54
55
56
57
58

    if (err != parsing::errorType::NONE)
    {
        FatalIOErrorInFunction("unknown")
            << parsing::errorNames[err] << " '" << buf << "'"
            << exit(FatalIOError);
    }

    return val;
59
60
61
}


62
bool Foam::readInt64(const char* buf, int64_t& val)
63
{
64
65
66
    char *endptr = nullptr;
    errno = 0;
    const intmax_t parsed = ::strtoimax(buf, &endptr, 10);
67

68
69
    val = int64_t(parsed);

70
71
72
73
74
75
    return
    (
        (parsed < INT64_MIN || parsed > INT64_MAX)
      ? false
      : (parsing::checkConversion(buf, endptr) == parsing::errorType::NONE)
    );
76
}
77

78

79
Foam::Istream& Foam::operator>>(Istream& is, int64_t& val)
80
81
82
83
84
{
    token t(is);

    if (!t.good())
    {
85
86
87
        FatalIOErrorInFunction(is)
            << "Bad token - could not get int64"
            << exit(FatalIOError);
88
89
90
91
92
93
        is.setBad();
        return is;
    }

    if (t.isLabel())
    {
94
        val = int64_t(t.labelToken());
95
96
97
    }
    else
    {
98
        FatalIOErrorInFunction(is)
99
100
            << "Wrong token type - expected label (int64), found "
            << t.info()
101
            << exit(FatalIOError);
102
        is.setBad();
103
104
105
        return is;
    }

106
    is.check(FUNCTION_NAME);
107
108
109
110
    return is;
}


111
int64_t Foam::readInt64(Istream& is)
112
{
113
    int64_t val(0);
Mark Olesen's avatar
Mark Olesen committed
114
    is >> val;
115

Mark Olesen's avatar
Mark Olesen committed
116
    return val;
117
118
119
}


120
Foam::Ostream& Foam::operator<<(Ostream& os, const int64_t val)
121
{
122
    os.write(label(val));
123
    os.check(FUNCTION_NAME);
124
125
126
127
    return os;
}


128
#if defined(__APPLE__)
129
130
131
132
133
Foam::Istream& Foam::operator>>(Istream& is, long& val)
{
    return operator>>(is, reinterpret_cast<int64_t&>(val));
}

134
135
Foam::Ostream& Foam::operator<<(Ostream& os, const long val)
{
136
    return (os << int64_t(val));
137
138
139
140
}
#endif


141
// ************************************************************************* //