ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
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 147 of file resources.c.

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

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

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

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

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

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