ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
platform/wasm/string.c
Go to the documentation of this file.
1
9#include <string.h>
10#include <stddef.h>
11#include <stdarg.h>
12
13size_t platform_strlcpy(char *dst, const char *src, size_t size) {
14 size_t src_len = strlen(src);
15
16 if (size == 0) {
17 return src_len;
18 }
19
20 size_t copy_len = (src_len >= size) ? size - 1 : src_len;
21 memcpy(dst, src, copy_len);
22 dst[copy_len] = '\0';
23
24 return src_len;
25}
26
27size_t platform_strlcat(char *dst, const char *src, size_t size) {
28 size_t dst_len = strlen(dst);
29 size_t src_len = strlen(src);
30
31 if (dst_len >= size) {
32 return dst_len + src_len;
33 }
34
35 size_t remaining = size - dst_len;
36 size_t copy_len = (src_len >= remaining) ? remaining - 1 : src_len;
37 memcpy(dst + dst_len, src, copy_len);
38 dst[dst_len + copy_len] = '\0';
39
40 return dst_len + src_len;
41}
42
43int platform_strcasecmp(const char *s1, const char *s2) {
44 // Use standard strcasecmp (available in WASM)
45 return strcasecmp(s1, s2);
46}
47
48int platform_asprintf(char **strp, const char *fmt, ...) {
49 // Use standard asprintf (available in WASM/Emscripten)
50 va_list args;
51 va_start(args, fmt);
52 int ret = vasprintf(strp, fmt, args);
53 va_end(args);
54 return ret;
55}
56
57char *platform_strdup(const char *s) {
58 // Use standard strdup (available in WASM)
59 return strdup(s);
60}
61
62asciichat_error_t platform_memcpy(void *dest, size_t dest_size, const void *src, size_t count) {
63 if (!dest || !src) {
65 }
66 if (count > dest_size) {
67 return ERROR_INVALID_PARAM; // Buffer overflow protection
68 }
69 memcpy(dest, src, count);
70 return ASCIICHAT_OK;
71}
72
73asciichat_error_t platform_memset(void *dest, size_t dest_size, int ch, size_t count) {
74 if (!dest) {
76 }
77 if (count > dest_size) {
78 return ERROR_INVALID_PARAM; // Buffer overflow protection
79 }
80 memset(dest, ch, count);
81 return ASCIICHAT_OK;
82}
83
84int platform_vsnprintf(char *str, size_t size, const char *format, va_list ap) {
85 return vsnprintf(str, size, format, ap);
86}
87
88int platform_strncasecmp(const char *s1, const char *s2, size_t n) {
89 return strncasecmp(s1, s2, n);
90}
91
92asciichat_error_t platform_strcpy(char *dest, size_t dest_size, const char *src) {
93 if (!dest || !src) {
95 }
96 if (dest_size == 0) {
98 }
99
100 size_t src_len = strlen(src);
101 if (src_len >= dest_size) {
102 return ERROR_INVALID_PARAM;
103 }
104
105 strncpy(dest, src, dest_size - 1);
106 dest[dest_size - 1] = '\0';
107 return ASCIICHAT_OK;
108}
Platform abstraction layer umbrella header providing unified cross-platform API.
⚠️‼️ Comprehensive thread-local error context system for ascii-chat
asciichat_error_t
Error and exit codes - unified status values (0-255)
Definition error_codes.h:49
@ ASCIICHAT_OK
Definition error_codes.h:51
@ ERROR_INVALID_PARAM
int platform_asprintf(char **strp, const char *fmt,...)
Allocate formatted string (asprintf replacement)
int platform_strncasecmp(const char *s1, const char *s2, size_t n)
Case-insensitive string comparison with length limit.
asciichat_error_t platform_strcpy(char *dest, size_t dest_size, const char *src)
Platform-safe strcpy wrapper.
asciichat_error_t platform_memset(void *dest, size_t dest_size, int ch, size_t count)
Platform-safe memset wrapper.
int platform_strcasecmp(const char *s1, const char *s2)
Case-insensitive string comparison.
char * platform_strdup(const char *s)
Duplicate string (strdup replacement)
size_t platform_strlcpy(char *dst, const char *src, size_t size)
Safe string copy with size tracking (strlcpy)
asciichat_error_t platform_memcpy(void *dest, size_t dest_size, const void *src, size_t count)
Platform-safe memcpy wrapper.
int platform_vsnprintf(char *str, size_t size, const char *format, va_list ap)
Safe variable-argument string formatting.
action_args_t args
size_t platform_strlcat(char *dst, const char *src, size_t size)