<commit_before>/**
 * Copyright (C) 2005-2007 <NAME> (<EMAIL>).
 *
 * This program 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 2 of the License, or 
 * (at your option) any later version.
 *
 * See file COPYING.GPL2 and COPYING.GPL3 for License information.
 *
 */

#include <windows.h>

#include <stdio.h>
#include <string.h>
#include <ham/hamsterdb.h>
#include <ham/types.h>

#include "error.h"
#include "os.h"

static void
__utf8_string(const char *filename, wchar_t *wfilename, int wlen)
{
	MultiByteToWideChar(CP_ACP, 0, filename, -1, wfilename, wlen);
}

ham_size_t
os_get_pagesize(void)
{
    return (os_get_granularity());
}

ham_size_t
os_get_granularity(void)
{
    SYSTEM_INFO info;
    GetSystemInfo(&info);
    return ((ham_size_t)info.dwAllocationGranularity);
}

ham_status_t
os_mmap(ham_fd_t fd, ham_fd_t *mmaph, ham_offset_t position, 
        ham_size_t size, ham_bool_t readonly, ham_u8_t **buffer)
{
#ifndef UNDER_CE
    ham_status_t st;
    DWORD hsize, fsize=GetFileSize(fd, &hsize);
    DWORD protect=PAGE_WRITECOPY; /* | (readonly ? PAGE_READONLY : PAGE_READWRITE); */
    DWORD access =FILE_MAP_COPY; /* | (readonly ? FILE_MAP_READ : FILE_MAP_WRITE); */

    *mmaph=CreateFileMapping(fd, 0, protect, hsize, fsize, 0); 
    if (!*mmaph) {
	    *buffer=0;
        st=(ham_status_t)GetLastError();
        ham_trace(("CreateFileMapping failed with status %u", st));
        return (HAM_IO_ERROR);
    }

	*buffer=MapViewOfFile(*mmaph, access, 
            (unsigned long)(position&0xffffffff00000000),
			(unsigned long)(position&0x00000000ffffffff), size);
    if (!*buffer) {
        st=(ham_status_t)GetLastError();
        ham_trace(("MapViewOfFile failed with status %u", st));
        return (HAM_IO_ERROR);
    }

#endif /* UNDER_CE */
    return (HAM_SUCCESS);
}

ham_status_t
os_munmap(ham_fd_t *mmaph, void *buffer, ham_size_t size)
{
    ham_status_t st;

    if (!UnmapViewOfFile(buffer)) {
        st=(ham_status_t)GetLastError();
        ham_trace(("UnMapViewOfFile failed with status %u", st));
        return (HAM_IO_ERROR);
    }

    if (!CloseHandle(*mmaph)) {
        st=(ham_status_t)GetLastError();
        ham_trace(("CloseHandle failed with status %u", st));
        return (HAM_IO_ERROR);
    }

    *mmaph=0;

    return (HAM_SUCCESS);
}

ham_status_t
os_pread(ham_fd_t fd, ham_offset_t addr, void *buffer,
        ham_size_t bufferlen)
{
    ham_status_t st;
    DWORD read=0;

    st=os_seek(fd, addr, HAM_OS_SEEK_SET);
    if (st)
        return (st);

    if (!ReadFile((HANDLE)fd, buffer, bufferlen, &read, 0)) {
        st=(ham_status_t)GetLastError();
        ham_trace(("read failed with OS status %u", st));
        return (HAM_IO_ERROR);
    }

    return (read==bufferlen ? 0 : HAM_IO_ERROR);
}

ham_status_t
os_pwrite(ham_fd_t fd, ham_offset_t addr, const void *buffer,
        ham_size_t bufferlen)
{
    ham_status_t st;
    DWORD written=0;

    st=os_seek(fd, addr, HAM_OS_SEEK_SET);
    if (st)
        return (st);

    if (!WriteFile((HANDLE)fd, buffer, bufferlen, &written, 0)) {
        st=(ham_status_t)GetLastError();
        ham_trace(("write failed with OS status %u", st));
        return (HAM_IO_ERROR);
    }

    return (written==bufferlen ? HAM_SUCCESS : HAM_IO_ERROR);
}

