diff --git a/src/base/system.cpp b/src/base/system.cpp index b244163f9d6..f42341453c5 100644 --- a/src/base/system.cpp +++ b/src/base/system.cpp @@ -84,58 +84,6 @@ #include #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 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 @@ -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); @@ -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 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) @@ -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) { { @@ -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 *); diff --git a/src/base/system.h b/src/base/system.h index 3245ac16169..cf75ce3092a 100644 --- a/src/base/system.h +++ b/src/base/system.h @@ -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.