2010-11-20 10:37:14 +00:00
|
|
|
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
|
|
|
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
2008-07-06 11:21:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Title: OS Abstraction
|
|
|
|
*/
|
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
#ifndef BASE_SYSTEM_H
|
|
|
|
#define BASE_SYSTEM_H
|
|
|
|
|
2008-08-14 17:19:13 +00:00
|
|
|
#include "detect.h"
|
2023-12-11 14:40:09 +00:00
|
|
|
#include "types.h"
|
2018-07-25 14:06:00 +00:00
|
|
|
|
|
|
|
#ifndef __USE_GNU
|
|
|
|
#define __USE_GNU
|
|
|
|
#endif
|
|
|
|
|
2023-11-10 20:15:51 +00:00
|
|
|
#include <chrono>
|
2023-02-28 19:41:07 +00:00
|
|
|
#include <cinttypes>
|
2023-03-19 10:00:50 +00:00
|
|
|
#include <cstdarg>
|
2023-02-28 19:41:07 +00:00
|
|
|
#include <cstdint>
|
2023-11-19 10:57:34 +00:00
|
|
|
#include <cstring>
|
2023-02-28 19:41:07 +00:00
|
|
|
#include <ctime>
|
2023-11-10 20:15:51 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <mutex>
|
2023-12-01 19:04:48 +00:00
|
|
|
#include <optional>
|
2023-05-02 15:36:01 +00:00
|
|
|
#include <string>
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2022-12-25 19:41:19 +00:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
#undef PRId64
|
|
|
|
#undef PRIu64
|
2023-06-05 18:58:39 +00:00
|
|
|
#undef PRIX64
|
2022-12-25 19:41:19 +00:00
|
|
|
#define PRId64 "I64d"
|
|
|
|
#define PRIu64 "I64u"
|
2023-06-05 18:58:39 +00:00
|
|
|
#define PRIX64 "I64X"
|
2022-12-25 19:41:19 +00:00
|
|
|
#define PRIzu "Iu"
|
|
|
|
#else
|
|
|
|
#define PRIzu "zu"
|
|
|
|
#endif
|
|
|
|
|
2017-12-20 15:56:34 +00:00
|
|
|
#ifdef CONF_FAMILY_UNIX
|
|
|
|
#include <sys/un.h>
|
|
|
|
#endif
|
|
|
|
|
2018-07-25 14:06:00 +00:00
|
|
|
#ifdef CONF_PLATFORM_LINUX
|
2018-12-17 19:49:25 +00:00
|
|
|
#include <netinet/in.h>
|
2020-09-26 19:41:58 +00:00
|
|
|
#include <sys/socket.h>
|
2018-07-25 14:06:00 +00:00
|
|
|
#endif
|
|
|
|
|
2023-04-04 07:08:38 +00:00
|
|
|
#if __cplusplus >= 201703L
|
|
|
|
#define MAYBE_UNUSED [[maybe_unused]]
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
#define MAYBE_UNUSED __attribute__((unused))
|
|
|
|
#else
|
|
|
|
#define MAYBE_UNUSED
|
|
|
|
#endif
|
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* @defgroup Debug
|
|
|
|
*
|
|
|
|
* Utilities for debugging.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup Debug
|
|
|
|
*
|
|
|
|
* Breaks into the debugger based on a test.
|
|
|
|
*
|
|
|
|
* @param test Result of the test.
|
|
|
|
* @param msg Message that should be printed if the test fails.
|
|
|
|
*
|
|
|
|
* @remark Also works in release mode.
|
|
|
|
*
|
|
|
|
* @see dbg_break
|
|
|
|
*/
|
2020-09-26 19:41:58 +00:00
|
|
|
#define dbg_assert(test, msg) dbg_assert_imp(__FILE__, __LINE__, test, msg)
|
2023-12-04 01:54:38 +00:00
|
|
|
void dbg_assert_imp(const char *filename, int line, bool test, const char *msg);
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2012-08-17 16:32:56 +00:00
|
|
|
#ifdef __clang_analyzer__
|
2023-02-28 19:41:07 +00:00
|
|
|
#include <cassert>
|
2012-08-17 16:32:56 +00:00
|
|
|
#undef dbg_assert
|
2020-09-26 19:41:58 +00:00
|
|
|
#define dbg_assert(test, msg) assert(test)
|
2012-08-17 16:32:56 +00:00
|
|
|
#endif
|
|
|
|
|
2017-07-26 01:58:00 +00:00
|
|
|
#ifdef __GNUC__
|
|
|
|
#define GNUC_ATTRIBUTE(x) __attribute__(x)
|
|
|
|
#else
|
|
|
|
#define GNUC_ATTRIBUTE(x)
|
|
|
|
#endif
|
|
|
|
|
2022-04-22 23:04:48 +00:00
|
|
|
/**
|
|
|
|
* Checks whether the program is currently shutting down due to a failed
|
|
|
|
* assert.
|
|
|
|
*
|
|
|
|
* @ingroup Debug
|
|
|
|
*
|
|
|
|
* @return indication whether the program is currently shutting down due to a
|
|
|
|
* failed assert.
|
|
|
|
*/
|
|
|
|
bool dbg_assert_has_failed();
|
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Breaks into the debugger.
|
|
|
|
*
|
|
|
|
* @ingroup Debug
|
|
|
|
* @remark Also works in release mode.
|
|
|
|
*
|
|
|
|
* @see dbg_assert
|
|
|
|
*/
|
2022-05-02 20:54:50 +00:00
|
|
|
#if defined(__cplusplus)
|
|
|
|
[[noreturn]]
|
|
|
|
#endif
|
|
|
|
void
|
|
|
|
dbg_break();
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2023-05-03 18:28:37 +00:00
|
|
|
typedef std::function<void(const char *message)> DBG_ASSERT_HANDLER;
|
|
|
|
void dbg_assert_set_handler(DBG_ASSERT_HANDLER handler);
|
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Prints a debug message.
|
|
|
|
*
|
|
|
|
* @ingroup Debug
|
|
|
|
*
|
|
|
|
* @param sys A string that describes what system the message belongs to.
|
|
|
|
* @param fmt A printf styled format string.
|
|
|
|
*
|
|
|
|
* @remark Also works in release mode.
|
|
|
|
*
|
|
|
|
* @see dbg_assert
|
2022-06-06 10:50:08 +00:00
|
|
|
*/
|
2017-07-26 01:58:00 +00:00
|
|
|
void dbg_msg(const char *sys, const char *fmt, ...)
|
2020-09-26 19:41:58 +00:00
|
|
|
GNUC_ATTRIBUTE((format(printf, 2, 3)));
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* @defgroup Memory
|
|
|
|
* Memory management utilities.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies a a memory block.
|
|
|
|
*
|
|
|
|
* @ingroup Memory
|
|
|
|
*
|
|
|
|
* @param dest Destination.
|
|
|
|
* @param source Source to copy.
|
|
|
|
* @param size Size of the block to copy.
|
|
|
|
*
|
|
|
|
* @remark This functions DOES NOT handle cases where the source and destination is overlapping.
|
|
|
|
*
|
|
|
|
* @see mem_move
|
|
|
|
*/
|
2023-02-03 23:39:03 +00:00
|
|
|
void mem_copy(void *dest, const void *source, size_t size);
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Copies a a memory block.
|
|
|
|
*
|
|
|
|
* @ingroup Memory
|
|
|
|
*
|
|
|
|
* @param dest Destination.
|
|
|
|
* @param source Source to copy.
|
|
|
|
* @param size Size of the block to copy.
|
|
|
|
*
|
|
|
|
* @remark This functions handles the cases where the source and destination is overlapping.
|
|
|
|
*
|
|
|
|
* @see mem_copy
|
|
|
|
*/
|
2023-02-03 23:39:03 +00:00
|
|
|
void mem_move(void *dest, const void *source, size_t size);
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Sets a complete memory block to 0.
|
|
|
|
*
|
|
|
|
* @ingroup Memory
|
|
|
|
*
|
|
|
|
* @param block Pointer to the block to zero out.
|
|
|
|
* @param size Size of the block.
|
|
|
|
*/
|
2023-01-02 23:35:21 +00:00
|
|
|
template<typename T>
|
|
|
|
inline void mem_zero(T *block, size_t size)
|
|
|
|
{
|
2023-01-08 15:27:25 +00:00
|
|
|
static_assert((std::is_trivially_constructible<T>::value && std::is_trivially_destructible<T>::value) || std::is_fundamental<T>::value);
|
|
|
|
memset(block, 0, size);
|
2023-01-02 23:35:21 +00:00
|
|
|
}
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Compares two blocks of memory
|
|
|
|
*
|
|
|
|
* @ingroup Memory
|
|
|
|
*
|
|
|
|
* @param a First block of data
|
|
|
|
* @param b Second block of data
|
|
|
|
* @param size Size of the data to compare
|
|
|
|
*
|
|
|
|
* @return < 0 - Block a is less than block b.
|
|
|
|
* @return 0 - Block a is equal to block b.
|
|
|
|
* @return > 0 - Block a is greater than block b.
|
|
|
|
*/
|
2023-02-03 23:39:03 +00:00
|
|
|
int mem_comp(const void *a, const void *b, size_t size);
|
2008-07-06 11:21:21 +00:00
|
|
|
|
2022-06-14 18:03:22 +00:00
|
|
|
/**
|
|
|
|
* Checks whether a block of memory contains null bytes.
|
|
|
|
*
|
|
|
|
* @ingroup Memory
|
|
|
|
*
|
|
|
|
* @param block Pointer to the block to check for nulls.
|
|
|
|
* @param size Size of the block.
|
|
|
|
*
|
2023-02-03 23:36:26 +00:00
|
|
|
* @return true if the block has a null byte, false otherwise.
|
2022-06-14 18:03:22 +00:00
|
|
|
*/
|
2023-02-03 23:39:03 +00:00
|
|
|
bool mem_has_null(const void *block, size_t size);
|
2022-06-14 18:03:22 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* @defgroup File-IO
|
|
|
|
*
|
|
|
|
* I/O related operations.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup File-IO
|
|
|
|
*/
|
2020-09-26 19:41:58 +00:00
|
|
|
enum
|
|
|
|
{
|
2007-08-22 07:52:33 +00:00
|
|
|
IOFLAG_READ = 1,
|
|
|
|
IOFLAG_WRITE = 2,
|
2021-08-23 10:30:12 +00:00
|
|
|
IOFLAG_APPEND = 4,
|
2021-12-17 20:56:31 +00:00
|
|
|
IOFLAG_SKIP_BOM = 8,
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2007-08-25 08:48:24 +00:00
|
|
|
IOSEEK_START = 0,
|
2007-08-22 07:52:33 +00:00
|
|
|
IOSEEK_CUR = 1,
|
2021-08-23 10:49:15 +00:00
|
|
|
IOSEEK_END = 2,
|
2007-08-22 07:52:33 +00:00
|
|
|
};
|
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Opens a file.
|
|
|
|
*
|
|
|
|
* @ingroup File-IO
|
|
|
|
*
|
|
|
|
* @param File to open.
|
2022-06-06 10:50:08 +00:00
|
|
|
* @param flags A set of IOFLAG flags.
|
2022-02-18 10:35:46 +00:00
|
|
|
*
|
|
|
|
* @sa IOFLAG_READ, IOFLAG_WRITE, IOFLAG_APPEND, IOFLAG_SKIP_BOM.
|
|
|
|
*
|
|
|
|
* @return A handle to the file on success and 0 on failure.
|
|
|
|
*
|
|
|
|
*/
|
2007-08-22 07:52:33 +00:00
|
|
|
IOHANDLE io_open(const char *filename, int flags);
|
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Reads data into a buffer from a file.
|
|
|
|
*
|
|
|
|
* @ingroup File-IO
|
|
|
|
*
|
|
|
|
* @param io Handle to the file to read data from.
|
|
|
|
* @param buffer Pointer to the buffer that will receive the data.
|
|
|
|
* @param size Number of bytes to read from the file.
|
|
|
|
*
|
|
|
|
* @return Number of bytes read.
|
|
|
|
*
|
|
|
|
*/
|
2007-08-22 07:52:33 +00:00
|
|
|
unsigned io_read(IOHANDLE io, void *buffer, unsigned size);
|
|
|
|
|
2022-06-14 18:03:22 +00:00
|
|
|
/**
|
|
|
|
* Reads the rest of the file into a buffer.
|
|
|
|
*
|
|
|
|
* @ingroup File-IO
|
|
|
|
*
|
|
|
|
* @param io Handle to the file to read data from.
|
|
|
|
* @param result Receives the file's remaining contents.
|
|
|
|
* @param result_len Receives the file's remaining length.
|
|
|
|
*
|
|
|
|
* @remark Does NOT guarantee that there are no internal null bytes.
|
|
|
|
* @remark The result must be freed after it has been used.
|
|
|
|
*/
|
|
|
|
void io_read_all(IOHANDLE io, void **result, unsigned *result_len);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads the rest of the file into a zero-terminated buffer with
|
|
|
|
* no internal null bytes.
|
|
|
|
*
|
|
|
|
* @ingroup File-IO
|
|
|
|
*
|
|
|
|
* @param io Handle to the file to read data from.
|
|
|
|
*
|
|
|
|
* @return The file's remaining contents or null on failure.
|
|
|
|
*
|
|
|
|
* @remark Guarantees that there are no internal null bytes.
|
|
|
|
* @remark Guarantees that result will contain zero-termination.
|
|
|
|
* @remark The result must be freed after it has been used.
|
|
|
|
*/
|
|
|
|
char *io_read_all_str(IOHANDLE io);
|
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Skips data in a file.
|
|
|
|
*
|
|
|
|
* @ingroup File-IO
|
|
|
|
*
|
|
|
|
* @param io Handle to the file.
|
|
|
|
* @param size Number of bytes to skip.
|
|
|
|
*
|
2023-10-18 19:00:23 +00:00
|
|
|
* @return 0 on success.
|
2022-02-18 10:35:46 +00:00
|
|
|
*/
|
2023-10-18 19:00:23 +00:00
|
|
|
int io_skip(IOHANDLE io, int size);
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Writes data from a buffer to 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 Number of bytes written.
|
|
|
|
*/
|
2007-08-22 07:52:33 +00:00
|
|
|
unsigned io_write(IOHANDLE io, const void *buffer, unsigned size);
|
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
2023-02-18 13:13:55 +00:00
|
|
|
* Writes a platform dependent newline to file.
|
2022-02-18 10:35:46 +00:00
|
|
|
*
|
|
|
|
* @ingroup File-IO
|
|
|
|
*
|
|
|
|
* @param io Handle to the file.
|
|
|
|
*
|
2023-02-18 13:13:55 +00:00
|
|
|
* @return true on success, false on failure.
|
2022-02-18 10:35:46 +00:00
|
|
|
*/
|
2023-02-18 13:13:55 +00:00
|
|
|
bool io_write_newline(IOHANDLE io);
|
2011-12-29 22:36:53 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Seeks to a specified offset in the file.
|
|
|
|
*
|
|
|
|
* @ingroup File-IO
|
|
|
|
*
|
|
|
|
* @param io Handle to the file.
|
|
|
|
* @param offset Offset from pos to stop.
|
|
|
|
* @param origin Position to start searching from.
|
|
|
|
*
|
|
|
|
* @return 0 on success.
|
|
|
|
*/
|
2007-08-22 07:52:33 +00:00
|
|
|
int io_seek(IOHANDLE io, int offset, int origin);
|
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Gets the current position in the file.
|
|
|
|
*
|
|
|
|
* @ingroup File-IO
|
|
|
|
*
|
|
|
|
* @param io Handle to the file.
|
|
|
|
*
|
|
|
|
* @return The current position. @c -1L if an error occurred.
|
|
|
|
*/
|
2007-08-22 07:52:33 +00:00
|
|
|
long int io_tell(IOHANDLE io);
|
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Gets the total length of the file. Resetting cursor to the beginning
|
|
|
|
*
|
|
|
|
* @ingroup File-IO
|
|
|
|
*
|
|
|
|
* @param io Handle to the file.
|
|
|
|
*
|
|
|
|
* @return The total size. @c -1L if an error occurred.
|
|
|
|
*/
|
2007-08-22 07:52:33 +00:00
|
|
|
long int io_length(IOHANDLE io);
|
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Closes a file.
|
|
|
|
*
|
|
|
|
* @ingroup File-IO
|
|
|
|
*
|
|
|
|
* @param io Handle to the file.
|
|
|
|
*
|
|
|
|
* @return 0 on success.
|
|
|
|
*/
|
2007-08-22 07:52:33 +00:00
|
|
|
int io_close(IOHANDLE io);
|
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Empties all buffers and writes all pending data.
|
|
|
|
*
|
|
|
|
* @ingroup File-IO
|
|
|
|
*
|
|
|
|
* @param io Handle to the file.
|
|
|
|
*
|
|
|
|
* @return 0 on success.
|
|
|
|
*/
|
2008-03-10 00:48:45 +00:00
|
|
|
int io_flush(IOHANDLE io);
|
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Synchronize file changes to disk.
|
|
|
|
*
|
|
|
|
* @ingroup File-IO
|
|
|
|
*
|
|
|
|
* @param io Handle to the file.
|
|
|
|
*
|
|
|
|
* @return 0 on success.
|
|
|
|
*/
|
2022-01-31 21:07:51 +00:00
|
|
|
int io_sync(IOHANDLE io);
|
2021-11-28 23:19:46 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Checks whether an error occurred during I/O with the file.
|
|
|
|
*
|
|
|
|
* @ingroup File-IO
|
|
|
|
*
|
|
|
|
* @param io Handle to the file.
|
|
|
|
*
|
|
|
|
* @return nonzero on error, 0 otherwise.
|
|
|
|
*/
|
2017-10-10 01:33:54 +00:00
|
|
|
int io_error(IOHANDLE io);
|
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* @ingroup File-IO
|
|
|
|
* @return An <IOHANDLE> to the standard input.
|
|
|
|
*/
|
2021-06-15 02:15:24 +00:00
|
|
|
IOHANDLE io_stdin();
|
2008-07-06 11:21:21 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* @ingroup File-IO
|
|
|
|
* @return An <IOHANDLE> to the standard output.
|
|
|
|
*/
|
2021-06-15 02:15:24 +00:00
|
|
|
IOHANDLE io_stdout();
|
2008-07-06 11:21:21 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* @ingroup File-IO
|
|
|
|
* @return An <IOHANDLE> to the standard error.
|
|
|
|
*/
|
2021-06-15 02:15:24 +00:00
|
|
|
IOHANDLE io_stderr();
|
2008-07-06 11:21:21 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* @ingroup File-IO
|
|
|
|
* @return An <IOHANDLE> to the current executable.
|
|
|
|
*/
|
2022-01-31 00:54:17 +00:00
|
|
|
IOHANDLE io_current_exe();
|
|
|
|
|
2017-10-10 01:33:54 +00:00
|
|
|
typedef struct ASYNCIO ASYNCIO;
|
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Wraps a @link IOHANDLE @endlink for asynchronous writing.
|
|
|
|
*
|
|
|
|
* @ingroup File-IO
|
|
|
|
*
|
|
|
|
* @param io Handle to the file.
|
|
|
|
*
|
|
|
|
* @return The handle for asynchronous writing.
|
|
|
|
*
|
|
|
|
*/
|
2017-10-13 00:48:42 +00:00
|
|
|
ASYNCIO *aio_new(IOHANDLE io);
|
2017-10-10 01:33:54 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Locks the ASYNCIO structure so it can't be written into by
|
|
|
|
* other threads.
|
|
|
|
*
|
|
|
|
* @ingroup File-IO
|
|
|
|
*
|
|
|
|
* @param aio Handle to the file.
|
|
|
|
*/
|
2017-11-26 16:01:11 +00:00
|
|
|
void aio_lock(ASYNCIO *aio);
|
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Unlocks the ASYNCIO structure after finishing the contiguous
|
|
|
|
* write.
|
|
|
|
*
|
|
|
|
* @ingroup File-IO
|
|
|
|
*
|
|
|
|
* @param aio Handle to the file.
|
|
|
|
*/
|
2017-11-26 16:01:11 +00:00
|
|
|
void aio_unlock(ASYNCIO *aio);
|
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Queues a chunk of data for writing.
|
|
|
|
*
|
|
|
|
* @ingroup File-IO
|
|
|
|
*
|
|
|
|
* @param aio Handle to the file.
|
|
|
|
* @param buffer Pointer to the data that should be written.
|
|
|
|
* @param size Number of bytes to write.
|
|
|
|
*/
|
2017-10-13 00:48:42 +00:00
|
|
|
void aio_write(ASYNCIO *aio, const void *buffer, unsigned size);
|
2017-10-10 01:33:54 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Queues a newline for writing.
|
|
|
|
*
|
|
|
|
* @ingroup File-IO
|
|
|
|
*
|
|
|
|
* @param aio Handle to the file.
|
|
|
|
*
|
|
|
|
*/
|
2017-10-13 00:48:42 +00:00
|
|
|
void aio_write_newline(ASYNCIO *aio);
|
2017-10-10 01:33:54 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Queues a chunk of data for writing. The ASYNCIO struct must be
|
|
|
|
* locked using @link aio_lock @endlink first.
|
|
|
|
*
|
|
|
|
* @ingroup File-IO
|
|
|
|
*
|
|
|
|
* @param aio Handle to the file.
|
|
|
|
* @param buffer Pointer to the data that should be written.
|
|
|
|
* @param size Number of bytes to write.
|
|
|
|
*
|
|
|
|
*/
|
2017-11-26 16:01:11 +00:00
|
|
|
void aio_write_unlocked(ASYNCIO *aio, const void *buffer, unsigned size);
|
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Queues a newline for writing. The ASYNCIO struct must be locked
|
|
|
|
* using @link aio_lock @endlink first.
|
|
|
|
*
|
|
|
|
* @ingroup File-IO
|
|
|
|
*
|
|
|
|
* @param aio Handle to the file.
|
|
|
|
*
|
|
|
|
*/
|
2017-11-26 16:01:11 +00:00
|
|
|
void aio_write_newline_unlocked(ASYNCIO *aio);
|
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Checks whether errors have occurred during the asynchronous
|
|
|
|
* writing.
|
|
|
|
*
|
|
|
|
* Call this function regularly to see if there are errors. Call
|
|
|
|
* this function after <aio_wait> to see if the process of writing
|
|
|
|
* to the file succeeded.
|
|
|
|
*
|
|
|
|
* @ingroup File-IO
|
|
|
|
*
|
|
|
|
* @param aio Handle to the file.
|
|
|
|
*
|
|
|
|
* @eturn 0 if no error occurred, and nonzero on error.
|
|
|
|
*
|
|
|
|
*/
|
2017-10-13 00:48:42 +00:00
|
|
|
int aio_error(ASYNCIO *aio);
|
2017-10-10 01:33:54 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Queues file closing.
|
|
|
|
*
|
|
|
|
* @ingroup File-IO
|
|
|
|
*
|
|
|
|
* @param aio Handle to the file.
|
|
|
|
*
|
|
|
|
*/
|
2017-10-13 00:48:42 +00:00
|
|
|
void aio_close(ASYNCIO *aio);
|
2017-10-10 01:33:54 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Wait for the asynchronous operations to complete.
|
|
|
|
*
|
|
|
|
* @ingroup File-IO
|
|
|
|
*
|
|
|
|
* @param aio Handle to the file.
|
|
|
|
*
|
|
|
|
*/
|
2017-10-13 00:48:42 +00:00
|
|
|
void aio_wait(ASYNCIO *aio);
|
2017-10-10 01:33:54 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Frees the resources associated to the asynchronous file handle.
|
|
|
|
*
|
|
|
|
* @ingroup File-IO
|
|
|
|
*
|
|
|
|
* @param aio Handle to the file.
|
|
|
|
*
|
2022-06-06 10:50:08 +00:00
|
|
|
*/
|
2017-10-13 00:48:42 +00:00
|
|
|
void aio_free(ASYNCIO *aio);
|
2008-07-06 11:21:21 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* @defgroup Threads
|
|
|
|
* Threading related functions.
|
|
|
|
*
|
|
|
|
* @see Locks
|
2024-02-25 14:22:20 +00:00
|
|
|
* @see Semaphore
|
2022-02-18 10:35:46 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new thread.
|
|
|
|
*
|
|
|
|
* @ingroup Threads
|
|
|
|
*
|
|
|
|
* @param threadfunc Entry point for the new thread.
|
|
|
|
* @param user Pointer to pass to the thread.
|
2024-02-25 14:22:20 +00:00
|
|
|
* @param name Name describing the use of the thread.
|
|
|
|
*
|
|
|
|
* @return Handle for the new thread.
|
2022-02-18 10:35:46 +00:00
|
|
|
*/
|
2019-03-19 10:44:16 +00:00
|
|
|
void *thread_init(void (*threadfunc)(void *), void *user, const char *name);
|
2008-07-06 11:21:21 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Waits for a thread to be done or destroyed.
|
|
|
|
*
|
|
|
|
* @ingroup Threads
|
|
|
|
*
|
|
|
|
* @param thread Thread to wait for.
|
|
|
|
*/
|
2008-07-06 11:21:21 +00:00
|
|
|
void thread_wait(void *thread);
|
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
2024-02-25 14:22:20 +00:00
|
|
|
* Yield the current thread's execution slice.
|
2022-02-18 10:35:46 +00:00
|
|
|
*
|
|
|
|
* @ingroup Threads
|
|
|
|
*/
|
2021-06-15 02:15:24 +00:00
|
|
|
void thread_yield();
|
2008-07-06 11:21:21 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
2024-02-25 14:22:20 +00:00
|
|
|
* Puts the thread in the detached state, guaranteeing that
|
2022-02-18 10:35:46 +00:00
|
|
|
* resources of the thread will be freed immediately when the
|
|
|
|
* thread terminates.
|
|
|
|
*
|
|
|
|
* @ingroup Threads
|
|
|
|
*
|
2024-02-25 14:22:20 +00:00
|
|
|
* @param thread Thread to detach.
|
2022-06-06 10:50:08 +00:00
|
|
|
*/
|
2011-05-01 13:48:09 +00:00
|
|
|
void thread_detach(void *thread);
|
2008-07-06 11:21:21 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
2024-02-25 14:22:20 +00:00
|
|
|
* Creates a new thread and detaches it.
|
2022-02-18 10:35:46 +00:00
|
|
|
*
|
|
|
|
* @ingroup Threads
|
|
|
|
*
|
|
|
|
* @param threadfunc Entry point for the new thread.
|
|
|
|
* @param user Pointer to pass to the thread.
|
2024-02-25 14:22:20 +00:00
|
|
|
* @param name Name describing the use of the thread.
|
2022-02-18 10:35:46 +00:00
|
|
|
*/
|
2024-02-25 14:22:20 +00:00
|
|
|
void thread_init_and_detach(void (*threadfunc)(void *), void *user, const char *name);
|
2019-03-19 10:44:16 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
2023-11-10 20:15:51 +00:00
|
|
|
* @defgroup Semaphore
|
2022-02-18 10:35:46 +00:00
|
|
|
* @see Threads
|
|
|
|
*/
|
|
|
|
|
2017-08-31 17:13:55 +00:00
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
2020-09-26 19:41:58 +00:00
|
|
|
typedef void *SEMAPHORE;
|
2021-02-12 12:40:29 +00:00
|
|
|
#elif defined(CONF_PLATFORM_MACOS)
|
2020-09-26 19:41:58 +00:00
|
|
|
#include <semaphore.h>
|
|
|
|
typedef sem_t *SEMAPHORE;
|
2017-08-31 17:13:55 +00:00
|
|
|
#elif defined(CONF_FAMILY_UNIX)
|
2020-09-26 19:41:58 +00:00
|
|
|
#include <semaphore.h>
|
|
|
|
typedef sem_t SEMAPHORE;
|
2017-08-31 08:59:12 +00:00
|
|
|
#else
|
2020-09-26 19:41:58 +00:00
|
|
|
#error not implemented on this platform
|
2012-01-01 14:56:28 +00:00
|
|
|
#endif
|
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
2023-11-10 20:15:51 +00:00
|
|
|
* @ingroup Semaphore
|
2022-02-18 10:35:46 +00:00
|
|
|
*/
|
2017-08-31 10:30:42 +00:00
|
|
|
void sphore_init(SEMAPHORE *sem);
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
2023-11-10 20:15:51 +00:00
|
|
|
* @ingroup Semaphore
|
2022-02-18 10:35:46 +00:00
|
|
|
*/
|
2017-08-31 10:30:42 +00:00
|
|
|
void sphore_wait(SEMAPHORE *sem);
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
2023-11-10 20:15:51 +00:00
|
|
|
* @ingroup Semaphore
|
2022-02-18 10:35:46 +00:00
|
|
|
*/
|
2017-08-31 10:30:42 +00:00
|
|
|
void sphore_signal(SEMAPHORE *sem);
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
2023-11-10 20:15:51 +00:00
|
|
|
* @ingroup Semaphore
|
2022-02-18 10:35:46 +00:00
|
|
|
*/
|
2017-08-31 10:30:42 +00:00
|
|
|
void sphore_destroy(SEMAPHORE *sem);
|
2017-08-31 08:59:12 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* @defgroup Time
|
|
|
|
*
|
|
|
|
* Time utilities.
|
|
|
|
*/
|
2018-03-12 15:12:06 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* @ingroup Time
|
|
|
|
*/
|
|
|
|
void set_new_tick();
|
2018-03-12 15:12:06 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Fetches a sample from a high resolution timer.
|
|
|
|
*
|
|
|
|
* @ingroup Time
|
|
|
|
*
|
|
|
|
* @return Current value of the timer.
|
|
|
|
*
|
|
|
|
* @remark To know how fast the timer is ticking, see @link time_freq @endlink.
|
|
|
|
*
|
|
|
|
* @see time_freq
|
|
|
|
*/
|
2021-06-15 02:15:24 +00:00
|
|
|
int64_t time_get_impl();
|
2018-03-12 15:12:06 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Fetches a sample from a high resolution timer.
|
|
|
|
*
|
|
|
|
* @ingroup Time
|
|
|
|
*
|
|
|
|
* @return Current value of the timer.
|
|
|
|
*
|
|
|
|
* @remark To know how fast the timer is ticking, see @link time_freq @endlink.
|
|
|
|
* @remark Uses @link time_get_impl @endlink to fetch the sample.
|
|
|
|
*
|
|
|
|
* @see time_freq time_get_impl
|
|
|
|
*/
|
2021-06-15 02:15:24 +00:00
|
|
|
int64_t time_get();
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* @ingroup Time
|
|
|
|
*
|
|
|
|
* @return The frequency of the high resolution timer.
|
|
|
|
*/
|
2021-06-15 02:15:24 +00:00
|
|
|
int64_t time_freq();
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Retrieves the current time as a UNIX timestamp
|
|
|
|
*
|
|
|
|
* @ingroup Time
|
|
|
|
*
|
|
|
|
* @return The time as a UNIX timestamp
|
|
|
|
*/
|
2021-06-15 02:15:24 +00:00
|
|
|
int time_timestamp();
|
2008-07-06 11:21:21 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Retrieves the hours since midnight (0..23)
|
|
|
|
*
|
|
|
|
* @ingroup Time
|
|
|
|
*
|
|
|
|
* @return The current hour of the day
|
|
|
|
*/
|
2021-06-15 02:15:24 +00:00
|
|
|
int time_houroftheday();
|
2020-09-18 16:45:42 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* @ingroup Time
|
|
|
|
*/
|
2023-10-20 20:16:35 +00:00
|
|
|
enum ETimeSeason
|
2020-09-18 16:45:42 +00:00
|
|
|
{
|
|
|
|
SEASON_SPRING = 0,
|
|
|
|
SEASON_SUMMER,
|
|
|
|
SEASON_AUTUMN,
|
2020-12-28 16:20:29 +00:00
|
|
|
SEASON_WINTER,
|
2023-10-20 20:16:35 +00:00
|
|
|
SEASON_EASTER,
|
|
|
|
SEASON_HALLOWEEN,
|
|
|
|
SEASON_XMAS,
|
2020-12-28 16:20:29 +00:00
|
|
|
SEASON_NEWYEAR
|
2020-09-18 16:45:42 +00:00
|
|
|
};
|
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Retrieves the current season of the year.
|
|
|
|
*
|
|
|
|
* @ingroup Time
|
|
|
|
*
|
|
|
|
* @return One of the SEASON_* enum literals
|
|
|
|
*
|
|
|
|
* @see SEASON_SPRING
|
|
|
|
*/
|
2023-10-20 20:16:35 +00:00
|
|
|
ETimeSeason time_season();
|
2020-09-18 16:45:42 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* @defgroup Network-General
|
|
|
|
*/
|
|
|
|
|
2023-12-11 14:40:09 +00:00
|
|
|
extern const NETADDR NETADDR_ZEROED;
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* @ingroup Network-General
|
|
|
|
*/
|
2023-11-27 21:29:14 +00:00
|
|
|
|
2017-12-20 15:56:34 +00:00
|
|
|
#ifdef CONF_FAMILY_UNIX
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* @ingroup Network-General
|
|
|
|
*/
|
2017-12-20 15:56:34 +00:00
|
|
|
typedef int UNIXSOCKET;
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* @ingroup Network-General
|
|
|
|
*/
|
2017-12-20 15:56:34 +00:00
|
|
|
typedef struct sockaddr_un UNIXSOCKETADDR;
|
|
|
|
#endif
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Initiates network functionality.
|
|
|
|
*
|
|
|
|
* @ingroup Network-General
|
|
|
|
*
|
|
|
|
* @remark You must call this function before using any other network functions.
|
|
|
|
*/
|
2023-09-16 11:37:53 +00:00
|
|
|
void net_init();
|
2008-08-05 21:37:33 +00:00
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
/*
|
2007-08-22 07:52:33 +00:00
|
|
|
Function: net_host_lookup
|
2008-08-05 21:37:33 +00:00
|
|
|
Does a hostname lookup by name and fills out the passed
|
2018-02-04 15:00:47 +00:00
|
|
|
NETADDR struct with the received details.
|
2007-08-22 07:52:33 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
0 on success.
|
2008-07-06 11:21:21 +00:00
|
|
|
*/
|
|
|
|
int net_host_lookup(const char *hostname, NETADDR *addr, int types);
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Compares two network addresses.
|
|
|
|
*
|
|
|
|
* @ingroup Network-General
|
|
|
|
*
|
|
|
|
* @param a Address to compare
|
|
|
|
* @param b Address to compare to.
|
|
|
|
*
|
|
|
|
* @return `< 0` - Address a is less than address b
|
|
|
|
* @return `0` - Address a is equal to address b
|
|
|
|
* @return `> 0` - Address a is greater than address b
|
|
|
|
*/
|
2008-08-05 21:37:33 +00:00
|
|
|
int net_addr_comp(const NETADDR *a, const NETADDR *b);
|
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Compares two network addresses ignoring port.
|
|
|
|
*
|
|
|
|
* @ingroup Network-General
|
|
|
|
*
|
|
|
|
* @param a Address to compare
|
|
|
|
* @param b Address to compare to.
|
|
|
|
*
|
|
|
|
* @return `< 0` - Address a is less than address b
|
|
|
|
* @return `0` - Address a is equal to address b
|
|
|
|
* @return `> 0` - Address a is greater than address b
|
|
|
|
*/
|
2018-10-08 16:56:51 +00:00
|
|
|
int net_addr_comp_noport(const NETADDR *a, const NETADDR *b);
|
2018-10-07 22:59:07 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Turns a network address into a representative string.
|
|
|
|
*
|
|
|
|
* @ingroup Network-General
|
|
|
|
*
|
|
|
|
* @param addr Address to turn into a string.
|
|
|
|
* @param string Buffer to fill with the string.
|
|
|
|
* @param max_length Maximum size of the string.
|
|
|
|
* @param add_port add port to string or not
|
|
|
|
*
|
|
|
|
* @remark The string will always be zero terminated
|
|
|
|
*/
|
2011-12-29 22:36:53 +00:00
|
|
|
void net_addr_str(const NETADDR *addr, char *string, int max_length, int add_port);
|
2008-08-05 21:37:33 +00:00
|
|
|
|
2023-07-16 12:04:58 +00:00
|
|
|
/**
|
|
|
|
* Turns url string into a network address struct.
|
|
|
|
* The url format is tw-0.6+udp://{ipaddr}[:{port}]
|
|
|
|
* ipaddr: can be ipv4 or ipv6
|
|
|
|
* port: is a optional internet protocol port
|
|
|
|
*
|
|
|
|
* This format is used for parsing the master server, be careful before changing it.
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
* tw-0.6+udp://127.0.0.1
|
|
|
|
* tw-0.6+udp://127.0.0.1:8303
|
|
|
|
*
|
|
|
|
* @param addr Address to fill in.
|
|
|
|
* @param string String to parse.
|
|
|
|
* @param host_buf Pointer to a buffer to write the host to
|
|
|
|
* It will include the port if one is included in the url
|
|
|
|
* It can also be set to NULL then it will be ignored
|
|
|
|
* @param host_buf_size Size of the host buffer or 0 if no host_buf pointer is given
|
|
|
|
*
|
|
|
|
* @return 0 on success,
|
|
|
|
* positive if the input wasn't a valid DDNet URL,
|
|
|
|
* negative if the input is a valid DDNet URL but the host part was not a valid IPv4/IPv6 address
|
|
|
|
*/
|
|
|
|
int net_addr_from_url(NETADDR *addr, const char *string, char *host_buf, size_t host_buf_size);
|
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Turns string into a network address.
|
|
|
|
*
|
|
|
|
* @param addr Address to fill in.
|
|
|
|
* @param string String to parse.
|
|
|
|
*
|
|
|
|
* @return 0 on success
|
|
|
|
*/
|
2008-09-03 21:02:30 +00:00
|
|
|
int net_addr_from_str(NETADDR *addr, const char *string);
|
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* @defgroup Network-UDP
|
|
|
|
* @ingroup Network-General
|
|
|
|
*/
|
|
|
|
|
2022-03-01 16:34:42 +00:00
|
|
|
/*
|
|
|
|
Function: net_socket_type
|
|
|
|
Determine a socket's type.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
sock - Socket whose type should be determined.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The socket type, a bitset of `NETTYPE_IPV4`, `NETTYPE_IPV6` and
|
|
|
|
`NETTYPE_WEBSOCKET_IPV4`.
|
|
|
|
*/
|
|
|
|
int net_socket_type(NETSOCKET sock);
|
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
/*
|
|
|
|
Function: net_udp_create
|
|
|
|
Creates a UDP socket and binds it to a port.
|
2007-08-22 07:52:33 +00:00
|
|
|
|
|
|
|
Parameters:
|
2008-08-05 21:37:33 +00:00
|
|
|
bindaddr - Address to bind the socket to.
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
Returns:
|
2008-08-05 21:37:33 +00:00
|
|
|
On success it returns an handle to the socket. On failure it
|
|
|
|
returns NETSOCKET_INVALID.
|
2008-07-06 11:21:21 +00:00
|
|
|
*/
|
|
|
|
NETSOCKET net_udp_create(NETADDR bindaddr);
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Sends a packet over an UDP socket.
|
|
|
|
*
|
|
|
|
* @ingroup Network-UDP
|
|
|
|
*
|
|
|
|
* @param sock Socket to use.
|
|
|
|
* @param addr Where to send the packet.
|
|
|
|
* @param data Pointer to the packet data to send.
|
|
|
|
* @param size Size of the packet.
|
|
|
|
*
|
|
|
|
* @return On success it returns the number of bytes sent. Returns -1
|
|
|
|
* on error.
|
|
|
|
*/
|
2008-07-06 11:21:21 +00:00
|
|
|
int net_udp_send(NETSOCKET sock, const NETADDR *addr, const void *data, int size);
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
/*
|
|
|
|
Function: net_udp_recv
|
2018-07-10 09:29:02 +00:00
|
|
|
Receives a packet over an UDP socket.
|
2018-07-25 14:06:00 +00:00
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
Parameters:
|
|
|
|
sock - Socket to use.
|
2018-07-10 09:29:02 +00:00
|
|
|
addr - Pointer to an NETADDR that will receive the address.
|
2022-03-01 18:27:48 +00:00
|
|
|
data - Received data. Will be invalidated when this function is
|
|
|
|
called again.
|
2018-07-25 14:06:00 +00:00
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
Returns:
|
2018-02-04 15:00:47 +00:00
|
|
|
On success it returns the number of bytes received. Returns -1
|
2008-08-05 21:37:33 +00:00
|
|
|
on error.
|
2008-07-06 11:21:21 +00:00
|
|
|
*/
|
2022-03-01 18:27:48 +00:00
|
|
|
int net_udp_recv(NETSOCKET sock, NETADDR *addr, unsigned char **data);
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Closes an UDP socket.
|
|
|
|
*
|
|
|
|
* @ingroup Network-UDP
|
|
|
|
*
|
|
|
|
* @param sock Socket to close.
|
|
|
|
*
|
|
|
|
* @return 0 on success. -1 on error.
|
|
|
|
*/
|
2008-07-06 11:21:21 +00:00
|
|
|
int net_udp_close(NETSOCKET sock);
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* @defgroup Network-TCP
|
|
|
|
* @ingroup Network-General
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a TCP socket.
|
|
|
|
*
|
|
|
|
* @ingroup Network-TCP
|
|
|
|
*
|
|
|
|
* @param bindaddr Address to bind the socket to.
|
|
|
|
*
|
|
|
|
* @return On success it returns an handle to the socket. On failure it returns NETSOCKET_INVALID.
|
|
|
|
*/
|
2011-07-06 16:20:46 +00:00
|
|
|
NETSOCKET net_tcp_create(NETADDR bindaddr);
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Makes the socket start listening for new connections.
|
|
|
|
*
|
|
|
|
* @ingroup Network-TCP
|
|
|
|
*
|
|
|
|
* @param sock Socket to start listen to.
|
|
|
|
* @param backlog Size of the queue of incoming connections to keep.
|
|
|
|
*
|
|
|
|
* @return 0 on success.
|
|
|
|
*/
|
2008-07-06 11:21:21 +00:00
|
|
|
int net_tcp_listen(NETSOCKET sock, int backlog);
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Polls a listning socket for a new connection.
|
|
|
|
*
|
|
|
|
* @ingroup Network-TCP
|
|
|
|
*
|
|
|
|
* @param sock - Listning socket to poll.
|
|
|
|
* @param new_sock - Pointer to a socket to fill in with the new socket.
|
|
|
|
* @param addr - Pointer to an address that will be filled in the remote address (optional, can be NULL).
|
|
|
|
*
|
|
|
|
* @return A non-negative integer on success. Negative integer on failure.
|
|
|
|
*/
|
2008-08-05 21:37:33 +00:00
|
|
|
int net_tcp_accept(NETSOCKET sock, NETSOCKET *new_sock, NETADDR *addr);
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Connects one socket to another.
|
|
|
|
*
|
|
|
|
* @ingroup Network-TCP
|
|
|
|
*
|
|
|
|
* @param sock Socket to connect.
|
|
|
|
* @param addr Address to connect to.
|
|
|
|
*
|
|
|
|
* @return 0 on success.
|
|
|
|
*
|
|
|
|
*/
|
2008-08-05 21:37:33 +00:00
|
|
|
int net_tcp_connect(NETSOCKET sock, const NETADDR *addr);
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Sends data to a TCP stream.
|
|
|
|
*
|
|
|
|
* @ingroup Network-TCP
|
|
|
|
*
|
|
|
|
* @param sock Socket to send data to.
|
|
|
|
* @param data Pointer to the data to send.
|
|
|
|
* @param size Size of the data to send.
|
|
|
|
*
|
|
|
|
* @return Number of bytes sent. Negative value on failure.
|
|
|
|
*/
|
2008-07-06 11:21:21 +00:00
|
|
|
int net_tcp_send(NETSOCKET sock, const void *data, int size);
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2022-02-18 10:35:46 +00:00
|
|
|
/**
|
|
|
|
* Recvives data from a TCP stream.
|
|
|
|
*
|
|
|
|
* @ingroup Network-TCP
|
|
|
|
*
|
|
|
|
* @param sock Socket to recvive data from.
|
|
|
|
* @param data Pointer to a buffer to write the data to
|
|
|
|
* @param max_size Maximum of data to write to the buffer.
|
|
|
|
*
|
|
|
|
* @return Number of bytes recvived. Negative value on failure. When in
|
|
|
|
* non-blocking mode, it returns 0 when there is no more data to
|
|
|
|
* be fetched.
|
|
|
|
*/
|
2008-07-06 11:21:21 +00:00
|
|
|
int net_tcp_recv(NETSOCKET sock, void *data, int maxsize);
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* Closes a TCP socket.
|
|
|
|
*
|
|
|
|
* @ingroup Network-TCP
|
|
|
|
*
|
|
|
|
* @param sock Socket to close.
|
|
|
|
*
|
|
|
|
* @return 0 on success. Negative value on failure.
|
|
|
|
*/
|
2008-07-06 11:21:21 +00:00
|
|
|
int net_tcp_close(NETSOCKET sock);
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2017-12-20 15:56:34 +00:00
|
|
|
#if defined(CONF_FAMILY_UNIX)
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* @defgroup Network-Unix-Sockets
|
|
|
|
* @ingroup Network-General
|
|
|
|
*/
|
2017-12-20 15:56:34 +00:00
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* Creates an unnamed unix datagram socket.
|
|
|
|
*
|
|
|
|
* @ingroup Network-Unix-Sockets
|
|
|
|
*
|
|
|
|
* @return On success it returns a handle to the socket. On failure it returns -1.
|
|
|
|
*/
|
2021-06-15 02:15:24 +00:00
|
|
|
UNIXSOCKET net_unix_create_unnamed();
|
2017-12-20 15:56:34 +00:00
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* Sends data to a Unix socket.
|
|
|
|
*
|
|
|
|
* @ingroup Network-Unix-Sockets
|
|
|
|
*
|
|
|
|
* @param sock Socket to use.
|
|
|
|
* @param addr Where to send the packet.
|
|
|
|
* @param data Pointer to the packet data to send.
|
|
|
|
* @param size Size of the packet.
|
|
|
|
*
|
|
|
|
* @return Number of bytes sent. Negative value on failure.
|
|
|
|
*/
|
2017-12-20 15:56:34 +00:00
|
|
|
int net_unix_send(UNIXSOCKET sock, UNIXSOCKETADDR *addr, void *data, int size);
|
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* Sets the unixsocketaddress for a path to a socket file.
|
|
|
|
*
|
|
|
|
* @ingroup Network-Unix-Sockets
|
|
|
|
*
|
|
|
|
* @param addr Pointer to the addressstruct to fill.
|
|
|
|
* @param path Path to the (named) unix socket.
|
|
|
|
*/
|
2017-12-20 15:56:34 +00:00
|
|
|
void net_unix_set_addr(UNIXSOCKETADDR *addr, const char *path);
|
2017-12-20 15:56:44 +00:00
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* Closes a Unix socket.
|
|
|
|
*
|
|
|
|
* @ingroup Network-Unix-Sockets
|
|
|
|
*
|
|
|
|
* @param sock Socket to close.
|
|
|
|
*/
|
2017-12-20 15:56:44 +00:00
|
|
|
void net_unix_close(UNIXSOCKET sock);
|
|
|
|
|
2023-01-20 20:35:45 +00:00
|
|
|
#elif defined(CONF_FAMILY_WINDOWS)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats a Windows error code as a human-readable string.
|
|
|
|
*
|
|
|
|
* @param error The Windows error code.
|
|
|
|
*
|
2023-05-02 15:41:42 +00:00
|
|
|
* @return A new std::string representing the error code.
|
2023-01-20 20:35:45 +00:00
|
|
|
*/
|
2023-05-02 15:41:42 +00:00
|
|
|
std::string windows_format_system_message(unsigned long error);
|
2023-01-20 20:35:45 +00:00
|
|
|
|
2017-12-20 15:56:34 +00:00
|
|
|
#endif
|
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* @defgroup Strings
|
|
|
|
*
|
|
|
|
* String related functions.
|
|
|
|
*/
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* Appends a string to another.
|
|
|
|
*
|
|
|
|
* @ingroup Strings
|
|
|
|
*
|
|
|
|
* @param dst Pointer to a buffer that contains a string.
|
|
|
|
* @param src String to append.
|
|
|
|
* @param dst_size Size of the buffer of the dst string.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
* @remark Guarantees that dst string will contain zero-termination.
|
|
|
|
*/
|
2008-07-06 11:21:21 +00:00
|
|
|
void str_append(char *dst, const char *src, int dst_size);
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2023-06-13 19:06:48 +00:00
|
|
|
/**
|
|
|
|
* Appends a string to a fixed-size array of chars.
|
|
|
|
*
|
|
|
|
* @ingroup Strings
|
|
|
|
*
|
|
|
|
* @param dst Array that shall receive the string.
|
|
|
|
* @param src String to append.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
* @remark Guarantees that dst string will contain zero-termination.
|
|
|
|
*/
|
|
|
|
template<int N>
|
|
|
|
void str_append(char (&dst)[N], const char *src)
|
|
|
|
{
|
|
|
|
str_append(dst, src, N);
|
|
|
|
}
|
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* Copies a string to another.
|
|
|
|
*
|
|
|
|
* @ingroup Strings
|
|
|
|
*
|
|
|
|
* @param dst Pointer to a buffer that shall receive the string.
|
|
|
|
* @param src String to be copied.
|
|
|
|
* @param dst_size Size of the buffer dst.
|
|
|
|
*
|
2023-01-28 13:18:02 +00:00
|
|
|
* @return Length of written string, even if it has been truncated
|
|
|
|
*
|
2022-03-04 11:02:18 +00:00
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
* @remark Guarantees that dst string will contain zero-termination.
|
|
|
|
*/
|
2023-01-28 13:18:02 +00:00
|
|
|
int str_copy(char *dst, const char *src, int dst_size);
|
2008-02-10 21:54:52 +00:00
|
|
|
|
2023-06-13 19:08:59 +00:00
|
|
|
/**
|
|
|
|
* Copies a string to a fixed-size array of chars.
|
|
|
|
*
|
|
|
|
* @ingroup Strings
|
|
|
|
*
|
|
|
|
* @param dst Array that shall receive the string.
|
|
|
|
* @param src String to be copied.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
* @remark Guarantees that dst string will contain zero-termination.
|
|
|
|
*/
|
|
|
|
template<int N>
|
|
|
|
void str_copy(char (&dst)[N], const char *src)
|
|
|
|
{
|
|
|
|
str_copy(dst, src, N);
|
|
|
|
}
|
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* Truncates a utf8 encoded string to a given length.
|
|
|
|
*
|
|
|
|
* @ingroup Strings
|
|
|
|
*
|
|
|
|
* @param dst Pointer to a buffer that shall receive the string.
|
|
|
|
* @param dst_size Size of the buffer dst.
|
|
|
|
* @param str String to be truncated.
|
|
|
|
* @param truncation_len Maximum codepoints in the returned string.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as utf8-encoded zero-terminated strings.
|
|
|
|
* @remark Guarantees that dst string will contain zero-termination.
|
|
|
|
*/
|
2020-08-02 11:45:20 +00:00
|
|
|
void str_utf8_truncate(char *dst, int dst_size, const char *src, int truncation_len);
|
2019-04-16 00:24:24 +00:00
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* Truncates a string to a given length.
|
|
|
|
*
|
|
|
|
* @ingroup Strings
|
|
|
|
*
|
|
|
|
* @param dst Pointer to a buffer that shall receive the string.
|
|
|
|
* @param dst_size Size of the buffer dst.
|
|
|
|
* @param src String to be truncated.
|
|
|
|
* @param truncation_len Maximum length of the returned string (not
|
|
|
|
* counting the zero termination).
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
2022-10-25 16:51:56 +00:00
|
|
|
* @remark Guarantees that dst string will contain zero-termination.
|
2022-03-04 11:02:18 +00:00
|
|
|
*/
|
2020-09-03 12:08:26 +00:00
|
|
|
void str_truncate(char *dst, int dst_size, const char *src, int truncation_len);
|
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* Returns the length of a zero terminated string.
|
|
|
|
*
|
|
|
|
* @ingroup Strings
|
|
|
|
*
|
|
|
|
* @param str Pointer to the string.
|
|
|
|
*
|
|
|
|
* @return Length of string in bytes excluding the zero termination.
|
2022-06-06 10:50:08 +00:00
|
|
|
*/
|
2008-11-08 08:27:11 +00:00
|
|
|
int str_length(const char *str);
|
|
|
|
|
2023-03-19 10:00:50 +00:00
|
|
|
/**
|
|
|
|
* Performs printf formatting into a buffer.
|
|
|
|
*
|
|
|
|
* @ingroup Strings
|
|
|
|
*
|
|
|
|
* @param buffer Pointer to the buffer to receive the formatted string.
|
|
|
|
* @param buffer_size Size of the buffer.
|
|
|
|
* @param format printf formatting string.
|
|
|
|
* @param args The variable argument list.
|
|
|
|
*
|
|
|
|
* @return Length of written string, even if it has been truncated.
|
|
|
|
*
|
|
|
|
* @remark See the C manual for syntax for the printf formatting string.
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
* @remark Guarantees that buffer string will contain zero-termination.
|
|
|
|
*/
|
|
|
|
int str_format_v(char *buffer, int buffer_size, const char *format, va_list args)
|
|
|
|
GNUC_ATTRIBUTE((format(printf, 3, 0)));
|
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* Performs printf formatting into a buffer.
|
|
|
|
*
|
|
|
|
* @ingroup Strings
|
|
|
|
*
|
|
|
|
* @param buffer Pointer to the buffer to receive the formatted string.
|
|
|
|
* @param buffer_size Size of the buffer.
|
|
|
|
* @param format printf formatting string.
|
|
|
|
* @param ... Parameters for the formatting.
|
|
|
|
*
|
2023-03-19 10:00:50 +00:00
|
|
|
* @return Length of written string, even if it has been truncated.
|
2022-03-04 11:02:18 +00:00
|
|
|
*
|
|
|
|
* @remark See the C manual for syntax for the printf formatting string.
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
2023-03-19 10:00:50 +00:00
|
|
|
* @remark Guarantees that buffer string will contain zero-termination.
|
2022-03-04 11:02:18 +00:00
|
|
|
*/
|
2017-07-26 01:58:00 +00:00
|
|
|
int str_format(char *buffer, int buffer_size, const char *format, ...)
|
2020-09-26 19:41:58 +00:00
|
|
|
GNUC_ATTRIBUTE((format(printf, 3, 4)));
|
2008-01-29 21:39:41 +00:00
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* Trims specific number of words at the start of a string.
|
|
|
|
*
|
|
|
|
* @ingroup Strings
|
|
|
|
*
|
|
|
|
* @param str String to trim the words from.
|
|
|
|
* @param words Count of words to trim.
|
|
|
|
*
|
|
|
|
* @return Trimmed string
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
2022-09-28 11:53:05 +00:00
|
|
|
* @remark Leading whitespace is always trimmed.
|
2022-03-04 11:02:18 +00:00
|
|
|
*/
|
2022-09-28 11:53:05 +00:00
|
|
|
const char *str_trim_words(const char *str, int words);
|
2015-02-19 21:54:47 +00:00
|
|
|
|
2022-09-11 21:39:16 +00:00
|
|
|
/**
|
|
|
|
* Check whether string has ASCII control characters.
|
|
|
|
*
|
|
|
|
* @ingroup Strings
|
|
|
|
*
|
|
|
|
* @param str String to check.
|
|
|
|
*
|
|
|
|
* @return Whether the string has ASCII control characters.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
|
|
|
bool str_has_cc(const char *str);
|
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* Replaces all characters below 32 with whitespace.
|
|
|
|
*
|
|
|
|
* @ingroup Strings
|
|
|
|
*
|
|
|
|
* @param str String to sanitize.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
2010-08-12 13:22:07 +00:00
|
|
|
void str_sanitize_cc(char *str);
|
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* Replaces all characters below 32 with whitespace with
|
|
|
|
* exception to \t, \n and \r.
|
|
|
|
*
|
|
|
|
* @ingroup Strings
|
|
|
|
*
|
|
|
|
* @param str String to sanitize.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
2022-06-06 10:50:08 +00:00
|
|
|
*/
|
2008-02-24 16:03:58 +00:00
|
|
|
void str_sanitize(char *str);
|
2008-07-06 11:21:21 +00:00
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* Replaces all invalid filename characters with whitespace.
|
|
|
|
*
|
|
|
|
* @param str String to sanitize.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
2022-06-06 10:50:08 +00:00
|
|
|
*/
|
2017-09-28 13:59:30 +00:00
|
|
|
void str_sanitize_filename(char *str);
|
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* Removes leading and trailing spaces and limits the use of multiple spaces.
|
|
|
|
*
|
|
|
|
* @ingroup Strings
|
|
|
|
*
|
|
|
|
* @param str String to clean up
|
|
|
|
*
|
2022-09-28 21:29:55 +00:00
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
2022-03-04 11:02:18 +00:00
|
|
|
*/
|
2013-09-04 14:44:04 +00:00
|
|
|
void str_clean_whitespaces(char *str);
|
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
2022-09-28 14:41:03 +00:00
|
|
|
* Skips leading non-whitespace characters.
|
2022-03-04 11:02:18 +00:00
|
|
|
*
|
|
|
|
* @ingroup Strings
|
|
|
|
*
|
|
|
|
* @param str Pointer to the string.
|
|
|
|
*
|
|
|
|
* @return Pointer to the first whitespace character found
|
|
|
|
* within the string.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
2022-09-28 14:41:03 +00:00
|
|
|
* @remark Whitespace is defined according to str_isspace.
|
2022-03-04 11:02:18 +00:00
|
|
|
*/
|
2010-10-25 16:41:15 +00:00
|
|
|
char *str_skip_to_whitespace(char *str);
|
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* @ingroup Strings
|
|
|
|
* @see str_skip_to_whitespace
|
|
|
|
*/
|
2020-02-04 16:30:36 +00:00
|
|
|
const char *str_skip_to_whitespace_const(const char *str);
|
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
2022-09-28 14:41:03 +00:00
|
|
|
* Skips leading whitespace characters.
|
2022-03-04 11:02:18 +00:00
|
|
|
*
|
|
|
|
* @ingroup Strings
|
|
|
|
*
|
|
|
|
* @param str Pointer to the string.
|
|
|
|
*
|
|
|
|
* Pointer to the first non-whitespace character found
|
|
|
|
* within the string.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
2022-09-28 14:41:03 +00:00
|
|
|
* @remark Whitespace is defined according to str_isspace.
|
2022-06-06 10:50:08 +00:00
|
|
|
*/
|
2010-08-12 13:22:07 +00:00
|
|
|
char *str_skip_whitespaces(char *str);
|
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* @ingroup Strings
|
|
|
|
* @see str_skip_whitespaces
|
|
|
|
*/
|
2020-02-04 16:30:36 +00:00
|
|
|
const char *str_skip_whitespaces_const(const char *str);
|
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* Compares to strings case insensitively.
|
|
|
|
*
|
|
|
|
* @ingroup Strings
|
|
|
|
*
|
|
|
|
* @param a String to compare.
|
|
|
|
* @param b String to compare.
|
|
|
|
*
|
|
|
|
* @return `< 0` - String a is less than string b
|
|
|
|
* @return `0` - String a is equal to string b
|
|
|
|
* @return `> 0` - String a is greater than string b
|
|
|
|
*
|
|
|
|
* @remark Only guaranteed to work with a-z/A-Z.
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
2008-03-29 17:20:21 +00:00
|
|
|
int str_comp_nocase(const char *a, const char *b);
|
2008-07-06 11:21:21 +00:00
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* Compares up to num characters of two strings case insensitively.
|
|
|
|
*
|
|
|
|
* @ingroup Strings
|
|
|
|
*
|
|
|
|
* @param a String to compare.
|
|
|
|
* @param b String to compare.
|
|
|
|
* @param num Maximum characters to compare
|
|
|
|
*
|
|
|
|
* @return `< 0` - String a is less than string b
|
|
|
|
* @return `0` - String a is equal to string b
|
|
|
|
* @return `> 0` - String a is greater than string b
|
|
|
|
*
|
|
|
|
* @remark Only guaranteed to work with a-z/A-Z.
|
|
|
|
* (use str_utf8_comp_nocase_num for unicode support)
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
2019-01-07 22:49:20 +00:00
|
|
|
int str_comp_nocase_num(const char *a, const char *b, int num);
|
2009-06-13 16:54:04 +00:00
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* Compares two strings case sensitive.
|
|
|
|
*
|
|
|
|
* @ingroup Strings
|
|
|
|
*
|
|
|
|
* @param a String to compare.
|
|
|
|
* @param b String to compare.
|
|
|
|
*
|
|
|
|
* @return `< 0` - String a is less than string b
|
|
|
|
* @return `0` - String a is equal to string b
|
|
|
|
* @return `> 0` - String a is greater than string b
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
2009-06-13 16:54:04 +00:00
|
|
|
int str_comp(const char *a, const char *b);
|
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* Compares up to num characters of two strings case sensitive.
|
|
|
|
*
|
|
|
|
* @ingroup Strings
|
|
|
|
*
|
|
|
|
* @param a String to compare.
|
|
|
|
* @param b String to compare.
|
|
|
|
* @param num Maximum characters to compare
|
|
|
|
*
|
|
|
|
* @return `< 0` - String a is less than string b
|
|
|
|
* @return `0` - String a is equal to string b
|
|
|
|
* @return `> 0` - String a is greater than string b
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
2022-06-06 10:50:08 +00:00
|
|
|
*/
|
2019-01-07 22:49:20 +00:00
|
|
|
int str_comp_num(const char *a, const char *b, int num);
|
2010-05-29 07:25:38 +00:00
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
2022-09-28 22:00:37 +00:00
|
|
|
* Compares two strings case insensitive, digit chars will be compared as numbers.
|
2022-03-04 11:02:18 +00:00
|
|
|
*
|
|
|
|
* @ingroup Strings
|
|
|
|
*
|
|
|
|
* @param a String to compare.
|
|
|
|
* @param b String to compare.
|
|
|
|
*
|
|
|
|
* @return `< 0` - String a is less than string b
|
|
|
|
* @return `0` - String a is equal to string b
|
|
|
|
* @return `> 0` - String a is greater than string b
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
2010-09-28 22:53:53 +00:00
|
|
|
int str_comp_filenames(const char *a, const char *b);
|
|
|
|
|
2022-03-13 10:42:45 +00:00
|
|
|
/*
|
|
|
|
Function: str_startswith_nocase
|
|
|
|
Checks case insensitive whether the string begins with a certain prefix.
|
|
|
|
|
|
|
|
Parameter:
|
|
|
|
str - String to check.
|
|
|
|
prefix - Prefix to look for.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A pointer to the string str after the string prefix, or 0 if
|
|
|
|
the string prefix isn't a prefix of the string str.
|
|
|
|
|
|
|
|
Remarks:
|
|
|
|
- The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
|
|
|
const char *str_startswith_nocase(const char *str, const char *prefix);
|
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
2022-04-29 08:26:45 +00:00
|
|
|
* Checks case sensitive whether the string begins with a certain prefix.
|
2022-03-04 11:02:18 +00:00
|
|
|
*
|
|
|
|
* @ingroup Strings
|
|
|
|
*
|
|
|
|
* @param str String to check.
|
|
|
|
* @param prefix Prefix to look for.
|
|
|
|
*
|
|
|
|
* @return A pointer to the string str after the string prefix, or 0 if
|
|
|
|
* the string prefix isn't a prefix of the string str.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
2018-07-25 08:23:07 +00:00
|
|
|
const char *str_startswith(const char *str, const char *prefix);
|
|
|
|
|
2022-03-13 10:42:45 +00:00
|
|
|
/*
|
|
|
|
Function: str_endswith_nocase
|
|
|
|
Checks case insensitive whether the string ends with a certain suffix.
|
|
|
|
|
|
|
|
Parameter:
|
|
|
|
str - String to check.
|
|
|
|
suffix - Suffix to look for.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A pointer to the beginning of the suffix in the string str, or
|
|
|
|
0 if the string suffix isn't a suffix of the string str.
|
|
|
|
|
|
|
|
Remarks:
|
|
|
|
- The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
|
|
|
const char *str_endswith_nocase(const char *str, const char *suffix);
|
|
|
|
|
2018-07-25 08:23:07 +00:00
|
|
|
/*
|
|
|
|
Function: str_endswith
|
2022-03-13 10:42:45 +00:00
|
|
|
Checks case sensitive whether the string ends with a certain suffix.
|
2018-07-25 08:23:07 +00:00
|
|
|
|
|
|
|
Parameter:
|
|
|
|
str - String to check.
|
|
|
|
suffix - Suffix to look for.
|
|
|
|
|
|
|
|
Returns:
|
2018-07-26 12:04:44 +00:00
|
|
|
A pointer to the beginning of the suffix in the string str, or
|
|
|
|
0 if the string suffix isn't a suffix of the string str.
|
2018-07-25 08:23:07 +00:00
|
|
|
|
|
|
|
Remarks:
|
|
|
|
- The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
2018-07-26 12:04:44 +00:00
|
|
|
const char *str_endswith(const char *str, const char *suffix);
|
2018-07-25 08:23:07 +00:00
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* Computes the edit distance between two strings.
|
|
|
|
*
|
|
|
|
* @param a First string for the edit distance.
|
|
|
|
* @param b Second string for the edit distance.
|
|
|
|
*
|
|
|
|
* @return The edit distance between the both strings.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
2022-06-06 10:50:08 +00:00
|
|
|
*/
|
2018-03-06 17:41:18 +00:00
|
|
|
int str_utf8_dist(const char *a, const char *b);
|
|
|
|
|
2022-03-04 11:02:18 +00:00
|
|
|
/**
|
|
|
|
* Computes the edit distance between two strings, allows buffers
|
|
|
|
* to be passed in.
|
|
|
|
*
|
|
|
|
* @ingroup Strings
|
|
|
|
*
|
|
|
|
* @param a - First string for the edit distance.
|
|
|
|
* @param b - Second string for the edit distance.
|
|
|
|
* @param buf - Buffer for the function.
|
|
|
|
* @param buf_len Length of the buffer, must be at least as long as
|
|
|
|
* twice the length of both strings combined plus two.
|
|
|
|
*
|
|
|
|
* @return The edit distance between the both strings.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
2022-06-06 10:50:08 +00:00
|
|
|
*/
|
2018-03-06 17:41:18 +00:00
|
|
|
int str_utf8_dist_buffer(const char *a, const char *b, int *buf, int buf_len);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Function: str_utf32_dist_buffer
|
|
|
|
Computes the edit distance between two strings, allows buffers
|
|
|
|
to be passed in.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
a - First string for the edit distance.
|
|
|
|
a_len - Length of the first string.
|
|
|
|
b - Second string for the edit distance.
|
|
|
|
b_len - Length of the second string.
|
|
|
|
buf - Buffer for the function.
|
|
|
|
buf_len - Length of the buffer, must be at least as long as
|
|
|
|
the length of both strings combined plus two.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The edit distance between the both strings.
|
|
|
|
|
|
|
|
Remarks:
|
|
|
|
- The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
|
|
|
int str_utf32_dist_buffer(const int *a, int a_len, const int *b, int b_len, int *buf, int buf_len);
|
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
/*
|
|
|
|
Function: str_find_nocase
|
2019-01-07 22:49:20 +00:00
|
|
|
Finds a string inside another string case insensitively.
|
2008-07-06 11:21:21 +00:00
|
|
|
|
|
|
|
Parameters:
|
|
|
|
haystack - String to search in
|
|
|
|
needle - String to search for
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
Returns:
|
|
|
|
A pointer into haystack where the needle was found.
|
2019-04-16 00:24:24 +00:00
|
|
|
Returns NULL if needle could not be found.
|
2008-07-06 11:21:21 +00:00
|
|
|
|
|
|
|
Remarks:
|
2019-01-07 22:49:20 +00:00
|
|
|
- Only guaranteed to work with a-z/A-Z.
|
|
|
|
(use str_utf8_find_nocase for unicode support)
|
2018-07-25 06:59:29 +00:00
|
|
|
- The strings are treated as zero-terminated strings.
|
2008-07-06 11:21:21 +00:00
|
|
|
*/
|
2008-03-29 17:20:21 +00:00
|
|
|
const char *str_find_nocase(const char *haystack, const char *needle);
|
2008-07-06 11:21:21 +00:00
|
|
|
|
2009-06-13 16:54:04 +00:00
|
|
|
/*
|
|
|
|
Function: str_find
|
|
|
|
Finds a string inside another string case sensitive.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
haystack - String to search in
|
|
|
|
needle - String to search for
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2009-06-13 16:54:04 +00:00
|
|
|
Returns:
|
|
|
|
A pointer into haystack where the needle was found.
|
2019-04-16 00:24:24 +00:00
|
|
|
Returns NULL if needle could not be found.
|
2009-06-13 16:54:04 +00:00
|
|
|
|
|
|
|
Remarks:
|
2018-07-25 06:59:29 +00:00
|
|
|
- The strings are treated as zero-terminated strings.
|
2009-06-13 16:54:04 +00:00
|
|
|
*/
|
|
|
|
const char *str_find(const char *haystack, const char *needle);
|
2008-07-06 11:21:21 +00:00
|
|
|
|
2022-09-29 08:33:39 +00:00
|
|
|
/**
|
2022-10-25 16:51:56 +00:00
|
|
|
* Finds the last occurrence of a character
|
2022-09-29 08:33:39 +00:00
|
|
|
*
|
|
|
|
* @ingroup Strings
|
|
|
|
*
|
|
|
|
* @param haystack String to search in
|
|
|
|
* @param needle Character to search for
|
2019-04-16 00:24:24 +00:00
|
|
|
|
2022-09-29 08:33:39 +00:00
|
|
|
* @return A pointer into haystack where the needle was found.
|
|
|
|
* Returns NULL if needle could not be found.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
* @remark The zero-terminator character can also be found with this function.
|
|
|
|
*/
|
2019-04-16 00:24:24 +00:00
|
|
|
const char *str_rchr(const char *haystack, char needle);
|
|
|
|
|
2022-09-29 08:41:22 +00:00
|
|
|
/**
|
|
|
|
* Counts the number of occurrences of a character in a string.
|
|
|
|
*
|
|
|
|
* @ingroup Strings
|
|
|
|
*
|
|
|
|
* @param haystack String to count in
|
|
|
|
* @param needle Character to count
|
2022-08-17 17:02:18 +00:00
|
|
|
|
2022-09-29 08:41:22 +00:00
|
|
|
* @return The number of characters in the haystack string matching
|
|
|
|
* the needle character.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
* @remark The number of zero-terminator characters cannot be counted.
|
|
|
|
*/
|
2022-08-17 17:02:18 +00:00
|
|
|
int str_countchr(const char *haystack, char needle);
|
|
|
|
|
2023-01-17 23:08:33 +00:00
|
|
|
/**
|
|
|
|
* Takes a datablock and generates a hex string of it, with spaces between bytes.
|
|
|
|
*
|
|
|
|
* @param dst Buffer to fill with hex data.
|
|
|
|
* @param dst_size Size of the buffer (at least 3 * data_size + 1 to contain all data).
|
|
|
|
* @param data Data to turn into hex.
|
|
|
|
* @param data_size Size of the data.
|
|
|
|
*
|
|
|
|
* @remark The destination buffer will be zero-terminated.
|
|
|
|
*/
|
2008-04-05 14:50:43 +00:00
|
|
|
void str_hex(char *dst, int dst_size, const void *data, int data_size);
|
2008-02-24 16:03:58 +00:00
|
|
|
|
2023-01-17 23:09:07 +00:00
|
|
|
/**
|
|
|
|
* Takes a datablock and generates a hex string of it, in the C style array format,
|
|
|
|
* i.e. with bytes formatted in 0x00-0xFF notation and commas with spaces between the bytes.
|
|
|
|
* The output can be split over multiple lines by specifying the maximum number of bytes
|
|
|
|
* that should be printed per line.
|
|
|
|
*
|
|
|
|
* @param dst Buffer to fill with hex data.
|
|
|
|
* @param dst_size Size of the buffer (at least 6 * data_size + 1 to contain all data).
|
|
|
|
* @param data Data to turn into hex.
|
|
|
|
* @param data_size Size of the data.
|
|
|
|
* @param bytes_per_line After this many printed bytes a newline will be printed.
|
|
|
|
*
|
|
|
|
* @remark The destination buffer will be zero-terminated.
|
|
|
|
*/
|
|
|
|
void str_hex_cstyle(char *dst, int dst_size, const void *data, int data_size, int bytes_per_line = 12);
|
|
|
|
|
2017-03-02 15:16:29 +00:00
|
|
|
/*
|
|
|
|
Function: str_hex_decode
|
2018-08-19 10:12:11 +00:00
|
|
|
Takes a hex string *without spaces between bytes* and returns a
|
|
|
|
byte array.
|
2017-03-02 15:16:29 +00:00
|
|
|
|
2018-03-06 17:41:18 +00:00
|
|
|
Parameters:
|
|
|
|
dst - Buffer for the byte array
|
|
|
|
dst_size - size of the buffer
|
|
|
|
data - String to decode
|
2017-03-04 20:06:07 +00:00
|
|
|
|
2018-03-06 17:41:18 +00:00
|
|
|
Returns:
|
|
|
|
2 - String doesn't exactly fit the buffer
|
|
|
|
1 - Invalid character in string
|
|
|
|
0 - Success
|
2017-03-04 20:06:07 +00:00
|
|
|
|
2018-03-06 17:41:18 +00:00
|
|
|
Remarks:
|
|
|
|
- The contents of the buffer is only valid on success
|
2017-03-02 15:16:29 +00:00
|
|
|
*/
|
2018-08-19 10:12:11 +00:00
|
|
|
int str_hex_decode(void *dst, int dst_size, const char *src);
|
2021-07-16 00:55:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Function: str_base64
|
|
|
|
Takes a datablock and generates the base64 encoding of it.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
dst - Buffer to fill with base64 data
|
|
|
|
dst_size - Size of the buffer
|
|
|
|
data - Data to turn into base64
|
|
|
|
data - Size of the data
|
|
|
|
|
|
|
|
Remarks:
|
|
|
|
- The destination buffer will be zero-terminated
|
|
|
|
*/
|
|
|
|
void str_base64(char *dst, int dst_size, const void *data, int data_size);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Function: str_base64_decode
|
|
|
|
Takes a base64 string without any whitespace and correct
|
|
|
|
padding and returns a byte array.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
dst - Buffer for the byte array
|
|
|
|
dst_size - Size of the buffer
|
|
|
|
data - String to decode
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
<0 - Error
|
|
|
|
>= 0 - Success, length of the resulting byte buffer
|
|
|
|
|
|
|
|
Remarks:
|
|
|
|
- The contents of the buffer is only valid on success
|
|
|
|
*/
|
|
|
|
int str_base64_decode(void *dst, int dst_size, const char *data);
|
|
|
|
|
2010-12-07 23:26:55 +00:00
|
|
|
/*
|
|
|
|
Function: str_timestamp
|
|
|
|
Copies a time stamp in the format year-month-day_hour-minute-second to the string.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
buffer - Pointer to a buffer that shall receive the time stamp string.
|
|
|
|
buffer_size - Size of the buffer.
|
|
|
|
|
|
|
|
Remarks:
|
|
|
|
- Guarantees that buffer string will contain zero-termination.
|
|
|
|
*/
|
|
|
|
void str_timestamp(char *buffer, int buffer_size);
|
2019-07-14 13:42:16 +00:00
|
|
|
void str_timestamp_format(char *buffer, int buffer_size, const char *format)
|
2020-09-26 19:41:58 +00:00
|
|
|
GNUC_ATTRIBUTE((format(strftime, 3, 0)));
|
2017-07-26 01:58:00 +00:00
|
|
|
void str_timestamp_ex(time_t time, char *buffer, int buffer_size, const char *format)
|
2020-09-26 19:41:58 +00:00
|
|
|
GNUC_ATTRIBUTE((format(strftime, 4, 0)));
|
2010-12-07 23:26:55 +00:00
|
|
|
|
2023-12-31 13:32:33 +00:00
|
|
|
/**
|
|
|
|
* Parses a string into a timestamp following a specified format.
|
|
|
|
*
|
|
|
|
* @ingroup Timestamp
|
|
|
|
*
|
|
|
|
* @param string Pointer to the string to parse
|
|
|
|
* @param format The time format to use (for example FORMAT_NOSPACE below)
|
|
|
|
* @param timestamp Pointer to the timestamp result
|
|
|
|
*
|
|
|
|
* @return true on success, false if the string could not be parsed with the specified format
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
bool timestamp_from_str(const char *string, const char *format, time_t *timestamp)
|
|
|
|
GNUC_ATTRIBUTE((format(strftime, 2, 0)));
|
|
|
|
|
2017-08-04 20:38:22 +00:00
|
|
|
#define FORMAT_TIME "%H:%M:%S"
|
|
|
|
#define FORMAT_SPACE "%Y-%m-%d %H:%M:%S"
|
|
|
|
#define FORMAT_NOSPACE "%Y-%m-%d_%H-%M-%S"
|
|
|
|
|
2020-10-18 20:44:02 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
TIME_DAYS,
|
|
|
|
TIME_HOURS,
|
|
|
|
TIME_MINS,
|
|
|
|
TIME_HOURS_CENTISECS,
|
|
|
|
TIME_MINS_CENTISECS,
|
2023-11-20 11:23:56 +00:00
|
|
|
TIME_SECS_CENTISECS,
|
2020-10-18 20:44:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Function: str_times
|
|
|
|
Formats a time string.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
centisecs - Time in centiseconds, minimum value clamped to 0
|
|
|
|
format - Format of the time string, see enum above, for example TIME_DAYS
|
|
|
|
buffer - Pointer to a buffer that shall receive the time stamp string.
|
|
|
|
buffer_size - Size of the buffer.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Number of bytes written, -1 on invalid format or buffer_size <= 0
|
|
|
|
|
|
|
|
Remarks:
|
|
|
|
- Guarantees that buffer string will contain zero-termination, assuming
|
|
|
|
buffer_size > 0.
|
|
|
|
*/
|
2021-06-23 05:05:49 +00:00
|
|
|
int str_time(int64_t centisecs, int format, char *buffer, int buffer_size);
|
2020-10-18 20:44:02 +00:00
|
|
|
int str_time_float(float secs, int format, char *buffer, int buffer_size);
|
|
|
|
|
2017-07-08 09:03:51 +00:00
|
|
|
/*
|
|
|
|
Function: str_escape
|
|
|
|
Escapes \ and " characters in a string.
|
|
|
|
|
|
|
|
Parameters:
|
2017-07-08 11:06:03 +00:00
|
|
|
dst - Destination array pointer, gets increased, will point to
|
|
|
|
the terminating null.
|
2017-07-08 09:03:51 +00:00
|
|
|
src - Source array
|
|
|
|
end - End of destination array
|
|
|
|
*/
|
|
|
|
void str_escape(char **dst, const char *src, const char *end);
|
|
|
|
|
2023-01-21 19:54:46 +00:00
|
|
|
/**
|
|
|
|
* @defgroup Filesystem
|
|
|
|
*
|
|
|
|
* Utilities for accessing the file system.
|
|
|
|
*/
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2023-01-21 19:54:46 +00:00
|
|
|
/**
|
|
|
|
* Lists the files and folders in a directory.
|
|
|
|
*
|
|
|
|
* @ingroup Filesystem
|
|
|
|
*
|
|
|
|
* @param dir Directory to list.
|
|
|
|
* @param cb Callback function to call for each entry.
|
|
|
|
* @param type Type of the directory.
|
|
|
|
* @param user Pointer to give to the callback.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
2021-08-23 10:49:15 +00:00
|
|
|
void fs_listdir(const char *dir, FS_LISTDIR_CALLBACK cb, int type, void *user);
|
|
|
|
|
2023-01-21 19:54:46 +00:00
|
|
|
/**
|
|
|
|
* Lists the files and folders in a directory and gets additional file information.
|
|
|
|
*
|
|
|
|
* @ingroup Filesystem
|
|
|
|
*
|
|
|
|
* @param dir Directory to list.
|
|
|
|
* @param cb Callback function to call for each entry.
|
|
|
|
* @param type Type of the directory.
|
|
|
|
* @param user Pointer to give to the callback.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
2021-08-23 10:49:15 +00:00
|
|
|
void fs_listdir_fileinfo(const char *dir, FS_LISTDIR_CALLBACK_FILEINFO cb, int type, void *user);
|
2008-07-06 11:21:21 +00:00
|
|
|
|
2023-01-21 19:54:46 +00:00
|
|
|
/**
|
|
|
|
* Creates a directory.
|
|
|
|
*
|
|
|
|
* @ingroup Filesystem
|
|
|
|
*
|
|
|
|
* @param path Directory to create.
|
|
|
|
*
|
|
|
|
* @return 0 on success. Negative value on failure.
|
|
|
|
*
|
|
|
|
* @remark Does not create several directories if needed. "a/b/c" will
|
|
|
|
* result in a failure if b or a does not exist.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
2008-07-06 11:21:21 +00:00
|
|
|
int fs_makedir(const char *path);
|
|
|
|
|
2023-01-21 19:54:46 +00:00
|
|
|
/**
|
|
|
|
* Removes a directory.
|
|
|
|
*
|
|
|
|
* @ingroup Filesystem
|
|
|
|
*
|
|
|
|
* @param path Directory to remove.
|
|
|
|
*
|
|
|
|
* @return 0 on success. Negative value on failure.
|
|
|
|
*
|
|
|
|
* @remark Cannot remove a non-empty directory.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
2021-04-21 10:52:27 +00:00
|
|
|
int fs_removedir(const char *path);
|
|
|
|
|
2023-01-21 19:54:46 +00:00
|
|
|
/**
|
2024-03-23 19:39:52 +00:00
|
|
|
* Recursively creates parent directories for a file or directory.
|
2023-01-21 19:54:46 +00:00
|
|
|
*
|
|
|
|
* @ingroup Filesystem
|
|
|
|
*
|
2024-03-23 19:39:52 +00:00
|
|
|
* @param path File or directory for which to create parent directories.
|
2023-01-21 19:54:46 +00:00
|
|
|
*
|
|
|
|
* @return 0 on success. Negative value on failure.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
2016-05-01 12:20:55 +00:00
|
|
|
int fs_makedir_rec_for(const char *path);
|
|
|
|
|
2023-01-21 19:54:46 +00:00
|
|
|
/**
|
|
|
|
* Fetches per user configuration directory.
|
|
|
|
*
|
|
|
|
* @ingroup Filesystem
|
|
|
|
*
|
|
|
|
* @param appname Name of the application.
|
|
|
|
* @param path Buffer that will receive the storage path.
|
|
|
|
* @param max Size of the buffer.
|
|
|
|
*
|
|
|
|
* @return 0 on success. Negative value on failure.
|
|
|
|
*
|
|
|
|
* @remark Returns ~/.appname on UNIX based systems.
|
|
|
|
* @remark Returns ~/Library/Applications Support/appname on macOS.
|
|
|
|
* @remark Returns %APPDATA%/Appname on Windows based systems.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
2008-07-06 11:21:21 +00:00
|
|
|
int fs_storage_path(const char *appname, char *path, int max);
|
|
|
|
|
2023-01-22 15:50:10 +00:00
|
|
|
/**
|
|
|
|
* Checks if a file exists.
|
|
|
|
*
|
|
|
|
* @ingroup Filesystem
|
|
|
|
*
|
|
|
|
* @param path the path to check.
|
|
|
|
*
|
|
|
|
* @return 1 if a file with the given path exists,
|
|
|
|
* 0 on failure or if the file does not exist.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
|
|
|
int fs_is_file(const char *path);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2023-01-21 19:54:46 +00:00
|
|
|
/**
|
|
|
|
* Checks if a folder exists.
|
|
|
|
*
|
|
|
|
* @ingroup Filesystem
|
|
|
|
*
|
|
|
|
* @param path the path to check.
|
|
|
|
*
|
|
|
|
* @return 1 if a folder with the given path exists,
|
|
|
|
* 0 on failure or if the folder does not exist.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
2008-10-01 17:16:22 +00:00
|
|
|
int fs_is_dir(const char *path);
|
|
|
|
|
2023-01-21 19:54:46 +00:00
|
|
|
/**
|
|
|
|
* Checks whether a given path is relative or absolute.
|
|
|
|
*
|
|
|
|
* @ingroup Filesystem
|
|
|
|
*
|
|
|
|
* @param path Path to check.
|
|
|
|
*
|
|
|
|
* @return 1 if relative, 0 if absolute.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
2022-08-20 18:54:25 +00:00
|
|
|
int fs_is_relative_path(const char *path);
|
|
|
|
|
2023-01-21 19:54:46 +00:00
|
|
|
/**
|
|
|
|
* Changes the current working directory.
|
|
|
|
*
|
|
|
|
* @ingroup Filesystem
|
|
|
|
*
|
|
|
|
* @param path New working directory path.
|
|
|
|
*
|
|
|
|
* @return 0 on success, 1 on failure.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
2008-10-01 17:16:22 +00:00
|
|
|
int fs_chdir(const char *path);
|
2008-07-06 11:21:21 +00:00
|
|
|
|
2023-01-21 19:54:46 +00:00
|
|
|
/**
|
|
|
|
* Gets the current working directory.
|
|
|
|
*
|
|
|
|
* @ingroup Filesystem
|
|
|
|
*
|
|
|
|
* @param buffer Buffer that will receive the current working directory.
|
|
|
|
* @param buffer_size Size of the buffer.
|
|
|
|
*
|
|
|
|
* @return Pointer to the buffer on success, nullptr on failure.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
2010-12-11 22:10:13 +00:00
|
|
|
char *fs_getcwd(char *buffer, int buffer_size);
|
|
|
|
|
2023-06-20 16:54:30 +00:00
|
|
|
/**
|
|
|
|
* Gets the name of a file or folder specified by a path,
|
|
|
|
* i.e. the last segment of the path.
|
|
|
|
*
|
|
|
|
* @ingroup Filesystem
|
|
|
|
*
|
|
|
|
* @param path Path from which to retrieve the filename.
|
|
|
|
*
|
|
|
|
* @return Filename of the path.
|
|
|
|
*
|
|
|
|
* @remark Supports forward and backward slashes as path segment separator.
|
|
|
|
* @remark No distinction between files and folders is being made.
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
|
|
|
const char *fs_filename(const char *path);
|
|
|
|
|
2023-06-25 09:59:12 +00:00
|
|
|
/**
|
|
|
|
* Splits a filename into name (without extension) and file extension.
|
|
|
|
*
|
|
|
|
* @ingroup Filesystem
|
|
|
|
*
|
|
|
|
* @param filename The filename to split.
|
|
|
|
* @param name Buffer that will receive the name without extension, may be nullptr.
|
|
|
|
* @param name_size Size of the name buffer (ignored if name is nullptr).
|
|
|
|
* @param extension Buffer that will receive the extension, may be nullptr.
|
|
|
|
* @param extension_size Size of the extension buffer (ignored if extension is nullptr).
|
|
|
|
*
|
|
|
|
* @remark Does NOT handle forward and backward slashes.
|
|
|
|
* @remark No distinction between files and folders is being made.
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
|
|
|
void fs_split_file_extension(const char *filename, char *name, size_t name_size, char *extension = nullptr, size_t extension_size = 0);
|
|
|
|
|
2023-01-21 19:54:46 +00:00
|
|
|
/**
|
|
|
|
* Get the parent directory of a directory.
|
|
|
|
*
|
|
|
|
* @ingroup Filesystem
|
|
|
|
*
|
|
|
|
* @param path Path of the directory. The parent will be store in this buffer as well.
|
|
|
|
*
|
|
|
|
* @return 0 on success, 1 on failure.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
2010-10-07 21:51:07 +00:00
|
|
|
int fs_parent_dir(char *path);
|
2010-09-12 11:15:59 +00:00
|
|
|
|
2023-01-21 19:54:46 +00:00
|
|
|
/**
|
|
|
|
* Deletes a file.
|
|
|
|
*
|
|
|
|
* @ingroup Filesystem
|
|
|
|
*
|
|
|
|
* @param filename Path of the file to delete.
|
|
|
|
*
|
|
|
|
* @return 0 on success, 1 on failure.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
* @remark Returns an error if the path specifies a directory name.
|
|
|
|
*/
|
2010-12-07 23:13:59 +00:00
|
|
|
int fs_remove(const char *filename);
|
|
|
|
|
2023-01-21 19:54:46 +00:00
|
|
|
/**
|
|
|
|
* Renames the file or directory. If the paths differ the file will be moved.
|
|
|
|
*
|
|
|
|
* @ingroup Filesystem
|
|
|
|
*
|
|
|
|
* @param oldname The current path of a file or directory.
|
|
|
|
* @param newname The new path for the file or directory.
|
|
|
|
*
|
|
|
|
* @return 0 on success, 1 on failure.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
2010-12-07 23:09:18 +00:00
|
|
|
int fs_rename(const char *oldname, const char *newname);
|
|
|
|
|
2023-01-21 19:54:46 +00:00
|
|
|
/**
|
|
|
|
* Gets the creation and the last modification date of a file or directory.
|
|
|
|
*
|
|
|
|
* @ingroup Filesystem
|
|
|
|
*
|
|
|
|
* @param name Path of a file or directory.
|
|
|
|
* @param created Pointer where the creation time will be stored.
|
|
|
|
* @param modified Pointer where the modification time will be stored.
|
|
|
|
*
|
|
|
|
* @return 0 on success, non-zero on failure.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
* @remark Returned time is in seconds since UNIX Epoch.
|
|
|
|
*/
|
2021-08-23 11:28:50 +00:00
|
|
|
int fs_file_time(const char *name, time_t *created, time_t *modified);
|
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
/*
|
|
|
|
Group: Undocumented
|
|
|
|
*/
|
|
|
|
|
2008-08-05 21:37:33 +00:00
|
|
|
/*
|
|
|
|
Function: net_tcp_connect_non_blocking
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2008-08-05 21:37:33 +00:00
|
|
|
DOCTODO: serp
|
|
|
|
*/
|
2011-07-06 16:20:46 +00:00
|
|
|
int net_tcp_connect_non_blocking(NETSOCKET sock, NETADDR bindaddr);
|
2008-08-05 21:37:33 +00:00
|
|
|
|
|
|
|
/*
|
2011-07-06 16:20:46 +00:00
|
|
|
Function: net_set_non_blocking
|
2008-08-05 21:37:33 +00:00
|
|
|
|
|
|
|
DOCTODO: serp
|
|
|
|
*/
|
2011-07-06 16:20:46 +00:00
|
|
|
int net_set_non_blocking(NETSOCKET sock);
|
2008-08-05 21:37:33 +00:00
|
|
|
|
|
|
|
/*
|
2011-07-06 16:20:46 +00:00
|
|
|
Function: net_set_non_blocking
|
2008-08-05 21:37:33 +00:00
|
|
|
|
|
|
|
DOCTODO: serp
|
|
|
|
*/
|
2011-07-06 16:20:46 +00:00
|
|
|
int net_set_blocking(NETSOCKET sock);
|
2008-08-05 21:37:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Function: net_errno
|
|
|
|
|
|
|
|
DOCTODO: serp
|
|
|
|
*/
|
2021-06-15 02:15:24 +00:00
|
|
|
int net_errno();
|
2008-08-05 21:37:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Function: net_would_block
|
|
|
|
|
|
|
|
DOCTODO: serp
|
|
|
|
*/
|
2021-06-15 02:15:24 +00:00
|
|
|
int net_would_block();
|
2008-08-05 21:37:33 +00:00
|
|
|
|
2008-07-06 11:21:21 +00:00
|
|
|
int net_socket_read_wait(NETSOCKET sock, int time);
|
|
|
|
|
2023-10-14 11:17:27 +00:00
|
|
|
/**
|
|
|
|
* Swaps the endianness of data. Each element is swapped individually by reversing its bytes.
|
|
|
|
*
|
|
|
|
* @param data Pointer to data to be swapped.
|
|
|
|
* @param elem_size Size in bytes of each element.
|
|
|
|
* @param num Number of elements.
|
|
|
|
*
|
|
|
|
* @remark The caller must ensure that the data is at least `elem_size * num` bytes in size.
|
|
|
|
*/
|
2008-07-06 11:21:21 +00:00
|
|
|
void swap_endian(void *data, unsigned elem_size, unsigned num);
|
|
|
|
|
2008-04-05 14:50:43 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2021-11-06 12:03:51 +00:00
|
|
|
uint64_t sent_packets;
|
|
|
|
uint64_t sent_bytes;
|
|
|
|
uint64_t recv_packets;
|
|
|
|
uint64_t recv_bytes;
|
2008-04-05 14:50:43 +00:00
|
|
|
} NETSTATS;
|
|
|
|
|
|
|
|
void net_stats(NETSTATS *stats);
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
int str_toint(const char *str);
|
2023-12-18 01:08:12 +00:00
|
|
|
bool str_toint(const char *str, int *out);
|
2015-08-20 10:51:30 +00:00
|
|
|
int str_toint_base(const char *str, int base);
|
2019-05-15 16:11:22 +00:00
|
|
|
unsigned long str_toulong_base(const char *str, int base);
|
2023-06-05 18:57:54 +00:00
|
|
|
int64_t str_toint64_base(const char *str, int base = 10);
|
2010-05-29 07:25:38 +00:00
|
|
|
float str_tofloat(const char *str);
|
2023-12-18 01:08:12 +00:00
|
|
|
bool str_tofloat(const char *str, float *out);
|
2022-09-28 12:12:10 +00:00
|
|
|
|
2023-08-20 11:03:01 +00:00
|
|
|
void str_from_int(int value, char *buffer, size_t buffer_size);
|
|
|
|
|
|
|
|
template<size_t N>
|
|
|
|
void str_from_int(int value, char (&dst)[N])
|
|
|
|
{
|
|
|
|
str_from_int(value, dst, N);
|
|
|
|
}
|
|
|
|
|
2022-09-28 12:12:10 +00:00
|
|
|
/**
|
|
|
|
* Determines whether a character is whitespace.
|
|
|
|
*
|
|
|
|
* @ingroup Strings
|
|
|
|
*
|
|
|
|
* @param c the character to check
|
|
|
|
*
|
|
|
|
* @return 1 if the character is whitespace, 0 otherwise.
|
|
|
|
*
|
|
|
|
* @remark The following characters are considered whitespace: ' ', '\n', '\r', '\t'
|
|
|
|
*/
|
2008-11-08 08:27:11 +00:00
|
|
|
int str_isspace(char c);
|
2022-09-28 12:12:10 +00:00
|
|
|
|
2009-06-15 08:15:53 +00:00
|
|
|
char str_uppercase(char c);
|
2023-06-20 21:01:55 +00:00
|
|
|
|
2019-02-27 19:24:31 +00:00
|
|
|
int str_isallnum(const char *str);
|
2023-06-20 21:01:55 +00:00
|
|
|
|
|
|
|
int str_isallnum_hex(const char *str);
|
|
|
|
|
2009-06-15 13:01:04 +00:00
|
|
|
unsigned str_quickhash(const char *str);
|
2008-11-08 08:27:11 +00:00
|
|
|
|
2018-03-06 17:41:18 +00:00
|
|
|
int str_utf8_to_skeleton(const char *str, int *buf, int buf_len);
|
|
|
|
|
2016-10-30 11:25:49 +00:00
|
|
|
/*
|
|
|
|
Function: str_utf8_comp_confusable
|
|
|
|
Compares two strings for visual appearance.
|
|
|
|
|
|
|
|
Parameters:
|
2022-01-22 13:12:59 +00:00
|
|
|
str1 - String to compare.
|
|
|
|
str2 - String to compare.
|
2016-10-30 11:25:49 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
0 if the strings are confusable.
|
|
|
|
!=0 otherwise.
|
|
|
|
*/
|
2022-01-22 13:12:59 +00:00
|
|
|
int str_utf8_comp_confusable(const char *str1, const char *str2);
|
2014-11-24 15:22:00 +00:00
|
|
|
|
2019-01-07 22:49:20 +00:00
|
|
|
/*
|
|
|
|
Function: str_utf8_tolower
|
|
|
|
Converts the given Unicode codepoint to lowercase (locale insensitive).
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
code - Unicode codepoint to convert.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Lowercase codepoint
|
|
|
|
*/
|
|
|
|
int str_utf8_tolower(int code);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Function: str_utf8_comp_nocase
|
|
|
|
Compares two utf8 strings case insensitively.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
a - String to compare.
|
|
|
|
b - String to compare.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
<0 - String a is less than string b
|
|
|
|
0 - String a is equal to string b
|
|
|
|
>0 - String a is greater than string b
|
|
|
|
*/
|
|
|
|
int str_utf8_comp_nocase(const char *a, const char *b);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Function: str_utf8_comp_nocase_num
|
|
|
|
Compares up to num bytes of two utf8 strings case insensitively.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
a - String to compare.
|
|
|
|
b - String to compare.
|
2019-02-11 16:11:33 +00:00
|
|
|
num - Maximum bytes to compare
|
2019-01-07 22:49:20 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
<0 - String a is less than string b
|
|
|
|
0 - String a is equal to string b
|
|
|
|
>0 - String a is greater than string b
|
|
|
|
*/
|
|
|
|
int str_utf8_comp_nocase_num(const char *a, const char *b, int num);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Function: str_utf8_find_nocase
|
|
|
|
Finds a utf8 string inside another utf8 string case insensitively.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
haystack - String to search in
|
|
|
|
needle - String to search for
|
2023-07-14 16:04:57 +00:00
|
|
|
end - A pointer that will be set to a pointer into haystack directly behind the
|
|
|
|
last character where the needle was found. Will be set to nullptr if needle
|
|
|
|
could not be found. Optional parameter.
|
2019-01-07 22:49:20 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
A pointer into haystack where the needle was found.
|
2019-04-16 00:24:24 +00:00
|
|
|
Returns NULL if needle could not be found.
|
2019-01-07 22:49:20 +00:00
|
|
|
|
|
|
|
Remarks:
|
|
|
|
- The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
2023-07-14 16:04:57 +00:00
|
|
|
const char *str_utf8_find_nocase(const char *haystack, const char *needle, const char **end = nullptr);
|
2019-01-07 22:49:20 +00:00
|
|
|
|
2018-03-14 01:27:15 +00:00
|
|
|
/*
|
|
|
|
Function: str_utf8_isspace
|
|
|
|
Checks whether the given Unicode codepoint renders as space.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
code - Unicode codepoint to check.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
0 if the codepoint does not render as space, != 0 if it does.
|
|
|
|
*/
|
2014-11-08 19:14:12 +00:00
|
|
|
int str_utf8_isspace(int code);
|
|
|
|
|
2015-07-11 17:26:57 +00:00
|
|
|
int str_utf8_isstart(char c);
|
|
|
|
|
2018-03-14 01:27:15 +00:00
|
|
|
/*
|
|
|
|
Function: str_utf8_skip_whitespaces
|
|
|
|
Skips leading characters that render as spaces.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
str - Pointer to the string.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Pointer to the first non-whitespace character found
|
|
|
|
within the string.
|
|
|
|
|
|
|
|
Remarks:
|
|
|
|
- The strings are treated as zero-terminated strings.
|
|
|
|
*/
|
2013-04-26 15:10:05 +00:00
|
|
|
const char *str_utf8_skip_whitespaces(const char *str);
|
2009-06-13 08:22:37 +00:00
|
|
|
|
2018-03-14 01:27:15 +00:00
|
|
|
/*
|
|
|
|
Function: str_utf8_trim_right
|
|
|
|
Removes trailing characters that render as spaces by modifying
|
|
|
|
the string in-place.
|
|
|
|
|
|
|
|
Parameters:
|
2022-01-22 13:12:59 +00:00
|
|
|
param - Pointer to the string.
|
2018-03-14 01:27:15 +00:00
|
|
|
|
|
|
|
Remarks:
|
|
|
|
- The strings are treated as zero-terminated strings.
|
|
|
|
- The string is modified in-place.
|
|
|
|
*/
|
2022-01-22 13:12:59 +00:00
|
|
|
void str_utf8_trim_right(char *param);
|
2018-03-14 01:27:15 +00:00
|
|
|
|
2009-06-13 08:22:37 +00:00
|
|
|
/*
|
|
|
|
Function: str_utf8_rewind
|
|
|
|
Moves a cursor backwards in an utf8 string
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2009-06-13 08:22:37 +00:00
|
|
|
Parameters:
|
|
|
|
str - utf8 string
|
|
|
|
cursor - position in the string
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
New cursor position.
|
|
|
|
|
|
|
|
Remarks:
|
|
|
|
- Won't move the cursor less then 0
|
|
|
|
*/
|
|
|
|
int str_utf8_rewind(const char *str, int cursor);
|
|
|
|
|
2021-12-20 01:05:07 +00:00
|
|
|
/*
|
|
|
|
Function: str_utf8_fix_truncation
|
|
|
|
Fixes truncation of a Unicode character at the end of a UTF-8
|
|
|
|
string.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The new string length.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
str - utf8 string
|
|
|
|
*/
|
|
|
|
int str_utf8_fix_truncation(char *str);
|
|
|
|
|
2009-06-13 08:22:37 +00:00
|
|
|
/*
|
|
|
|
Function: str_utf8_forward
|
|
|
|
Moves a cursor forwards in an utf8 string
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2009-06-13 08:22:37 +00:00
|
|
|
Parameters:
|
|
|
|
str - utf8 string
|
|
|
|
cursor - position in the string
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2009-06-13 08:22:37 +00:00
|
|
|
Returns:
|
|
|
|
New cursor position.
|
|
|
|
|
|
|
|
Remarks:
|
|
|
|
- Won't move the cursor beyond the zero termination marker
|
|
|
|
*/
|
|
|
|
int str_utf8_forward(const char *str, int cursor);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Function: str_utf8_decode
|
2015-06-30 19:09:43 +00:00
|
|
|
Decodes a utf8 codepoint
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2009-06-13 08:22:37 +00:00
|
|
|
Parameters:
|
2015-06-30 19:09:43 +00:00
|
|
|
ptr - Pointer to a utf8 string. This pointer will be moved forward.
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2009-06-13 08:22:37 +00:00
|
|
|
Returns:
|
2015-06-30 19:09:43 +00:00
|
|
|
The Unicode codepoint. -1 for invalid input and 0 for end of string.
|
2009-06-13 08:22:37 +00:00
|
|
|
|
|
|
|
Remarks:
|
|
|
|
- This function will also move the pointer forward.
|
2018-02-04 15:00:47 +00:00
|
|
|
- You may call this function again after an error occurred.
|
2009-06-13 08:22:37 +00:00
|
|
|
*/
|
|
|
|
int str_utf8_decode(const char **ptr);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Function: str_utf8_encode
|
|
|
|
Encode an utf8 character
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2009-06-13 08:22:37 +00:00
|
|
|
Parameters:
|
2015-06-30 19:09:43 +00:00
|
|
|
ptr - Pointer to a buffer that should receive the data. Should be able to hold at least 4 bytes.
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2009-06-13 08:22:37 +00:00
|
|
|
Returns:
|
|
|
|
Number of bytes put into the buffer.
|
|
|
|
|
|
|
|
Remarks:
|
|
|
|
- Does not do zero termination of the string.
|
|
|
|
*/
|
|
|
|
int str_utf8_encode(char *ptr, int chr);
|
|
|
|
|
2010-09-30 22:55:16 +00:00
|
|
|
/*
|
|
|
|
Function: str_utf8_check
|
|
|
|
Checks if a strings contains just valid utf8 characters.
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-09-30 22:55:16 +00:00
|
|
|
Parameters:
|
|
|
|
str - Pointer to a possible utf8 string.
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-09-30 22:55:16 +00:00
|
|
|
Returns:
|
|
|
|
0 - invalid characters found.
|
|
|
|
1 - only valid characters found.
|
|
|
|
|
|
|
|
Remarks:
|
|
|
|
- The string is treated as zero-terminated utf8 string.
|
|
|
|
*/
|
|
|
|
int str_utf8_check(const char *str);
|
|
|
|
|
2021-11-24 22:24:46 +00:00
|
|
|
/*
|
|
|
|
Function: str_utf8_stats
|
2021-11-25 18:54:50 +00:00
|
|
|
Determines the byte size and utf8 character count of a utf8 string.
|
2021-11-24 22:24:46 +00:00
|
|
|
|
|
|
|
Parameters:
|
|
|
|
str - Pointer to the string.
|
|
|
|
max_size - Maximum number of bytes to count.
|
|
|
|
max_count - Maximum number of utf8 characters to count.
|
|
|
|
size - Pointer to store size (number of non-zero bytes) of the string.
|
|
|
|
count - Pointer to store count of utf8 characters of the string.
|
|
|
|
|
|
|
|
Remarks:
|
2021-11-25 18:54:50 +00:00
|
|
|
- The string is treated as zero-terminated utf8 string.
|
|
|
|
- It's the user's responsibility to make sure the bounds are aligned.
|
2021-11-24 22:24:46 +00:00
|
|
|
*/
|
Port line input and IME support from 0.7
Port the line input (UI edit boxes, chat, console) and Input Method Editor (IME) support from upstream. Closes #4397.
General
------------------------------
Fix issues with the text input. Closes #4346. Closes #4524.
Word skipping (when holding Ctrl) is overhauled to be consistent with the Windows / Firefox experience that I took as reference.
Improve usability by not blinking (i.e. always rendering) the caret shortly after is has been moved.
UI text input
------------------------------
Fix inconsistent mouse-based left and right scrolling (closes #4347).
Support smooth left and right scrolling.
Chat
------------------------------
Support keyboard-based text selection of the chat input.
Mouse-based selection could be support in the future when we decide to add something like an ingame UI cursor.
Support smooth up and down scrolling of the chat input, removing the old hack that offsets the input string to simulate scrolling.
Console
------------------------------
Also support mouse-based text selection of the command input.
Only text from either the command input or the console log can be selected at the same time. This ensures that Ctrl+C will always copy the text that is currently visually selected in the console.
Check for Ctrl+C input event in event handler instead of in render function, to hopefully fix the issue that copying does not work sometimes (closes #5974 until further notice).
When Ctrl+C is used to copy text from the console log, the selection is cleared. This should make it more clear when text was copied from the log.
Fix an issue that was preventing the console log selection from being cleared, when all log lines are selected.
Remove Ctrl+A/E hotkeys that move cursor to beginning/end respectively. Ctrl+A now selectes all text like for all other inputs. Home and End keys can still be used to go the beginning and end.
Remove Ctrl+U/K hotkeys that clear everything before/after the cursor respectively. Hold shift and use Home/End to select everything instead.
IME support
------------------------------
Render list of IME candidates in the client on Windows, so the candidate list can also be viewed in fullscreen mode. There is no API available to retrieve a candidate list on the other operating systems.
Improve composition rendering by underlining the composition text instead of putting it in square brackets.
Track active input globally to properly activate and deactivate IME through the SDL functions.
Closes #1030. Closes #1008.
Password rendering
------------------------------
Fix rendering of passwords containing unicode. Instead of rendering one star character for each UTF-8 `char`, render on star for every unicode codepoint.
Show the composition text also for passwords. Without seeing the composition text it's hard to type a password containing those characters. The candidate window exposes the composition anyway. If you don't want to expose your password this way, e.g. while streaming, you could:
1. Use a latin password and switch off the IME for the password input with the IME hotkey.
2. Blank your screen with an external program while you are streaming and entering passwords.
3. Use binds to authenticate in rcon or to set the server browser password.
Refactoring
------------------------------
Move all text input logic and general rendering to `CLineInput`.
A `CLineInput` is associated with a particular `char` buffer given as a pointer either in the constructor or with `SetBuffer`. The maximum byte size of the buffer must also be specified. The maximum length in unicode codepoints can also be specified separately (e.g. on upstream, name are limited by the number of unicode codepoints instead).
Add `CLineInputBuffered`, which is a `CLineInput` that own a `char` buffer of a fixed size, which is specified as a template argument. As `CLineInput` does not own a buffer anymore, this reduces duplicate code for line inputs that need their own buffer.
Add `CLineInputNumber` which has additional convenience functions to consider the text as an `int` or `float`, to reduce duplicate code in those cases. In the future we could also add an input filter function so that only numbers can be entered in the number input.
Add `CLineInput::SetClipboardLineCallback` to handle the case that multiple lines of text are pasted into a lineinput. This reduces duplicate code, as this behavior was previously implemented separately for chat and console. The behavior is also fixed to be consistent with the console on Windows, so the first line being pasted edits the current input text and then sends it instead of being sent on its own without the existing input text.
Add `CalcFontSizeAndBoundingBox` to UI to reduce duplicate code. Expose `CalcAlignedCursorPos` as static member function to reuse it for line input.
Dispatch input events to UI inputs through the event handler instead of storing them in a duplicate buffer.
Use `size_t` for line input cursor position, length etc. and for `str_utf8_stats`.
Add `IButtonColorFunction` to UI to describe a functions that defines colors for the Default, Active and Hovered states of UI elements. Add some default button color functions. Use button color function to reduce duplicate code in scrollbar rendering.
Use `vec2` instead of two `floats` to represent the mouse positions in the text renderer.
Remove `CaretPosition` again, as it does not calculate the correct Y position near line breaks due to the wrapping being different when not rendering the entire string. Instead, calculate the exact caret position when rending a text container and store the caret position in the text cursor for later use.
IME usage guide (Windows)
------------------------------
1. Install the respective language and the Microsoft-IME keyboard (e.g. for Chinese, Japanese or Korean).
2. Launch the game (or a text editor to first try out the IME). Note that Windows may track the input language separately for every application. You can change this in the Windows input settings so the input language is changed globally.
2. Switch the input language using the hotkey Windows+Space or another hotkey that you configured in the Windows input settings (Alt+Shift is the default, but you should consider disabling it, to avoid accidentally changing the input language while playing).
3. Switch from Latin/English input mode to the respective asian input mode.
- Chinese: Use Ctrl+Space to switch between English and Chinese input mode. You can change this hotkey in the IME's settings.
- Japanese: Use Ctrl+Space to switch between Alphanumeric and Hiragana/Katakana input mode. You can change this hotkey in the IME's settings.
- Korean: Use Right Alt to switch between English and Hangul input mode. You cannot change this hotkey as of yet.
- Note that the input mode is also tracked per application, but there is no setting to change this behavior as far as I know, so you'll need to switch for every application separately.
4. Start typing. The underlined text is the current composition text. While a composition is active, you can only edit the composition text. Confirm the composition with Space or by selecting a candidate from the candidate list with the arrow keys. Cancel the composition with Escape or by using Backspace to delete the composition text. Note that not all languages offer a candidate list.
SDL version-specific issues
------------------------------
- 2.26.5, 2.24.2, 2.0.22: IME candidates work. But there are minor bugs when moving the composition cursor.
- 2.0.18, 2.0.20: IME candidates work.
- 2.0.16 (our current version): IME candidates cannot be determined with Windows API. Windows tries to draw the composition window like before, so this does not work in fullscreen mode.
- 2.0.8 (upstream 0.7): IME candidates work. But this SDL version is too old for us.
2023-01-03 21:28:38 +00:00
|
|
|
void str_utf8_stats(const char *str, size_t max_size, size_t max_count, size_t *size, size_t *count);
|
2021-11-24 22:24:46 +00:00
|
|
|
|
2023-08-15 19:32:56 +00:00
|
|
|
/**
|
|
|
|
* Converts a byte offset of a utf8 string to the utf8 character offset.
|
|
|
|
*
|
|
|
|
* @param text Pointer to the string.
|
|
|
|
* @param byte_offset Offset in bytes.
|
|
|
|
*
|
|
|
|
* @return Offset in utf8 characters. Clamped to the maximum length of the string in utf8 characters.
|
|
|
|
*
|
|
|
|
* @remark The string is treated as a zero-terminated utf8 string.
|
|
|
|
* @remark It's the user's responsibility to make sure the bounds are aligned.
|
|
|
|
*/
|
|
|
|
size_t str_utf8_offset_bytes_to_chars(const char *str, size_t byte_offset);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a utf8 character offset of a utf8 string to the byte offset.
|
|
|
|
*
|
|
|
|
* @param text Pointer to the string.
|
|
|
|
* @param char_offset Offset in utf8 characters.
|
|
|
|
*
|
|
|
|
* @return Offset in bytes. Clamped to the maximum length of the string in bytes.
|
|
|
|
*
|
|
|
|
* @remark The string is treated as a zero-terminated utf8 string.
|
|
|
|
* @remark It's the user's responsibility to make sure the bounds are aligned.
|
|
|
|
*/
|
|
|
|
size_t str_utf8_offset_chars_to_bytes(const char *str, size_t char_offset);
|
|
|
|
|
2019-03-05 09:46:29 +00:00
|
|
|
/*
|
2019-03-11 11:39:54 +00:00
|
|
|
Function: str_next_token
|
|
|
|
Writes the next token after str into buf, returns the rest of the string.
|
2021-11-25 19:01:29 +00:00
|
|
|
|
2019-03-05 09:46:29 +00:00
|
|
|
Parameters:
|
|
|
|
str - Pointer to string.
|
|
|
|
delim - Delimiter for tokenization.
|
2019-03-11 11:39:54 +00:00
|
|
|
buffer - Buffer to store token in.
|
|
|
|
buffer_size - Size of the buffer.
|
2021-11-25 19:01:29 +00:00
|
|
|
|
2019-03-05 09:46:29 +00:00
|
|
|
Returns:
|
2019-03-11 11:39:54 +00:00
|
|
|
Pointer to rest of the string.
|
2021-11-25 19:01:29 +00:00
|
|
|
|
2019-03-05 09:46:29 +00:00
|
|
|
Remarks:
|
|
|
|
- The token is always null-terminated.
|
|
|
|
*/
|
2019-03-11 11:39:54 +00:00
|
|
|
const char *str_next_token(const char *str, const char *delim, char *buffer, int buffer_size);
|
2019-03-05 09:46:29 +00:00
|
|
|
|
2019-02-06 12:30:47 +00:00
|
|
|
/*
|
|
|
|
Function: str_in_list
|
|
|
|
Checks if needle is in list delimited by delim
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
list - List
|
|
|
|
delim - List delimiter.
|
|
|
|
needle - Item that is being looked for.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
1 - Item is in list.
|
|
|
|
0 - Item isn't in list.
|
|
|
|
*/
|
|
|
|
int str_in_list(const char *list, const char *delim, const char *needle);
|
|
|
|
|
2023-02-04 00:22:57 +00:00
|
|
|
/**
|
|
|
|
* Packs 4 big endian bytes into an unsigned.
|
|
|
|
*
|
|
|
|
* @param bytes Pointer to an array of bytes that will be packed.
|
|
|
|
*
|
|
|
|
* @return The packed unsigned.
|
|
|
|
*
|
|
|
|
* @remark Assumes the passed array is least 4 bytes in size.
|
|
|
|
* @remark Assumes unsigned is 4 bytes in size.
|
|
|
|
*
|
|
|
|
* @see uint_to_bytes_be
|
|
|
|
*/
|
2021-11-08 19:21:02 +00:00
|
|
|
unsigned bytes_be_to_uint(const unsigned char *bytes);
|
|
|
|
|
2023-02-04 00:22:57 +00:00
|
|
|
/**
|
|
|
|
* Packs an unsigned into 4 big endian bytes.
|
|
|
|
*
|
|
|
|
* @param bytes Pointer to an array where the bytes will be stored.
|
|
|
|
* @param value The values that will be packed into the array.
|
|
|
|
*
|
|
|
|
* @remark Assumes the passed array is least 4 bytes in size.
|
|
|
|
* @remark Assumes unsigned is 4 bytes in size.
|
|
|
|
*
|
|
|
|
* @see bytes_be_to_uint
|
|
|
|
*/
|
2021-11-08 19:21:02 +00:00
|
|
|
void uint_to_bytes_be(unsigned char *bytes, unsigned value);
|
|
|
|
|
2024-02-12 21:44:15 +00:00
|
|
|
/**
|
|
|
|
* @defgroup Shell
|
|
|
|
* Shell, process management, OS specific functionality.
|
|
|
|
*/
|
2022-06-06 10:50:08 +00:00
|
|
|
|
2024-02-12 21:44:15 +00:00
|
|
|
/**
|
|
|
|
* Returns the ID of the current process.
|
|
|
|
*
|
|
|
|
* @ingroup Shell
|
|
|
|
*
|
|
|
|
* @return PID of the current process.
|
|
|
|
*/
|
2021-06-15 02:15:24 +00:00
|
|
|
int pid();
|
2014-08-22 12:18:16 +00:00
|
|
|
|
2024-02-12 21:44:15 +00:00
|
|
|
/**
|
|
|
|
* Fixes the command line arguments to be encoded in UTF-8 on all systems.
|
|
|
|
*
|
|
|
|
* @ingroup Shell
|
|
|
|
*
|
|
|
|
* @param argc A pointer to the argc parameter that was passed to the main function.
|
|
|
|
* @param argv A pointer to the argv parameter that was passed to the main function.
|
|
|
|
*
|
|
|
|
* @remark You need to call @link cmdline_free @endlink once you're no longer using the results.
|
|
|
|
*/
|
2021-11-21 17:16:14 +00:00
|
|
|
void cmdline_fix(int *argc, const char ***argv);
|
|
|
|
|
2024-02-12 21:44:15 +00:00
|
|
|
/**
|
|
|
|
* Frees memory that was allocated by @link cmdline_fix @endlink.
|
|
|
|
*
|
|
|
|
* @ingroup Shell
|
|
|
|
*
|
|
|
|
* @param argc The argc obtained from `cmdline_fix`.
|
|
|
|
* @param argv The argv obtained from `cmdline_fix`.
|
|
|
|
*/
|
2021-11-21 17:16:14 +00:00
|
|
|
void cmdline_free(int argc, const char **argv);
|
|
|
|
|
2020-09-05 22:38:35 +00:00
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
2024-02-12 21:44:15 +00:00
|
|
|
/**
|
|
|
|
* A handle for a process.
|
|
|
|
*
|
|
|
|
* @ingroup Shell
|
|
|
|
*/
|
2020-09-05 22:38:35 +00:00
|
|
|
typedef void *PROCESS;
|
2024-02-12 21:44:15 +00:00
|
|
|
/**
|
|
|
|
* A handle that denotes an invalid process.
|
|
|
|
*
|
|
|
|
* @ingroup Shell
|
|
|
|
*/
|
2023-03-10 16:39:24 +00:00
|
|
|
constexpr PROCESS INVALID_PROCESS = nullptr;
|
2020-09-05 22:38:35 +00:00
|
|
|
#else
|
2024-02-12 21:44:15 +00:00
|
|
|
/**
|
|
|
|
* A handle for a process.
|
|
|
|
*
|
|
|
|
* @ingroup Shell
|
|
|
|
*/
|
2020-09-05 22:38:35 +00:00
|
|
|
typedef pid_t PROCESS;
|
2024-02-12 21:44:15 +00:00
|
|
|
/**
|
|
|
|
* A handle that denotes an invalid process.
|
|
|
|
*
|
|
|
|
* @ingroup Shell
|
|
|
|
*/
|
2023-03-10 16:39:24 +00:00
|
|
|
constexpr PROCESS INVALID_PROCESS = 0;
|
2020-09-05 22:38:35 +00:00
|
|
|
#endif
|
|
|
|
|
2024-02-12 21:44:15 +00:00
|
|
|
/**
|
|
|
|
* Determines the initial window state when using @link shell_execute @endlink
|
|
|
|
* to execute a process.
|
|
|
|
*
|
|
|
|
* @ingroup Shell
|
|
|
|
*
|
|
|
|
* @remark Currently only supported on Windows.
|
|
|
|
*/
|
2024-01-25 18:32:53 +00:00
|
|
|
enum class EShellExecuteWindowState
|
|
|
|
{
|
2024-02-12 21:44:15 +00:00
|
|
|
/**
|
|
|
|
* The process window is opened in the foreground and activated.
|
|
|
|
*/
|
2024-01-25 18:32:53 +00:00
|
|
|
FOREGROUND,
|
2024-02-12 21:44:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The process window is opened in the background without focus.
|
|
|
|
*/
|
2024-01-25 18:32:53 +00:00
|
|
|
BACKGROUND,
|
|
|
|
};
|
2020-09-03 12:08:26 +00:00
|
|
|
|
2024-01-25 18:23:48 +00:00
|
|
|
/**
|
|
|
|
* Executes a given file.
|
|
|
|
*
|
2024-02-12 21:44:15 +00:00
|
|
|
* @ingroup Shell
|
|
|
|
*
|
2024-01-25 18:23:48 +00:00
|
|
|
* @param file The file to execute.
|
2024-01-25 18:32:53 +00:00
|
|
|
* @param window_state The window state how the process window should be shown.
|
2024-01-25 18:23:48 +00:00
|
|
|
*
|
2024-02-12 21:44:15 +00:00
|
|
|
* @return Handle of the new process, or @link INVALID_PROCESS @endlink on error.
|
2024-01-25 18:23:48 +00:00
|
|
|
*/
|
2024-01-25 18:32:53 +00:00
|
|
|
PROCESS shell_execute(const char *file, EShellExecuteWindowState window_state);
|
2020-09-03 12:08:26 +00:00
|
|
|
|
2024-01-25 18:23:48 +00:00
|
|
|
/**
|
|
|
|
* Sends kill signal to a process.
|
|
|
|
*
|
2024-02-12 21:44:15 +00:00
|
|
|
* @ingroup Shell
|
|
|
|
*
|
2024-01-25 18:23:48 +00:00
|
|
|
* @param process Handle of the process to kill.
|
|
|
|
*
|
2024-02-12 21:44:15 +00:00
|
|
|
* @return `1` on success, `0` on error.
|
2024-01-25 18:23:48 +00:00
|
|
|
*/
|
2020-09-05 22:38:35 +00:00
|
|
|
int kill_process(PROCESS process);
|
2014-12-31 14:07:44 +00:00
|
|
|
|
2023-09-22 16:30:39 +00:00
|
|
|
/**
|
|
|
|
* Checks if a process is alive.
|
2023-12-18 19:01:26 +00:00
|
|
|
*
|
2024-02-12 21:44:15 +00:00
|
|
|
* @ingroup Shell
|
|
|
|
*
|
2023-09-22 16:30:39 +00:00
|
|
|
* @param process Handle/PID of the process.
|
2023-12-18 19:01:26 +00:00
|
|
|
*
|
2024-02-12 21:44:15 +00:00
|
|
|
* @return `true` if the process is currently running,
|
|
|
|
* `false` if the process is not running (dead).
|
2023-09-22 16:30:39 +00:00
|
|
|
*/
|
|
|
|
bool is_process_alive(PROCESS process);
|
|
|
|
|
2024-02-12 21:44:15 +00:00
|
|
|
/**
|
|
|
|
* Opens a link in the browser.
|
|
|
|
*
|
|
|
|
* @ingroup Shell
|
|
|
|
*
|
|
|
|
* @param link The link to open in a browser.
|
|
|
|
*
|
|
|
|
* @return `1` on success, `0` on failure.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
* @remark This may not be called with untrusted input or it'll result in arbitrary code execution, especially on Windows.
|
|
|
|
*/
|
|
|
|
int open_link(const char *link);
|
2016-10-01 21:04:16 +00:00
|
|
|
|
2024-02-12 21:44:15 +00:00
|
|
|
/**
|
|
|
|
* Opens a file or directory with default program.
|
|
|
|
*
|
|
|
|
* @ingroup Shell
|
|
|
|
*
|
|
|
|
* @param path The path to open.
|
|
|
|
*
|
|
|
|
* @return `1` on success, `0` on failure.
|
|
|
|
*
|
|
|
|
* @remark The strings are treated as zero-terminated strings.
|
|
|
|
* @remark This may not be called with untrusted input or it'll result in arbitrary code execution, especially on Windows.
|
|
|
|
*/
|
|
|
|
int open_file(const char *path);
|
2016-10-01 21:04:16 +00:00
|
|
|
|
2024-02-12 21:44:15 +00:00
|
|
|
/**
|
|
|
|
* @defgroup Secure-Random
|
|
|
|
* Secure random number generation.
|
|
|
|
*/
|
2015-03-05 23:53:59 +00:00
|
|
|
|
2024-02-12 21:44:15 +00:00
|
|
|
/**
|
|
|
|
* Generates a null-terminated password of length `2 * random_length`.
|
|
|
|
*
|
|
|
|
* @ingroup Secure-Random
|
|
|
|
*
|
|
|
|
* @param buffer Pointer to the start of the output buffer.
|
|
|
|
* @param length Length of the buffer.
|
|
|
|
* @param random Pointer to a randomly-initialized array of shorts.
|
|
|
|
* @param random_length Length of the short array.
|
|
|
|
*/
|
|
|
|
void generate_password(char *buffer, unsigned length, const unsigned short *random, unsigned random_length);
|
2015-03-05 23:53:59 +00:00
|
|
|
|
2024-02-12 21:44:15 +00:00
|
|
|
/**
|
|
|
|
* Initializes the secure random module.
|
|
|
|
* You *MUST* check the return value of this function.
|
|
|
|
*
|
|
|
|
* @ingroup Secure-Random
|
|
|
|
*
|
|
|
|
* @return `0` on success.
|
|
|
|
*/
|
|
|
|
int secure_random_init();
|
2022-02-13 19:04:17 +00:00
|
|
|
|
2024-02-12 21:44:15 +00:00
|
|
|
/**
|
|
|
|
* Uninitializes the secure random module.
|
|
|
|
*
|
|
|
|
* @ingroup Secure-Random
|
|
|
|
*
|
|
|
|
* @return `0` on success.
|
|
|
|
*/
|
2022-02-13 19:04:17 +00:00
|
|
|
int secure_random_uninit();
|
|
|
|
|
2024-02-12 21:44:15 +00:00
|
|
|
/**
|
|
|
|
* Fills the buffer with the specified amount of random password characters.
|
|
|
|
*
|
|
|
|
* @ingroup Secure-Random
|
|
|
|
*
|
|
|
|
* @param buffer Pointer to the start of the buffer.
|
|
|
|
* @param length Length of the buffer.
|
|
|
|
* @param pw_length Length of the desired password.
|
|
|
|
*
|
|
|
|
* @remark The desired password length must be greater or equal to 6,
|
|
|
|
* even and smaller or equal to 128.
|
|
|
|
*/
|
2016-10-01 21:04:16 +00:00
|
|
|
void secure_random_password(char *buffer, unsigned length, unsigned pw_length);
|
|
|
|
|
2024-02-12 21:44:15 +00:00
|
|
|
/**
|
|
|
|
* Fills the buffer with the specified amount of random bytes.
|
|
|
|
*
|
|
|
|
* @ingroup Secure-Random
|
|
|
|
*
|
|
|
|
* @param buffer Pointer to the start of the buffer.
|
|
|
|
* @param length Length of the buffer.
|
|
|
|
*/
|
2016-10-01 21:04:16 +00:00
|
|
|
void secure_random_fill(void *bytes, unsigned length);
|
2015-03-05 23:53:59 +00:00
|
|
|
|
2024-02-12 21:44:15 +00:00
|
|
|
/**
|
|
|
|
* Returns random `int`.
|
|
|
|
*
|
|
|
|
* @ingroup Secure-Random
|
|
|
|
*
|
|
|
|
* @return Random int.
|
|
|
|
*
|
|
|
|
* @remark Can be used as a replacement for the `rand` function.
|
|
|
|
*/
|
2021-06-15 02:15:24 +00:00
|
|
|
int secure_rand();
|
2016-01-02 14:37:44 +00:00
|
|
|
|
2024-02-12 21:44:15 +00:00
|
|
|
/**
|
|
|
|
* Returns a random nonnegative integer below the given number,
|
|
|
|
* with a uniform distribution.
|
|
|
|
*
|
|
|
|
* @ingroup Secure-Random
|
|
|
|
*
|
|
|
|
* @param below Upper limit (exclusive) of integers to return.
|
|
|
|
*
|
|
|
|
* @return Random nonnegative below the given number.
|
|
|
|
*/
|
2021-03-13 15:52:35 +00:00
|
|
|
int secure_rand_below(int below);
|
|
|
|
|
2023-03-16 19:02:59 +00:00
|
|
|
/**
|
|
|
|
* Returns a human-readable version string of the operating system.
|
|
|
|
*
|
2024-02-12 21:44:15 +00:00
|
|
|
* @ingroup Shell
|
|
|
|
*
|
2023-03-16 19:02:59 +00:00
|
|
|
* @param version Buffer to use for the output.
|
|
|
|
* @param length Length of the output buffer.
|
|
|
|
*
|
2024-02-12 21:44:15 +00:00
|
|
|
* @return `true` on success, `false` on failure.
|
2023-03-16 19:02:59 +00:00
|
|
|
*/
|
|
|
|
bool os_version_str(char *version, size_t length);
|
2022-01-27 01:35:08 +00:00
|
|
|
|
2023-03-19 15:04:42 +00:00
|
|
|
/**
|
|
|
|
* Returns a string of the preferred locale of the user / operating system.
|
|
|
|
* The string conforms to [RFC 3066](https://www.ietf.org/rfc/rfc3066.txt)
|
|
|
|
* and only contains the characters `a`-`z`, `A`-`Z`, `0`-`9` and `-`.
|
|
|
|
* If the preferred locale could not be determined this function
|
|
|
|
* falls back to the locale `"en-US"`.
|
|
|
|
*
|
2024-02-12 21:44:15 +00:00
|
|
|
* @ingroup Shell
|
|
|
|
*
|
2023-03-19 15:04:42 +00:00
|
|
|
* @param locale Buffer to use for the output.
|
|
|
|
* @param length Length of the output buffer.
|
|
|
|
*
|
|
|
|
* @remark The destination buffer will be zero-terminated.
|
|
|
|
*/
|
|
|
|
void os_locale_str(char *locale, size_t length);
|
|
|
|
|
2022-02-18 09:46:05 +00:00
|
|
|
#if defined(CONF_EXCEPTION_HANDLING)
|
2024-02-12 21:44:15 +00:00
|
|
|
/**
|
|
|
|
* @defgroup Exception-Handling
|
|
|
|
* Exception handling (crash logging).
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the exception handling module.
|
|
|
|
*
|
|
|
|
* @ingroup Exception-Handling
|
|
|
|
*/
|
2022-02-18 09:46:05 +00:00
|
|
|
void init_exception_handler();
|
2024-02-12 21:44:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the filename for writing the crash log.
|
|
|
|
*
|
|
|
|
* @ingroup Exception-Handling
|
|
|
|
*
|
|
|
|
* @param log_file_path Absolute path to which crash log file should be written.
|
|
|
|
*/
|
2022-03-13 18:45:01 +00:00
|
|
|
void set_exception_handler_log_file(const char *log_file_path);
|
2022-02-18 09:46:05 +00:00
|
|
|
#endif
|
2022-05-18 16:00:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches a sample from a high resolution timer and converts it in nanoseconds.
|
|
|
|
*
|
|
|
|
* @ingroup Time
|
|
|
|
*
|
|
|
|
* @return Current value of the timer in nanoseconds.
|
2022-06-06 10:50:08 +00:00
|
|
|
*/
|
2022-06-13 16:07:29 +00:00
|
|
|
std::chrono::nanoseconds time_get_nanoseconds();
|
2022-05-18 16:00:05 +00:00
|
|
|
|
|
|
|
int net_socket_read_wait(NETSOCKET sock, std::chrono::nanoseconds nanoseconds);
|
|
|
|
|
2022-06-05 14:43:00 +00:00
|
|
|
/**
|
|
|
|
* Fixes the command line arguments to be encoded in UTF-8 on all systems.
|
2024-02-12 21:44:15 +00:00
|
|
|
* This is a RAII wrapper for @link cmdline_fix @endlink and @link cmdline_free @endlink.
|
|
|
|
*
|
|
|
|
* @ingroup Shell
|
2022-06-05 14:43:00 +00:00
|
|
|
*/
|
|
|
|
class CCmdlineFix
|
|
|
|
{
|
|
|
|
int m_Argc;
|
|
|
|
const char **m_ppArgv;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CCmdlineFix(int *pArgc, const char ***pppArgv)
|
|
|
|
{
|
|
|
|
cmdline_fix(pArgc, pppArgv);
|
|
|
|
m_Argc = *pArgc;
|
|
|
|
m_ppArgv = *pppArgv;
|
|
|
|
}
|
|
|
|
~CCmdlineFix()
|
|
|
|
{
|
|
|
|
cmdline_free(m_Argc, m_ppArgv);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-08-20 21:26:05 +00:00
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
2023-05-02 15:36:01 +00:00
|
|
|
/**
|
2024-02-12 21:44:15 +00:00
|
|
|
* Converts a UTF-8 encoded string to a wide character string
|
2023-05-02 15:36:01 +00:00
|
|
|
* for use with the Windows API.
|
|
|
|
*
|
2024-02-12 21:44:15 +00:00
|
|
|
* @ingroup Shell
|
|
|
|
*
|
|
|
|
* @param str The UTF-8 encoded string to convert.
|
2023-05-02 15:36:01 +00:00
|
|
|
*
|
|
|
|
* @return The argument as a wide character string.
|
|
|
|
*
|
|
|
|
* @remark The argument string must be zero-terminated.
|
2024-02-12 21:44:15 +00:00
|
|
|
* @remark Fails with assertion error if passed UTF-8 is invalid.
|
2023-05-02 15:36:01 +00:00
|
|
|
*/
|
|
|
|
std::wstring windows_utf8_to_wide(const char *str);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a wide character string obtained from the Windows API
|
2024-02-12 21:44:15 +00:00
|
|
|
* to a UTF-8 encoded string.
|
|
|
|
*
|
|
|
|
* @ingroup Shell
|
2023-05-02 15:36:01 +00:00
|
|
|
*
|
|
|
|
* @param wide_str The wide character string to convert.
|
|
|
|
*
|
2024-02-12 21:44:15 +00:00
|
|
|
* @return The argument as a UTF-8 encoded string, wrapped in an optional.
|
2023-12-01 19:04:48 +00:00
|
|
|
* The optional is empty, if the wide string contains invalid codepoints.
|
2023-05-02 15:36:01 +00:00
|
|
|
*
|
|
|
|
* @remark The argument string must be zero-terminated.
|
|
|
|
*/
|
2023-12-01 19:04:48 +00:00
|
|
|
std::optional<std::string> windows_wide_to_utf8(const wchar_t *wide_str);
|
2023-05-02 15:36:01 +00:00
|
|
|
|
2022-08-20 21:26:05 +00:00
|
|
|
/**
|
|
|
|
* This is a RAII wrapper to initialize/uninitialize the Windows COM library,
|
2024-02-12 21:44:15 +00:00
|
|
|
* which may be necessary for using the @link open_file @endlink and
|
|
|
|
* @link open_link @endlink functions.
|
2022-10-07 20:18:15 +00:00
|
|
|
* Must be used on every thread. It's automatically used on threads created
|
2024-02-12 21:44:15 +00:00
|
|
|
* with @link thread_init @endlink. Pass `true` to the constructor on threads
|
|
|
|
* that own a window (i.e. pump a message queue).
|
|
|
|
*
|
|
|
|
* @ingroup Shell
|
2022-08-20 21:26:05 +00:00
|
|
|
*/
|
|
|
|
class CWindowsComLifecycle
|
|
|
|
{
|
|
|
|
public:
|
2022-10-07 20:18:15 +00:00
|
|
|
CWindowsComLifecycle(bool HasWindow);
|
2022-08-20 21:26:05 +00:00
|
|
|
~CWindowsComLifecycle();
|
|
|
|
};
|
2022-12-25 17:13:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a protocol handler.
|
|
|
|
*
|
|
|
|
* @ingroup Shell
|
|
|
|
*
|
|
|
|
* @param protocol_name The name of the protocol.
|
|
|
|
* @param executable The absolute path of the executable that will be associated with the protocol.
|
2024-02-12 21:44:15 +00:00
|
|
|
* @param updated Pointer to a variable that will be set to `true`, iff the shell needs to be updated.
|
2022-12-25 17:13:28 +00:00
|
|
|
*
|
2024-02-12 21:44:15 +00:00
|
|
|
* @return `true` on success, `false` on failure.
|
2022-12-29 13:08:59 +00:00
|
|
|
*
|
2024-02-12 21:44:15 +00:00
|
|
|
* @remark The caller must later call @link shell_update @endlink, iff the shell needs to be updated.
|
2022-12-25 17:13:28 +00:00
|
|
|
*/
|
2022-12-29 13:08:59 +00:00
|
|
|
bool shell_register_protocol(const char *protocol_name, const char *executable, bool *updated);
|
2022-12-26 13:27:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a file extension.
|
|
|
|
*
|
|
|
|
* @ingroup Shell
|
|
|
|
*
|
|
|
|
* @param extension The file extension, including the leading dot.
|
|
|
|
* @param description A readable description for the file extension.
|
|
|
|
* @param executable_name A unique name that will used to describe the application.
|
|
|
|
* @param executable The absolute path of the executable that will be associated with the file extension.
|
2024-02-12 21:44:15 +00:00
|
|
|
* @param updated Pointer to a variable that will be set to `true`, iff the shell needs to be updated.
|
2022-12-26 13:27:47 +00:00
|
|
|
*
|
2024-02-12 21:44:15 +00:00
|
|
|
* @return `true` on success, `false` on failure.
|
2022-12-29 13:08:59 +00:00
|
|
|
*
|
2024-02-12 21:44:15 +00:00
|
|
|
* @remark The caller must later call @link shell_update @endlink, iff the shell needs to be updated.
|
2022-12-29 13:08:59 +00:00
|
|
|
*/
|
|
|
|
bool shell_register_extension(const char *extension, const char *description, const char *executable_name, const char *executable, bool *updated);
|
|
|
|
|
2023-01-02 14:28:53 +00:00
|
|
|
/**
|
|
|
|
* Registers an application.
|
|
|
|
*
|
|
|
|
* @ingroup Shell
|
|
|
|
*
|
|
|
|
* @param name Readable name of the application.
|
|
|
|
* @param executable The absolute path of the executable being registered.
|
2024-02-12 21:44:15 +00:00
|
|
|
* @param updated Pointer to a variable that will be set to `true`, iff the shell needs to be updated.
|
2023-01-02 14:28:53 +00:00
|
|
|
*
|
2024-02-12 21:44:15 +00:00
|
|
|
* @return `true` on success, `false` on failure.
|
2023-01-02 14:28:53 +00:00
|
|
|
*
|
2024-02-12 21:44:15 +00:00
|
|
|
* @remark The caller must later call @link shell_update @endlink, iff the shell needs to be updated.
|
2023-01-02 14:28:53 +00:00
|
|
|
*/
|
|
|
|
bool shell_register_application(const char *name, const char *executable, bool *updated);
|
|
|
|
|
2022-12-29 15:47:17 +00:00
|
|
|
/**
|
|
|
|
* Unregisters a protocol or file extension handler.
|
|
|
|
*
|
|
|
|
* @ingroup Shell
|
|
|
|
*
|
|
|
|
* @param shell_class The shell class to delete.
|
|
|
|
* For protocols this is the name of the protocol.
|
|
|
|
* For file extensions this is the program ID associated with the file extension.
|
2024-02-12 21:44:15 +00:00
|
|
|
* @param updated Pointer to a variable that will be set to `true`, iff the shell needs to be updated.
|
2022-12-29 15:47:17 +00:00
|
|
|
*
|
2024-02-12 21:44:15 +00:00
|
|
|
* @return `true` on success, `false` on failure.
|
2022-12-29 15:47:17 +00:00
|
|
|
*
|
2024-02-12 21:44:15 +00:00
|
|
|
* @remark The caller must later call @link shell_update @endlink, iff the shell needs to be updated.
|
2022-12-29 15:47:17 +00:00
|
|
|
*/
|
2023-01-02 14:28:53 +00:00
|
|
|
bool shell_unregister_class(const char *shell_class, bool *updated);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unregisters an application.
|
|
|
|
*
|
|
|
|
* @ingroup Shell
|
|
|
|
*
|
|
|
|
* @param executable The absolute path of the executable being unregistered.
|
2024-02-12 21:44:15 +00:00
|
|
|
* @param updated Pointer to a variable that will be set to `true`, iff the shell needs to be updated.
|
2023-01-02 14:28:53 +00:00
|
|
|
*
|
2024-02-12 21:44:15 +00:00
|
|
|
* @return `true` on success, `false` on failure.
|
2023-01-02 14:28:53 +00:00
|
|
|
*
|
2024-02-12 21:44:15 +00:00
|
|
|
* @remark The caller must later call @link shell_update @endlink, iff the shell needs to be updated.
|
2023-01-02 14:28:53 +00:00
|
|
|
*/
|
|
|
|
bool shell_unregister_application(const char *executable, bool *updated);
|
2022-12-29 15:47:17 +00:00
|
|
|
|
2022-12-29 13:08:59 +00:00
|
|
|
/**
|
|
|
|
* Notifies the system that a protocol or file extension has been changed and the shell needs to be updated.
|
|
|
|
*
|
|
|
|
* @ingroup Shell
|
2023-01-02 23:35:21 +00:00
|
|
|
*
|
2022-12-29 13:08:59 +00:00
|
|
|
* @remark This is a potentially expensive operation, so it should only be called when necessary.
|
2022-12-26 13:27:47 +00:00
|
|
|
*/
|
2022-12-29 13:08:59 +00:00
|
|
|
void shell_update();
|
2022-08-20 21:26:05 +00:00
|
|
|
#endif
|
|
|
|
|
2022-05-23 18:14:06 +00:00
|
|
|
template<>
|
|
|
|
struct std::hash<NETADDR>
|
|
|
|
{
|
|
|
|
size_t operator()(const NETADDR &Addr) const noexcept;
|
|
|
|
};
|
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
#endif
|