1
0
Fork 0
mirror of git://git.psyc.eu/libpsyc synced 2024-08-15 03:19:02 +00:00

refactoring - renamed types

This commit is contained in:
tg(x) 2011-10-31 20:26:47 +01:00
parent f1028663dd
commit f25e768482
23 changed files with 257 additions and 257 deletions

View file

@ -30,7 +30,7 @@ typedef enum
PSYC_MODIFIER_NO_LENGTH = 2,
/// Routing modifier, which implies that it doesn't need length.
PSYC_MODIFIER_ROUTING = 3,
} psycModifierFlag;
} PsycModifierFlag;
/** List flags. */
typedef enum
@ -41,7 +41,7 @@ typedef enum
PSYC_LIST_NEED_LENGTH = 1,
/// List doesn't need length.
PSYC_LIST_NO_LENGTH = 2,
} psycListFlag;
} PsycListFlag;
/** Packet flags. */
typedef enum
@ -52,46 +52,46 @@ typedef enum
PSYC_PACKET_NEED_LENGTH = 1,
/// Packet doesn't need content length.
PSYC_PACKET_NO_LENGTH = 2,
} psycPacketFlag;
} PsycPacketFlag;
/** Structure for a modifier. */
typedef struct
{
char oper;
psycString name;
psycString value;
psycModifierFlag flag;
} psycModifier;
PsycString name;
PsycString value;
PsycModifierFlag flag;
} PsycModifier;
/** Structure for an entity or routing header. */
typedef struct
{
size_t lines;
psycModifier *modifiers;
} psycHeader;
PsycModifier *modifiers;
} PsycHeader;
/** Structure for a list. */
typedef struct
{
size_t num_elems;
psycString *elems;
PsycString *elems;
size_t length;
psycListFlag flag;
} psycList;
PsycListFlag flag;
} PsycList;
/** Intermediate struct for a PSYC packet */
typedef struct
{
psycHeader routing; ///< Routing header.
psycHeader entity; ///< Entity header.
psycString method; ///< Contains the method.
psycString data; ///< Contains the data.
psycString content; ///< Contains the whole content.
PsycHeader routing; ///< Routing header.
PsycHeader entity; ///< Entity header.
PsycString method; ///< Contains the method.
PsycString data; ///< Contains the data.
PsycString content; ///< Contains the whole content.
size_t routingLength; ///< Length of routing part.
size_t contentLength; ///< Length of content part.
size_t length; ///< Total length of packet.
psycPacketFlag flag; ///< Packet flag.
} psycPacket;
PsycPacketFlag flag; ///< Packet flag.
} PsycPacket;
/**
* Return the number of digits a number has in its base 10 representation.
@ -107,9 +107,9 @@ size_t psyc_num_length (size_t n)
* Check if a modifier needs length.
*/
static inline
psycModifierFlag psyc_modifier_length_check (psycModifier *m)
PsycModifierFlag psyc_modifier_length_check (PsycModifier *m)
{
psycModifierFlag flag;
PsycModifierFlag flag;
if (m->value.length > PSYC_MODIFIER_SIZE_THRESHOLD)
flag = PSYC_MODIFIER_NEED_LENGTH;
@ -123,12 +123,12 @@ psycModifierFlag psyc_modifier_length_check (psycModifier *m)
/** Create new modifier */
static inline
psycModifier psyc_modifier_new (char oper,
PsycModifier psyc_modifier_new (char oper,
char *name, size_t namelen,
char *value, size_t valuelen,
psycModifierFlag flag){
PsycModifierFlag flag){
psycModifier m = {oper, {namelen, name}, {valuelen, value}, flag};
PsycModifier m = {oper, {namelen, name}, {valuelen, value}, flag};
if (flag == PSYC_MODIFIER_CHECK_LENGTH) // find out if it needs a length
m.flag = psyc_modifier_length_check(&m);
@ -140,45 +140,45 @@ psycModifier psyc_modifier_new (char oper,
* \internal
* Get the total length of a modifier when rendered.
*/
size_t psyc_modifier_length (psycModifier *m);
size_t psyc_modifier_length (PsycModifier *m);
/**
* \internal
* Check if a list needs length.
*/
psycListFlag psyc_list_length_check (psycList *list);
PsycListFlag psyc_list_length_check (PsycList *list);
/**
* \internal
* Get the total length of a list when rendered.
*/
psycListFlag psyc_list_length (psycList *list);
PsycListFlag psyc_list_length (PsycList *list);
/**
* \internal
* Check if a packet needs length.
*/
psycPacketFlag psyc_packet_length_check (psycPacket *p);
PsycPacketFlag psyc_packet_length_check (PsycPacket *p);
/**
* Calculate and set the rendered length of packet parts and total packet length.
*/
size_t psyc_packet_length_set (psycPacket *p);
size_t psyc_packet_length_set (PsycPacket *p);
/** Create new list. */
psycList psyc_list_new (psycString *elems, size_t num_elems, psycListFlag flag);
PsycList psyc_list_new (PsycString *elems, size_t num_elems, PsycListFlag flag);
/** Create new packet. */
psycPacket psyc_packet_new (psycModifier *routing, size_t routinglen,
psycModifier *entity, size_t entitylen,
PsycPacket psyc_packet_new (PsycModifier *routing, size_t routinglen,
PsycModifier *entity, size_t entitylen,
char *method, size_t methodlen,
char *data, size_t datalen,
psycPacketFlag flag);
PsycPacketFlag flag);
/** Create new packet with raw content. */
psycPacket psyc_packet_new_raw (psycModifier *routing, size_t routinglen,
PsycPacket psyc_packet_new_raw (PsycModifier *routing, size_t routinglen,
char *content, size_t contentlen,
psycPacketFlag flag);
PsycPacketFlag flag);
/** @} */ // end of packet group

View file

@ -24,12 +24,12 @@
* To parse a packet you first have to initialize a state:
*
* @code
* psycParseState state;
* PsycParseState state;
* psyc_parse_state_init(&state, flags);
* @endcode
*
* With the flags parameter you can fine-tune what
* part of the packet should be parsed. @see psycParseFlag
* part of the packet should be parsed. @see PsycParseFlag
*
* Next, you have to tell the parser what it should parse. Assuming the variable
* raw_data points to our packet and raw_len contains the length, you can pass
@ -46,7 +46,7 @@
* declared:
*
* @code
* psycString name, // Name of the variable or method
* PsycString name, // Name of the variable or method
* value; // Value of the variable or body
* char oper; // operator of the variable (if any)
* @endcode
@ -105,7 +105,7 @@
* receive incomplete packets but still want to access the data. This code would
* simply reject incomplete packets as error. A more detailed tutorial for
* incomplete packets will follow. In the mean time, have look at the return
* codes in psycParseRC and their explanations. @see psycParseRC
* codes in PsycParseRC and their explanations. @see PsycParseRC
*/
/** @{ */ // begin of parser group
@ -122,7 +122,7 @@ typedef enum {
/// Parse only the content.
/// Parsing starts at the content and the content must be complete.
PSYC_PARSE_START_AT_CONTENT = 2,
} psycParseFlag;
} PsycParseFlag;
/**
* The return value definitions for the packet parsing function.
@ -188,7 +188,7 @@ typedef enum {
PSYC_PARSE_CONTENT = 10,
/// Finished parsing packet.
PSYC_PARSE_COMPLETE = 11,
} psycParseRC;
} PsycParseRC;
/**
* The return value definitions for the list parsing function.
@ -205,7 +205,7 @@ typedef enum {
PSYC_PARSE_LIST_END = 2,
/// Binary list is incomplete.
PSYC_PARSE_LIST_INCOMPLETE = 3,
} psycParseListRC;
} PsycParseListRC;
/**
* Struct for keeping parser state.
@ -213,18 +213,18 @@ typedef enum {
typedef struct {
size_t cursor; ///< Current position in buffer.
size_t startc; ///< Position where the parsing would be resumed.
psycString buffer; ///< Buffer with data to be parsed.
uint8_t flags; ///< Flags for the parser, see psycParseFlag.
psycPart part; ///< Part of the packet being parsed currently.
PsycString buffer; ///< Buffer with data to be parsed.
uint8_t flags; ///< Flags for the parser, see PsycParseFlag.
PsycPart part; ///< Part of the packet being parsed currently.
size_t routingLength; ///< Length of routing part parsed so far.
size_t contentParsed; ///< Number of bytes parsed from the content so far.
size_t contentLength; ///< Expected length of the content.
psycBool contentLengthFound; ///< Is there a length given for this packet?
PsycBool contentLengthFound; ///< Is there a length given for this packet?
size_t valueParsed; ///< Number of bytes parsed from the value so far.
size_t valueLength; ///< Expected length of the value.
psycBool valueLengthFound; ///< Is there a length given for this modifier?
} psycParseState;
PsycBool valueLengthFound; ///< Is there a length given for this modifier?
} PsycParseState;
/**
* Struct for keeping list parser state.
@ -232,24 +232,24 @@ typedef struct {
typedef struct {
size_t cursor; ///< Current position in buffer.
size_t startc; ///< Line start position.
psycString buffer; ///< Buffer with data to be parsed.
psycListType type; ///< List type.
PsycString buffer; ///< Buffer with data to be parsed.
PsycListType type; ///< List type.
size_t elemParsed; ///< Number of bytes parsed from the elem so far.
size_t elemLength; ///< Expected length of the elem.
} psycParseListState;
} PsycParseListState;
/**
* Initializes the state struct.
*
* @param state Pointer to the state struct that should be initialized.
* @param flags Flags to be set for the parser, see psycParseFlag.
* @see psycParseFlag
* @param flags Flags to be set for the parser, see PsycParseFlag.
* @see PsycParseFlag
*/
static inline
void psyc_parse_state_init (psycParseState *state, uint8_t flags)
void psyc_parse_state_init (PsycParseState *state, uint8_t flags)
{
memset(state, 0, sizeof(psycParseState));
memset(state, 0, sizeof(PsycParseState));
state->flags = flags;
if (flags & PSYC_PARSE_START_AT_CONTENT)
@ -265,12 +265,12 @@ void psyc_parse_state_init (psycParseState *state, uint8_t flags)
* @param state Pointer to the initialized state of the parser
* @param buffer pointer to the data that should be parsed
* @param length length of the data in bytes
* @see psycString
* @see PsycString
*/
static inline
void psyc_parse_buffer_set (psycParseState *state, char *buffer, size_t length)
void psyc_parse_buffer_set (PsycParseState *state, char *buffer, size_t length)
{
state->buffer = (psycString) {length, buffer};
state->buffer = (PsycString) {length, buffer};
state->cursor = 0;
if (state->flags & PSYC_PARSE_START_AT_CONTENT) {
@ -285,65 +285,65 @@ void psyc_parse_buffer_set (psycParseState *state, char *buffer, size_t length)
* @param state Pointer to the list state struct that should be initialized.
*/
static inline
void psyc_parse_list_state_init (psycParseListState *state)
void psyc_parse_list_state_init (PsycParseListState *state)
{
memset(state, 0, sizeof(psycParseListState));
memset(state, 0, sizeof(PsycParseListState));
}
/**
* Sets a new buffer in the list parser state struct with data to be parsed.
*/
static inline
void psyc_parse_list_buffer_set (psycParseListState *state, char *buffer, size_t length)
void psyc_parse_list_buffer_set (PsycParseListState *state, char *buffer, size_t length)
{
state->buffer = (psycString) {length, buffer};
state->buffer = (PsycString) {length, buffer};
state->cursor = 0;
}
static inline
size_t psyc_parse_content_length (psycParseState *state)
size_t psyc_parse_content_length (PsycParseState *state)
{
return state->contentLength;
}
static inline
psycBool psyc_parse_content_length_found (psycParseState *state)
PsycBool psyc_parse_content_length_found (PsycParseState *state)
{
return state->contentLengthFound;
}
static inline
size_t psyc_parse_value_length (psycParseState *state)
size_t psyc_parse_value_length (PsycParseState *state)
{
return state->valueLength;
}
static inline
psycBool psyc_parse_value_length_found (psycParseState *state)
PsycBool psyc_parse_value_length_found (PsycParseState *state)
{
return state->valueLengthFound;
}
static inline
size_t psyc_parse_cursor (psycParseState *state)
size_t psyc_parse_cursor (PsycParseState *state)
{
return state->cursor;
}
static inline
size_t psyc_parse_buffer_length (psycParseState *state)
size_t psyc_parse_buffer_length (PsycParseState *state)
{
return state->buffer.length;
}
static inline
size_t psyc_parse_remaining_length (psycParseState *state)
size_t psyc_parse_remaining_length (PsycParseState *state)
{
return state->buffer.length - state->cursor;
}
static inline
const char * psyc_parse_remaining_buffer (psycParseState *state)
const char * psyc_parse_remaining_buffer (PsycParseState *state)
{
return state->buffer.ptr + state->cursor;
}
@ -353,11 +353,11 @@ const char * psyc_parse_remaining_buffer (psycParseState *state)
*
* This function parses a full or partial PSYC packet while keeping parsing
* state in a state variable that you have to pass in every time, and returns
* whenever a modifier or the body is found. See psycParseRC for the possible
* whenever a modifier or the body is found. See PsycParseRC for the possible
* return codes. When it returns oper, name & value will point to the respective
* parts of the buffer, no memory allocation is done.
*
* @param state An initialized psycParseState.
* @param state An initialized PsycParseState.
* @param oper In case of a modifier it will be set to the operator.
* @param name In case of a modifier it will point to the name,
* in case of the body it will point to the method.
@ -367,8 +367,8 @@ const char * psyc_parse_remaining_buffer (psycParseState *state)
#ifdef __INLINE_PSYC_PARSE
static inline
#endif
psycParseRC psyc_parse (psycParseState *state, char *oper,
psycString *name, psycString *value);
PsycParseRC psyc_parse (PsycParseState *state, char *oper,
PsycString *name, PsycString *value);
/**
* List parser.
@ -378,16 +378,16 @@ psycParseRC psyc_parse (psycParseState *state, char *oper,
* every time. When it returns elem will point to the next element in value, no
* memory allocation is done.
*
* @param state An initialized psycParseListState.
* @param state An initialized PsycParseListState.
* @param elem It will point to the next element in the list.
*/
#ifdef __INLINE_PSYC_PARSE
static inline
#endif
psycParseListRC psyc_parse_list (psycParseListState *state, psycString *elem);
PsycParseListRC psyc_parse_list (PsycParseListState *state, PsycString *elem);
static inline
psycBool psyc_parse_number (const char *value, size_t len, ssize_t *n)
PsycBool psyc_parse_number (const char *value, size_t len, ssize_t *n)
{
size_t c = 0;
uint8_t neg = 0;
@ -412,13 +412,13 @@ psycBool psyc_parse_number (const char *value, size_t len, ssize_t *n)
}
static inline
psycBool psyc_parse_time (const char *value, size_t len, time_t *t)
PsycBool psyc_parse_time (const char *value, size_t len, time_t *t)
{
return psyc_parse_number(value, len, t);
}
static inline
psycBool psyc_parse_date (const char *value, size_t len, time_t *t)
PsycBool psyc_parse_date (const char *value, size_t len, time_t *t)
{
if (psyc_parse_number(value, len, t)) {
*t += PSYC_EPOCH;

View file

@ -29,7 +29,7 @@ typedef enum
PSYC_RENDER_ERROR = -1,
/// Packet is rendered successfully in the buffer.
PSYC_RENDER_SUCCESS = 0,
} psycRenderRC;
} PsycRenderRC;
/**
* Return codes for psyc_render_list.
@ -40,7 +40,7 @@ typedef enum
PSYC_RENDER_LIST_ERROR = -1,
/// List is rendered successfully in the buffer.
PSYC_RENDER_LIST_SUCCESS = 0,
} psycRenderListRC;
} PsycRenderListRC;
/**
* Render a PSYC packet into a buffer.
@ -59,7 +59,7 @@ typedef enum
#ifdef __INLINE_PSYC_RENDER
static inline
#endif
psycRenderRC psyc_render (psycPacket *packet, char *buffer, size_t buflen);
PsycRenderRC psyc_render (PsycPacket *packet, char *buffer, size_t buflen);
/**
* Render a PSYC list into a buffer.
@ -67,7 +67,7 @@ psycRenderRC psyc_render (psycPacket *packet, char *buffer, size_t buflen);
#ifdef __INLINE_PSYC_RENDER
static inline
#endif
psycRenderListRC psyc_render_list (psycList *list, char *buffer, size_t buflen);
PsycRenderListRC psyc_render_list (PsycList *list, char *buffer, size_t buflen);
/** @} */ // end of render group

View file

@ -28,10 +28,10 @@ typedef enum
/// Text template parsing & rendering is incomplete, because the buffer was too small.
/// Another call is required to this function after setting a new buffer.
PSYC_TEXT_INCOMPLETE = 1,
} psycTextRC;
} PsycTextRC;
/**
* Return values for psycTextCB.
* Return values for PsycTextCB.
*/
typedef enum
{
@ -39,7 +39,7 @@ typedef enum
PSYC_TEXT_VALUE_NOT_FOUND = -1,
/// Value found, substitute contents of the value variable.
PSYC_TEXT_VALUE_FOUND = 0,
} psycTextValueRC;
} PsycTextValueRC;
/**
* Struct for keeping PSYC text parser state.
@ -48,11 +48,11 @@ typedef struct
{
size_t cursor; ///< current position in the template
size_t written; ///< number of bytes written to buffer
psycString tmpl; ///< input buffer with text template to parse
psycString buffer; ///< output buffer for rendered text
psycString open;
psycString close;
} psycTextState;
PsycString tmpl; ///< input buffer with text template to parse
PsycString buffer; ///< output buffer for rendered text
PsycString open;
PsycString close;
} PsycTextState;
/**
* Callback for psyc_text() that produces a value for a match.
@ -64,7 +64,7 @@ typedef struct
* PSYC_TEXT_VALUE_NOT_FOUND if no match found in which case psyc_text
* leaves the original template text as is.
*/
typedef psycTextValueRC (*psycTextCB)(const char *name, size_t len, psycString *value, void *extra);
typedef PsycTextValueRC (*PsycTextCB)(const char *name, size_t len, PsycString *value, void *extra);
/**
* Initializes the PSYC text state struct.
@ -76,16 +76,16 @@ typedef psycTextValueRC (*psycTextCB)(const char *name, size_t len, psycString *
* @param buflen Length of output buffer.
*/
static inline
void psyc_text_state_init (psycTextState *state,
void psyc_text_state_init (PsycTextState *state,
char *tmpl, size_t tmplen,
char *buffer, size_t buflen)
{
state->cursor = 0;
state->written = 0;
state->tmpl = (psycString) {tmplen, tmpl};
state->buffer = (psycString) {buflen, buffer};
state->open = (psycString) {1, "["};
state->close = (psycString) {1, "]"};
state->tmpl = (PsycString) {tmplen, tmpl};
state->buffer = (PsycString) {buflen, buffer};
state->open = (PsycString) {1, "["};
state->close = (PsycString) {1, "]"};
}
/**
@ -102,7 +102,7 @@ void psyc_text_state_init (psycTextState *state,
* @param closelen Length of closing brace.
*/
static inline
void psyc_text_state_init_custom (psycTextState *state,
void psyc_text_state_init_custom (PsycTextState *state,
char *tmpl, size_t tmplen,
char *buffer, size_t buflen,
char *open, size_t openlen,
@ -110,25 +110,25 @@ void psyc_text_state_init_custom (psycTextState *state,
{
state->cursor = 0;
state->written = 0;
state->tmpl = (psycString) {tmplen, tmpl};
state->buffer = (psycString) {buflen, buffer};
state->open = (psycString) {openlen, open};
state->close = (psycString) {closelen, close};
state->tmpl = (PsycString) {tmplen, tmpl};
state->buffer = (PsycString) {buflen, buffer};
state->open = (PsycString) {openlen, open};
state->close = (PsycString) {closelen, close};
}
/**
* Sets a new output buffer in the PSYC text state struct.
*/
static inline
void psyc_text_buffer_set (psycTextState *state,
void psyc_text_buffer_set (PsycTextState *state,
char *buffer, size_t length)
{
state->buffer = (psycString){length, buffer};
state->buffer = (PsycString){length, buffer};
state->written = 0;
}
static inline
size_t psyc_text_bytes_written (psycTextState *state)
size_t psyc_text_bytes_written (PsycTextState *state)
{
return state->written;
}
@ -148,7 +148,7 @@ size_t psyc_text_bytes_written (psycTextState *state)
*
* @see http://about.psyc.eu/psyctext
**/
psycTextRC psyc_text (psycTextState *state, psycTextCB getValue, void *extra);
PsycTextRC psyc_text (PsycTextState *state, PsycTextCB getValue, void *extra);
/** @} */ // end of text group

View file

@ -11,31 +11,31 @@ typedef enum {
PSYC_SCHEME_IRC = 1,
PSYC_SCHEME_XMPP = 2,
PSYC_SCHEME_SIP = 3,
} psycScheme;
} PsycScheme;
typedef struct {
// essential parts
uint8_t valid;
psycScheme type;
psycString scheme;
psycString user;
psycString pass;
psycString host;
psycString port;
psycString transport;
psycString resource;
psycString query;
psycString channel;
PsycScheme type;
PsycString scheme;
PsycString user;
PsycString pass;
PsycString host;
PsycString port;
PsycString transport;
PsycString resource;
PsycString query;
PsycString channel;
// convenient snippets of the URL
psycString full; // the URL as such
psycString body; // the URL without scheme and '//'
psycString user_host; // mailto and xmpp style
psycString host_port; // just host:port (and transport)
psycString root; // root UNI of peer/server
psycString slashes; // the // if the protocol has them
psycString nick; // whatever works as a nickname
} psycUniform;
PsycString full; // the URL as such
PsycString body; // the URL without scheme and '//'
PsycString user_host; // mailto and xmpp style
PsycString host_port; // just host:port (and transport)
PsycString root; // root UNI of peer/server
PsycString slashes; // the // if the protocol has them
PsycString nick; // whatever works as a nickname
} PsycUniform;
typedef enum {
PSYC_UNIFORM_SCHEME = 0,
@ -48,7 +48,7 @@ typedef enum {
PSYC_UNIFORM_RESOURCE,
PSYC_UNIFORM_QUERY,
PSYC_UNIFORM_CHANNEL,
} psycUniformPart;
} PsycUniformPart;
typedef enum {
PSYC_PARSE_UNIFORM_INVALID_SLASHES = -7,
@ -58,22 +58,22 @@ typedef enum {
PSYC_PARSE_UNIFORM_INVALID_PORT = -3,
PSYC_PARSE_UNIFORM_INVALID_HOST = -2,
PSYC_PARSE_UNIFORM_INVALID_SCHEME = -1,
} psycParseUniformRC;
} PsycParseUniformRC;
typedef enum {
PSYC_TRANSPORT_TCP = 'c',
PSYC_TRANSPORT_UDP = 'd',
PSYC_TRANSPORT_TLS = 's',
PSYC_TRANSPORT_GNUNET = 'g',
} psycTransport;
} PsycTransport;
typedef enum {
PSYC_ENTITY_PERSON = '~',
PSYC_ENTITY_PLACE = '@',
PSYC_ENTITY_SERVICE = '$',
} psycEntityType;
} PsycEntityType;
int psyc_uniform_parse (psycUniform *uni, char *str, size_t length);
int psyc_uniform_parse (PsycUniform *uni, char *str, size_t length);
#define PSYC_UNIFORM_H
#endif

View file

@ -5,10 +5,10 @@
*/
/// Routing variables in alphabetical order.
extern const psycString psyc_routing_vars[];
extern const PsycString psyc_routing_vars[];
// Variable types in alphabetical order.
extern const psycMatchVar psyc_var_types[];
extern const PsycMatchVar psyc_var_types[];
extern const size_t psyc_routing_vars_num;
extern const size_t psyc_var_types_num;
@ -16,18 +16,18 @@ extern const size_t psyc_var_types_num;
/**
* Is this a routing variable name?
*/
psycBool psyc_var_is_routing (const char *name, size_t len);
PsycBool psyc_var_is_routing (const char *name, size_t len);
/**
* Get the type of variable name.
*/
psycType psyc_var_type (const char *name, size_t len);
PsycType psyc_var_type (const char *name, size_t len);
/**
* Is this a list variable name?
*/
static inline
psycBool psyc_var_is_list (const char *name, size_t len)
PsycBool psyc_var_is_list (const char *name, size_t len)
{
return len < 5 || memcmp(name, "_list", 5) != 0 ||
(len > 5 && name[5] != '_') ? PSYC_FALSE : PSYC_TRUE;