#ifndef INVALID_SET_FILE_POINTER
#   define INVALID_SET_FILE_POINTER  ((DWORD)-1)
#endif

ham_status_t
os_seek(ham_fd_t fd, ham_offset_t offset, int whence)
{
    DWORD st;
    LARGE_INTEGER i;
    i.QuadPart=offset;
    
    i.LowPart=SetFilePointer((HANDLE)fd, i.LowPart, 
            &i.HighPart, whence);
    if (i.LowPart==INVALID_SET_FILE_POINTER && 
        (st=GetLastError())!=NO_ERROR) {
        ham_trace(("seek failed with OS status %u", st));
        return (HAM_IO_ERROR);
    }

    return (0);
}

ham_status_t
os_tell(ham_fd_t fd, ham_offset_t *offset)
{
    DWORD st;
    LARGE_INTEGER i;
    i.QuadPart=0;

    i.LowPart=SetFilePointer((HANDLE)fd, i.LowPart, 
            &i.HighPart, HAM_OS_SEEK_CUR);
    if (i.LowPart==INVALID_SET_FILE_POINTER && 
        (st=GetLastError())!=NO_ERROR) {
        ham_trace(("tell failed with OS status %u", st));
        return (HAM_IO_ERROR);
    }

    *offset=(ham_offset_t)i.QuadPart;
    return (0);
}

ham_status_t
os_get_filesize(ham_fd_t fd, ham_offset_t *size)
{
	DWORD upper, lower=GetFileSize(fd, &upper);

	if (lower==(DWORD)-1) {
        ham_trace(("GetFileSizeEx failed with OS status %u", 
                    GetLastError()));
        return (HAM_IO_ERROR);
	}

	*size=(((ham_offset_t)upper)<<32)+lower;
    return (0);
}<commit_msg>fixed a MSVC warning (x64)<commit_after>/**
 * Copyright (C) 2005-2007 <NAME> (<EMAIL>).
 *
 * This program 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 2 of the License, or 
 * (at your option) any later version.
 *
 * See file COPYING.GPL2 and COPYING.GPL3 for License information.
 *
 */

#include <windows.h>

#include <stdio.h>
#include <string.h>
#include <ham/hamsterdb.h>
#include <ham/types.h>

#include "error.h"
#include "os.h"

static void
__utf8_string(const char *filename, wchar_t *wfilename, int wlen)
{
	MultiByteToWideChar(CP_ACP, 0, filename, -1, wfilename, wlen);
}

ham_size_t
os_get_pagesize(void)
{
    return (os_get_granularity());
}

ham_size_t
os_get_granularity(void)
{
    SYSTEM_INFO info;
    GetSystemInfo(&info);
    return ((ham_size_t)info.dwAllocationGranularity);
}

ham_status_t
os_mmap(ham_fd_t fd, ham_fd_t *mmaph, ham_offset_t position, 
        ham_size_t size, ham_bool_t readonly, ham_u8_t **buffer)
{
#ifndef UNDER_CE
    ham_status_t st;
    DWORD hsize, fsize=GetFileSize(fd, &hsize);
    DWORD protect=PAGE_WRITECOPY; /* | (readonly ? PAGE_READONLY : PAGE_READWRITE); */
    DWORD access =FILE_MAP_COPY; /* | (readonly ? FILE_MAP_READ : FILE_MAP_WRITE); */

    *mmaph=CreateFileMapping(fd, 0, protect, hsize, fsize, 0); 
    if (!*mmaph) {
	    *buffer=0;
        st=(ham_status_t)GetLastError();
        ham_trace(("CreateFileMapping failed with status %u", st));
        return (HAM_IO_ERROR);
    }

	*buffer=MapViewOfFile(*mmaph, access, 
            (unsigned long)(position&0xffffffff00000000),
			(unsigned long)(position&0x00000000ffffffff), size);
    if (!*buffer) {
        st=(ham_status_t)GetLastError();
        ham_trace(("MapViewOfFile failed with status %u", st));
        return (HAM_IO_ERROR);
    }

#endif /* UNDER_CE */
    return (HAM_SUCCESS);
}

