#ifndef UCX_PROPERTIES_H
#define UCX_PROPERTIES_H
#include "common.h"
#include "string.h"
#include "map.h"
#include "buffer.h"
#include <stdio.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
struct cx_properties_config_s {
char delimiter;
char comment1;
char comment2;
char comment3;
};
typedef struct cx_properties_config_s CxPropertiesConfig;
extern const CxPropertiesConfig cx_properties_config_default;
enum cx_properties_status {
CX_PROPERTIES_NO_ERROR,
CX_PROPERTIES_NO_DATA,
CX_PROPERTIES_INCOMPLETE_DATA,
CX_PROPERTIES_OK,
CX_PROPERTIES_NULL_INPUT,
CX_PROPERTIES_INVALID_EMPTY_KEY,
CX_PROPERTIES_INVALID_MISSING_DELIMITER,
CX_PROPERTIES_BUFFER_ALLOC_FAILED,
CX_PROPERTIES_READ_INIT_FAILED,
CX_PROPERTIES_READ_FAILED,
CX_PROPERTIES_SINK_FAILED,
};
typedef enum cx_properties_status CxPropertiesStatus;
struct cx_properties_s {
CxPropertiesConfig config;
CxBuffer input;
CxBuffer buffer;
};
typedef struct cx_properties_s CxProperties;
typedef struct cx_properties_sink_s CxPropertiesSink;
cx_attr_nonnull
typedef int(*cx_properties_sink_func)(
CxProperties *prop,
CxPropertiesSink *sink,
cxstring key,
cxstring value
);
struct cx_properties_sink_s {
void *sink;
void *data;
cx_properties_sink_func sink_func;
};
typedef struct cx_properties_source_s CxPropertiesSource;
cx_attr_nonnull
typedef int(*cx_properties_read_func)(
CxProperties *prop,
CxPropertiesSource *src,
cxstring *target
);
cx_attr_nonnull
typedef int(*cx_properties_read_init_func)(
CxProperties *prop,
CxPropertiesSource *src
);
cx_attr_nonnull
typedef void(*cx_properties_read_clean_func)(
CxProperties *prop,
CxPropertiesSource *src
);
struct cx_properties_source_s {
void *src;
void *data_ptr;
size_t data_size;
cx_properties_read_func read_func;
cx_properties_read_init_func read_init_func;
cx_properties_read_clean_func read_clean_func;
};
cx_attr_nonnull
void cxPropertiesInit(CxProperties *prop, CxPropertiesConfig config);
cx_attr_nonnull
void cxPropertiesDestroy(CxProperties *prop);
cx_attr_nonnull
static inline
void cxPropertiesReset(CxProperties *prop) {
CxPropertiesConfig config = prop->config;
cxPropertiesDestroy(prop);
cxPropertiesInit(prop, config);
}
#define cxPropertiesInitDefault(prop) \
cxPropertiesInit(prop, cx_properties_config_default)
cx_attr_nonnull
cx_attr_access_r(
2,
3)
int cxPropertiesFilln(
CxProperties *prop,
const char *buf,
size_t len
);
#ifdef __cplusplus
}
cx_attr_nonnull
static inline
int cxPropertiesFill(
CxProperties *prop,
cxstring str
) {
return cxPropertiesFilln(prop, str.ptr, str.length);
}
cx_attr_nonnull
static inline
int cxPropertiesFill(
CxProperties *prop,
cxmutstr str
) {
return cxPropertiesFilln(prop, str.ptr, str.length);
}
cx_attr_nonnull
cx_attr_cstr_arg(
2)
static inline
int cxPropertiesFill(
CxProperties *prop,
const char *str
) {
return cxPropertiesFilln(prop, str, strlen(str));
}
extern "C" {
#else
#define cxPropertiesFill(prop, str) _Generic((str), \
cxstring: cx_properties_fill_cxstr, \
cxmutstr: cx_properties_fill_mutstr, \
char*: cx_properties_fill_str, \
const char*: cx_properties_fill_str) \
(prop, str)
cx_attr_nonnull
static inline
int cx_properties_fill_cxstr(
CxProperties *prop,
cxstring str
) {
return cxPropertiesFilln(prop, str.ptr, str.length);
}
cx_attr_nonnull
static inline
int cx_properties_fill_mutstr(
CxProperties *prop,
cxmutstr str
) {
return cxPropertiesFilln(prop, str.ptr, str.length);
}
cx_attr_nonnull
cx_attr_cstr_arg(
2)
static inline
int cx_properties_fill_str(
CxProperties *prop,
const char *str
) {
return cxPropertiesFilln(prop, str, strlen(str));
}
#endif
cx_attr_nonnull
void cxPropertiesUseStack(
CxProperties *prop,
char *buf,
size_t capacity
);
cx_attr_nonnull
cx_attr_nodiscard
CxPropertiesStatus cxPropertiesNext(
CxProperties *prop,
cxstring *key,
cxstring *value
);
cx_attr_nonnull
cx_attr_nodiscard
CxPropertiesSink cxPropertiesMapSink(CxMap *map);
cx_attr_nodiscard
CxPropertiesSource cxPropertiesStringSource(cxstring str);
cx_attr_nonnull
cx_attr_nodiscard
cx_attr_access_r(
1,
2)
CxPropertiesSource cxPropertiesCstrnSource(
const char *str,
size_t len);
cx_attr_nonnull
cx_attr_nodiscard
cx_attr_cstr_arg(
1)
CxPropertiesSource cxPropertiesCstrSource(
const char *str);
cx_attr_nonnull
cx_attr_nodiscard
cx_attr_access_r(
1)
CxPropertiesSource cxPropertiesFileSource(
FILE *file,
size_t chunk_size);
cx_attr_nonnull
CxPropertiesStatus cxPropertiesLoad(
CxProperties *prop,
CxPropertiesSink sink,
CxPropertiesSource source
);
#ifdef __cplusplus
}
#endif
#endif