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

Embedded resource management for production builds. More...

Go to the source code of this file.

Functions

int get_manpage_template (FILE **out_file, const char **out_content, size_t *out_len)
 Get man page template source (embedded or filesystem)
 
int get_manpage_content (FILE **out_file, const char **out_content, size_t *out_len)
 Get man page content source (embedded or filesystem)
 
void release_manpage_resources (FILE *file)
 Release resources obtained from get_manpage_*.
 

Variables

const char embedded_manpage_template []
 Behavior based on build type.
 
const size_t embedded_manpage_template_size
 Size of embedded_manpage_template (excluding null terminator)
 

Detailed Description

Embedded resource management for production builds.

This module provides unified access to embedded documentation resources in production builds while maintaining fast iteration in development.

Build-Time Behavior:

  • Production builds (Release, RelWithDebInfo): Resources are embedded at compile time using CMake scripts. The binary is self-contained.
  • Development builds (Debug, Dev): Resources are read from filesystem for fast iteration (edit → rebuild → test → no wait).

Resource Types:

  1. Man page template (share/man/man1/ascii-chat.1.in)
  2. Man page content (share/man/man1/ascii-chat.1.content)

Example Usage:

// Get man page template (works in both production and development)
FILE *template_file = NULL;
const char *template_str = NULL;
size_t template_len = 0;
int result = get_manpage_template(&template_file, &template_str, &template_len);
if (result != 0) {
log_error("Failed to load man page template");
return;
}
// Use template (either from FILE* or const char*)
if (template_str) {
// Production: parse in-memory string
parse_from_memory(template_str, template_len);
} else {
// Development: parse from file
parse_from_file(template_file);
}
// Always clean up
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
#define log_error(...)
Log an ERROR message.
Definition log/log.h:587

Definition in file embedded_resources.h.

Function Documentation

◆ get_manpage_content()

int get_manpage_content ( FILE **  out_file,
const char **  out_content,
size_t *  out_len 
)

Get man page content source (embedded or filesystem)

Same behavior as get_manpage_template() but for the content file. See get_manpage_template() documentation for usage details.

Parameters
out_fileOutput pointer for FILE* (development) or NULL (production)
out_contentOutput pointer for const char* (production) or NULL (dev)
out_lenOutput pointer for size_t (production) or NULL (development)
Returns
0 on success, -1 on error

Definition at line 75 of file embedded.c.

75 {
76 // Content is now consolidated into the template - return empty
77 if (out_content) {
78 *out_content = "";
79 }
80 if (out_len) {
81 *out_len = 0;
82 }
83 if (out_file) {
84 *out_file = NULL;
85 }
86
87 log_debug("Man page content is now consolidated into template (empty)");
88 return 0;
89}
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:548

References log_debug.

◆ get_manpage_template()

int get_manpage_template ( FILE **  out_file,
const char **  out_content,
size_t *  out_len 
)

Get man page template source (embedded or filesystem)

Automatically selects between embedded and filesystem resources based on build type:

  • Release builds (NDEBUG defined): Returns embedded string
  • Debug builds (NDEBUG undefined): Reads from filesystem

Parameter Usage:

  • In production: out_content and out_len are set, out_file is NULL
  • In development: out_file is set to open FILE*, others are NULL
  • Caller must check which one is non-NULL to determine source
Parameters
out_fileOutput pointer for FILE* (development) or NULL (production)
out_contentOutput pointer for const char* (production) or NULL (dev)
out_lenOutput pointer for size_t (production) or NULL (development)
Returns
0 on success, -1 on error
Note
Caller must call release_manpage_resources(out_file) when done
Do NOT free out_content - it's either static or managed elsewhere

Definition at line 22 of file embedded.c.