ham_status_t
os_munmap(ham_fd_t *mmaph, void *buffer, ham_size_t size)
{
    ham_status_t st;

    if (!UnmapViewOfFile(buffer)) {
        st=(ham_status_t)GetLastError();
        ham_trace(("UnMapViewOfFile failed with status %u", st));
        return (HAM_IO_ERROR);
    }

    if (!CloseHandle(*mmaph)) {
        st=(ham_status_t)GetLastError();
        ham_trace(("CloseHandle failed with status %u", st));
        return (HAM_IO_ERROR);
    }

    *mmaph=0;

    return (HAM_SUCCESS);
}

ham_status_t
os_pread(ham_fd_t fd, ham_offset_t addr, void *buffer,
        ham_size_t bufferlen)
{
    ham_status_t st;
    DWORD read=0;

    st=os_seek(fd, addr, HAM_OS_SEEK_SET);
    if (st)
        return (st);

    if (!ReadFile((HANDLE)fd, buffer, bufferlen, &read, 0)) {
        st=(ham_status_t)GetLastError();
        ham_trace(("read failed with OS status %u", st));
        return (HAM_IO_ERROR);
    }

    return (read==bufferlen ? 0 : HAM_IO_ERROR);
}

ham_status_t
os_pwrite(ham_fd_t fd, ham_offset_t addr, const void *buffer,
        ham_size_t bufferlen)
{
    ham_status_t st;
    DWORD written=0;

    st=os_seek(fd, addr, HAM_OS_SEEK_SET);
    if (st)
        return (st);

    if (!WriteFile((HANDLE)fd, buffer, bufferlen, &written, 0)) {
        st=(ham_status_t)GetLastError();
        ham_trace(("write failed with OS status %u", st));
        return (HAM_IO_ERROR);
    }

    return (written==bufferlen ? HAM_SUCCESS : HAM_IO_ERROR);
}

#ifndef INVALID_SET_FILE_POINTER
#   define INVALID_SET_FILE_POINTER  ((DWORD)-1)
#endif

ham_status_t
os_seek(ham_fd_t fd, ham_offset_t offset, int whence)
{
    DWORD st;
    LARGE_INTEGER i;
    i.QuadPart=offset;
    
    i.LowPart=SetFilePointer((HANDLE)fd, i.LowPart, 
            &i.HighPart, whence);
    if (i.LowPart==INVALID_SET_FILE_POINTER && 
        (st=GetLastError())!=NO_ERROR) {
        ham_trace(("seek failed with OS status %u", st));
        return (HAM_IO_ERROR);
    }

    return (0);
}

ham_status_t
os_tell(ham_fd_t fd, ham_offset_t *offset)
{
    DWORD st;
    LARGE_INTEGER i;
    i.QuadPart=0;

    i.LowPart=SetFilePointer((HANDLE)fd, i.LowPart, 
            &i.HighPart, HAM_OS_SEEK_CUR);
    if (i.LowPart==INVALID_SET_FILE_POINTER && 
        (st=GetLastError())!=NO_ERROR) {
        ham_trace(("tell failed with OS status %u", st));
        return (HAM_IO_ERROR);
    }

    *offset=(ham_offset_t)i.QuadPart;
    return (0);
}

ham_status_t
os_get_filesize(ham_fd_t fd, ham_offset_t *size)
{
	DWORD upper, lower=GetFileSize(fd, &upper);

	if (lower==(DWORD)-1) {
        ham_trace(("GetFileSizeEx failed with OS status %u", 
                    GetLastError()));
        return (HAM_IO_ERROR);
	}

	/* ugly casts to avoid warnings on MSVC 8 for 64bit */
	*size=(ham_offset_t)((unsigned long long)upper<<32)+lower;
    return (0);
}
{'fraction_non_alphanumeric': 0.12852763978437798, 'fraction_numerical': 0.011098192580065532, 'mean_word_length': 3.4339268978444237, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 8, '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': '7079843', 'n_tokens_mistral': 3670, 'n_tokens_neox': 3497, 'n_words': 756}