ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
platform/util.h
Go to the documentation of this file.
1
31#pragma once
32
33#include <stddef.h>
34#include <stdarg.h>
35#include <stdbool.h>
36#include <stdio.h>
37
38// Define ssize_t for all platforms
39#ifdef _WIN32
40#include <BaseTsd.h>
41typedef SSIZE_T ssize_t;
42#else
43#include <sys/types.h> // For ssize_t on POSIX systems
44#endif
45
46/* ============================================================================
47 * String Operations
48 * ============================================================================
49 */
50
64int platform_snprintf(char *str, size_t size, const char *format, ...);
65
78int platform_vsnprintf(char *str, size_t size, const char *format, va_list ap);
79
90char *platform_strdup(const char *s);
91
103char *platform_strndup(const char *s, size_t n);
104
115int platform_strcasecmp(const char *s1, const char *s2);
116
128int platform_strncasecmp(const char *s1, const char *s2, size_t n);
129
141char *platform_strtok_r(char *str, const char *delim, char **saveptr);
142
155size_t platform_strlcpy(char *dst, const char *src, size_t size);
156
169size_t platform_strlcat(char *dst, const char *src, size_t size);
170
184int platform_strncpy(char *dst, size_t dst_size, const char *src, size_t count);
185
186/* ============================================================================
187 * Memory Operations
188 * ============================================================================
189 */
190
202void *platform_aligned_alloc(size_t alignment, size_t size);
203
212void platform_aligned_free(void *ptr);
213
224
225/* ============================================================================
226 * Error Handling
227 * ============================================================================
228 */
229
239const char *platform_strerror(int errnum);
240
250
260
261/* ============================================================================
262 * File Operations
263 * ============================================================================
264 */
265
278int platform_open(const char *pathname, int flags, ...);
279
290FILE *platform_fopen(const char *filename, const char *mode);
291
302FILE *platform_fdopen(int fd, const char *mode);
303
315ssize_t platform_read(int fd, void *buf, size_t count);
316
328ssize_t platform_write(int fd, const void *buf, size_t count);
329
339int platform_close(int fd);
340
350int platform_unlink(const char *pathname);
351
362int platform_chmod(const char *pathname, int mode);
363
364/* ============================================================================
365 * File Open Flags (Cross-platform)
366 * ============================================================================
367 */
368
417#ifdef _WIN32
418#define PLATFORM_O_RDONLY _O_RDONLY
419#define PLATFORM_O_WRONLY _O_WRONLY
420#define PLATFORM_O_RDWR _O_RDWR
421#define PLATFORM_O_CREAT _O_CREAT
422#define PLATFORM_O_EXCL _O_EXCL
423#define PLATFORM_O_TRUNC _O_TRUNC
424#define PLATFORM_O_APPEND _O_APPEND
425#define PLATFORM_O_BINARY _O_BINARY
426#else
427#define PLATFORM_O_RDONLY O_RDONLY
428#define PLATFORM_O_WRONLY O_WRONLY
429#define PLATFORM_O_RDWR O_RDWR
430#define PLATFORM_O_CREAT O_CREAT
431#define PLATFORM_O_EXCL O_EXCL
432#define PLATFORM_O_TRUNC O_TRUNC
433#define PLATFORM_O_APPEND O_APPEND
434#define PLATFORM_O_BINARY 0 // Not needed on POSIX
435#endif
436
FILE * platform_fopen(const char *filename, const char *mode)
Safe file open stream (fopen replacement)
int platform_strncasecmp(const char *s1, const char *s2, size_t n)
Case-insensitive string comparison with length limit.
char * platform_strndup(const char *s, size_t n)
Duplicate string with length limit (strndup replacement)
int platform_get_last_error(void)
Get last platform error code.
void platform_aligned_free(void *ptr)
Free aligned memory.
int platform_snprintf(char *str, size_t size, const char *format,...)
Safe string formatting (snprintf replacement)
void * platform_aligned_alloc(size_t alignment, size_t size)
Allocate aligned memory.
int platform_strcasecmp(const char *s1, const char *s2)
Case-insensitive string comparison.
int platform_chmod(const char *pathname, int mode)
Change file permissions/mode.
int platform_unlink(const char *pathname)
Delete/unlink file.
char * platform_strdup(const char *s)
Duplicate string (strdup replacement)
FILE * platform_fdopen(int fd, const char *mode)
Convert file descriptor to stream (fdopen replacement)
const char * platform_strerror(int errnum)
Get thread-safe error string.
int platform_close(int fd)
Safe file close (close replacement)
int platform_strncpy(char *dst, size_t dst_size, const char *src, size_t count)
Safe string copy with explicit size bounds (strncpy replacement)
int platform_open(const char *pathname, int flags,...)
Safe file open (open replacement)
size_t platform_strlcpy(char *dst, const char *src, size_t size)
Safe string copy with size tracking (strlcpy)
char * platform_strtok_r(char *str, const char *delim, char **saveptr)
Thread-safe string tokenization (strtok_r replacement)
int platform_vsnprintf(char *str, size_t size, const char *format, va_list ap)
Safe variable-argument string formatting.
ssize_t platform_read(int fd, void *buf, size_t count)
Safe file read (read replacement)
void platform_memory_barrier(void)
Perform memory barrier/fence operation.
void platform_set_last_error(int error)
Set platform error code.
size_t platform_strlcat(char *dst, const char *src, size_t size)
Safe string concatenation with size tracking (strlcat)
ssize_t platform_write(int fd, const void *buf, size_t count)
Platform-safe write function.