Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ruby includes work with current version #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 56 additions & 7 deletions vtparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ void vtparse_init(vtparse_t *parser, vtparse_callback_t cb)
parser->num_params = 0;
parser->ignore_flagged = 0;
parser->cb = cb;
parser->characterBytes = 1;
parser->utf8Character = 0;
}

static void do_action(vtparse_t *parser, vtparse_action_t action, char ch)
static void do_action(vtparse_t *parser, vtparse_action_t action, unsigned int ch)
{
/* Some actions we handle internally (like parsing parameters), others
* we hand to our client for processing */
Expand Down Expand Up @@ -46,7 +48,7 @@ static void do_action(vtparse_t *parser, vtparse_action_t action, char ch)
if(parser->num_intermediate_chars + 1 > MAX_INTERMEDIATE_CHARS)
parser->ignore_flagged = 1;
else
parser->intermediate_chars[parser->num_intermediate_chars++] = ch;
parser->intermediate_chars[parser->num_intermediate_chars++] = (unsigned char)ch;

break;
}
Expand All @@ -72,7 +74,7 @@ static void do_action(vtparse_t *parser, vtparse_action_t action, char ch)

current_param = parser->num_params - 1;
parser->params[current_param] *= 10;
parser->params[current_param] += (ch - '0');
parser->params[current_param] += ch - '0';
}

break;
Expand All @@ -89,7 +91,7 @@ static void do_action(vtparse_t *parser, vtparse_action_t action, char ch)
}
}

static void do_state_change(vtparse_t *parser, state_change_t change, char ch)
static void do_state_change(vtparse_t *parser, state_change_t change, unsigned int ch)
{
/* A state change is an action and/or a new state to transition to. */

Expand Down Expand Up @@ -125,14 +127,61 @@ static void do_state_change(vtparse_t *parser, state_change_t change, char ch)
}
}

void vtparse(vtparse_t *parser, unsigned char *data, int len)
void vtparse(vtparse_t *parser, unsigned char *data, unsigned int len)
{
int i;
for(i = 0; i < len; i++)
{
unsigned char ch = data[i];
state_change_t change = STATE_TABLE[parser->state-1][ch];
do_state_change(parser, change, ch);
if(parser->characterBytes != 1)
{
parser->utf8Character = (parser->utf8Character << 6) | (ch & 0x3F);
parser->characterBytes--;

if(parser->characterBytes == 1)
{
state_change_t change = VTPARSE_ACTION_PRINT;
do_state_change(parser, change, parser->utf8Character);
}
}
else if((ch&(1<<7)) != 0)
{
int bit = 6;
do
{
if((ch&(1<<bit)) == 0)
{
break;
}
bit--;
}while(bit > 1);

parser->utf8Character = 0;
parser->characterBytes = 7-bit;
switch(parser->characterBytes)
{
case 2:
parser->utf8Character = ch & (1 | (1<<1) | (1<<2) | (1<<3) | (1<<4));
break;
case 3:
parser->utf8Character = ch & (1 | (1<<1) | (1<<2) | (1<<3));
break;
case 4:
parser->utf8Character = ch & (1 | (1<<1) | (1<<2));
break;
case 5:
parser->utf8Character = ch & (1 | (1<<1));
break;
case 6:
parser->utf8Character = ch & 1;
break;
}
}
else
{
state_change_t change = STATE_TABLE[parser->state-1][ch];
do_state_change(parser, change, (unsigned int)ch);
}
}
}

6 changes: 4 additions & 2 deletions vtparse.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extern "C" {

struct vtparse;

typedef void (*vtparse_callback_t)(struct vtparse*, vtparse_action_t, unsigned char);
typedef void (*vtparse_callback_t)(struct vtparse*, vtparse_action_t, unsigned int);

typedef struct vtparse {
vtparse_state_t state;
Expand All @@ -32,10 +32,12 @@ typedef struct vtparse {
int params[16];
int num_params;
void* user_data;
int characterBytes;
unsigned int utf8Character;
} vtparse_t;

void vtparse_init(vtparse_t *parser, vtparse_callback_t cb);
void vtparse(vtparse_t *parser, unsigned char *data, int len);
void vtparse(vtparse_t *parser, unsigned char *data, unsigned int len);

#ifdef __cplusplus
}
Expand Down
2 changes: 1 addition & 1 deletion vtparse_check_tables.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

require 'vtparse_tables'
require './vtparse_tables.rb'

#
# check that for every state, there is a transition defined
Expand Down
2 changes: 1 addition & 1 deletion vtparse_gen_c_tables.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

require 'vtparse_tables'
require './vtparse_tables.rb'

class String
def pad(len)
Expand Down
2 changes: 1 addition & 1 deletion vtparse_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <unistd.h>
#include "vtparse.h"

void parser_callback(vtparse_t *parser, vtparse_action_t action, unsigned char ch)
void parser_callback(vtparse_t *parser, vtparse_action_t action, unsigned int ch)
{
int i;

Expand Down