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

D-binding: more work on the rendering, removed unneeded functions, changed various other things

This commit is contained in:
Marenz 2011-06-12 18:06:00 +02:00
parent af06387ec9
commit cd3533ae5e
5 changed files with 141 additions and 109 deletions

View file

@ -11,11 +11,15 @@ const EPOCH = 1440444041; // 2015-08-24 21:20:41 CET (Monday)
extern (C): extern (C):
enum Bool /+enum Bool
{ {
FALSE = 0, FALSE = 0,
TRUE = 1, TRUE = 1,
} }+/
alias bool Bool;
static const FALSE = false;
static const TRUE = true;
/** /**
* PSYC packet parts. * PSYC packet parts.
@ -73,7 +77,7 @@ struct String
ubyte *ptr; ubyte *ptr;
}+/ }+/
alias char[] String; alias ubyte[] String;
struct MatchVar struct MatchVar
{ {
@ -93,9 +97,12 @@ extern (C) MatchVar varTypes[];
/** /**
* Get the type of variable name. * Get the type of variable name.
*/ */
Bool psyc_isRoutingVar(char *name, size_t len); Bool psyc_isRoutingVar (char[]* name);
alias psyc_isRoutingVar isRoutingVar; bool isRoutingVar (char[] name)
{
return psyc_isRoutingVar(&name);
}
/** /**
* Get the type of variable name. * Get the type of variable name.

View file

@ -6,8 +6,9 @@ module psyc.packet;
import psyc.common; import psyc.common;
import psyc.syntax; import psyc.syntax;
import psyc.render;
import tango.stdc.string : memchr;
extern (C): extern (C):
/** Modifier flags. */ /** Modifier flags. */
@ -49,11 +50,17 @@ enum PacketFlag
struct Modifier struct Modifier
{ {
char operator; char operator;
String name; char[] name;
String value; String value;
ModifierFlag flag; ModifierFlag flag;
static Modifier opCall ( char op, char[] nam, char[] val ) static Modifier opCall ( char op, char[] nam, char[] val,
ModifierFlag flg = ModifierFlag.CHECK_LENGTH )
{
return opCall(op, nam, cast(ubyte[])val, flg);
}
static Modifier opCall ( char op, char[] nam, ubyte[] val,
ModifierFlag flg = ModifierFlag.CHECK_LENGTH )
{ {
Modifier v; Modifier v;
@ -62,11 +69,18 @@ struct Modifier
operator = op; operator = op;
name = nam; name = nam;
value = val; value = val;
flag = (flg == ModifierFlag.CHECK_LENGTH) ?
checkLength(val) : flg;
} }
return v; return v;
} }
char[] valuestr ( )
{
return cast(char[]) value;
}
bool opEquals ( ref Modifier n ) bool opEquals ( ref Modifier n )
{ {
return operator == n.operator && return operator == n.operator &&
@ -79,17 +93,29 @@ struct Modifier
auto v = M(operator, name.dup, value.dup); auto v = M(operator, name.dup, value.dup);
return v; return v;
} }
size_t length ( )
{
return psyc_getModifierLength (this);
}
private ModifierFlag checkLength ( ubyte[] value )
{
ModifierFlag flag;
if (value.length > PSYC_MODIFIER_SIZE_THRESHOLD)
flag = ModifierFlag.NEED_LENGTH;
else if (memchr(value.ptr, cast(int)'\n', value.length))
flag = ModifierFlag.NEED_LENGTH;
else
flag = ModifierFlag.NO_LENGTH;
return flag;
}
}; };
alias Modifier M; alias Modifier M;
/** Structure for an entity or routing header. */
struct Header
{
size_t lines;
Modifier *modifiers;
} ;
/** Structure for a list. */ /** Structure for a list. */
struct List struct List
{ {
@ -102,66 +128,68 @@ struct List
/** intermediate struct for a PSYC packet */ /** intermediate struct for a PSYC packet */
struct Packet struct Packet
{ {
Header routing; ///< Routing header. Modifier[] routing; ///< Routing header.
Header entity; ///< Entity header. Modifier[] entity; ///< Entity header.
String method; ///< Contains the method. String method; ///< Contains the method.
String data; ///< Contains the data. String data; ///< Contains the data.
String content; ///< Contains the whole content. String content; ///< Contains the whole content.
size_t routingLength; ///< Length of routing part. size_t routingLength; ///< Length of routing part.
size_t contentLength; ///< Length of content part. size_t contentLength; ///< Length of content part.
size_t length; ///< Total length of packet. size_t _length; ///< Total length of packet.
PacketFlag flag; ///< Packet flag. PacketFlag flag; ///< Packet flag.
static Packet opCall (Modifier[] routing, Modifier[] entity,
char[] method, ubyte[] data,
PacketFlag flag = PacketFlag.CHECK_LENGTH)
{
return psyc_newPacket (&routing, &entity, cast(ubyte[]*)&method, &data, flag);
}
static Packet opCall (Modifier[] routing, ubyte[] content,
PacketFlag flag = PacketFlag.CHECK_LENGTH)
{
return psyc_newRawPacket (&routing, &content, flag);
}
size_t length ( )
{
psyc_setPacketLength(this);
return this._length;
}
ubyte[] render ( ubyte[] buffer )
{
psyc_setPacketLength(this);
with (RenderRC)
switch (psyc_render(this, buffer.ptr, buffer.length))
{
case ERROR_METHOD_MISSING:
throw new Exception("Method missing");
break;
case ERROR_MODIFIER_NAME_MISSING:
throw new Exception("Modifier name missing");
break;
case ERROR:
throw new Exception("Buffer to small");
break;
case SUCCESS:
return buffer[0 .. this._length];
default:
throw new Exception("Unknown Return Code");
}
}
}; };
/**
* \internal
* Check if a modifier needs length.
*/
ModifierFlag psyc_checkModifierLength (Modifier *m)
{
ModifierFlag flag;
if (m->value.length > PSYC_MODIFIER_SIZE_THRESHOLD)
flag = ModifierFlag.NEED_LENGTH;
else if (memchr(m->value.ptr, (int)'\n', m->value.length))
flag = ModifierFlag.NEED_LENGTH;
else
flag = ModifierFlag.NO_LENGTH;
return flag;
}
/** Create new modifier. */
Modifier psyc_newModifier (char oper, String *name, String *value,
ModifierFlag flag)
{
Modifier m = {oper, *name, *value, flag};
if (flag == ModifierFlag.CHECK_LENGTH) // find out if it needs a length
m.flag = psyc_checkModifierLength(&m);
return m;
}
/** Create new modifier */
Modifier psyc_newModifier2 (char oper,
char *name, size_t namelen,
char *value, size_t valuelen,
ModifierFlag flag)
{
String n = {namelen, name};
String v = {valuelen, value};
return psyc_newModifier(oper, &n, &v, flag);
}
/** /**
* \internal * \internal
* Get the total length of a modifier when rendered.
*/ */
size_t psyc_getModifierLength (Modifier *m); private size_t psyc_getModifierLength (Modifier *m);
/** /**
* \internal * \internal
@ -184,17 +212,20 @@ PacketFlag psyc_checkPacketLength (Packet *p);
/** /**
* Calculate and set the rendered length of packet parts and total packet length. * Calculate and set the rendered length of packet parts and total packet length.
*/ */
size_t psyc_setPacketLength (Packet *p); private size_t psyc_setPacketLength (Packet *p);
/** Create new list. */ /** Create new list. */
List psyc_newList (String *elems, size_t num_elems, ListFlag flag); List psyc_newList (String *elems, size_t num_elems, ListFlag flag);
/** Create new packet. */ /** Create new packet. */
Packet psyc_newPacket (Header *routing, private Packet psyc_newPacket (Modifier[]* routing,
Header *entity, Modifier[]* entity,
String *method, String *data, String *method, String *data,
PacketFlag flag); PacketFlag flag);
/** Create new packet. */ /** Create new packet. */
Packet psyc_newPacket2 (Modifier *routing, size_t routinglen, Packet psyc_newPacket2 (Modifier *routing, size_t routinglen,
Modifier *entity, size_t entitylen, Modifier *entity, size_t entitylen,
@ -203,7 +234,7 @@ Packet psyc_newPacket2 (Modifier *routing, size_t routinglen,
PacketFlag flag); PacketFlag flag);
/** Create new packet with raw content. */ /** Create new packet with raw content. */
Packet psyc_newRawPacket (Header *routing, String *content, Packet psyc_newRawPacket (Modifier[] *routing, ubyte[] *content,
PacketFlag flag); PacketFlag flag);
/** Create new packet with raw content. */ /** Create new packet with raw content. */

View file

@ -141,6 +141,24 @@ struct ParseState
return inst; return inst;
} }
/**
* Parse PSYC packets.
*
* Generalized line-based packet parser.
*
* Params:
* oper = A reference to a character. In case of a variable, it will
* be set to the operator of that variable
* name = A reference to a String. It will point to the name of
* the variable or method and its length will be set accordingly
* value = A reference to a String. It will point to the
* value/body the variable/method and its length will be set accordingly
*/
ParseRC parse ( ref char oper, ref char[] name, ref ubyte[] value )
{
return psyc_parse(this, &oper, cast(String*) &name, cast(String*) &value);
}
/** /**
* Parse PSYC packets. * Parse PSYC packets.
* *
@ -175,7 +193,7 @@ struct ParseState
if (this.flags & ParseFlag.START_AT_CONTENT) if (this.flags & ParseFlag.START_AT_CONTENT)
{ {
this.contentLength = buffer.length; this.contentLength = buffer.length;
this.contentLengthFound = Bool.TRUE; this.contentLengthFound = TRUE;
} }
} }
@ -187,23 +205,11 @@ struct ParseState
* *
* See_Also: String * See_Also: String
*/ */
void setParseBuffer ( ubyte[] buffer ) void setParseBuffer ( char[] buffer )
{ {
this.setParseBuffer(*(cast(String*) &buffer)); this.setParseBuffer(*(cast(String*) &buffer));
} }
/**
* Sets a new buffer in the parser state struct with data to be parsed.
*
* Params:
* buffer = pointer to the buffer that should be parsed now
* length = length of the buffer
*/
void setParseBuffer ( ubyte* buffer, size_t length )
{
this.setParseBuffer(String(length, cast(ubyte*)buffer));
}
size_t getContentLength ( ) size_t getContentLength ( )
{ {
return this.contentLength; return this.contentLength;
@ -211,7 +217,7 @@ struct ParseState
bool isContentLengthFound ( ) bool isContentLengthFound ( )
{ {
return this.contentLengthFound == Bool.TRUE; return this.contentLengthFound == TRUE;
} }
size_t getValueLength ( ) size_t getValueLength ( )
@ -221,7 +227,7 @@ struct ParseState
bool isValueLengthFound ( ) bool isValueLengthFound ( )
{ {
return this.valueLengthFound == Bool.TRUE; return this.valueLengthFound == true;
} }
size_t getCursor ( ) size_t getCursor ( )
@ -241,13 +247,13 @@ struct ParseState
ubyte* getRemainingBuffer ( ) ubyte* getRemainingBuffer ( )
{ {
return this.buffer.ptr + this.cursor; return cast(ubyte*)this.buffer.ptr + this.cursor;
} }
void getRemainingBuffer ( ref ubyte[] buf ) void getRemainingBuffer ( ref ubyte[] buf )
{ {
buf = this.buffer.ptr[cursor .. cursor + getRemainingLength()]; buf = cast(ubyte[])this.buffer.ptr[cursor .. cursor + getRemainingLength()];
} }
} }
@ -283,22 +289,10 @@ struct ParseListState
* Params: * Params:
* buffer = the buffer to be parsed * buffer = the buffer to be parsed
*/ */
void setBuffer ( ubyte[] buffer ) void setBuffer ( char[] buffer )
{ {
this.setBuffer(*(cast(String*) &buffer)); this.setBuffer(*(cast(String*) &buffer));
} }
/**
* Sets a new buffer with data to be parsed
*
* Params:
* buffer = pointer to the buffer to be parsed
* length = size of the buffer
*/
void setBuffer ( ubyte* buffer, size_t length )
{
this.setBuffer(String(length, buffer));
}
} }
private: private:

