ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
resources.c
Go to the documentation of this file.
1
10
11#include <defer.h>
14#include <ascii-chat/log/log.h>
15#include <stdlib.h>
16#include <string.h>
17
29static asciichat_error_t load_single_resource(const char **out_content, size_t *out_len, int (*get_resource_func)(FILE **out_file, const char **out_content, size_t *out_len),
30 const char *resource_name) {
31 if (!out_content || !out_len) {
32 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid output pointers for %s", resource_name);
33 }
34
35 FILE *file = NULL;
36 const char *content = NULL;
37 size_t len = 0;
38
39 // Call the resource function to get template or content
40 int result = get_resource_func(&file, &content, &len);
41 if (result != 0) {
42 return SET_ERRNO(ERROR_CONFIG, "Failed to load %s resource", resource_name);
43 }
44
45 // Determine if we got embedded (content) or filesystem (file) resource
46 if (content) {
47 // Production: embedded resource
48 // No allocation needed, content is static
49 *out_content = content;
50 *out_len = len;
51 log_debug("Loaded %s from embedded resources (%zu bytes)", resource_name, len);
52 return ASCIICHAT_OK;
53 } else if (file) {
54 // Development: filesystem resource
55 // Need to read entire file into memory for easy access
56 defer(release_manpage_resources(file));
57
58 // Seek to end to determine file size
59 if (fseek(file, 0, SEEK_END) != 0) {
60 return SET_ERRNO_SYS(ERROR_CONFIG, "Failed to seek %s file", resource_name);
61 }
62
63 long file_size = ftell(file);
64 if (file_size < 0) {
65 return SET_ERRNO_SYS(ERROR_CONFIG, "Failed to get %s file size", resource_name);
66 }
67
68 if (fseek(file, 0, SEEK_SET) != 0) {
69 return SET_ERRNO_SYS(ERROR_CONFIG, "Failed to seek %s file", resource_name);
70 }
71
72 // Allocate buffer (file_size + 1 for null terminator)
73 size_t buffer_size = (size_t)file_size + 1;
74 SAFE_MALLOC_DEFER_FREE(buffer, buffer_size, char *);
75 if (!buffer) {
76 return SET_ERRNO(ERROR_CONFIG, "Failed to allocate memory for %s (%zu bytes)", resource_name, buffer_size);
77 }
78
79 // Read file content
80 size_t bytes_read = fread(buffer, 1, (size_t)file_size, file);
81
82 if (bytes_read != (size_t)file_size) {
83 return SET_ERRNO_SYS(ERROR_CONFIG, "Failed to read complete %s file (%zu/%lu bytes read)", resource_name, bytes_read, (unsigned long)file_size);
84 }
85
86 // Ensure null termination
87 buffer[(size_t)file_size] = '\0';
88
89 *out_content = buffer;
90 buffer = NULL;
91 *out_len = (size_t)file_size;
92 log_debug("Loaded %s from filesystem (%zu bytes)", resource_name, *out_len);
93 return ASCIICHAT_OK;
94 } else {
95 // Should not happen - either file or content should be set
96 return SET_ERRNO(ERROR_CONFIG, "Invalid %s resource state (neither file nor content available)", resource_name);
97 }
98}
99
101 if (!resources) {
102 return SET_ERRNO(ERROR_INVALID_PARAM, "resources pointer cannot be NULL");
103 }
104
105 // Initialize to safe state
106 resources->template_content = NULL;
107 resources->template_len = 0;
108 resources->content_sections = NULL;
109 resources->content_len = 0;
110 resources->is_embedded = false;
111 resources->allocated = false;
112
113 // Load template resource
114 asciichat_error_t err = load_single_resource(&resources->template_content, &resources->template_len, get_manpage_template, "man page template");
115 if (err != ASCIICHAT_OK) {
116 return err;
117 }
118
119 // Content is now merged into template - no separate content file needed
120 // Set to NULL so cleanup doesn't try to free a string literal
121 resources->content_sections = NULL;
122 resources->content_len = 0;
123
124 // Determine if resources came from embedded binary
125 // In production (NDEBUG), both would be embedded; in development, both would be files
126#ifdef NDEBUG
127 resources->is_embedded = true;
128#else
129 resources->is_embedded = false;
130#endif
131
132 // Mark that we may have allocated memory (only in debug builds for filesystem reads)
133#ifdef NDEBUG
134 resources->allocated = false;
135#else
136 resources->allocated = true;
137#endif
138
139 log_debug("Resources loaded successfully (embedded=%d, allocated=%d)", resources->is_embedded, resources->allocated);
140 return ASCIICHAT_OK;
141}
142
144 if (!resources) {
145 return;
146 }
147
148 // Only free content if it was allocated (i.e., loaded from filesystem in debug builds)
149 if (resources->allocated) {
150#ifndef NDEBUG
151 // In debug builds, template and content were allocated by load_single_resource
152 if (resources->template_content) {
153 char *template_ptr = (char *)resources->template_content;
154 SAFE_FREE(template_ptr);
155 }
156 if (resources->content_sections) {
157 char *content_ptr = (char *)resources->content_sections;
158 SAFE_FREE(content_ptr);
159 }
160#endif
161 }
162
163 // Clear the structure
164 resources->template_content = NULL;
165 resources->template_len = 0;
166 resources->content_sections = NULL;
167 resources->content_len = 0;
168 resources->is_embedded = false;
169 resources->allocated = false;
170}
171
173 if (!resources) {
174 return false;
175 }
176
177 // Resources are valid if template is loaded (content is now optional/merged into template)
178 return resources->template_content != NULL && resources->template_len > 0;
179}
#define SET_ERRNO(code,...)
#define SET_ERRNO_SYS(code,...)
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
void release_manpage_resources(FILE *file)
Release resources obtained from get_manpage_*.
Definition embedded.c:91
Embedded resource management for production builds.
int buffer_size
Size of circular buffer.
Definition grep.c:69
#define SAFE_FREE(ptr)
#define SAFE_MALLOC_DEFER_FREE(name, size, cast)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_CONFIG
Definition error_codes.h:56
@ ERROR_INVALID_PARAM
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:565
📝 Logging API with multiple log levels and terminal output control
void manpage_resources_destroy(manpage_resources_t *resources)
Cleanup allocated man page resources.
Definition resources.c:143
asciichat_error_t manpage_resources_load(manpage_resources_t *resources)
Load man page resources from embedded or filesystem sources.
Definition resources.c:100
bool manpage_resources_is_valid(const manpage_resources_t *resources)
Check if resources are available.
Definition resources.c:172
Resource management for man page generation.
🔤 String Manipulation and Shell Escaping Utilities
Man page resource container.
Definition resources.h:27
bool allocated
True if resources were allocated (need freeing).
Definition resources.h:33
size_t template_len
Length of template_content.
Definition resources.h:29
const char * content_sections
Content sections file (.1.content).
Definition resources.h:30
bool is_embedded
True if resources from embedded binary.
Definition resources.h:32
size_t content_len
Length of content_sections.
Definition resources.h:31
const char * template_content
Template file content (.1.in).
Definition resources.h:28