lnm/include/lnm/common.h

101 lines
2.8 KiB
C

#ifndef LNM_COMMON
#define LNM_COMMON
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#define LNM_RES(x) \
{ \
lnm_err res = x; \
if (res != lnm_err_ok) \
return res; \
}
#define LNM_RES2(x, e) \
{ \
lnm_err res = x; \
if (res != lnm_err_ok) { \
e; \
return res; \
} \
}
#define LNM_MIN(x, y) ((x) < (y) ? (x) : (y))
#define LNM_MAX(x, y) ((x) > (y) ? (x) : (y))
typedef enum lnm_err {
lnm_err_ok = 0,
lnm_err_failed_alloc,
lnm_err_failed_network,
lnm_err_failed_poll,
lnm_err_not_setup,
lnm_err_bad_regex,
lnm_err_not_found,
lnm_err_already_present,
lnm_err_invalid_route,
lnm_err_overlapping_route,
} lnm_err;
typedef struct lnm_loop lnm_http_loop;
typedef struct lnm_loop_conn lnm_http_conn;
typedef struct lnm_http_step lnm_http_step;
typedef struct lnm_http_route lnm_http_route;
/**
* Returns whether the two strings are equal.
*
* @param s1 first string to compare
* @param s1_len length of `s1`
* @param s2 second string to compare
* @param s2_len length of `s2`
*/
bool lnm_strneq(const char *s1, size_t s1_len, const char *s2, size_t s2_len);
/**
* Returns whether the two strings are equal, ignoring capitalisation.
*
* @param s1 first string to compare
* @param s1_len length of `s1`
* @param s2 second string to compare
* @param s2_len length of `s2`
*/
bool lnm_strnieq(const char *s1, size_t s1_len, const char *s2, size_t s2_len);
/**
* Calculate integer exponentation.
*
* @param base
* @param power
*/
uint64_t lnm_ipow(uint64_t base, uint64_t power);
/**
* Parse the given string into a number.
*
* @param s string to parse
* @param len length of s
* @return the parsed number, or 0 if the number is invalid
*/
uint64_t lnm_atoi(const char *s, size_t len);
/**
* Calculate how many base 10 digits the given number consists of.
*
* @param num number to use
*/
uint64_t lnm_digits(uint64_t num);
/**
* Check whether the given nul-terminated string solely consists of ASCII
* characters.
*
* @param s nul-terminated string to check
*/
bool lnm_is_ascii(const char *s);
#endif