added unistring type, working on parser

This commit is contained in:
Josh Holtrop 2010-04-01 20:12:34 -04:00
parent cbb64f82e5
commit ed03f19a6d
3 changed files with 65 additions and 4 deletions

View File

@ -1,21 +1,26 @@
#include <iostream>
#include <string>
#include <stdio.h>
#include <ctype.h> /* isspace() */
#include "parse-input.h"
using namespace std;
#define SET_ERROR(err, args...) \
sprintf(errstr, err " at line %d, column %d", ##args, lineno, colno)
do { \
error = true; \
sprintf(errstr, err " at line %d, column %d", ##args, lineno, colno); \
} while(0)
void parse_input(refptr< vector<unichar_t> > ucs)
{
enum State { INITIAL, LB, SECTION_NAME, RB };
enum State { INITIAL, SECTION_NAME, RULES };
State state = INITIAL;
int lineno = 1;
int colno = 1;
bool error = false;
char errstr[200];
unistring build_str;
for (int i = 0, sz = ucs->size(); i < sz; i++)
{
@ -34,18 +39,36 @@ void parse_input(refptr< vector<unichar_t> > ucs)
case INITIAL:
if (c == '[')
{
state = LB;
state = SECTION_NAME;
build_str = "";
}
else if (isspace(c))
{
}
else
{
error = true;
SET_ERROR("Unexpected character 0x%x (%c) in input file",
c, c);
}
break;
case SECTION_NAME:
switch (c)
{
case ']':
if (build_str == "rules")
{
state = RULES;
}
else
{
SET_ERROR("Unknown section name");
}
break;
case '\n':
SET_ERROR("Unterminated section header");
break;
}
break;
}
}
}

27
unicode.cc Normal file
View File

@ -0,0 +1,27 @@
#include "unicode.h"
#include <string.h>
unistring & unistring::operator=(const char * ascii_str)
{
chars.clear();
const char * as_ptr = ascii_str;
while (*as_ptr != '\0')
{
chars.push_back(*as_ptr);
}
return *this;
}
bool unistring::operator==(const char * ascii_str)
{
int len = chars.size();
if (len != strlen(ascii_str))
return false;
for (int i = 0; i < len; i++)
{
if (chars[i] != ascii_str[i])
return false;
}
return true;
}

View File

@ -2,7 +2,18 @@
#ifndef UNICODE_H
#include <stdint.h>
#include <vector>
typedef uint32_t unichar_t;
class unistring
{
public:
unistring & operator=(const char * ascii_str);
bool operator==(const char * ascii_str);
protected:
std::vector<unichar_t> chars;
};
#endif