bool Foam::fileFormats::OFSsurfaceFormat<Face>::read
(
    Istream& is,
    MeshedSurface<Face>& surf
)
{
    surf.clear();

    if (!is.good())
    {
        FatalErrorIn
        (
            "fileFormats::OFSsurfaceFormat::read"
            "(Istream&, MeshedSurface<Face>&)"
        )
            << "read error "
            << exit(FatalError);
    }

    pointField pointLst;
    List<Face> faceLst;
    List<surfZone> zoneLst;

    read(is, pointLst, faceLst, zoneLst);

    surf.reset
    (
        xferMove(pointLst),
        xferMove(faceLst),
        xferMove(zoneLst)
    );

    return true;
}
bool Foam::fileFormats::OFSsurfaceFormat<Face>::read
(
    Istream& is,
    pointField& pointLst,
    List<Face>& faceLst,
    List<surfZone>& zoneLst
)
{
    if (!is.good())
    {
        FatalErrorIn
        (
            "fileFormats::OFSsurfaceFormat::read"
            "(Istream&, pointField&, List<Face>&, List<surfZone>&)"
        )
            << "read error "
            << exit(FatalError);
    }

    // read surfZones:
    is >> zoneLst;

    // read points:
    is >> pointLst;

    // must triangulate?
    if (MeshedSurface<Face>::isTri())
    {
        // read faces as 'face' and transcribe to 'triFace'
        List<face> origFaces(is);

        MeshedSurface<face> origSurf
        (
            xferMove(pointLst),
            xferMove(origFaces),
            xferMove(zoneLst)
        );

        MeshedSurface<Face> surf;
        surf.transcribe(origSurf);
    }
    else
    {
        // read faces directly
        is >> faceLst;
    }

    return true;
}
Example #3
0
Foam::scalarRanges::scalarRanges(Istream& is)
:
    List<scalarRange>(0)
{
    DynamicList<scalarRange> lst;

    while (is.good())
    {
        scalarRange sr(is);
        if (sr.isDefined())
        {
            lst.append(sr);
        }
    }

    transfer(lst);
}
bool Foam::IOobject::readHeader(Istream& is)
{
    if (IOobject::debug)
    {
        Info<< "IOobject::readHeader(Istream&) : reading header for file "
            << is.name() << endl;
    }

    // Check Istream not already bad
    if (!is.good())
    {
        if (rOpt_ == MUST_READ || rOpt_ == MUST_READ_IF_MODIFIED)
        {
            FatalIOErrorIn("IOobject::readHeader(Istream&)", is)
                << " stream not open for reading essential object from file "
                << is.name()
                << exit(FatalIOError);
        }

        if (IOobject::debug)
        {
            SeriousIOErrorIn("IOobject::readHeader(Istream&)", is)
                << " stream not open for reading from file "
                << is.name() << endl;
        }

        return false;
    }

    token firstToken(is);

    if
    (
        is.good()
     && firstToken.isWord()
     && firstToken.wordToken() == "FoamFile"
    )
    {
        dictionary headerDict(is);

        is.version(headerDict.lookup("version"));
        is.format(headerDict.lookup("format"));
        headerClassName_ = word(headerDict.lookup("class"));

        const word headerObject(headerDict.lookup("object"));
        if (IOobject::debug && headerObject != name())
        {
            IOWarningIn("IOobject::readHeader(Istream&)", is)
                << " object renamed from "
                << name() << " to " << headerObject
                << " for file " << is.name() << endl;
        }

        // The note entry is optional
        headerDict.readIfPresent("note", note_);
    }
    else
    {
        SeriousIOErrorIn("IOobject::readHeader(Istream&)", is)
            << "First token could not be read or is not the keyword 'FoamFile'"
            << nl << nl << "Check header is of the form:" << nl << endl;

        writeHeader(Info);

        return false;
    }

    // Check stream is still OK
    if (is.good())
    {
        objState_ = GOOD;
    }
    else
    {
        if (rOpt_ == MUST_READ || rOpt_ == MUST_READ_IF_MODIFIED)
        {
            FatalIOErrorIn("IOobject::readHeader(Istream&)", is)
                << " stream failure while reading header"
                << " on line " << is.lineNumber()
                << " of file " << is.name()
                << " for essential object" << name()
                << exit(FatalIOError);
        }

        if (IOobject::debug)
        {
            Info<< "IOobject::readHeader(Istream&) :"
                << " stream failure while reading header"
                << " on line " << is.lineNumber()
                << " of file " << is.name() << endl;
        }

        objState_ = BAD;

        return false;
    }

    if (IOobject::debug)
    {
        Info<< " .... read" << endl;
    }

    return true;
}
Example #5
0
void test_ok (const charT*, const Traits*,
              const char *cname, const char *tname)
{
    typedef std::basic_istream<charT, Traits>   Istream;
    typedef typename Istream::sentry            Sentry;

    memfun_info (__LINE__, cname, tname, "operator bool () const");

    const charT cbuf[] = { 'a', 'b', 'c', 'd', 'e', ' ', 'f', '\0' };

    const std::ios_base::iostate states[] = {
        std::ios_base::badbit,
        std::ios_base::eofbit,
        std::ios_base::failbit,
        std::ios_base::goodbit,
        std::ios_base::badbit | std::ios_base::eofbit,
        std::ios_base::badbit | std::ios_base::failbit,
        std::ios_base::eofbit | std::ios_base::failbit,
        std::ios_base::badbit | std::ios_base::eofbit | std::ios_base::failbit
    };

    //////////////////////////////////////////////////////////////
    // exercise 27.6.1.1.2, p5:
    //     -  is.good() is true
    //     -  noskipws is zero
    //     -  is.flags() & ios_base::skipws
    //     -  the function extracts and discards each character as long
    //        as the next available input character c is a whitespace
    //        character
    //     =  if, after any preparation is completed, is.good() is true,
    //        ok_ != false otherwise, ok_ == false.

    unsigned iter = 0;     // iteration counter

    for (unsigned i = 0; i != sizeof states / sizeof *states; ++i) {
        for (unsigned j = 0; j != 2; ++j /* noskipws */) {
            for (unsigned k = 0; k != 2; ++k /* ios_base::skipws */) {
                for (charT wc = charT ('a'); wc != charT ('c'); ++wc) {

                    const Ctype<charT> ctp (1, wc);

                    Streambuf<charT, Traits>
                        sb (cbuf, cbuf + sizeof cbuf / sizeof *cbuf);

                    Istream is (&sb);

                    is.setstate (states [i]);

                    if (k)
                        is.setf (std::ios_base::skipws);
                    else
                        is.unsetf (std::ios_base::skipws);

                    const std::locale loc = 
                        is.imbue (std::locale (is.getloc (), &ctp));

                    // imbue the previous locale into the stream
                    // buffer to verify that the sentry ctor uses
                    // the locale imbued in the stream object and
                    // not the one in the stream buffer
                    sb.pubimbue (loc);

                    const Sentry guard (is, 0 != j);

                    _RWSTD_UNUSED (guard);

                    const bool success =
                           is.good () && guard
                        || !is.good () && !guard;

                    rw_assert (success, 0, __LINE__,
                               "%u. %{$SENTRY}"
                               "(%{$ISTREAM} &is, bool noskipws "
                               "= %d).operator bool() == %d; initial "
                               "is.state() = %{Is}, is.flags() & "
                               "ios::skipws = %d",
                               iter, j, is.good (),
                               states [i], k);

                    ++iter;
                }
            }
        }
    }
}
Example #6
0
void test_ctor (const charT*, const Traits*,
                const char *cname, const char *tname)
{
    typedef std::basic_istream<charT, Traits>   Istream;
    typedef typename Istream::sentry            Sentry;

    memfun_info (__LINE__, cname, tname, "sentry (%{$ISTREAM}&, bool)");

    const charT cbuf[] = { 'a', 'b', 'c', 'd', 'e', ' ', 'f', '\0' };

    const std::ios_base::iostate states[] = {
        std::ios_base::badbit,
        std::ios_base::eofbit,
        std::ios_base::failbit,
        std::ios_base::goodbit,
        std::ios_base::badbit | std::ios_base::eofbit,
        std::ios_base::badbit | std::ios_base::failbit,
        std::ios_base::eofbit | std::ios_base::failbit,
        std::ios_base::badbit | std::ios_base::eofbit | std::ios_base::failbit
    };

    //////////////////////////////////////////////////////////////
    // exercise 27.6.1.1.2, p1:
    //     -  is.good() is true
    //     -  is.tie() is not null
    //     =  the function calls is.tie().flush()

    unsigned iter = 0;     // iteration counter

    for (unsigned i = 0; i != sizeof states / sizeof *states; ++i) {
        for (unsigned j = 0; j != 2; ++j /* noskipws */) {
            Streambuf<charT, Traits>
                sb (cbuf, cbuf + sizeof cbuf / sizeof *cbuf);

            Istream is (&sb);

            // flush() is called iff
            // all of the following conditions hold
            const bool flush_called = is.good () && 0 != is.tie ();

            const Sentry guard (is, 0 != j);

            _RWSTD_UNUSED (guard);

            rw_assert (flush_called == sb.nsyncs_, 0, __LINE__,
                       "%u. basic_istream<%s, %s>::sentry::sentry"
                       "(basic_istream &is, bool noskipws = %d); "
                       "expected to call is.flush () %d times, got %d"
                       "initial is.state () = %{Is}, is.flags() & "
                       "ios::skipws = %d",
                       iter, cname, tname, 0 != j, flush_called, sb.nsyncs_,
                       states [i], is.flags () & std::ios_base::skipws);

            ++iter;
        }
    }

    //////////////////////////////////////////////////////////////
    // exercise 27.6.1.1.2, p1:
    //     -  is.good() is true
    //     -  noskipws is zero
    //     -  is.flags() & ios_base::skipws
    //     =  the function extracts and discards each character as long
    //        as the next available input character c is a whitespace
    //        character.

    for (unsigned i = 0; i != sizeof states / sizeof *states; ++i) {
        for (unsigned j = 0; j != 2; ++j /* noskipws */) {
            for (unsigned k = 0; k != 2; ++k /* ios_base::skipws */) {
                for (charT wc = charT ('a'); wc != charT ('c'); ++wc) {

                    const Ctype<charT> ctp (1, wc);

                    Streambuf<charT, Traits>
                        sb (cbuf, cbuf + sizeof cbuf / sizeof *cbuf);

                    Istream is (&sb);

                    is.setstate (states [i]);

                    if (k)
                        is.setf (std::ios_base::skipws);
                    else
                        is.unsetf (std::ios_base::skipws);

                    const std::locale loc = 
                        is.imbue (std::locale (is.getloc (), &ctp));

                    // imbue the previous locale into the stream
                    // buffer to verify that the sentry ctor uses
                    // the locale imbued in the stream object and
                    // not the one in the stream buffer
                    sb.pubimbue (loc);

                    // a whitespace character is extracted iff
                    // all of the following conditions hold
                    const bool extract =
                           is.good ()
                        && 0 == j
                        && is.flags () & std::ios_base::skipws
                        && cbuf [0] == wc;

                    const Sentry guard (is, 0 != j);

                    _RWSTD_UNUSED (guard);

                    rw_assert (cbuf + extract == sb.pubgptr (), 0, __LINE__, 
                               "%u. %{$SENTRY}::sentry"
                               "(%{$ISTREAM} &is, bool noskipws "
                               "= %b); expected to extract %d "
                               "whitespace chars ('%c') from %{*Ac}, "
                               "extracted %u; initial is.state () = "
                               "%{Is}, is.flags() & ios::skipws = %d",
                               iter, j, extract + 0, char (wc),
                               int (sizeof (*cbuf)), cbuf,
                               sb.pubgptr () - sb.pubeback (),
                               states [i], k);

                    // verify that the ctor doesn't affect gcount()
                    rw_assert (0 == is.gcount (), 0, __LINE__, 
                               "%u. %{$SENTRY}::sentry"
                               "(%{$ISTREAM} &is = %{*Ac}, bool noskipws "
                               "= %b); changed is.gcount() from 0 to %i",
                               iter, int (sizeof (*cbuf)), cbuf, j,
                               is.gcount ());

                    ++iter;
                }
            }
        }
    }
}
Example #7
0
void test_readsome (const charT *cbuf, const Traits*,
                    unsigned cbuf_size,
                    unsigned i,   // index into states
                    unsigned j,   // number of chars to read
                    unsigned k,   // when underflow() will fail
                    int      l,   // value returned from showmanyc()
                    unsigned m)   // how underflow should fail()
{
    typedef std::basic_istream<charT, Traits> Istream;

    static unsigned iter = 0;   // iteration counter

    // construct a stream buffer object and initialize its read
    // sequence with the character buffer
    Streambuf<charT, Traits> sb (cbuf, cbuf + cbuf_size - 1);

    sb.showmanyc_ = l;

    const char* err_type = 0;

    if (m < 1) {
        // have the stream buffer object's underflow() fail (by
        // throwing an exception if possible) after k characters
        // have been extracted (this object calls underflow() for
        // every char)
        sb.throw_after_ = k;
        err_type        = "threw";
    }
    else {
        // have the stream buffer object's underflow() fail by
        // returning eof after k characters have been extracted
        // (this object calls underflow() for every char)
        sb.fail_after_ = k;
        err_type       = "returned EOF";
    }

    // construct an istream object and initialize it with the user
    // defined streambuf object
    Istream is (&sb);

    // set the stream object's initial state
    is.setstate (states [i]);

    // the number of extracted whitespace characters expected to
    // be reported by gcount() must equal the number of successful
    // extractions computed by the the stream buffer overridden
    // underflow member function
    const int extract =
        is.good () ? j < k ? int (j) < l ? j
                                         : l < 0 ? 0 : l
                           : int (k) < l ? k
                                         : l < 0 ? 0 : l
                   : 0;

    charT buf [256] = { 0 };

    std::streamsize nread = -1;

    // format the FCALL environment variable so that it can be
    // conveniently used in diagnostics below
    rw_fprintf (0, "%u. %{$FCALL!:@}",
                iter,
                "%{$ISTREAM}(%{*Ac}).readsome(%p, %d)",
                int (sizeof *cbuf), cbuf, buf, j);

#ifndef _RWSTD_NO_EXCEPTIONS

    const char *caught = 0;

    // on every other iteration set badbit in exceptions to check
    // that the thrown object is propagated by the function
    if (k % 2 && !is.bad ())
        is.exceptions (std::ios_base::badbit);

    try {
        nread = is.readsome (buf, j);
    }
    catch (const char *s) {
        caught = s;
    }
    catch (...) {

        caught = "...";

        rw_assert (0, 0, __LINE__,
                   "%{$FCALL} threw an exception of unexpected type");
    }

    //////////////////////////////////////////////////////////////////
    // verify that the function propagates exceptions thrown from the
    // streambuf object only when badbit is set in the stream object's
    // exceptions()

    rw_assert (!caught || (k % 2), 0, __LINE__,
               "%{$FCALL} unexpectedly propagated an exception");

#else   // if defined (_RWSTD_NO_EXCEPTIONS)

    nread = is.readsome (buf, j);

#endif   // _RWSTD_NO_EXCEPTIONS

    //////////////////////////////////////////////////////////////////
    // verify that the function returned the expected number of
    // extracted characters

    const std::streamsize extracted = sb.pubgptr () - sb.pubeback ();

    rw_assert (extract == extracted, 0, __LINE__,
               "%{$FCALL} expected to extract %d chars, got %u; "
               "initial state = %{Is}, underflow %s at extraction %u",
               extract, extracted, states [i],
               err_type, k);

    //////////////////////////////////////////////////////////////////
    // verify that the expected number of characters have been
    // extracted from the stream

    rw_assert (cbuf + extract == sb.pubgptr (), 0, __LINE__,
               "%{$FCALL} expected to extract %d chars, got %u; "
               "initial state = %{Is}, underflow %s at extraction %u",
               extract, extracted, states [i],
               err_type, k);

    //////////////////////////////////////////////////////////////////
    // verify that the extracted characters match those in the buffer

    rw_assert (0 == std::char_traits<charT>::compare (buf, cbuf, extract),
               0, __LINE__,
               "%{$FCALL} expected to extract the first %d chars, got %{*Ac}",
               extract, int (sizeof *buf), buf);

    //////////////////////////////////////////////////////////////////
    // verify that gcount() correctly reflects the number of
    // characters successfully extracted from the stream

    rw_assert (extract == is.gcount (), 0, __LINE__,
               "%{$FCALL}: gcount() == %d, got %d; initial state = %{Is}, "
               "underflow %s at extraction %u",
               extract, is.gcount (), states [i],
               err_type, k);

    //////////////////////////////////////////////////////////////////
    // verify the state of the stream object after the function call

    // expected stream state after the function call is unchanged
    // (i.e., the initial stream state), except...
    std::ios_base::iostate expect_state = states [i];

    if (!states [i]) {

#ifndef _RWSTD_NO_EXCEPTIONS

        // ...if an extraction is attempted, or even if the first
        // character on the stream is peeked at, and an exception
        // is thrown during input, badbit should be set, otherwise
        // if in_avail() returned -1, eofbit should be set, else
        // the state should be good
        if (-2 == l)
            expect_state = std::ios_base::badbit;
        else if (l < 0)
            expect_state = std::ios_base::eofbit;
        else
            expect_state = std::ios_base::goodbit;

#else   // if defined (_RWSTD_NO_EXCEPTIONS)

        if (l < 0)
            expect_state = std::ios_base::eofbit;
        else
            expect_state = std::ios_base::goodbit;

#endif   // _RWSTD_NO_EXCEPTIONS

    }
    else {

        // ...if the initial stream state is not good, failbit
        // must be set
        expect_state = states [i] | std::ios_base::failbit;
    }

    rw_assert (is.rdstate () == expect_state, 0, __LINE__,
               "%{$FCALL}: rdstate() == %{Is}, got %{Is}; "
               "extracted %u characters; "
               "initial state = %{Is}, underflow %s at extraction %u",
               expect_state, is.rdstate (), extracted,
               states [i], err_type, k);

    ++iter;
}