Boost logo

Boost :

From: Takeshi Mouri (takeshi.mouri.net_at_[hidden])
Date: 2006-01-22 03:26:01


Hello,

On Windows platform,
I noticed that how to treat boost::file_descriptor and
std::fstream's openmode was different.

Here's the test code:
(I tested on VC++8.0 with Boost 1.33.1)

#include <boost/test/unit_test.hpp>
#include <boost/iostreams/detail/ios.hpp>
#include <windows.h>

DWORD make_create_flags(BOOST_IOS::openmode m)
{
    // copy from file_descriptor::open()

    DWORD dwDesiredAccess;
    DWORD dwCreationDisposition;
    if ( (m & (BOOST_IOS::in | BOOST_IOS::out))
             ==
         (BOOST_IOS::in | BOOST_IOS::out) )
    {
        assert(!(m & BOOST_IOS::app));
        dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
        dwCreationDisposition =
            (m & BOOST_IOS::trunc) ?
#ifndef FIX
                OPEN_ALWAYS :
#else
                CREATE_ALWAYS :
#endif
                OPEN_EXISTING;
    } else if (m & BOOST_IOS::in) {
        assert(!(m & (BOOST_IOS::app |BOOST_IOS::trunc)));
        dwDesiredAccess = GENERIC_READ;
        dwCreationDisposition = OPEN_EXISTING;
    } else if (m & BOOST_IOS::out) {
        dwDesiredAccess = GENERIC_WRITE;
#ifndef FIX
        dwCreationDisposition = OPEN_ALWAYS;
#else
        dwCreationDisposition =
            (m & BOOST_IOS::app) ?
                OPEN_ALWAYS :
                CREATE_ALWAYS;
#endif
    }

    return dwCreationDisposition;
}

struct test : BOOST_IOS
{
  static void run()
  {
    BOOST_CHECK_EQUAL(make_create_flags(out), CREATE_ALWAYS);
    BOOST_CHECK_EQUAL(make_create_flags(out|app), OPEN_ALWAYS);
    BOOST_CHECK_EQUAL(make_create_flags(out|trunc), CREATE_ALWAYS);
    BOOST_CHECK_EQUAL(make_create_flags(in), OPEN_EXISTING);
    BOOST_CHECK_EQUAL(make_create_flags(in|out), OPEN_EXISTING);
    BOOST_CHECK_EQUAL(make_create_flags(in|out|trunc), CREATE_ALWAYS);

    // binary modes are omitted...
  }
};

using boost::unit_test::test_suite;
test_suite* init_unit_test_suite(int, char*[])
{
    test_suite* test = BOOST_TEST_SUITE("openmode test");
    test->add(BOOST_TEST_CASE(&test::run));
    return test;
}

These test cases are the one having derived it
from the standard and some implementation of std::fstream.
If a macro "FIX" is defined, all test cases are passed.

Regards,
Takeshi Mouri


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk