ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
abstraction.h
Go to the documentation of this file.
1#pragma once
2
125// ============================================================================
126// Platform Detection
127// ============================================================================
128
134#ifdef _WIN32
135// Suppress Microsoft deprecation warnings for POSIX functions
136#ifndef _CRT_SECURE_NO_WARNINGS
137#define _CRT_SECURE_NO_WARNINGS
138#endif
139#ifndef _CRT_NONSTDC_NO_WARNINGS
140#define _CRT_NONSTDC_NO_WARNINGS
141#endif
142
151#define PLATFORM_WINDOWS 1
152
161#define PLATFORM_POSIX 0
162#else
164#define PLATFORM_WINDOWS 0
166#define PLATFORM_POSIX 1
167#endif
168
171// ============================================================================
172// Compiler Attributes
173// ============================================================================
174
180#ifdef _WIN32
181// MSVC doesn't support __attribute__((packed)) - use #pragma pack instead
182
201#define PACKED_STRUCT_BEGIN __pragma(pack(push, 1))
202
210#define PACKED_STRUCT_END __pragma(pack(pop))
211
219#define PACKED_ATTR
220
233#define ALIGNED_ATTR(x) __declspec(align(x))
234#else
235// GCC/Clang packed struct support
236
238#define PACKED_STRUCT_BEGIN
240#define PACKED_STRUCT_END
241
256#define PACKED_ATTR __attribute__((packed))
257
263#define ALIGNED_ATTR(x) __attribute__((aligned(x)))
264#endif
265
268// ============================================================================
269// Standard Headers
270// ============================================================================
271
272#include <stdio.h>
273#include <stdlib.h>
274#include <string.h>
275#include <stdint.h>
276#include <stdbool.h>
277#include <time.h>
278
279// ============================================================================
280// Platform Abstraction Modules
281// ============================================================================
282
283#include "platform/thread.h"
284#include "platform/mutex.h"
285#include "platform/rwlock.h"
286#include "platform/cond.h"
287// Windows socket shutdown constants - define before socket.h includes winsock2.h
288#ifdef _WIN32
289#ifndef SHUT_RDWR
291#define SHUT_RD 0
293#define SHUT_WR 1
295#define SHUT_RDWR 2
296#endif
297#endif
298
299#include "platform/socket.h"
300#include "platform/terminal.h"
301#include "platform/system.h"
302#include "platform/memory.h"
303#include "platform/process.h"
304#include "platform/fs.h"
305#include "util/uthash.h" // Wrapper ensures common.h is included first
306#include "platform/file.h"
307#include "platform/pipe.h"
308#include "debug/lock.h"
309
310// ============================================================================
311// Thread-Local Storage and Alignment Macros
312// ============================================================================
313
319#ifdef _WIN32
320// Windows-specific thread-local storage and alignment
321#ifdef _MSC_VER
337#define THREAD_LOCAL __declspec(thread)
338
346#define ALIGNED_32 __declspec(align(32))
347
355#define ALIGNED_16 __declspec(align(16))
356#else
357// Clang on Windows
362#define THREAD_LOCAL __thread
367#define ALIGNED_32 __attribute__((aligned(32)))
372#define ALIGNED_16 __attribute__((aligned(16)))
373#endif
374#else
375// POSIX thread-local storage and alignment
376#ifndef THREAD_LOCAL
381#define THREAD_LOCAL __thread
386#define ALIGNED_32 __attribute__((aligned(32)))
391#define ALIGNED_16 __attribute__((aligned(16)))
392#endif
393#endif
394
397// ============================================================================
398// Platform-Specific Compatibility
399// ============================================================================
400
401#ifdef _WIN32
402// Windows-specific compatibility
403
409#ifndef PATH_MAX
421#define PATH_MAX 260
422#endif
423
424// POSIX-style file permissions (not used on Windows, provided for compatibility)
425#ifndef S_IRUSR
427#define S_IRUSR 0400
429#define S_IWUSR 0200
431#define S_IXUSR 0100
433#define S_IRGRP 0040
435#define S_IWGRP 0020
437#define S_IXGRP 0010
439#define S_IROTH 0004
441#define S_IWOTH 0002
443#define S_IXOTH 0001
444#endif
445
446// Signal constants
447#ifndef SIGPIPE
455#define SIGPIPE 13
456#endif
457
458// Standard file descriptor constants
459#ifndef STDIN_FILENO
461#define STDIN_FILENO 0
463#define STDOUT_FILENO 1
465#define STDERR_FILENO 2
466#endif
467
468// Windows socket types
475typedef unsigned long nfds_t;
476
477// Windows socket control codes
478#ifndef SIO_KEEPALIVE_VALS
485#define SIO_KEEPALIVE_VALS _WSAIOW(IOC_VENDOR, 4)
486#endif
487
495// Use platform-safe write wrapper
496#ifndef write
504#define write _write
505#endif
506
507// Windows headers for POSIX-like functions
508#include <io.h>
509#include <fcntl.h>
510#include <process.h>
511
512// POSIX function aliases for Windows
517#define close _close
522#define lseek _lseek
527#define read _read
532#define unlink _unlink
533
534// Additional POSIX function aliases for Windows
539#define isatty _isatty
544#define getpid _getpid
545
566void *aligned_alloc(size_t alignment, size_t size);
567
579int clock_gettime(int clk_id, struct timespec *tp);
580
592struct tm *gmtime_r(const time_t *timep, struct tm *result);
593
604typedef unsigned int useconds_t;
605
611// Missing POSIX file flags for Windows
612#ifndef O_CLOEXEC
621#define O_CLOEXEC 0
622#endif
623
624// Missing POSIX time functions and constants for Windows
625#include <time.h>
626#ifndef CLOCK_REALTIME
634#define CLOCK_REALTIME 0
635#ifndef CLOCK_MONOTONIC
643#define CLOCK_MONOTONIC 1
644#endif
645#endif
646
649#else
650// POSIX-specific includes
651#include <unistd.h>
652#include <limits.h>
653#endif
654
655// ============================================================================
656// Cross-Platform Sleep Functions
657// ============================================================================
658
682void platform_sleep_usec(unsigned int usec);
683
699ssize_t platform_write(int fd, const void *buf, size_t count);
700
703// ============================================================================
704// Utility Macros
705// ============================================================================
706
732#ifndef UNUSED
733#define UNUSED(x) ((void)(x))
734#endif
735
738// ============================================================================
739// Restore Default Packing for Application Code
740// ============================================================================
741// NOTE: windows_compat.h now handles pack(pop) automatically
Cross-platform condition variable interface for ascii-chat.
Cross-platform file I/O interface for ascii-chat.
Cross-platform file system operations.
void platform_sleep_usec(unsigned int usec)
High-precision sleep function with microsecond precision.
ssize_t platform_write(int fd, const void *buf, size_t count)
Platform-safe write function.
Application limits and constraints.
🔒 Lock debugging and deadlock detection system for ascii-chat
Cross-platform mutex interface for ascii-chat.
Cross-platform pipe/agent socket interface for ascii-chat.
Cross-platform memory management utilities.
🧵 Cross-platform thread interface for ascii-chat
Cross-platform process execution utilities.
Cross-platform read-write lock interface for ascii-chat.
Cross-platform socket interface for ascii-chat.
Cross-platform system functions interface for ascii-chat.
🖥️ Cross-platform terminal interface for ascii-chat
⏱️ High-precision timing utilities using sokol_time.h and uthash
#️⃣ Wrapper for uthash.h that ensures common.h is included first
🔤 String Manipulation and Shell Escaping Utilities