<commit_before>/*
 * Copyright(c) Sophist Solutions, Inc. 1990-2013.  All rights reserved
 */
#include    "../../StroikaPreComp.h"

#if     qPlatform_Windows
#include    <windows.h>
#elif   qPlatform_POSIX
#include    <unistd.h>
#include    <sys/mman.h>
#endif

#include    "../../Execution/ErrNoException.h"
#include    "../../Execution/Exceptions.h"
#if     qPlatform_Windows
#include    "../../Execution/Platform/Windows/Exception.h"
#include    "../../Execution/Platform/Windows/HRESULTErrorException.h"
#endif
#include    "../../Debug/Trace.h"
#include    "../../IO/FileAccessException.h"
#include    "../../IO/FileBusyException.h"
#include    "../../IO/FileFormatException.h"
#include    "PathName.h"

#include    "MemoryMappedFileReader.h"


using   namespace   Stroika::Foundation;
using   namespace   Stroika::Foundation::Characters;
using   namespace   Stroika::Foundation::IO;
using   namespace   Stroika::Foundation::IO::FileSystem;
using   namespace   Stroika::Foundation::Memory;


#if     qPlatform_Windows
using   Execution::Platform::Windows::ThrowIfFalseGetLastError;
#endif




/*
 ********************************************************************************
 ***************************** MemoryMappedFileReader ***************************
 ********************************************************************************
 */
MemoryMappedFileReader::MemoryMappedFileReader (const String& fileName)
    : fFileDataStart_ (nullptr)
    , fFileDataEnd_ (nullptr)
#if         qPlatform_Windows
    , fFileHandle_ (INVALID_HANDLE_VALUE)
    , fFileMapping_ (INVALID_HANDLE_VALUE)