View file

@ -51,10 +51,10 @@ enum RenderListRC
* @see psyc_newRawPacket2 * @see psyc_newRawPacket2
* @see psyc_setPacketLength * @see psyc_setPacketLength
*/ */
RenderRC psyc_render (Packet *packet, char *buffer, size_t buflen); RenderRC psyc_render (Packet *packet, ubyte *buffer, size_t buflen);
/** /**
* Render a PSYC list into a buffer. * Render a PSYC list into a buffer.
*/ */
RenderListRC psyc_renderList (List *list, char *buffer, size_t buflen); RenderListRC psyc_renderList (List *list, ubyte *buffer, size_t buflen);

View file

@ -3,7 +3,7 @@ module psyc.syntax;
const PSYC_LIST_SIZE_LIMIT = 404; const PSYC_LIST_SIZE_LIMIT = 404;
/* beyond this a content length must be provided */ /* beyond this a content length must be provided */
const = PSYC_CONTENT_SIZE_THRESHOLD = 444; const PSYC_CONTENT_SIZE_THRESHOLD = 444;
/* beyond this a modifier value length must be provided */ /* beyond this a modifier value length must be provided */
const PSYC_MODIFIER_SIZE_THRESHOLD = 404; const PSYC_MODIFIER_SIZE_THRESHOLD = 404;