22 {
23#ifdef NDEBUG
24 // Release build: Return embedded data
25 if (out_content) {
26 *out_content = embedded_manpage_template;
27 }
28 if (out_len) {
30 }
31 if (out_file) {
32 *out_file = NULL;
33 }
34
35 log_debug("Using embedded man page template (%zu bytes)", embedded_manpage_template_size);
36 return 0;
37#else
38 // Debug build: Read from filesystem
39 if (!out_file) {
40 SET_ERRNO(ERROR_INVALID_PARAM, "out_file cannot be NULL in development mode");
41 return -1;
42 }
43
44 // Construct absolute path using ASCIICHAT_RESOURCE_DIR (set at build time)
45#ifdef ASCIICHAT_RESOURCE_DIR
46 static char path_buffer[PATH_MAX];
47 safe_snprintf(path_buffer, sizeof(path_buffer), "%s/share/man/man1/ascii-chat.1.in", ASCIICHAT_RESOURCE_DIR);
48 const char *path = path_buffer;
49#else
50 // Fallback: try relative path if ASCIICHAT_RESOURCE_DIR not defined
51 const char *path = "share/man/man1/ascii-chat.1.in";
52#endif
53
54 // Use binary mode to ensure fread byte count matches ftell file size.
55 // Text mode on Windows translates \r\n to \n, causing size mismatch.
56 FILE *f = platform_fopen("file_stream", path, "rb");
57 if (!f) {
58 SET_ERRNO_SYS(ERROR_CONFIG, "Failed to open man page template: %s", path);
59 return -1;
60 }
61
62 *out_file = f;
63 if (out_content) {
64 *out_content = NULL;
65 }
66 if (out_len) {
67 *out_len = 0;
68 }
69
70 log_debug("Using filesystem man page template: %s", path);
71 return 0;
72#endif
73}
const size_t embedded_manpage_template_size
Size of embedded_manpage_template (excluding null terminator)
const char embedded_manpage_template[]
Behavior based on build type.
#define SET_ERRNO_SYS(code, context_msg,...)
Set error code with custom message and system error context, returning the error code.
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it, returning the error code.
@ ERROR_CONFIG
Definition error_codes.h:57
@ ERROR_INVALID_PARAM
int safe_snprintf(char *buffer, size_t buffer_size, const char *format,...)
Safe formatted string printing to buffer.
Definition system.c:148
FILE * platform_fopen(const char *name, const char *filename, const char *mode)
Safe file open stream (fopen replacement)

References embedded_manpage_template, embedded_manpage_template_size, ERROR_CONFIG, ERROR_INVALID_PARAM, log_debug, platform_fopen(), safe_snprintf(), SET_ERRNO, and SET_ERRNO_SYS.

Referenced by manpage_resources_load().

◆ release_manpage_resources()

void release_manpage_resources ( FILE *  file)

Release resources obtained from get_manpage_*.

Properly cleans up resources based on build type:

  • Production: No-op (embedded strings are static)
  • Development: Closes FILE* if non-NULL
Parameters
fileFILE* obtained from get_manpage_template/content(), or NULL
Note
Safe to call with NULL
Only closes file, does NOT free content pointer

Definition at line 91 of file embedded.c.

91 {
92#ifdef NDEBUG
93 // Release build: No-op (embedded data is static, not heap-allocated)
94 (void)file; // Suppress unused parameter warning
95#else
96 // Debug build: Close file handle if present
97 if (file) {
98 fclose(file);
99 }
100#endif
101}

Variable Documentation

◆ embedded_manpage_template

const char embedded_manpage_template[]
extern

Behavior based on build type.

In CMake:

  • NDEBUG is defined in Release/RelWithDebInfo builds → use embedded resources
  • NDEBUG is undefined in Debug/Dev builds → use filesystem resources

Embedded man page template content (auto-generated at build time)

Referenced by get_manpage_template().

◆ embedded_manpage_template_size

const size_t embedded_manpage_template_size
extern

Size of embedded_manpage_template (excluding null terminator)

Referenced by get_manpage_template().