ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
embedded.c
Go to the documentation of this file.
1
11#include <ascii-chat/common.h>
12#include <ascii-chat/log/log.h>
13#include <stdlib.h>
14#include <string.h>
15#include <stdio.h>
16#include <limits.h>
17
18// =============================================================================
19// Resource Access Implementation
20// =============================================================================
21
22int get_manpage_template(FILE **out_file, const char **out_content, size_t *out_len) {
23#ifdef NDEBUG
24 // Release build: Return embedded data
25 if (out_content) {
26 *out_content = embedded_manpage_template;
27 }
28 if (out_len) {
30 }
31 if (out_file) {
32 *out_file = NULL;
33 }
34
35 log_debug("Using embedded man page template (%zu bytes)", embedded_manpage_template_size);
36 return 0;
37#else
38 // Debug build: Read from filesystem
39 if (!out_file) {
40 SET_ERRNO(ERROR_INVALID_PARAM, "out_file cannot be NULL in development mode");
41 return -1;
42 }
43
44 // Construct absolute path using ASCIICHAT_RESOURCE_DIR (set at build time)
45#ifdef ASCIICHAT_RESOURCE_DIR
46 static char path_buffer[PATH_MAX];
47 safe_snprintf(path_buffer, sizeof(path_buffer), "%s/share/man/man1/ascii-chat.1.in", ASCIICHAT_RESOURCE_DIR);
48 const char *path = path_buffer;
49#else
50 // Fallback: try relative path if ASCIICHAT_RESOURCE_DIR not defined
51 const char *path = "share/man/man1/ascii-chat.1.in";
52#endif
53
54 // Use binary mode to ensure fread byte count matches ftell file size.
55 // Text mode on Windows translates \r\n to \n, causing size mismatch.
56 FILE *f = platform_fopen("file_stream", path, "rb");
57 if (!f) {
58 SET_ERRNO_SYS(ERROR_CONFIG, "Failed to open man page template: %s", path);
59 return -1;
60 }
61
62 *out_file = f;
63 if (out_content) {
64 *out_content = NULL;
65 }
66 if (out_len) {
67 *out_len = 0;
68 }
69
70 log_debug("Using filesystem man page template: %s", path);
71 return 0;
72#endif
73}
74
75int get_manpage_content(FILE **out_file, const char **out_content, size_t *out_len) {
76 // Content is now consolidated into the template - return empty
77 if (out_content) {
78 *out_content = "";
79 }
80 if (out_len) {
81 *out_len = 0;
82 }
83 if (out_file) {
84 *out_file = NULL;
85 }
86
87 log_debug("Man page content is now consolidated into template (empty)");
88 return 0;
89}
90
91void release_manpage_resources(FILE *file) {
92#ifdef NDEBUG
93 // Release build: No-op (embedded data is static, not heap-allocated)
94 (void)file; // Suppress unused parameter warning
95#else
96 // Debug build: Close file handle if present
97 if (file) {
98 fclose(file);
99 }
100#endif
101}
⚙️ Common definitions, error codes, macros, and types shared throughout the application
int get_manpage_template(FILE **out_file, const char **out_content, size_t *out_len)
Get man page template source (embedded or filesystem)
Definition embedded.c:22
int get_manpage_content(FILE **out_file, const char **out_content, size_t *out_len)
Get man page content source (embedded or filesystem)
Definition embedded.c:75
void release_manpage_resources(FILE *file)
Release resources obtained from get_manpage_*.
Definition embedded.c:91
Embedded resource management for production builds.
const size_t embedded_manpage_template_size
Size of embedded_manpage_template (excluding null terminator)
const char embedded_manpage_template[]
Behavior based on build type.
#define SET_ERRNO_SYS(code, context_msg,...)
Set error code with custom message and system error context, returning the error code.
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it, returning the error code.
@ ERROR_CONFIG
Definition error_codes.h:57
@ ERROR_INVALID_PARAM
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:548
int safe_snprintf(char *buffer, size_t buffer_size, const char *format,...)
Safe formatted string printing to buffer.
Definition system.c:148
FILE * platform_fopen(const char *name, const char *filename, const char *mode)
Safe file open stream (fopen replacement)
Application limits and constraints.
📝 Logging API with multiple log levels and terminal output control