#endif
{
#if         qPlatform_POSIX
    int fd = 3;///tmphack....
    AssertNotImplemented (); // chcek results
    size_t fileLength = 3;//size of file - compute
    fFileDataStart_ = reinterpret_cast<const Byte*> (::mmap (nullptr, fileLength, PROT_READ, fd, 0));
    fFileDataEnd_ = fFileDataStart_ + fileLength;
    ::close (fd);//http://linux.die.net/man/2/mmap says dont need to keep FD open while mmapped
#elif       qPlatform_Windows
    try {
        // FILE_READ_DATA fails on WinME - generates ERROR_INVALID_PARAMETER - so use GENERIC_READ
        fFileHandle_ = ::CreateFile (fileName.AsTString ().c_str (), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
        ThrowIfFalseGetLastError (fFileHandle_ != INVALID_HANDLE_VALUE);
        DWORD   fileSize    =   ::GetFileSize (fFileHandle_, nullptr);
        if (fileSize != 0) {
            fFileMapping_ = ::CreateFileMapping (fFileHandle_, nullptr, PAGE_READONLY, 0, fileSize, 0);
            ThrowIfFalseGetLastError (fFileMapping_ != nullptr);
            fFileDataStart_ = reinterpret_cast<const Byte*> (::MapViewOfFile (fFileMapping_, FILE_MAP_READ, 0, 0, 0));
            ThrowIfFalseGetLastError (fFileDataStart_ != nullptr);
            fFileDataEnd_ = fFileDataStart_ + fileSize;
        }
    }
    catch (...) {
        if (fFileMapping_ != INVALID_HANDLE_VALUE) {
            ::CloseHandle (fFileMapping_);
        }
        if (fFileHandle_ != INVALID_HANDLE_VALUE) {
            ::CloseHandle (fFileHandle_);
        }
        Execution::DoReThrow ();
    }
#else
    AssertNotImplemented ();
#endif
}

MemoryMappedFileReader::~MemoryMappedFileReader ()
{
#if         qPlatform_POSIX
    int res = ::munmap (const_cast<Byte*> (fFileDataStart_), fFileDataEnd_ - fFileDataStart_);
    // check result!
    AssertNotImplemented ();
#elif       qPlatform_Windows
    if (fFileDataStart_ != nullptr) {
        (void)::UnmapViewOfFile (fFileDataStart_);
    }
    if (fFileMapping_ != INVALID_HANDLE_VALUE) {
        ::CloseHandle (fFileMapping_);
    }
    if (fFileHandle_ != INVALID_HANDLE_VALUE) {
        ::CloseHandle (fFileHandle_);
    }
#else
    AssertNotImplemented ();
#endif
}<commit_msg>another partial MemoryMappedFileReader POSIX fix<commit_after>/*
 * Copyright(c) Sophist Solutions, Inc. 1990-2013.  All rights reserved
 */
#include    "../../StroikaPreComp.h"

#if     qPlatform_Windows
#include    <windows.h>
#elif   qPlatform_POSIX
#include    <unistd.h>
#include    <sys/mman.h>
#endif

#include    "../../Execution/ErrNoException.h"
#include    "../../Execution/Exceptions.h"
#if     qPlatform_Windows
#include    "../../Execution/Platform/Windows/Exception.h"
#include    "../../Execution/Platform/Windows/HRESULTErrorException.h"
#endif
#include    "../../Debug/Trace.h"
#include    "../../IO/FileAccessException.h"
#include    "../../IO/FileBusyException.h"
#include    "../../IO/FileFormatException.h"
#include    "PathName.h"

#include    "MemoryMappedFileReader.h"


using   namespace   Stroika::Foundation;
using   namespace   Stroika::Foundation::Characters;
using   namespace   Stroika::Foundation::IO;
using   namespace   Stroika::Foundation::IO::FileSystem;
using   namespace   Stroika::Foundation::Memory;


#if     qPlatform_Windows
using   Execution::Platform::Windows::ThrowIfFalseGetLastError;
#endif




/*
 ********************************************************************************
 ***************************** MemoryMappedFileReader ***************************
 ********************************************************************************
 */
MemoryMappedFileReader::MemoryMappedFileReader (const String& fileName)
    : fFileDataStart_ (nullptr)
    , fFileDataEnd_ (nullptr)
#if         qPlatform_Windows
    , fFileHandle_ (INVALID_HANDLE_VALUE)
    , fFileMapping_ (INVALID_HANDLE_VALUE)
#endif
{
#if         qPlatform_POSIX
    int fd = 3;///tmphack....
    AssertNotImplemented (); // chcek results
    size_t fileLength = 3;//size of file - compute
    void* mmap(void*, size_t, int, int, int, __off_t)
    fFileDataStart_ = reinterpret_cast<const Byte*> (::mmap (nullptr, fileLength, PROT_READ, MAP_PRIVATE, fd, 0));
    fFileDataEnd_ = fFileDataStart_ + fileLength;
    ::close (fd);//http://linux.die.net/man/2/mmap says dont need to keep FD open while mmapped
#elif       qPlatform_Windows
    try {
        // FILE_READ_DATA fails on WinME - generates ERROR_INVALID_PARAMETER - so use GENERIC_READ
        fFileHandle_ = ::CreateFile (fileName.AsTString ().c_str (), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
        ThrowIfFalseGetLastError (fFileHandle_ != INVALID_HANDLE_VALUE);
        DWORD   fileSize    =   ::GetFileSize (fFileHandle_, nullptr);
        if (fileSize != 0) {
            fFileMapping_ = ::CreateFileMapping (fFileHandle_, nullptr, PAGE_READONLY, 0, fileSize, 0);
            ThrowIfFalseGetLastError (fFileMapping_ != nullptr);
            fFileDataStart_ = reinterpret_cast<const Byte*> (::MapViewOfFile (fFileMapping_, FILE_MAP_READ, 0, 0, 0));
            ThrowIfFalseGetLastError (fFileDataStart_ != nullptr);
            fFileDataEnd_ = fFileDataStart_ + fileSize;
        }
    }
    catch (...) {
        if (fFileMapping_ != INVALID_HANDLE_VALUE) {
            ::CloseHandle (fFileMapping_);
        }
        if (fFileHandle_ != INVALID_HANDLE_VALUE) {
            ::CloseHandle (fFileHandle_);
        }
        Execution::DoReThrow ();
    }
#else
    AssertNotImplemented ();
#endif
}

MemoryMappedFileReader::~MemoryMappedFileReader ()
{
#if         qPlatform_POSIX
    int res = ::munmap (const_cast<Byte*> (fFileDataStart_), fFileDataEnd_ - fFileDataStart_);
    // check result!
    AssertNotImplemented ();
#elif       qPlatform_Windows
    if (fFileDataStart_ != nullptr) {
        (void)::UnmapViewOfFile (fFileDataStart_);
    }
    if (fFileMapping_ != INVALID_HANDLE_VALUE) {
        ::CloseHandle (fFileMapping_);
    }
    if (fFileHandle_ != INVALID_HANDLE_VALUE) {
        ::CloseHandle (fFileHandle_);
    }
#else
    AssertNotImplemented ();
#endif
}
{'fraction_non_alphanumeric': 0.1719316416364578, 'fraction_numerical': 0.004660797514241326, 'mean_word_length': 3.5043731778425657, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1375552', 'n_tokens_mistral': 2364, 'n_tokens_neox': 2319, 'n_words': 462}