Pharos 0.7.23
Modern Web Framework & Web App Hosting Service.
Loading...
Searching...
No Matches
native_support.h
Go to the documentation of this file.
1/**
2 * @file
3 * @brief Declares shared low-level support helpers used across the Pharos native runtime.
4 */
5
6#ifndef PHAROS_NATIVE_SUPPORT_H
7#define PHAROS_NATIVE_SUPPORT_H
8
9#include <stddef.h>
10
11/**
12 * @brief Growable byte buffer used by parsing and string assembly helpers.
13 */
14typedef struct {
15 char *data; /**< Owned buffer storage. */
16 size_t len; /**< Number of bytes currently populated. */
17 size_t cap; /**< Total allocated capacity in bytes. */
18} Buffer;
19
20/** @name Shared support helpers
21 * @{
22 */
23/**
24 * @brief Initializes a growable buffer to an empty state.
25 * @param buffer Buffer to initialize.
26 */
27void buffer_init(Buffer *buffer);
28/**
29 * @brief Releases memory owned by a growable buffer.
30 * @param buffer Buffer to clear.
31 */
32void buffer_free(Buffer *buffer);
33/**
34 * @brief Ensures a buffer can hold at least a requested number of bytes.
35 * @param buffer Buffer whose capacity may grow.
36 * @param needed Minimum required capacity in bytes.
37 * @return 0 on success, or -1 on allocation or overflow failure.
38 */
39int buffer_reserve(Buffer *buffer, size_t needed);
40/**
41 * @brief Appends bytes to a growable buffer and maintains a trailing NUL byte.
42 * @param buffer Buffer to append into.
43 * @param data Source bytes to append.
44 * @param len Number of bytes to append.
45 * @return 0 on success, or -1 on allocation failure.
46 */
47int buffer_append(Buffer *buffer, const void *data, size_t len);
48/**
49 * @brief Duplicates a byte range into a newly allocated NUL-terminated string.
50 * @param start Pointer to the first byte to copy.
51 * @param len Number of bytes to copy.
52 * @return Newly allocated string, or NULL on allocation failure.
53 */
54char *dup_range(const char *start, size_t len);
55/**
56 * @brief Trims leading and trailing ASCII whitespace from a string in place.
57 * @param text Mutable string buffer to normalize.
58 */
59void trim_in_place(char *text);
60/**
61 * @brief Tests whether one string starts with another string.
62 * @param value Candidate full string.
63 * @param prefix Prefix to test.
64 * @return Non-zero when value starts with prefix, otherwise 0.
65 */
66int starts_with(const char *value, const char *prefix);
67/**
68 * @brief Tests whether two strings are exactly equal.
69 * @param a First string.
70 * @param b Second string.
71 * @return Non-zero when the strings are equal, otherwise 0.
72 */
73int streq(const char *a, const char *b);
74/**
75 * @brief Joins two path segments using the native path separator.
76 * @param left Left path segment.
77 * @param right Right path segment.
78 * @return Newly allocated joined path, or NULL on allocation failure.
79 */
80char *path_join(const char *left, const char *right);
81/**
82 * @brief Returns the parent directory component of a path.
83 * @param path Source path.
84 * @return Newly allocated directory path.
85 */
86char *path_dirname(const char *path);
87/**
88 * @brief Tests whether a filesystem path currently exists.
89 * @param path Path to test.
90 * @return Non-zero when the path exists, otherwise 0.
91 */
92int path_exists(const char *path);
93/**
94 * @brief Tests whether a filesystem path refers to a directory.
95 * @param path Path to test.
96 * @return Non-zero when the path is a directory, otherwise 0.
97 */
98int path_is_directory(const char *path);
99/**
100 * @brief Tests whether a filesystem path is writable by the current process.
101 * @param path Path to test.
102 * @return Non-zero when the path is writable, otherwise 0.
103 */
104int path_is_writable(const char *path);
105/**
106 * @brief Normalizes a path by collapsing redundant separators and dot segments.
107 * @param path Path to canonicalize.
108 * @return Newly allocated canonicalized path, or NULL on failure.
109 */
110char *path_canonicalize(const char *path);
111/**
112 * @brief Resolves a path value relative to a base directory when needed.
113 * @param base_dir Base directory used for relative values.
114 * @param value Raw path value to resolve.
115 * @return Newly allocated resolved path, or NULL on failure.
116 */
117char *resolve_relative_to(const char *base_dir, const char *value);
118/**
119 * @brief Returns the current working directory.
120 * @return Newly allocated working directory path, or NULL on failure.
121 */
122char *current_working_directory(void);
123/**
124 * @brief Reads an entire file into a newly allocated text buffer.
125 * @param path File path to read.
126 * @return Newly allocated text buffer, or NULL on failure.
127 */
128char *read_file_text(const char *path);
129
130
131/** @} */
132#endif
void buffer_init(Buffer *buffer)
Initializes a growable buffer to an empty state.
char * path_join(const char *left, const char *right)
Joins two path segments using the native path separator.
void trim_in_place(char *text)
Trims leading and trailing ASCII whitespace from a string in place.
char * dup_range(const char *start, size_t len)
Duplicates a byte range into a newly allocated NUL-terminated string.
char * path_canonicalize(const char *path)
Normalizes a path by collapsing redundant separators and dot segments.
void buffer_free(Buffer *buffer)
Releases memory owned by a growable buffer.
char * current_working_directory(void)
Returns the current working directory.
int streq(const char *a, const char *b)
Tests whether two strings are exactly equal.
int path_is_directory(const char *path)
Tests whether a filesystem path refers to a directory.
char * path_dirname(const char *path)
Returns the parent directory component of a path.
char * resolve_relative_to(const char *base_dir, const char *value)
Resolves a path value relative to a base directory when needed.
int buffer_append(Buffer *buffer, const void *data, size_t len)
Appends bytes to a growable buffer and maintains a trailing NUL byte.
int buffer_reserve(Buffer *buffer, size_t needed)
Ensures a buffer can hold at least a requested number of bytes.
int path_is_writable(const char *path)
Tests whether a filesystem path is writable by the current process.
char * read_file_text(const char *path)
Reads an entire file into a newly allocated text buffer.
int starts_with(const char *value, const char *prefix)
Tests whether one string starts with another string.
int path_exists(const char *path)
Tests whether a filesystem path currently exists.
Growable byte buffer used by parsing and string assembly helpers.
size_t len
size_t cap
char * data
const char size_t len
Definition yyjson.h:8364