Skip to content

Commit 0cf4db7

Browse files
committed
lib: add xbps_fmt_* formatted printing functions
1 parent 426b309 commit 0cf4db7

File tree

4 files changed

+644
-2
lines changed

4 files changed

+644
-2
lines changed

doc/xbps_api_doxyfile.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ SUBGROUPING = YES
313313
# @ingroup) instead of on a separate page (for HTML and Man pages) or
314314
# section (for LaTeX and RTF).
315315

316-
INLINE_GROUPED_CLASSES = NO
316+
INLINE_GROUPED_CLASSES = YES
317317

318318
# When the INLINE_SIMPLE_STRUCTS tag is set to YES, structs, classes, and
319319
# unions with only public data fields will be shown inline in the documentation

include/xbps.h.in

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,6 +2365,178 @@ xbps_plist_dictionary_from_file(const char *path);
23652365

23662366
/**@}*/
23672367

2368+
/** @addtogroup format */
2369+
/**@{*/
2370+
2371+
/**
2372+
* @struct xbps_fmt xbps.h "xbps.h"
2373+
* @brief Structure of parsed format string.
2374+
*/
2375+
struct xbps_fmt;
2376+
2377+
/**
2378+
* @struct xbps_fmt xbps.h "xbps.h"
2379+
* @brief Structure of parsed format specifier.
2380+
*/
2381+
struct xbps_fmt_spec {
2382+
/**
2383+
* @var fill
2384+
* @brief Character to pad the output with.
2385+
*/
2386+
char fill;
2387+
/**
2388+
* @var align
2389+
* @brief Alignment modifier, can be `<`, `>` or `=`.
2390+
*
2391+
* Possible values are:
2392+
* - `<`: left align.
2393+
* - `>`: right align.
2394+
* - `=`: place padding after the sign.
2395+
*/
2396+
char align;
2397+
/**
2398+
* @var sign
2399+
* @brief Sign modifier can be `+`, `-` or a space.
2400+
*
2401+
* Possible values are:
2402+
* - `-`: sign negative numbers.
2403+
* - `+`: sign both negative and positive numbers.
2404+
* - space: sign negative numbers and add space before positive numbers.
2405+
*/
2406+
char sign;
2407+
/**
2408+
* @var width
2409+
* @brief Alignment width to pad the output to.
2410+
*/
2411+
unsigned int width;
2412+
/**
2413+
* @var prec
2414+
* @brief Precision of the output, usually for numbers.
2415+
*/
2416+
unsigned int prec;
2417+
/**
2418+
* @var type
2419+
* @brief Type specifier usually to change the output format type.
2420+
*
2421+
* Can contain any character, xbps_fmt_number() uses the following:
2422+
* - `u`: Unsigned decimal.
2423+
* - `d`: Decimal.
2424+
* - `x`: Hex with lowercase letters.
2425+
* - `X`: hex with uppercase letters.
2426+
* - `h`: Human readable using humanize_number(3).
2427+
*/
2428+
char type;
2429+
};
2430+
2431+
/**
2432+
* @brief Format callback, called for each variable in the format string.
2433+
*
2434+
* A callback function should write data associated with \a var to \a fp and use
2435+
* \a w as alignment specifier.
2436+
*
2437+
* @param[in] fp The file to print to.
2438+
* @param[in] spec The format specifier.
2439+
* @param[in] var The format string variable name.
2440+
* @param[in] data Userdata passed to the xbps_fmt() function.
2441+
*/
2442+
typedef int (xbps_fmt_cb)(FILE *fp, const struct xbps_fmt_spec *spec, const char *var, void *data);
2443+
2444+
/**
2445+
* @brief Parses the format string \a format.
2446+
*
2447+
* @param[in] format The format string.
2448+
*
2449+
* @return The parsed format structure, or NULL on error.
2450+
* The returned buffer must be freed with xbps_fmt_free().
2451+
* @retval EINVAL Invalid format string.
2452+
* @retval ERANGE Invalid alignment specifier.
2453+
* @retval ENOMEM Memory allocation failure.
2454+
*/
2455+
struct xbps_fmt *xbps_fmt_parse(const char *format);
2456+
2457+
/**
2458+
* @brief Releases memory associated with \a fmt.
2459+
*
2460+
* @param[in] fmt The format string.
2461+
*/
2462+
void xbps_fmt_free(struct xbps_fmt *fmt);
2463+
2464+
/**
2465+
* @brief Print formatted text to \a fp.
2466+
*
2467+
* @param[in] fmt Format returned by struct xbps_fmt_parse().
2468+
* @param[in] cb Callback function called for each variable in the format.
2469+
* @param[in] data Userdata passed to the callback \a cb.
2470+
* @param[in] fp File to print to.
2471+
*
2472+
* @return 0 on success or a negative errno.
2473+
* @retval 0 Success
2474+
*/
2475+
int xbps_fmt(const struct xbps_fmt *fmt, xbps_fmt_cb *cb, void *data, FILE *fp);
2476+
2477+
/**
2478+
* @brief Print formatted dictionary values to \a fp.
2479+
*
2480+
* Prints formatted dictionary values as specified by the parsed \a fmt
2481+
* format string to \a fp.
2482+
*
2483+
* @param[in] fmt Format returned by struct xbps_fmt_parse().
2484+
* @param[in] dict Dictionary to print values from.
2485+
* @param[in] fp File to print to.
2486+
*
2487+
* @return 0 on success or value returned by \a cb.
2488+
* @retval 0 Success
2489+
*/
2490+
int xbps_fmt_dictionary(const struct xbps_fmt *fmt, xbps_dictionary_t dict, FILE *fp);
2491+
2492+
/**
2493+
* @brief Print formatted dictionary to \a fp.
2494+
*
2495+
* Print the formatted dictionary according to the \a format format string
2496+
* to \a fp.
2497+
*
2498+
* @param[in] format Format string.
2499+
* @param[in] cb Callback function called for each variable in the format.
2500+
* @param[in] data Userdata passed to the callback \a cb.
2501+
* @param[in] fp File to print to.
2502+
*
2503+
* @return 0 on success.
2504+
* @retval 0 Success.
2505+
* @retval -EINVAL Invalid format string.
2506+
* @retval -ERANGE Invalid alignment specifier.
2507+
* @retval -ENOMEM Memory allocation failure.
2508+
*/
2509+
int xbps_fmts(const char *format, xbps_fmt_cb *cb, void *data, FILE *fp);
2510+
2511+
/**
2512+
* @brief Print formatted number to \a fp.
2513+
*
2514+
* Prints the number \d to \a fp according to the specification \a spec.
2515+
*
2516+
* @param[in] spec Format specification.
2517+
* @param[in] num Number to print.
2518+
* @param[in] fp File to print to.
2519+
*
2520+
* @return Returns 0 on success.
2521+
*/
2522+
int xbps_fmt_number(const struct xbps_fmt_spec *spec, int64_t num, FILE *fp);
2523+
2524+
/**
2525+
* @brief Print formatted string to \a fp.
2526+
*
2527+
* Prints the string \a str to \a fp according to the specification \a spec.
2528+
*
2529+
* @param[in] spec Format specification.
2530+
* @param[in] str String to print.
2531+
* @param[in] len Length of the string or 0.
2532+
* @param[in] fp File to print to.
2533+
*
2534+
* @return Returns 0 on success.
2535+
*/
2536+
int xbps_fmt_string(const struct xbps_fmt_spec *spec, const char *str, size_t len, FILE *fp);
2537+
2538+
/**@}*/
2539+
23682540
#ifdef __cplusplus
23692541
}
23702542
#endif

lib/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ OBJS += plist_remove.o plist_fetch.o util.o util_path.o util_hash.o
5151
OBJS += repo.o repo_sync.o
5252
OBJS += rpool.o cb_util.o proplib_wrapper.o
5353
OBJS += package_alternatives.o
54-
OBJS += conf.o log.o
54+
OBJS += conf.o log.o format.o
5555
OBJS += $(EXTOBJS) $(COMPAT_OBJS)
5656
# unnecessary unless pkgdb format changes
5757
# OBJS += pkgdb_conversion.o

0 commit comments

Comments
 (0)