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 <iostream>
#include <string>
#include <stdio.h> #include <stdio.h>
#include <ctype.h> /* isspace() */ #include <ctype.h> /* isspace() */
#include "parse-input.h" #include "parse-input.h"
using namespace std; using namespace std;
#define SET_ERROR(err, args...) \ #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) void parse_input(refptr< vector<unichar_t> > ucs)
{ {
enum State { INITIAL, LB, SECTION_NAME, RB }; enum State { INITIAL, SECTION_NAME, RULES };
State state = INITIAL; State state = INITIAL;
int lineno = 1; int lineno = 1;
int colno = 1; int colno = 1;
bool error = false; bool error = false;
char errstr[200]; char errstr[200];
unistring build_str;
for (int i = 0, sz = ucs->size(); i < sz; i++) for (int i = 0, sz = ucs->size(); i < sz; i++)
{ {
@ -34,18 +39,36 @@ void parse_input(refptr< vector<unichar_t> > ucs)
case INITIAL: case INITIAL:
if (c == '[') if (c == '[')
{ {
state = LB; state = SECTION_NAME;
build_str = "";
} }
else if (isspace(c)) else if (isspace(c))
{ {
} }
else else
{ {
error = true;
SET_ERROR("Unexpected character 0x%x (%c) in input file", SET_ERROR("Unexpected character 0x%x (%c) in input file",
c, c); c, c);
} }
break; 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 #ifndef UNICODE_H
#include <stdint.h> #include <stdint.h>
#include <vector>
typedef uint32_t unichar_t; 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 #endif