ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
embedded_resources.c
Go to the documentation of this file.
1
10#include <ascii-chat/embedded_resources.h>
11#include <ascii-chat/common.h>
12#include <ascii-chat/log/logging.h>
13#include <ascii-chat/platform/util.h>
14#include <stdlib.h>
15#include <string.h>
16#include <stdio.h>
17#include <limits.h>
18
19// =============================================================================
20// External Declarations for Embedded Data
21// =============================================================================
22//
23// These symbols are defined in auto-generated C files (generated at build time
24// by cmake/utils/EmbedTextFile.cmake). They always exist (generated from
25// CMakeLists.txt custom commands), but are only used in Release builds (NDEBUG defined)
26//
27extern const char embedded_manpage_template[];
28extern const size_t embedded_manpage_template_len;
29
30// =============================================================================
31// Resource Access Implementation
32// =============================================================================
33
34int get_manpage_template(FILE **out_file, const char **out_content, size_t *out_len) {
35#ifdef NDEBUG
36 // Release build: Return embedded data
37 if (out_content) {
38 *out_content = embedded_manpage_template;
39 }
40 if (out_len) {
42 }
43 if (out_file) {
44 *out_file = NULL;
45 }
46
47 log_debug("Using embedded man page template (%zu bytes)", embedded_manpage_template_len);
48 return 0;
49#else
50 // Debug build: Read from filesystem
51 if (!out_file) {
52 SET_ERRNO(ERROR_INVALID_PARAM, "out_file cannot be NULL in development mode");
53 return -1;
54 }
55
56 // Construct absolute path using ASCIICHAT_RESOURCE_DIR (set at build time)
57#ifdef ASCIICHAT_RESOURCE_DIR
58 static char path_buffer[PATH_MAX];
59 safe_snprintf(path_buffer, sizeof(path_buffer), "%s/share/man/man1/ascii-chat.1.in", ASCIICHAT_RESOURCE_DIR);
60 const char *path = path_buffer;
61#else
62 // Fallback: try relative path if ASCIICHAT_RESOURCE_DIR not defined
63 const char *path = "share/man/man1/ascii-chat.1.in";
64#endif
65
66 // Use binary mode to ensure fread byte count matches ftell file size.
67 // Text mode on Windows translates \r\n to \n, causing size mismatch.
68 FILE *f = platform_fopen(path, "rb");
69 if (!f) {
70 SET_ERRNO_SYS(ERROR_CONFIG, "Failed to open man page template: %s", path);
71 return -1;
72 }
73
74 *out_file = f;
75 if (out_content) {
76 *out_content = NULL;
77 }
78 if (out_len) {
79 *out_len = 0;
80 }
81
82 log_debug("Using filesystem man page template: %s", path);
83 return 0;
84#endif
85}
86
87int get_manpage_content(FILE **out_file, const char **out_content, size_t *out_len) {
88 // Content is now consolidated into the template - return empty
89 if (out_content) {
90 *out_content = "";
91 }
92 if (out_len) {
93 *out_len = 0;
94 }
95 if (out_file) {
96 *out_file = NULL;
97 }
98
99 log_debug("Man page content is now consolidated into template (empty)");
100 return 0;
101}
102
104#ifdef NDEBUG
105 // Release build: No-op (embedded data is static, not heap-allocated)
106 (void)file; // Suppress unused parameter warning
107#else
108 // Debug build: Close file handle if present
109 if (file) {
110 fclose(file);
111 }
112#endif
113}
int get_manpage_template(FILE **out_file, const char **out_content, size_t *out_len)
const size_t embedded_manpage_template_len
int get_manpage_content(FILE **out_file, const char **out_content, size_t *out_len)
void release_manpage_resources(FILE *file)
const char embedded_manpage_template[]
int safe_snprintf(char *buffer, size_t buffer_size, const char *format,...)
Safe formatted string printing to buffer.
Definition system.c:456
FILE * platform_fopen(const char *filename, const char *mode)