diff options
author | deva <deva> | 2009-01-13 10:14:12 +0000 |
---|---|---|
committer | deva <deva> | 2009-01-13 10:14:12 +0000 |
commit | 6a81913f64590c6e49431ce483e00c8fc60573a4 (patch) | |
tree | eae21b8cae2ed5f80febf982df2d2748e7cc6b41 /server | |
parent | bcabb130408b5d52bb73f8404d3b4805047d86ff (diff) |
Removed all 'complex type' code. No need for it anymore...
Diffstat (limited to 'server')
-rw-r--r-- | server/src/Makefile.am | 8 | ||||
-rw-r--r-- | server/src/formattools.cc | 168 | ||||
-rw-r--r-- | server/src/formattools.h | 63 | ||||
-rw-r--r-- | server/src/luaformatmapper.cc | 138 | ||||
-rw-r--r-- | server/src/luaformatmapper.h | 60 | ||||
-rw-r--r-- | server/src/luaformatmapperutils.cc | 230 | ||||
-rw-r--r-- | server/src/luaformatmapperutils.h | 38 | ||||
-rw-r--r-- | server/src/mltokenizer.cc | 196 | ||||
-rw-r--r-- | server/src/mltokenizer.h | 55 |
9 files changed, 0 insertions, 956 deletions
diff --git a/server/src/Makefile.am b/server/src/Makefile.am index 75e72d9..7e49e89 100644 --- a/server/src/Makefile.am +++ b/server/src/Makefile.am @@ -12,17 +12,13 @@ pracrod_SOURCES = \ configuration.cc \ configurationparser.cc \ exception.cc \ - formattools.cc \ queryhandler.cc \ queryparser.cc \ journal_commit.cc \ log.cc \ - luaformatmapper.cc \ - luaformatmapperutils.cc \ luaquerymapper.cc \ luaresume.cc \ macroparser.cc \ - mltokenizer.cc \ resumeparser.cc \ saxparser.cc \ server.cc \ @@ -39,17 +35,13 @@ EXTRA_DIST = \ database.h \ debug.h \ exception.h \ - formattools.h \ queryhandler.h \ queryparser.h \ journal_commit.h \ log.h \ - luaformatmapper.h \ - luaformatmapperutils.h \ luaquerymapper.h \ luaresume.h \ macroparser.h \ - mltokenizer.h \ resumeparser.h \ saxparser.h \ server.h \ diff --git a/server/src/formattools.cc b/server/src/formattools.cc deleted file mode 100644 index 6a58d74..0000000 --- a/server/src/formattools.cc +++ /dev/null @@ -1,168 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/*************************************************************************** - * formattools.cc - * - * Mon Oct 27 09:34:46 CET 2008 - * Copyright 2008 Bent Bisballe Nyeng - * deva@aasimon.org - ****************************************************************************/ - -/* - * This file is part of Pracro. - * - * Pracro is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * Pracro is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Pracro; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. - */ -#include "formattools.h" -#include "mltokenizer.h" -#include "luaformatmapper.h" - -/** - * Replace all ocurrences of c with cc. - */ -static std::string escape_string(std::string str, char c) -{ - std::string out; - for(size_t i = 0; i < str.length(); i++) { - if(str[i] == c) out += str[i]; - out += str[i]; - } - return out; -} - -/** - * Replace all ocurrences of cc with c. - */ -static std::string deescape_string(std::string str, char c) -{ - std::string out; - for(size_t i = 0; i < str.length(); i++) { - if(i < str.length() - 1 && str[i] == c && str[i + 1] == c) { - out += str[i]; - i++; // Skip the next character - } else { - out += str[i]; - } - } - return out; -} - -std::string escape_multilist_string(std::string str) -{ - return escape_string(escape_string(str, '{'), '}'); -} - -std::string escape_resume_string(std::string str) -{ - return escape_string(escape_string(str, '['), ']'); -} - -Fields get_multilist_values(std::string mlvalue) -{ - std::map<std::string, std::string> values; - - std::vector< mltoken_t > tokens = mltokenize(mlvalue); - std::vector< mltoken_t >::iterator i = tokens.begin(); - while(i != tokens.end()) { - if(i->type == MLTT_VALUE) values[i->name] = i->value; - i++; - } - - return values; -} - -std::string render_multilist_string(std::string mlvalue) -{ - std::string output; - - std::vector< mltoken_t > tokens = mltokenize(mlvalue); - std::vector< mltoken_t >::iterator i = tokens.begin(); - while(i != tokens.end()) { - output += i->value; - i++; - } - - return output; -} - -std::string render_resume_string(std::string str, Fields &fields) -{ - std::string out; - - LUAFormatMapper mapper(fields); - - // Replace ${foo|bar} with bar - for(size_t i = 0; i < str.length(); i++) { - if(str[i] == '$') { - if(i < str.length() - 2 && - str[i+1] == '[' && str[i+2] != '[') { - // We are in a key/value - - // Look for end marker - size_t j; - for(j = i + 2; j < str.length(); j++) { - if(str[j] == ']' && str[j + 1] != ']') { - // We have an end marker - break; - } - } - - std::string luaprogram = str.substr(i + 2, j - (i + 2)); - std::string value = mapper.map(luaprogram); - out += escape_resume_string(value); - - i = j; - - } else out += str[i]; - } else { - out += str[i]; - } - } - - return deescape_string(deescape_string(out, '['), ']'); -} - -#ifdef TEST_FORMATTOOLS - -int main() -{ - Fields fields; - fields["dingo"] = "[[meget dyr]]"; - fields["fnuld"] = "Zimbabwe"; - fields["mlstring"] = - "Ladidaa ${myname|bar} ${{dingo|dyt}} ${dims|{{dimmer}}}.\n" - "Ladidaa ${myname|bole} ${{dingo|dyt}} ${dims|[[fillerhejs]]}.\n" - "Ladidaa ${myname|daske} ${{dingo|dyt}} ${dims|buller}.\n"; - - /* - printf("%s\n", deescape_string(deescape_string("[[] []]", '['), ']').c_str()); - - if(escape_multilist_string("${} {{}}") != "${{}} {{{{}}}}") return 1; - if(escape_resume_string("$[] [[]]") != "$[[]] [[[[]]]]") return 1; - - std::string mlstring = "Ladidaa ${myname|bar} ${{dingo|dyt}} ${dims|dulle}."; - printf("{%s}\n", render_multilist_string(mlstring).c_str()); - */ - - std::string resumestring = "Ladidaa \n$[" - "printfmlval(mlstring, ' * ', true, '<p>', 'myname', '</p>', 'PRE', 'dims', 'POST')" - "] dalidaadoo."; - printf("{%s}\n", render_resume_string(resumestring, fields).c_str()); - - // std::string resumestring2 = "Ladidaa \n$[printfmlval(mlstring, ' * ', false, '<p>', 'myname', '</p>', 'PRE', 'dims', 'POST')] dalidaadoo."; - // printf("{%s}\n", render_resume_string(resumestring2, fields).c_str()); - return 0; -} - -#endif/*TEST_FORMATTOOLS*/ diff --git a/server/src/formattools.h b/server/src/formattools.h deleted file mode 100644 index 5bad492..0000000 --- a/server/src/formattools.h +++ /dev/null @@ -1,63 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/*************************************************************************** - * formattools.h - * - * Mon Oct 27 09:34:46 CET 2008 - * Copyright 2008 Bent Bisballe Nyeng - * deva@aasimon.org - ****************************************************************************/ - -/* - * This file is part of Pracro. - * - * Pracro is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * Pracro is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Pracro; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. - */ -#ifndef __PRACRO_FORMATTOOLS_H__ -#define __PRACRO_FORMATTOOLS_H__ - -#include <string> -#include <map> - -// For Fields -#include "transaction.h" - -/** - * Escape all { and } characters. - */ -std::string escape_multilist_string(std::string str); - -/** - * Escape all [ and ] characters. - */ -std::string escape_resume_string(std::string str); - -/** - * Get name/value pair list from multilist result string. - */ -Fields get_multilist_values(std::string str); - -/** - * Render tekst from multilist result string, by inserting the values at the - * $[name|value] placeholders and de-escaping [[ and ]]. - */ -std::string render_multilist_string(std::string str); - -/** - * Render resume tekst, by running the embedded LUA code and replacing the ${...} - * placeholders with the output and de-escaping {{ and }}. - */ -std::string render_resume_string(std::string str, Fields &fields); - -#endif/*__PRACRO_FORMATTOOLS_H__*/ diff --git a/server/src/luaformatmapper.cc b/server/src/luaformatmapper.cc deleted file mode 100644 index 652a34d..0000000 --- a/server/src/luaformatmapper.cc +++ /dev/null @@ -1,138 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/*************************************************************************** - * luaformatmapper.cc - * - * Wed Oct 29 16:02:03 CET 2008 - * Copyright 2008 Bent Bisballe Nyeng - * deva@aasimon.org - ****************************************************************************/ - -/* - * This file is part of Pracro. - * - * Pracro is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * Pracro is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Pracro; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. - */ -#include "luaformatmapper.h" - -#include <sstream> - -#include "exception.h" - -#include "luaformatmapperutils.h" -/* -static std::string loadresultstring(Fields &fields) -{ - std::string s; - - Fields::iterator v = fields.begin(); - while(v != fields.end()) { - lua_pushstring(L, v->second); // Push the pointer to 'this' instance - lua_setglobal(L, v->first); // Assign it to a global lua var. - // s += (*v).first + " = \"" + (*v).second + "\"\n"; - v++; - } - - return s; -} -*/ -LUAFormatMapper::LUAFormatMapper(Fields &fields) -{ - L = luaL_newstate(); - if(L == NULL) { - error("Could not create LUA state."); - return; - } - - luaL_openlibs(L); - - setGlobal(L, "LUAFormatMapper", this); - preload_formatutils(L); - - Fields::iterator v = fields.begin(); - while(v != fields.end()) { - lua_pushstring(L, v->second.c_str()); // Push the pointer to 'this' instance - lua_setglobal(L, v->first.c_str()); // Assign it to a global lua var. - v++; - } - /* - std::string preload = loadresultstring(fields); - - if(luaL_loadbuffer(L, preload.c_str(), preload.size(), "preload")) { - error(lua_tostring(L, lua_gettop(L))); - return; - } - - // Run program (init) - if(lua_pcall(L, 0, LUA_MULTRET, 0)) { - error(lua_tostring(L, lua_gettop(L))); - return; - } - */ - clean_top = lua_gettop(L); -} - -LUAFormatMapper::~LUAFormatMapper() -{ - lua_close(L); -} - -std::string LUAFormatMapper::map(const std::string &mapper) -{ - output = ""; - - if(L == NULL) { - error("LUA state not initialized!"); - return output; - } - - if(mapper == "") { - error("Empty LUA mapper detected in " + mapper); - return output; - } - - // Load the mapper - if(luaL_loadbuffer(L, mapper.c_str(), mapper.size(), "mapper")) { - error(lua_tostring(L, lua_gettop(L)) + std::string(" in ") + mapper); - return output; - } - - // Run the loaded code - if(lua_pcall(L, 0, LUA_MULTRET, 0)) { - error(lua_tostring(L, lua_gettop(L)) + std::string(" in ") + mapper); - return output; - } - - // Check if app messed up the stack. - if(lua_gettop(L) != clean_top) { - error("Wrong number of return values in " + mapper); - lua_pop(L, lua_gettop(L) - clean_top); - return output; - } - - // output = lua_tostring(L, lua_gettop(L)); - // lua_pop(L, 1); - - return output; -} - -void LUAFormatMapper::error(std::string message) -{ - throw Exception("ERROR in LUAFormatMapper: " + message); -} - -void LUAFormatMapper::bufferoutput(std::string value) -{ - output += value; -} diff --git a/server/src/luaformatmapper.h b/server/src/luaformatmapper.h deleted file mode 100644 index 8715eed..0000000 --- a/server/src/luaformatmapper.h +++ /dev/null @@ -1,60 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/*************************************************************************** - * luaformatmapper.h - * - * Wed Oct 29 16:02:03 CET 2008 - * Copyright 2008 Bent Bisballe Nyeng - * deva@aasimon.org - ****************************************************************************/ - -/* - * This file is part of Pracro. - * - * Pracro is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * Pracro is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Pracro; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. - */ -#ifndef __PRACRO_LUAFORMATMAPPER_H__ -#define __PRACRO_LUAFORMATMAPPER_H__ - -#include "transaction.h" - -#include <lua.hpp> -#include <lauxlib.h> - -/** - * The LUAQueryMapper class takes the result of an external data query and - * applies the associated map. - */ -class LUAFormatMapper { -public: - LUAFormatMapper(Fields &fields); - ~LUAFormatMapper(); - - /** - * Applies the mapping program to the result-namespace, and returns the - * resulting value. - */ - std::string map(const std::string &mapper); - - void error(std::string message); - - void bufferoutput(std::string value); - -private: - lua_State *L; - int clean_top; - std::string output; -}; - -#endif/*__PRACRO_LUAFORMATMAPPER_H__*/ diff --git a/server/src/luaformatmapperutils.cc b/server/src/luaformatmapperutils.cc deleted file mode 100644 index b163477..0000000 --- a/server/src/luaformatmapperutils.cc +++ /dev/null @@ -1,230 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/*************************************************************************** - * luaformatmapperutils.cc - * - * Mon Nov 3 08:46:51 CET 2008 - * Copyright 2008 Bent Bisballe Nyeng - * deva@aasimon.org - ****************************************************************************/ - -/* - * This file is part of Pracro. - * - * Pracro is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * Pracro is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Pracro; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. - */ -#include "luaformatmapperutils.h" -#include "luaformatmapper.h" -#include "mltokenizer.h" - -#include <string> -#include <vector> - -std::vector< void * > pointers; - -#define GLOBAL_PREFIX "magic_global_" - -void *getGlobal(lua_State *L, const char *name) -{ - unsigned int top; - unsigned int index; - - std::string var = std::string(GLOBAL_PREFIX) + name; - - lua_getfield(L, LUA_GLOBALSINDEX, var.c_str()); - top = lua_gettop(L); - index = lua_tointeger(L, top); - - return pointers.at(index); -} - -void setGlobal(lua_State *L, const char *name, void *p) -{ - // Put the value of this in the globals - char value_of_this[256]; - - std::string val = std::string(GLOBAL_PREFIX) + name; - - pointers.push_back(p); - unsigned int index = pointers.size() - 1; - - sprintf(value_of_this, "%s = %u\n", val.c_str(), index); - int s = luaL_loadstring(L, value_of_this); - switch(s) { - case 0: //no errors; - break; - case LUA_ERRSYNTAX: //syntax error during pre-compilation; - case LUA_ERRMEM: //memory allocation error. - fprintf(stderr, "Error: %s\n", lua_tostring(L, lua_gettop(L))); - default: - fprintf(stderr, "Unknown return value of luaL_loadstring.\n"); - } - - // Run program (init) - s = lua_pcall(L, 0, LUA_MULTRET, 0); - // Check for errors - switch(s) { - case 0: // Success - break; - case LUA_ERRRUN:// a runtime error. - case LUA_ERRMEM:// memory allocation error. - // For such errors, Lua does not call the error handler function. - case LUA_ERRERR:// error while running the error handler function. - fprintf(stderr, "Error: %s\n", lua_tostring(L, lua_gettop(L))); - break; - default: - fprintf(stderr, "Error: Unknown return value of lua_pcall.\n"); - break; - } -} - -typedef struct { - std::string prefix; - std::string postfix; -} transform_params_t; - -std::map< std::string, transform_params_t > transforms; - -/** - * Args: mlvalue, bullet, usetext, name1, prefix1, postfix1, name2, prefix2, postfix2, ... - * Iterates list items, prepends bullet, and replaces all name/value pairs with - * corresponding prefix/value/postfix. - */ -int printfmlval(lua_State *L) -{ - int n = lua_gettop(L); // number of arguments - if(n < 3 || (n - 3) % 3 != 0) { - char errstr[512]; - sprintf(errstr, "Minimum number of args expected 3, got %d", n); - fprintf(stderr, errstr); - lua_pushstring(L, errstr); - lua_error(L); - return 0; - } - - std::string mlvalue = lua_tostring(L, lua_gettop(L) - (n - 1)); - std::string bullet = lua_tostring(L, lua_gettop(L) - (n - 2)); - bool usetext = lua_toboolean(L, lua_gettop(L) - (n - 3)); - - // printf("[%s], [%s], [%d]\n", mlvalue.c_str(), bullet.c_str(), usetext); fflush(stdout); - - // Read in the (prefix, name, postfix) 3-tuples - int m = 4; - while(m < lua_gettop(L)) { - - transform_params_t p; - p.prefix = lua_tostring(L, lua_gettop(L) - (n - m)); - std::string name = lua_tostring(L, lua_gettop(L) - (n - (m + 1))); - p.postfix = lua_tostring(L, lua_gettop(L) - (n - (m + 2))); - - // printf("[%s], [%s], [%s]\n", p.prefix.c_str(), name.c_str(), p.postfix.c_str()); fflush(stdout); - - transforms[name] = p; - - m += 3; - } - - LUAFormatMapper *lfm = (LUAFormatMapper*)getGlobal(L, "LUAFormatMapper"); - - mltokentype_t lasttype = MLTT_ENDOFITEM; - std::vector< mltoken_t > tokens = mltokenize(mlvalue); - std::vector< mltoken_t >::iterator i = tokens.begin(); - while(i != tokens.end()) { - if(lasttype == MLTT_ENDOFITEM) lfm->bufferoutput(bullet); - - switch(i->type) { - case MLTT_VALUE: - lfm->bufferoutput(transforms[i->name].prefix); - lfm->bufferoutput(i->value); - lfm->bufferoutput(transforms[i->name].postfix); - break; - - case MLTT_TEXT: - if(usetext) lfm->bufferoutput(i->value); - break; - - case MLTT_ENDOFITEM: - if(usetext) lfm->bufferoutput(i->value); - break; - - case MLTT_UNDEFINED: - // Undefined - break; - } - - lasttype = i->type; - i++; - } - - return 0; -} - - -int printval(lua_State *L) -{ - int n = lua_gettop(L); // number of arguments - if(n != 1) { - char errstr[512]; - sprintf(errstr, "Number of args expected 1, got %d", n); - fprintf(stderr, errstr); - lua_pushstring(L, errstr); - lua_error(L); - return 0; - } - - std::string value = lua_tostring(L, lua_gettop(L)); - - LUAFormatMapper *lfm = (LUAFormatMapper*)getGlobal(L, "LUAFormatMapper"); - - lfm->bufferoutput(value); - - return 0; -} - -/* -int pg_query(lua_State *L) -{ - int n = lua_gettop(L); // number of arguments - if(n != 2) { - char errstr[512]; - sprintf(errstr, "Number of args expected 2, got %d", n); - fprintf(stderr, errstr); - lua_pushstring(L, errstr); - lua_error(L); - return 0; - } - - std::string query = lua_tostring(L, lua_gettop(L)); - unsigned int pg = lua_tointeger(L, lua_gettop(L) - 1); - - printf("query [%s]\n", query.c_str()); - - // lua_createtable(L, 0, 1); - // lua_pushinteger(L, 41); - // lua_rawseti(L, -2, 1); - // lua_setfenv(L, -2); - - lua_pushinteger(L, 42); - lua_pushinteger(L, 41); - lua_rawset(L, -3); - - return 1; -} -*/ - -void preload_formatutils(lua_State *L) -{ - lua_register(L, "printval", printval); - lua_register(L, "printfmlval", printfmlval); -} diff --git a/server/src/luaformatmapperutils.h b/server/src/luaformatmapperutils.h deleted file mode 100644 index 28f2b48..0000000 --- a/server/src/luaformatmapperutils.h +++ /dev/null @@ -1,38 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/*************************************************************************** - * luaformatmapperutils.h - * - * Mon Nov 3 08:46:51 CET 2008 - * Copyright 2008 Bent Bisballe Nyeng - * deva@aasimon.org - ****************************************************************************/ - -/* - * This file is part of Pracro. - * - * Pracro is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * Pracro is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Pracro; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. - */ -#ifndef __PRACRO_LUAFORMATMAPPERUTILS_H__ -#define __PRACRO_LUAFORMATMAPPERUTILS_H__ - -#include <lua.hpp> -#include <lauxlib.h> - -void preload_formatutils(lua_State *L); - -void *getGlobal(lua_State *L, const char *name); -void setGlobal(lua_State *L, const char *name, void *p); - -#endif/*__PRACRO_LUAFORMATMAPPERUTILS_H__*/ diff --git a/server/src/mltokenizer.cc b/server/src/mltokenizer.cc deleted file mode 100644 index 91d5b4b..0000000 --- a/server/src/mltokenizer.cc +++ /dev/null @@ -1,196 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/*************************************************************************** - * mltokenizer.cc - * - * Tue Nov 4 08:46:35 CET 2008 - * Copyright 2008 Bent Bisballe Nyeng - * deva@aasimon.org - ****************************************************************************/ - -/* - * This file is part of Pracro. - * - * Pracro is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * Pracro is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Pracro; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. - */ -#include "mltokenizer.h" - -static std::string rereplaceescaping(std::string mlvalue) -{ - std::string output; - size_t i = 0; - while(i < mlvalue.length()) { - if(mlvalue[i] == '\1') { - output += '{'; - i++; - } else if(mlvalue[i] == '\2') { - output += '}'; - i++; - } else { - output += mlvalue[i]; - i++; - } - } - return output; -} - -static std::string replaceescaping(std::string mlvalue) -{ - std::string output; - size_t i = 0; - while(i < mlvalue.length()) { - if(i < mlvalue.length() - 1 && mlvalue[i] == '{' && mlvalue[i + 1] == '{') { - output += '\1'; - i+=2; - } else if(i < mlvalue.length() - 1 && mlvalue[i] == '}' && mlvalue[i + 1] == '}') { - output += '\2'; - i+=2; - } else { - output += mlvalue[i]; - i++; - } - } - return output; -} - -static std::string gettoken(std::string input, size_t start, std::string term) -{ - std::string output; - - size_t i = start; - while(i < input.length()) { - - size_t j = 0; - while(j < term.length()) { - if(input[i] == term[j]) return output; - j++; - } - - output += input[i]; - i++; - } - - return output; -} - -typedef enum { - NAME, - VALUE, - TEXT, - ENDOFITEM, - UNDEFINED -} tokenizerstate_t; - -std::vector< mltoken_t > mltokenize(std::string mlvalue) -{ - std::vector< mltoken_t > tokens; - - mlvalue = replaceescaping(mlvalue); - - tokenizerstate_t state = UNDEFINED; - mltoken_t token; - size_t i = 0; - while(i < mlvalue.length()) { - switch(state) { - case NAME: - token.name = gettoken(mlvalue, i, "|"); - i += token.name.length() + 1; - token.type = MLTT_VALUE; - token.value = ""; - state = VALUE; - break; - - case VALUE: - token.value = gettoken(mlvalue, i, "}\n"); - i += token.value.length() + 1; - - token.value = rereplaceescaping(token.value); - token.type = MLTT_VALUE; - tokens.push_back(token); - - state = UNDEFINED; - break; - - case TEXT: - if(mlvalue[i] == '$') token.value = "$"; - else token.value = gettoken(mlvalue, i, "$\n"); - i += token.value.length(); - - token.value = rereplaceescaping(token.value); - token.type = MLTT_TEXT; - token.name = ""; - if(tokens.size() && tokens.back().type == MLTT_TEXT) tokens.back().value += token.value; - else tokens.push_back(token); - - state = UNDEFINED; - break; - - case ENDOFITEM: - token.value = "\n"; - i++; - - token.type = MLTT_ENDOFITEM; - token.name = ""; - tokens.push_back(token); - - state = UNDEFINED; - break; - - case UNDEFINED: - switch(mlvalue[i]) { - case '$': - if(i < mlvalue.length() - 1 && mlvalue[i + 1] == '{') { i++; break; } // ignore - else { state = TEXT; break; } - case '{': state = NAME; i++; break; - case '\n': state = ENDOFITEM; break; - default: state = TEXT; break; - } - } - } - - if(state != UNDEFINED) { - printf("Oups... missed something in the end!\n"); - tokens.push_back(token); - } - - return tokens; -} - -#ifdef TEST_MLTOKENIZER - -int main() -{ - std::string mlvalue = "$ab}}c\ndef ${na$me|${{va$lue}}}\n12${34}\n"; - - std::vector< mltoken_t > tokens = mltokenize(mlvalue); - std::vector< mltoken_t >::iterator i = tokens.begin(); - while(i != tokens.end()) { - printf("Token:\n"); - printf("\tType: "); - switch(i->type) { - case MLTT_VALUE: printf("VALUE\n"); break; - case MLTT_TEXT: printf("TEXT\n"); break; - case MLTT_ENDOFITEM: printf("ENDOFITEM\n"); break; - case MLTT_UNDEFINED: printf("UNDEFINED\n"); break; - } - printf("\tName: %s\n", i->name.c_str()); - printf("\tValue: %s\n", i->value.c_str()); - printf("\n"); - i++; - } - - return 0; -} - -#endif diff --git a/server/src/mltokenizer.h b/server/src/mltokenizer.h deleted file mode 100644 index d784178..0000000 --- a/server/src/mltokenizer.h +++ /dev/null @@ -1,55 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/*************************************************************************** - * mltokenizer.h - * - * Tue Nov 4 08:46:35 CET 2008 - * Copyright 2008 Bent Bisballe Nyeng - * deva@aasimon.org - ****************************************************************************/ - -/* - * This file is part of Pracro. - * - * Pracro is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * Pracro is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Pracro; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. - */ -#ifndef __PRACRO_MLTOKENIZER_H__ -#define __PRACRO_MLTOKENIZER_H__ - -#include <string> -#include <vector> - -typedef enum { - MLTT_VALUE, - MLTT_TEXT, - MLTT_ENDOFITEM, // newline - MLTT_UNDEFINED -} mltokentype_t; - -typedef struct { - mltokentype_t type; - std::string name; - std::string value; -} mltoken_t; - -/** - * Split a multilist string into a sequence of tokens, eg. - * This is ${{}} some text ${myname|myvalue}\n - * MLTT_TEXT(value="This is ${{}} some text "), - * MLTT_VALUE(name="myname", value="myvalue"), - * MLTT_ENDOFITEM() - */ -std::vector< mltoken_t > mltokenize(std::string mlvalue); - -#endif/*__PRACRO_MLTOKENIZER_H__*/ |