Skip to content

Commit

Permalink
Consistently order I/O functions declarations and definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Robyt3 committed Sep 26, 2024
1 parent 56cc1ce commit 18378c7
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 87 deletions.
136 changes: 68 additions & 68 deletions src/base/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,58 +84,6 @@
#include <sys/filio.h>
#endif

IOHANDLE io_stdin()
{
return stdin;
}

IOHANDLE io_stdout()
{
return stdout;
}

IOHANDLE io_stderr()
{
return stderr;
}

IOHANDLE io_current_exe()
{
// From https://stackoverflow.com/a/1024937.
#if defined(CONF_FAMILY_WINDOWS)
wchar_t wide_path[IO_MAX_PATH_LENGTH];
if(GetModuleFileNameW(NULL, wide_path, std::size(wide_path)) == 0 || GetLastError() != ERROR_SUCCESS)
{
return 0;
}
const std::optional<std::string> path = windows_wide_to_utf8(wide_path);
return path.has_value() ? io_open(path.value().c_str(), IOFLAG_READ) : 0;
#elif defined(CONF_PLATFORM_MACOS)
char path[IO_MAX_PATH_LENGTH];
uint32_t path_size = sizeof(path);
if(_NSGetExecutablePath(path, &path_size))
{
return 0;
}
return io_open(path, IOFLAG_READ);
#else
static const char *NAMES[] = {
"/proc/self/exe", // Linux, Android
"/proc/curproc/exe", // NetBSD
"/proc/curproc/file", // DragonFly
};
for(auto &name : NAMES)
{
IOHANDLE result = io_open(name, IOFLAG_READ);
if(result)
{
return result;
}
}
return 0;
#endif
}

static NETSTATS network_stats = {0};

#define VLEN 128
Expand Down Expand Up @@ -403,11 +351,6 @@ long int io_length(IOHANDLE io)
return length;
}

int io_error(IOHANDLE io)
{
return ferror((FILE *)io);
}

unsigned io_write(IOHANDLE io, const void *buffer, unsigned size)
{
return fwrite(buffer, 1, size, (FILE *)io);
Expand Down Expand Up @@ -445,6 +388,63 @@ int io_sync(IOHANDLE io)
#endif
}

int io_error(IOHANDLE io)
{
return ferror((FILE *)io);
}

IOHANDLE io_stdin()
{
return stdin;
}

IOHANDLE io_stdout()
{
return stdout;
}

IOHANDLE io_stderr()
{
return stderr;
}

IOHANDLE io_current_exe()
{
// From https://stackoverflow.com/a/1024937.
#if defined(CONF_FAMILY_WINDOWS)
wchar_t wide_path[IO_MAX_PATH_LENGTH];
if(GetModuleFileNameW(NULL, wide_path, std::size(wide_path)) == 0 || GetLastError() != ERROR_SUCCESS)
{
return 0;
}
const std::optional<std::string> path = windows_wide_to_utf8(wide_path);
return path.has_value() ? io_open(path.value().c_str(), IOFLAG_READ) : 0;
#elif defined(CONF_PLATFORM_MACOS)
char path[IO_MAX_PATH_LENGTH];
uint32_t path_size = sizeof(path);
if(_NSGetExecutablePath(path, &path_size))
{
return 0;
}
return io_open(path, IOFLAG_READ);
#else
static const char *NAMES[] = {
"/proc/self/exe", // Linux, Android
"/proc/curproc/exe", // NetBSD
"/proc/curproc/file", // DragonFly
};
for(auto &name : NAMES)
{
IOHANDLE result = io_open(name, IOFLAG_READ);
if(result)
{
return result;
}
}
return 0;
#endif
}

#define ASYNC_BUFSIZE (8 * 1024)
#define ASYNC_LOCAL_BUFSIZE (64 * 1024)

Expand Down Expand Up @@ -719,17 +719,6 @@ int aio_error(ASYNCIO *aio)
return aio->error;
}

void aio_free(ASYNCIO *aio)
{
aio->lock.lock();
if(aio->thread)
{
thread_detach(aio->thread);
aio->thread = 0;
}
aio_handle_free_and_unlock(aio);
}

void aio_close(ASYNCIO *aio)
{
{
Expand All @@ -755,6 +744,17 @@ void aio_wait(ASYNCIO *aio)
thread_wait(thread);
}

void aio_free(ASYNCIO *aio)
{
aio->lock.lock();
if(aio->thread)
{
thread_detach(aio->thread);
aio->thread = 0;
}
aio_handle_free_and_unlock(aio);
}

struct THREAD_RUN
{
void (*threadfunc)(void *);
Expand Down
38 changes: 19 additions & 19 deletions src/base/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,63 +328,63 @@ char *io_read_all_str(IOHANDLE io);
int io_skip(IOHANDLE io, int size);

/**
* Writes data from a buffer to a file.
* Seeks to a specified offset in the file.
*
* @ingroup File-IO
*
* @param io Handle to the file.
* @param buffer Pointer to the data that should be written.
* @param size Number of bytes to write.
* @param offset Offset from position to search.
* @param origin Position to start searching from.
*
* @return Number of bytes written.
* @return `0` on success.
*/
unsigned io_write(IOHANDLE io, const void *buffer, unsigned size);
int io_seek(IOHANDLE io, int offset, int origin);

/**
* Writes a platform dependent newline to a file.
* Gets the current position in the file.
*
* @ingroup File-IO
*
* @param io Handle to the file.
*
* @return `true` on success, `false` on failure.
* @return The current position, or `-1` on failure.
*/
bool io_write_newline(IOHANDLE io);
long int io_tell(IOHANDLE io);

/**
* Seeks to a specified offset in the file.
* Gets the total length of the file. Resets cursor to the beginning.
*
* @ingroup File-IO
*
* @param io Handle to the file.
* @param offset Offset from position to search.
* @param origin Position to start searching from.
*
* @return `0` on success.
* @return The total size, or `-1` on failure.
*/
int io_seek(IOHANDLE io, int offset, int origin);
long int io_length(IOHANDLE io);

/**
* Gets the current position in the file.
* Writes data from a buffer to a file.
*
* @ingroup File-IO
*
* @param io Handle to the file.
* @param buffer Pointer to the data that should be written.
* @param size Number of bytes to write.
*
* @return The current position, or `-1` on failure.
* @return Number of bytes written.
*/
long int io_tell(IOHANDLE io);
unsigned io_write(IOHANDLE io, const void *buffer, unsigned size);

/**
* Gets the total length of the file. Resets cursor to the beginning.
* Writes a platform dependent newline to a file.
*
* @ingroup File-IO
*
* @param io Handle to the file.
*
* @return The total size, or `-1` on failure.
* @return `true` on success, `false` on failure.
*/
long int io_length(IOHANDLE io);
bool io_write_newline(IOHANDLE io);

/**
* Closes a file.
Expand Down

0 comments on commit 18378c7

Please sign in to comment.