summaryrefslogtreecommitdiff
path: root/server/src/templateparser.cc
diff options
context:
space:
mode:
authordeva <deva>2008-05-20 10:17:14 +0000
committerdeva <deva>2008-05-20 10:17:14 +0000
commit91d56787bec83a3a7c2ad6afd84d2b82c5d869b9 (patch)
tree32cfe22c3024de280c7516b561b8bfd98b266ac4 /server/src/templateparser.cc
parent7d81d456702e3c5b0caead6e44f2c2f236d2fa41 (diff)
Removed obsolete macro.h and macro_parser.cc,h
Diffstat (limited to 'server/src/templateparser.cc')
-rw-r--r--server/src/templateparser.cc91
1 files changed, 76 insertions, 15 deletions
diff --git a/server/src/templateparser.cc b/server/src/templateparser.cc
index 956a0e8..652a870 100644
--- a/server/src/templateparser.cc
+++ b/server/src/templateparser.cc
@@ -38,6 +38,13 @@
// For vprintf and friends
#include <stdarg.h>
+#ifndef XML
+// For XML
+#include <config.h>
+#endif/*XML*/
+
+#include <errno.h>
+
void TemplateParser::error(char* fmt, ...)
{
// TODO: Throw exception here.
@@ -52,24 +59,37 @@ void TemplateParser::error(char* fmt, ...)
fprintf(stderr, "\n");
}
-TemplateParser::TemplateParser(std::string templatefile)
+TemplateParser::TemplateParser(std::string course)
{
state = UNDEFINED;
t = NULL;
current_macro = NULL;
current_map = NULL;
- fd = open(templatefile.c_str(), O_RDONLY);
- if(fd == -1) error("Could not open file %s", templatefile.c_str());
+
+ std::string file = XML"/";
+ file.append(course);
+ file.append(".xml");
+
+ printf("Using template file: %s\n", file.c_str());
+
+ fd = open(file.c_str(), O_RDONLY);
+ if(fd == -1) error("Could not open file %s", file.c_str());
}
TemplateParser::~TemplateParser()
{
if(fd != -1) close(fd);
+ if(t) delete t;
}
void TemplateParser::characterData(std::string &data)
{
- if(state == MAP) current_macro->maps.back().attributes["lua"].append(data);
+ if(state == MAP) {
+ // assert(current_macro->maps.size()); // No maps present!
+ // current_macro->maps.back().attributes["lua"].append(data);
+ assert(current_map); // No map present!
+ current_map->attributes["lua"].append(data);
+ }
}
void TemplateParser::startTag(std::string name, std::map< std::string, std::string> attributes)
@@ -107,7 +127,7 @@ void TemplateParser::startTag(std::string name, std::map< std::string, std::stri
assert(t); // A Template has not yet been allocated, cannot create macro!
Macro m;
- m.attributes = attributes;
+ // m.attributes = attributes;
t->course.macroes.push_back(m);
current_macro = &(t->course.macroes.back());
@@ -158,19 +178,27 @@ void TemplateParser::startTag(std::string name, std::map< std::string, std::stri
Map m;
m.attributes = attributes;
current_macro->maps.push_back(m);
+ current_map = &(current_macro->maps.back());
return;
}
// Enable widget parsing
if(name == "window") {
+
if(state != MACRO) error("window found outside macro.");
state = WINDOW;
assert(current_macro); // No macro is currently available, cannot create window!
current_macro->window.attributes = attributes;
- widgetstack.push_back(&(current_macro->window));
+ current_macro->window.attributes["type"] = name;
+
+ Widget *current = &(current_macro->window);
+
+ printf("%p %p\n", current); fflush(stdout);
+
+ widgetstack.push_back(current);
return;
}
@@ -183,8 +211,22 @@ void TemplateParser::startTag(std::string name, std::map< std::string, std::stri
Widget w;
w.attributes = attributes;
- widgetstack.back()->widgets.push_back(w);
- widgetstack.push_back(&(widgetstack.back()->widgets.back()));
+ w.attributes["type"] = name;
+
+ printf("1 %d\n", widgetstack.size() ); fflush(stdout);
+
+ Widget *parent = widgetstack.back();
+
+ printf("%d\n", parent->widgets.size()); fflush(stdout);
+
+ parent->widgets.push_back(w);
+
+ printf("2\n"); fflush(stdout);
+
+ Widget *current = &(parent->widgets.back());
+ widgetstack.push_back(current);
+
+ printf("3\n"); fflush(stdout);
return;
}
@@ -202,24 +244,43 @@ void TemplateParser::endTag(std::string name)
{
if(name == "template") state = UNDEFINED;
if(name == "course") state = TEMPLATE;
- if(name == "macro") state = COURSE;
+ if(name == "macro") {
+ current_macro = NULL;
+ state = COURSE;
+ }
if(name == "queries") state = MACRO;
if(name == "query") state = QUERIES;
if(name == "maps") state = MACRO;
- if(name == "map") state = MAPS;
+ if(name == "map") {
+ current_map = NULL;
+ state = MAPS;
+ }
if(name == "window") state = MACRO;
if(state == WINDOW) {
- assert(widgetstack.size()); // Widget stack is empty, cannot pop!
- widgetstack.pop_back();
- }
+ printf("4\n"); fflush(stdout);
+
+ assert(widgetstack.size()); // Widget stack is empty, cannot pop!
+ widgetstack.pop_back();
+
+ printf("5\n"); fflush(stdout);
+
+ if(widgetstack.size() == 0) state = MACRO;
+
+ printf("6\n"); fflush(stdout);
+ }
}
int TemplateParser::readData(char *data, size_t size)
{
if(fd == -1) return 0;
- return read(fd, data, size);
+ ssize_t r = read(fd, data, size);
+ if(r == -1) {
+ printf("Could not read...%s\n", strerror(errno)); fflush(stdout);
+ return 0;
+ }
+ return r;
}
Template *TemplateParser::getTemplate()
@@ -241,7 +302,7 @@ void print_attributes(std::string prefix,
int main()
{
- TemplateParser parser("../xml/example2.xml");
+ TemplateParser parser("example2");
parser.parse();
Template *t = parser.getTemplate();