Skip to content

Commit b72e1ac

Browse files
committed
lib: add xbps_fmt* functions for string formatting
1 parent baa9133 commit b72e1ac

File tree

3 files changed

+697
-1
lines changed

3 files changed

+697
-1
lines changed

include/xbps.h.in

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