#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;
char continuation;
};
typedef struct cx_properties_config_s CxPropertiesConfig;
CX_EXPORT 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;
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;
typedef int(*cx_properties_read_func)(
CxProperties *prop,
CxPropertiesSource *src,
cxstring *target
);
typedef int(*cx_properties_read_init_func)(
CxProperties *prop,
CxPropertiesSource *src
);
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
CX_EXPORT void cxPropertiesInit(CxProperties *prop, CxPropertiesConfig config);
cx_attr_nonnull
CX_EXPORT void cxPropertiesDestroy(CxProperties *prop);
cx_attr_nonnull
CX_EXPORT void cxPropertiesReset(CxProperties *prop);
#define cxPropertiesInitDefault(prop) \
cxPropertiesInit(prop, cx_properties_config_default)
cx_attr_nonnull cx_attr_access_r(
2,
3)
CX_EXPORT int cxPropertiesFilln(CxProperties *prop,
const char *buf,
size_t len);
cx_attr_nonnull
CX_INLINE int cx_properties_fill(CxProperties *prop, cxstring str) {
return cxPropertiesFilln(prop, str.ptr, str.length);
}
#define cxPropertiesFill(prop, str) cx_properties_fill(prop, cx_strcast(str))
cx_attr_nonnull
CX_EXPORT void cxPropertiesUseStack(CxProperties *prop,
char *buf,
size_t capacity);
cx_attr_nonnull cx_attr_nodiscard
CX_EXPORT CxPropertiesStatus cxPropertiesNext(CxProperties *prop, cxstring *key, cxstring *value);
cx_attr_nonnull cx_attr_nodiscard
CX_EXPORT CxPropertiesSink cxPropertiesMapSink(CxMap *map);
cx_attr_nodiscard
CX_EXPORT CxPropertiesSource cxPropertiesStringSource(cxstring str);
cx_attr_nonnull cx_attr_nodiscard cx_attr_access_r(
1,
2)
CX_EXPORT CxPropertiesSource cxPropertiesCstrnSource(
const char *str,
size_t len);
cx_attr_nonnull cx_attr_nodiscard cx_attr_cstr_arg(
1)
CX_EXPORT CxPropertiesSource cxPropertiesCstrSource(
const char *str);
cx_attr_nonnull cx_attr_nodiscard cx_attr_access_r(
1)
CX_EXPORT CxPropertiesSource cxPropertiesFileSource(
FILE *file,
size_t chunk_size);
cx_attr_nonnull
CX_EXPORT CxPropertiesStatus cxPropertiesLoad(CxProperties *prop,
CxPropertiesSink sink, CxPropertiesSource source);
#ifdef __cplusplus
}
#endif
#endif