ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
resources.h File Reference

Resource management for man page generation. More...

Go to the source code of this file.

Data Structures

struct  manpage_resources_t
 Man page resource container. More...
 

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

Resource management for man page generation.

This module abstracts loading man page resources from either embedded (production) or filesystem (development) sources. Provides a clean interface for accessing template and content sections.

Definition in file resources.h.

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 151 of file resources.c.

151 {
152 if (!resources) {
153 return;
154 }
155
156 // Only free content if it was allocated (i.e., loaded from filesystem in debug builds)
157 if (resources->allocated) {
158#ifndef NDEBUG
159 // In debug builds, template and content were allocated by load_single_resource
160 if (resources->template_content) {
161 char *template_ptr = (char *)resources->template_content;
162 SAFE_FREE(template_ptr);
163 }
164 if (resources->content_sections) {
165 char *content_ptr = (char *)resources->content_sections;
166 SAFE_FREE(content_ptr);
167 }
168#endif
169 }
170
171 // Clear the structure
172 resources->template_content = NULL;
173 resources->template_len = 0;
174 resources->content_sections = NULL;
175 resources->content_len = 0;
176 resources->is_embedded = false;
177 resources->allocated = false;
178}
#define SAFE_FREE(ptr)
Definition common.h:376
bool allocated
True if resources were allocated (need freeing)
Definition resources.h:28
size_t template_len
Length of template_content.
Definition resources.h:24
const char * content_sections
Content sections file (.1.content)
Definition resources.h:25
bool is_embedded
True if resources from embedded binary.
Definition resources.h:27
size_t content_len
Length of content_sections.
Definition resources.h:26
const char * template_content
Template file content (.1.in)
Definition resources.h:23

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 180 of file resources.c.

180 {
181 if (!resources) {
182 return false;
183 }
184
185 // Resources are valid if template is loaded (content is now optional/merged into template)
186 return resources->template_content != NULL && resources->template_len > 0;
187}

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 107 of file resources.c.

107 {
108 if (!resources) {
109 return SET_ERRNO(ERROR_INVALID_PARAM, "resources pointer cannot be NULL");
110 }
111
112 // Initialize to safe state
113 resources->template_content = NULL;
114 resources->template_len = 0;
115 resources->content_sections = NULL;
116 resources->content_len = 0;
117 resources->is_embedded = false;
118 resources->allocated = false;
119
120 // Load template resource
121 asciichat_error_t err = load_single_resource(&resources->template_content, &resources->template_len,
122 get_manpage_template, "man page template");
123 if (err != ASCIICHAT_OK) {
124 return err;
125 }
126
127 // Content is now merged into template - no separate content file needed
128 // Set to NULL so cleanup doesn't try to free a string literal
129 resources->content_sections = NULL;
130 resources->content_len = 0;
131
132 // Determine if resources came from embedded binary
133 // In production (NDEBUG), both would be embedded; in development, both would be files
134#ifdef NDEBUG
135 resources->is_embedded = true;
136#else
137 resources->is_embedded = false;
138#endif
139
140 // Mark that we may have allocated memory (only in debug builds for filesystem reads)
141#ifdef NDEBUG
142 resources->allocated = false;
143#else
144 resources->allocated = true;
145#endif
146
147 log_debug("Resources loaded successfully (embedded=%d, allocated=%d)", resources->is_embedded, resources->allocated);
148 return ASCIICHAT_OK;
149}
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
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it, returning the error code.
asciichat_error_t
Error and exit codes - unified status values (0-255)
Definition error_codes.h:49
@ ASCIICHAT_OK
Definition error_codes.h:51
@ ERROR_INVALID_PARAM
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:548

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().