Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
cib committed Nov 17, 2012
0 parents commit 2db28b5
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
81 changes: 81 additions & 0 deletions parser.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <stdlib.h>
#include <string.h>

typedef struct parser_
{
const char* buffer;
int pos;
int line;
} parser;

/** Create a new parser with the given string to parse. **/
parser* create_parser(const char* buffer)
{
parser* rval = malloc(sizeof(parser));
rval->buffer = buffer;
rval->pos = 0;
rval->line = 0;
};

/** Returns 1 if we're at the end of the buffer. **/
int parser_at_end(parser* p)
{
return p->pos >= strlen(p->buffer);
};

/** Returns a new string with the next amount chars in the buffer. **/
const char* parser_get_next(parser* p, int amount)
{
if(p->pos + amount > strlen(p->buffer))
{
return NULL;
}

char* rval = malloc(amount+1);
strncpy(rval, p->buffer+p->pos, amount);
return rval;
};

/** Returns the next character in the buffer. **/
char parser_get_char(parser* p)
{
if(p->pos < strlen(p->buffer))
{
return p->buffer[p->pos];
}
return 0;
};

/** Checks if the given word is ahead in the buffer.
Return 1 if it is, 0 otherwise.
**/
int parser_is_next(parser* p, const char* word)
{
const char* compare = parser_get_next(p, strlen(word));

return strcmp(word, compare) == 0;
};

/** Skips the next amount chars in the buffer. Returns the skipped string. **/
const char* parser_skip_amount(parser* p, int amount)
{
int start = p->pos;
p->pos += amount;
if(p->pos > strlen(p->buffer)) p->pos = strlen(p->buffer);

int length = p->pos - start + 1;
char* rval = malloc(length);
strncpy(rval, p->buffer+start, length);
return rval;
};

/** Checks if the word is in the buffer, and if so, skips it. Returns 1 if word matched. **/
int parser_skip(parser* p, const char* word)
{
if(parser_is_next(p, word))
{
parser_skip_amount(p, strlen(word));
return 1;
}
else return 0;
};
35 changes: 35 additions & 0 deletions parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <stdlib.h>
#include <string.h>

typedef struct parser_
{
const char* buffer;
int pos;
int line;
} parser;

/** Create a new parser with the given string to parse. **/
parser* create_parser(const char* buffer);

/** Returns 1 if we're at the end of the buffer. **/
int parser_at_end(parser* p);

/** Returns a new string with the next amount chars in the buffer. **/
const char* parser_get_next(parser* p, int amount);

/** Returns the next character in the buffer. **/
char parser_get_char(parser* p);

/** Checks if the given word is ahead in the buffer.
Return 1 if it is, 0 otherwise.
**/
int parser_is_next(parser* p, const char* word);

/** Skips the next amount chars in the buffer. Returns the skipped string. **/
const char* parser_skip_amount(parser* p, int amount);

/** Checks if the word is in the buffer, and if so, skips it. Returns 1 if word matched. **/
int parser_skip(parser* p, const char* word);

/** Reads an integer and writes it into the pointer. Returns 1 on success, 0 on failure. **/
int parser_read_integer(parser* p, int* i);

0 comments on commit 2db28b5

Please sign in to comment.