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 File Reference

Man page resource loading and management. More...

Go to the source code of this file.

Functions

asciichat_error_t manpage_resources_load (manpage_resources_t *resources)
 Load man page resources from embedded or filesystem sources.
void manpage_resources_destroy (manpage_resources_t *resources)
 Cleanup allocated man page resources.
bool manpage_resources_is_valid (const manpage_resources_t *resources)
 Check if resources are available.

Detailed Description

Man page resource loading and management.

Abstracts loading man page resources from either embedded (production) or filesystem (development) sources. Provides a clean interface that hides the complexity of the underlying embedded_resources API.

Definition in file resources.c.

Function Documentation

◆ manpage_resources_destroy()

void manpage_resources_destroy ( manpage_resources_t * resources)

Cleanup allocated man page resources.

Frees any dynamically allocated resources. Safe to call even if manpage_resources_load() failed or resources were from embedded binary.

Parameters
[in]resourcesResources to cleanup (can be NULL)

Definition at line 143 of file resources.c.

143 {
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}
#define SAFE_FREE(ptr)
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

References manpage_resources_t::allocated, manpage_resources_t::content_len, manpage_resources_t::content_sections, manpage_resources_t::is_embedded, SAFE_FREE, manpage_resources_t::template_content, and manpage_resources_t::template_len.

Referenced by options_config_generate_manpage_merged().

◆ manpage_resources_is_valid()

bool manpage_resources_is_valid ( const manpage_resources_t * resources)

Check if resources are available.

Returns true only if both template and content resources are loaded.

Parameters
[in]resourcesResources to check (can be NULL)
Returns
true if resources are complete and valid

Definition at line 172 of file resources.c.

172 {
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}

References manpage_resources_t::template_content, and manpage_resources_t::template_len.

Referenced by options_config_generate_manpage_merged().

◆ manpage_resources_load()

asciichat_error_t manpage_resources_load ( manpage_resources_t * resources)

Load man page resources from embedded or filesystem sources.

Automatically selects resource source based on build type:

  • Debug builds: Load from filesystem (share/man/man1/)
  • Release builds: Load from embedded resources in binary

The caller should call manpage_resources_destroy() when done.

Parameters
[out]resourcesPopulated with loaded resource pointers
Returns
ASCIICHAT_OK on success
ERROR_CONFIG if resources cannot be loaded
ERROR_INVALID_PARAM if resources is NULL
Note
On success, caller must call manpage_resources_destroy()
For embedded resources, no cleanup needed (pointers to static data)
For filesystem resources, content is allocated and must be freed

Definition at line 100 of file resources.c.

100 {
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}
#define SET_ERRNO(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
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_INVALID_PARAM
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:565

References manpage_resources_t::allocated, ASCIICHAT_OK, manpage_resources_t::content_len, manpage_resources_t::content_sections, ERROR_INVALID_PARAM, get_manpage_template(), manpage_resources_t::is_embedded, log_debug, SET_ERRNO, manpage_resources_t::template_content, and manpage_resources_t::template_len.

Referenced by options_config_generate_manpage_merged().