/*
 * Copyright (C) Tristan Franssen, <tristanfranssen@strawhats.nl>.
 *
 * This software is licensed under the Apache License, Version 2.0 (the "License");
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License in the file LICENSE or at
 * http://www.apache.org/licenses/LICENSE-2.0
 */
#ifndef COSMS_CORE_FILE
#define COSMS_CORE_FILE

#if defined(_WIN32)
#include <windows.h>
#endif

typedef enum {
    COSMS_CORE_FILE_OK = 0,
    COSMS_CORE_FILE_NOT_FOUND = -1,
    COSMS_CORE_FILE_NO_ACCESS = -2,
    COSMS_CORE_FILE_LIMIT_REACHED = -3,
    COSMS_CORE_FILE_COULD_NOT_READ_SIZE = -4,
    COSMS_CORE_FILE_UNKOWN_ERROR = -5,
    COSMS_CORE_FILE_FAILED_TO_ALLOCATE = -6,
    COSMS_CORE_FILE_FAILED_TO_READ = -7,
    COSMS_CORE_FILE_FAILED_TO_WRITE = -8,
    COSMS_CORE_FILE_STILL_OPEN = -9,
    COSMS_CORE_FILE_INVALID_FILE = -10,
    COSMS_CORE_FILE_INVALID_OPERATION = -11
} CosmsCoreFileError;

#define COSMS_CORE_FILE_LARGE 0x7FFFFFFF

#define COSMS_CORE_FILE_MODE_READ (1u << 0)
#define COSMS_CORE_FILE_MODE_WRITE (1u << 1)
#define COSMS_CORE_FILE_MODE_APPEND (1u << 2)
#define COSMS_CORE_FILE_MODE_CREATE (1u << 3)

struct cosms_core_file {
    int mode;

    #if defined(__GNUC__)
    int native_file;
    #elif defined(_WIN32)
    HANDLE native_file;
    #endif
};

CosmsCoreFileError cosms_core_file_open(struct cosms_core_file *file, const char *path, int mode);
CosmsCoreFileError cosms_core_file_close(struct cosms_core_file *file);

CosmsCoreFileError cosms_core_file_get_size(struct cosms_core_file *file, unsigned long long *size);

CosmsCoreFileError cosms_core_file_read(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_read, unsigned int *bytes_read);
CosmsCoreFileError cosms_core_file_read_all(struct cosms_core_file *file, char **buffer, unsigned long long *bytes_read);

CosmsCoreFileError cosms_core_file_write(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_write, unsigned int *bytes_written);
CosmsCoreFileError cosms_core_file_write_all(struct cosms_core_file *file, char *buffer, unsigned long long bytes_to_write);

CosmsCoreFileError cosms_core_file_delete(const char *path);

const char *cosms_core_file_error_string(CosmsCoreFileError error);

#endif
