From 6e5274045d2fb060d9ee437a254a0eb32036f281 Mon Sep 17 00:00:00 2001 From: deva Date: Mon, 21 Jun 2010 10:50:11 +0000 Subject: Rename JournalWriter class to just Journal. --- server/src/Makefile.am | 4 +- server/src/connection.cc | 2 +- server/src/environment.cc | 2 +- server/src/journal.cc | 373 +++++++++++++++++++++++++++++++++++++++ server/src/journal.h | 70 ++++++++ server/src/journalwriter.cc | 373 --------------------------------------- server/src/journalwriter.h | 70 -------- server/src/session.cc | 8 +- server/src/session.h | 6 +- server/src/sessionserialiser.cc | 16 +- server/src/transactionhandler.cc | 2 +- 11 files changed, 463 insertions(+), 463 deletions(-) create mode 100644 server/src/journal.cc create mode 100644 server/src/journal.h delete mode 100644 server/src/journalwriter.cc delete mode 100644 server/src/journalwriter.h diff --git a/server/src/Makefile.am b/server/src/Makefile.am index fdfeb42..23fa367 100644 --- a/server/src/Makefile.am +++ b/server/src/Makefile.am @@ -25,7 +25,7 @@ pracrod_SOURCES = \ httpd.cc \ inotify.cc \ journal_commit.cc \ - journalwriter.cc \ + journal.cc \ log.cc \ luaquerymapper.cc \ luaresume.cc \ @@ -105,7 +105,7 @@ EXTRA_DIST = \ httpd.h \ inotify.h \ journal_commit.h \ - journalwriter.h \ + journal.h \ log.h \ luaquerymapper.h \ luaresume.h \ diff --git a/server/src/connection.cc b/server/src/connection.cc index 9c69a9b..dec3967 100644 --- a/server/src/connection.cc +++ b/server/src/connection.cc @@ -153,7 +153,7 @@ std::string Connection::getSessionID() } #ifdef TEST_CONNECTION -//deps: debug.cc transactionparser.cc session.cc xml_encode_decode.cc saxparser.cc transactionhandler.cc journalwriter.cc mutex.cc templateparser.cc exception.cc configuration.cc macroparser.cc semaphore.cc entitylist.cc luaquerymapper.cc inotify.cc log.cc queryhandlerpentominos.cc widgetgenerator.cc queryhandlerpracro.cc resumeparser.cc journal_commit.cc versionstr.cc luaresume.cc luautil.cc artefact.cc environment.cc database.cc macrolist.cc templatelist.cc pracrodao.cc templateheaderparser.cc macroheaderparser.cc pracrodaotest.cc pracrodaopgsql.cc +//deps: debug.cc transactionparser.cc session.cc xml_encode_decode.cc saxparser.cc transactionhandler.cc journal.cc mutex.cc templateparser.cc exception.cc configuration.cc macroparser.cc semaphore.cc entitylist.cc luaquerymapper.cc inotify.cc log.cc queryhandlerpentominos.cc widgetgenerator.cc queryhandlerpracro.cc resumeparser.cc journal_commit.cc versionstr.cc luaresume.cc luautil.cc artefact.cc environment.cc database.cc macrolist.cc templatelist.cc pracrodao.cc templateheaderparser.cc macroheaderparser.cc pracrodaotest.cc pracrodaopgsql.cc //cflags: -DWITHOUT_DATABASE -DWITHOUT_ARTEFACT -I.. $(LUA_CFLAGS) $(EXPAT_CFLAGS) $(PTHREAD_CFLAGS) $(PQXX_CXXFLAGS) //libs: $(LUA_LIBS) $(EXPAT_LIBS) $(PTHREAD_LIBS) $(PQXX_LIBS) #include "test.h" diff --git a/server/src/environment.cc b/server/src/environment.cc index f1e035e..a2b7cb9 100644 --- a/server/src/environment.cc +++ b/server/src/environment.cc @@ -66,7 +66,7 @@ Environment::~Environment() } #ifdef TEST_ENVIRONMENT -//deps: configuration.cc database.cc artefact.cc pracrodao.cc session.cc mutex.cc semaphore.cc debug.cc pracrodaotest.cc pracrodaopgsql.cc journalwriter.cc journal_commit.cc entitylist.cc inotify.cc exception.cc versionstr.cc tcpsocket.cc macrolist.cc templatelist.cc saxparser.cc log.cc macroheaderparser.cc templateheaderparser.cc +//deps: configuration.cc database.cc artefact.cc pracrodao.cc session.cc mutex.cc semaphore.cc debug.cc pracrodaotest.cc pracrodaopgsql.cc journal.cc journal_commit.cc entitylist.cc inotify.cc exception.cc versionstr.cc tcpsocket.cc macrolist.cc templatelist.cc saxparser.cc log.cc macroheaderparser.cc templateheaderparser.cc //cflags: -DWITHOUT_ARTEFACT -I.. $(PQXX_CXXFLAGS) $(PTHREAD_CFLAGS) $(EXPAT_CFLAGS) //libs: $(PQXX_LIBS) -lpthread $(EXPAT_LIBS) $(PTHREAD_LIBS) #include "test.h" diff --git a/server/src/journal.cc b/server/src/journal.cc new file mode 100644 index 0000000..f8e0af4 --- /dev/null +++ b/server/src/journal.cc @@ -0,0 +1,373 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * journal.cc + * + * Mon Jun 21 12:42:15 CEST 2010 + * Copyright 2010 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 "journal.h" + +#include "debug.h" +#include "journal_commit.h" + +static inline bool iswhitespace(char c) +{ + return c == ' ' || c == '\n' || c == '\t' || c == '\r'; +} + +/** + * Remove all spaces, tabs and newline trailing the string. + */ +static std::string stripTrailingWhitepace(const std::string &str) +{ + if(str == "") return str; + + ssize_t end = str.size() - 1; + + while(end >= 0 && iswhitespace(str[end])) end--; + end++; + + return str.substr(0, end); +} + +static bool isInsideUTF8(const std::string &str, size_t idx) +{ + // Two byte character + if(idx > 0 && + (str[idx] & 0xC0 ) == 0x80 && + (str[idx - 1] & 0xE0) == 0xC0) + return true; + + // Three byte character + if(idx > 1 && + (str[idx] & 0xC0 ) == 0x80 && + (str[idx - 1] & 0xC0 ) == 0x80 && + (str[idx - 2] & 0xF0) == 0xE0) + return true; + + if(idx > 0 && + (str[idx] & 0xC0 ) == 0x80 && + (str[idx - 1] & 0xF0) == 0xE0) + return true; + + // Four byte character + if(idx > 2 && + (str[idx] & 0xC0 ) == 0x80 && + (str[idx - 1] & 0xC0 ) == 0x80 && + (str[idx - 2] & 0xC0 ) == 0x80 && + (str[idx - 3] & 0xF8) == 0xF0) + return true; + + if(idx > 1 && + (str[idx] & 0xC0 ) == 0x80 && + (str[idx - 1] & 0xC0 ) == 0x80 && + (str[idx - 2] & 0xF8) == 0xF0) + return true; + + if(idx > 0 && + (str[idx] & 0xC0 ) == 0x80 && + (str[idx - 1] & 0xF8) == 0xF0) + return true; + + return false; +} + +static size_t UTF8Length(const std::string &str) +{ + size_t size = 0; + for(size_t i = 0; i < str.size(); i++) { + if(!isInsideUTF8(str, i)) size++; + } + return size; +} + +/** + * Find all lines longer than 'width', and insert a newline in the + * first backward occurring space. Force split any lines without a space. + */ +static std::string addNewlines(const std::string &str, size_t width) +{ + std::string output; + size_t len = 0; + for(size_t i = 0; i < str.size(); i++) { + char c = str[i]; + + /* + fprintf(stderr, "i: %d, char: '%c', width: %d, len: %d, output: '%s'\n", + i, c, width, len, output.c_str()); + */ + + output += c; + + if(isInsideUTF8(str, i)) continue; + + len++; + if(c == '\n') len = 0; + + // Try to split line at whitespace. + if(len > width) { + size_t p = 0; + while(p < width) { + p++; + + size_t pos = output.size() - p; + + if(isInsideUTF8(output, pos)) continue; + + if(iswhitespace(output[pos])) { + output[pos] = '\n'; + len = UTF8Length(output.substr(pos+1)); + break; + } + } + } + + // Force split line at current pos. + if(len > width) { + // replace last char with a newline, and append the character again, after the newline. + output[output.size()-1] = '\n'; + output += c; + len = 1; + } + } + + return output; +} + +Journal::Journal(std::string host, unsigned short int port) +{ + this->host = host; + this->port = port; +} + +void Journal::addEntry(Transaction &transaction, Commit &commit, + std::string resume, Template *templ) +{ + size_t index = 0; + std::vector< Macro >::iterator i = templ->macros.begin(); + while(i != templ->macros.end()) { + Macro &m = *i; + if(commit.macro == m.attributes["name"]) break; + index++; + i++; + } + + if(index >= templ->macros.size()) { + PRACRO_ERR(journal, "Could not find macro %s in template %s\n", + commit.macro.c_str(), templ->attributes["name"].c_str()); + // return; + } else { + PRACRO_DEBUG(journal, "Found macro %s as index %u in template %s\n", + commit.macro.c_str(), index, templ->attributes["name"].c_str()); + } + + // First run - initialize username and cpr. + if(currentuser == "" && entrylist.size() == 0) currentuser = transaction.user; + if(currentcpr == "" && entrylist.size() == 0) currentcpr = transaction.cpr; + + PRACRO_DEBUG(journal, "addEntry: template(%s)\n", templ->attributes["name"].c_str()); + +#if 0 // this feature is no longer nessecary... + // Add the template resume as the header (ie. first entry) + // of the journal entry. + if(entrylist.size() == 0 && templ->attributes["name"] != "") { + std::string template_resume = templ->attributes["resume"]; + + PRACRO_DEBUG(journal, "TemplateResume: %s\n", template_resume.c_str()); + + if(template_resume != "") { + ResumeEntry re; + re.resume = template_resume; + re.macro = "template_header"; + entrylist[-1] = re; // Make sure it comes first. + } + } +#endif + + // Test if the username or the cpr has changed... + // if so, commit and clear the list. + if(currentuser != transaction.user || currentcpr != transaction.cpr) { + this->commit(); + entrylist.clear(); + } + + addEntry(resume, commit.macro, index); +} + +void Journal::addEntry(std::string resume, std::string macro, int index) +{ + // Strip trailing whitespace, and add newlines. + std::string r = resume; + std::string m = macro; + + ResumeEntry re; + re.resume = r; + re.macro = m; + entrylist[index] = re; +} + +void Journal::commit() +{ + std::string resume; + + // Iterate through all resumes, and create a string containing them all. + std::map< int, ResumeEntry >::iterator i = entrylist.begin(); + while(i != entrylist.end()) { + if(resume != "") resume += "\n\n"; + // resume += i->macro + "\n"; + resume += stripTrailingWhitepace(addNewlines(i->second.resume, 60)); + i++; + } + + if(resume == "") return; + + // Connect to praxisuploadserver and commit all resumes in one bulk. + journal_commit(currentcpr.c_str(), currentuser.c_str(), + host.c_str(), port, + resume.c_str(), resume.size()); +} + +std::string Journal::getEntry(std::string macro) +{ + std::map< int, ResumeEntry >::iterator i = entrylist.begin(); + while(i != entrylist.end()) { + if(i->second.macro == macro) return i->second.resume; + i++; + } + return ""; +} + +void Journal::removeEntry(std::string macro) +{ + std::map< int, ResumeEntry >::iterator i = entrylist.begin(); + while(i != entrylist.end()) { + if(i->second.macro == macro) { + entrylist.erase(i); + break; + } + i++; + } +} + + +#ifdef TEST_JOURNAL +//deps: debug.cc journal_commit.cc +//cflags: -I.. +//libs: +#include "test.h" + +#define LONG "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do\neiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. \n\n \t"; + +TEST_BEGIN; + +TEST_EQUAL_STR(stripTrailingWhitepace + ("Lorem ipsum dolor sit amet. \n\n \t"), + "Lorem ipsum dolor sit amet.", "Test wspace remover."); + +TEST_EQUAL_STR(stripTrailingWhitepace(""), "", "Test wspace remover on empty string."); + +TEST_EQUAL_STR(stripTrailingWhitepace("\n\t "), "", "Test wspace remover on wspace-only string."); + +TEST_EQUAL_STR(stripTrailingWhitepace("\n"), "", "Test wspace remover on newline only."); +TEST_EQUAL_STR(stripTrailingWhitepace("\t"), "", "Test wspace remover on tab only."); +TEST_EQUAL_STR(stripTrailingWhitepace("\r"), "", "Test wspace remover on space only."); +TEST_EQUAL_STR(stripTrailingWhitepace(" "), "", "Test wspace remover on space only."); + +TEST_EQUAL_STR(stripTrailingWhitepace("ø "), "ø", "Test wspace remover on utf-8 char."); +TEST_EQUAL_STR(stripTrailingWhitepace("ø"), "ø", "Test wspace remover on utf-8 char only."); + +TEST_EQUAL_STR(stripTrailingWhitepace("a "), "a", "Test wspace remover on single char only."); +TEST_EQUAL_STR(stripTrailingWhitepace("a"), "a", "Test wspace remover on single char only."); + +TEST_EQUAL_STR(addNewlines + ("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do.", 60), + "Lorem ipsum dolor sit amet, consectetur adipisicing elit,\nsed do.", + "Test single linesplit."); + +TEST_EQUAL_STR(addNewlines + ("Lorem ipsum dolor sit amet, consectetur adipisicing elit, øsed do.", 60), + "Lorem ipsum dolor sit amet, consectetur adipisicing elit,\nøsed do.", + "Test single linesplit around utf-8 char."); + +TEST_EQUAL_STR(addNewlines + ("Lorem ipsum dolor sit amet, consectetur adipisicing elitø, sed do.", 60), + "Lorem ipsum dolor sit amet, consectetur adipisicing elitø,\nsed do.", + "Test single linesplit around utf-8 char."); + +TEST_EQUAL_STR(addNewlines + ("Lorem\nipsum dolor sit amet.", 12), + "Lorem\nipsum dolor\nsit amet.", + "Test single linesplit with contained newline."); + +TEST_EQUAL_STR(addNewlines + ("Lorem ipsum dolor sitan met.", 11), + "Lorem ipsum\ndolor sitan\nmet.", + "Test single linesplit on exact border."); + +TEST_EQUAL_STR(addNewlines + ("Loremipsum", 6), + "Loremi\npsum", + "Test single linesplit inside word."); + +TEST_EQUAL_STR(addNewlines + ("abc Loremipsum", 6), + "abc\nLoremi\npsum", + "Test single linesplit inside word."); + +TEST_TRUE(isInsideUTF8("ø", 1), "Test positive utf8 match."); +TEST_TRUE(isInsideUTF8("aæb", 2), "Test positive utf8 match."); +TEST_TRUE(isInsideUTF8("aøb", 2), "Test positive utf8 match."); +TEST_TRUE(isInsideUTF8("aåb", 2), "Test positive utf8 match."); +TEST_TRUE(isInsideUTF8("aÆb", 2), "Test positive utf8 match."); +TEST_TRUE(isInsideUTF8("aØb", 2), "Test positive utf8 match."); +TEST_TRUE(isInsideUTF8("aÅb", 2), "Test positive utf8 match."); +TEST_FALSE(isInsideUTF8("ø", 0), "Test negative utf8 match."); +TEST_FALSE(isInsideUTF8("aæøb", 3), "Test negative utf8 match (between two utf8 chars)."); +TEST_FALSE(isInsideUTF8("aøb", 0), "Test negative utf8 match (before utf8 char)."); + +TEST_FALSE(isInsideUTF8("𤭢", 0), "Test positive utf8 match, len 4."); +TEST_TRUE(isInsideUTF8("𤭢", 1), "Test positive utf8 match, len 4."); +TEST_TRUE(isInsideUTF8("𤭢", 2), "Test positive utf8 match, len 4."); +TEST_TRUE(isInsideUTF8("𤭢", 3), "Test positive utf8 match, len 4."); + +TEST_FALSE(isInsideUTF8("€", 0), "Test positive utf8 match, len 3."); +TEST_TRUE(isInsideUTF8("€", 1), "Test positive utf8 match, len 3."); +TEST_TRUE(isInsideUTF8("€", 2), "Test positive utf8 match, len 3."); + +TEST_FALSE(isInsideUTF8("¢", 0), "Test positive utf8 match, len 2."); +TEST_TRUE(isInsideUTF8("¢", 1), "Test positive utf8 match, len 2."); + +TEST_EQUAL_INT(UTF8Length("ø"), 1, "Test utf8 string length."); +TEST_EQUAL_INT(UTF8Length("æø"), 2, "Test utf8 string length."); +TEST_EQUAL_INT(UTF8Length(""), 0, "Test utf8 string length."); +TEST_EQUAL_INT(UTF8Length("a"), 1, "Test utf8 string length."); +TEST_EQUAL_INT(UTF8Length("aø"), 2, "Test utf8 string length."); +TEST_EQUAL_INT(UTF8Length("aøb"), 3, "Test utf8 string length."); + +TEST_EQUAL_INT(UTF8Length("a𤭢€¢ø𤭢€¢øa"), 10, "Test utf8 string length, combi."); + +TEST_EQUAL_STR(stripTrailingWhitepace(addNewlines("", 60)), "", "Test on empty input."); + +TEST_END; + +#endif/*TEST_JOURNAL*/ diff --git a/server/src/journal.h b/server/src/journal.h new file mode 100644 index 0000000..1ba258d --- /dev/null +++ b/server/src/journal.h @@ -0,0 +1,70 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * journal.h + * + * Mon Jun 21 12:42:15 CEST 2010 + * Copyright 2010 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_JOURNAL_H__ +#define __PRACRO_JOURNAL_H__ + +#include +#include + +#include "transaction.h" +#include "template.h" + +class SessionSerialiser; + +class Journal { + friend class SessionSerialiser; +public: + Journal(std::string host, unsigned short int port); + + void addEntry(Transaction &transaction, Commit &commit, + std::string resume, Template *templ); + + void addEntry(std::string resume, std::string macro, int index); + + void commit(); + + std::string getEntry(std::string macro); + void removeEntry(std::string macro); + +private: + std::string host; + unsigned short int port; + + std::string currentuser; + std::string currentcpr; + + class ResumeEntry { + public: + std::string resume; + std::string macro; + }; + + std::map< int, ResumeEntry > entrylist; +}; + +#endif/*__PRACRO_JOURNAL_H__*/ diff --git a/server/src/journalwriter.cc b/server/src/journalwriter.cc deleted file mode 100644 index 5858de4..0000000 --- a/server/src/journalwriter.cc +++ /dev/null @@ -1,373 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; coding: utf-8 -*- */ -/* vim: set et sw=2 ts=2: */ -/*************************************************************************** - * journalwriter.cc - * - * Tue Jan 5 15:52:54 CET 2010 - * Copyright 2010 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 "journalwriter.h" - -#include "debug.h" -#include "journal_commit.h" - -static inline bool iswhitespace(char c) -{ - return c == ' ' || c == '\n' || c == '\t' || c == '\r'; -} - -/** - * Remove all spaces, tabs and newline trailing the string. - */ -static std::string stripTrailingWhitepace(const std::string &str) -{ - if(str == "") return str; - - ssize_t end = str.size() - 1; - - while(end >= 0 && iswhitespace(str[end])) end--; - end++; - - return str.substr(0, end); -} - -static bool isInsideUTF8(const std::string &str, size_t idx) -{ - // Two byte character - if(idx > 0 && - (str[idx] & 0xC0 ) == 0x80 && - (str[idx - 1] & 0xE0) == 0xC0) - return true; - - // Three byte character - if(idx > 1 && - (str[idx] & 0xC0 ) == 0x80 && - (str[idx - 1] & 0xC0 ) == 0x80 && - (str[idx - 2] & 0xF0) == 0xE0) - return true; - - if(idx > 0 && - (str[idx] & 0xC0 ) == 0x80 && - (str[idx - 1] & 0xF0) == 0xE0) - return true; - - // Four byte character - if(idx > 2 && - (str[idx] & 0xC0 ) == 0x80 && - (str[idx - 1] & 0xC0 ) == 0x80 && - (str[idx - 2] & 0xC0 ) == 0x80 && - (str[idx - 3] & 0xF8) == 0xF0) - return true; - - if(idx > 1 && - (str[idx] & 0xC0 ) == 0x80 && - (str[idx - 1] & 0xC0 ) == 0x80 && - (str[idx - 2] & 0xF8) == 0xF0) - return true; - - if(idx > 0 && - (str[idx] & 0xC0 ) == 0x80 && - (str[idx - 1] & 0xF8) == 0xF0) - return true; - - return false; -} - -static size_t UTF8Length(const std::string &str) -{ - size_t size = 0; - for(size_t i = 0; i < str.size(); i++) { - if(!isInsideUTF8(str, i)) size++; - } - return size; -} - -/** - * Find all lines longer than 'width', and insert a newline in the - * first backward occurring space. Force split any lines without a space. - */ -static std::string addNewlines(const std::string &str, size_t width) -{ - std::string output; - size_t len = 0; - for(size_t i = 0; i < str.size(); i++) { - char c = str[i]; - - /* - fprintf(stderr, "i: %d, char: '%c', width: %d, len: %d, output: '%s'\n", - i, c, width, len, output.c_str()); - */ - - output += c; - - if(isInsideUTF8(str, i)) continue; - - len++; - if(c == '\n') len = 0; - - // Try to split line at whitespace. - if(len > width) { - size_t p = 0; - while(p < width) { - p++; - - size_t pos = output.size() - p; - - if(isInsideUTF8(output, pos)) continue; - - if(iswhitespace(output[pos])) { - output[pos] = '\n'; - len = UTF8Length(output.substr(pos+1)); - break; - } - } - } - - // Force split line at current pos. - if(len > width) { - // replace last char with a newline, and append the character again, after the newline. - output[output.size()-1] = '\n'; - output += c; - len = 1; - } - } - - return output; -} - -JournalWriter::JournalWriter(std::string host, unsigned short int port) -{ - this->host = host; - this->port = port; -} - -void JournalWriter::addEntry(Transaction &transaction, Commit &commit, - std::string resume, Template *templ) -{ - size_t index = 0; - std::vector< Macro >::iterator i = templ->macros.begin(); - while(i != templ->macros.end()) { - Macro &m = *i; - if(commit.macro == m.attributes["name"]) break; - index++; - i++; - } - - if(index >= templ->macros.size()) { - PRACRO_ERR(journal, "Could not find macro %s in template %s\n", - commit.macro.c_str(), templ->attributes["name"].c_str()); - // return; - } else { - PRACRO_DEBUG(journal, "Found macro %s as index %u in template %s\n", - commit.macro.c_str(), index, templ->attributes["name"].c_str()); - } - - // First run - initialize username and cpr. - if(currentuser == "" && entrylist.size() == 0) currentuser = transaction.user; - if(currentcpr == "" && entrylist.size() == 0) currentcpr = transaction.cpr; - - PRACRO_DEBUG(journal, "addEntry: template(%s)\n", templ->attributes["name"].c_str()); - -#if 0 // this feature is no longer nessecary... - // Add the template resume as the header (ie. first entry) - // of the journal entry. - if(entrylist.size() == 0 && templ->attributes["name"] != "") { - std::string template_resume = templ->attributes["resume"]; - - PRACRO_DEBUG(journal, "TemplateResume: %s\n", template_resume.c_str()); - - if(template_resume != "") { - ResumeEntry re; - re.resume = template_resume; - re.macro = "template_header"; - entrylist[-1] = re; // Make sure it comes first. - } - } -#endif - - // Test if the username or the cpr has changed... - // if so, commit and clear the list. - if(currentuser != transaction.user || currentcpr != transaction.cpr) { - this->commit(); - entrylist.clear(); - } - - addEntry(resume, commit.macro, index); -} - -void JournalWriter::addEntry(std::string resume, std::string macro, int index) -{ - // Strip trailing whitespace, and add newlines. - std::string r = resume; - std::string m = macro; - - ResumeEntry re; - re.resume = r; - re.macro = m; - entrylist[index] = re; -} - -void JournalWriter::commit() -{ - std::string resume; - - // Iterate through all resumes, and create a string containing them all. - std::map< int, ResumeEntry >::iterator i = entrylist.begin(); - while(i != entrylist.end()) { - if(resume != "") resume += "\n\n"; - // resume += i->macro + "\n"; - resume += stripTrailingWhitepace(addNewlines(i->second.resume, 60)); - i++; - } - - if(resume == "") return; - - // Connect to praxisuploadserver and commit all resumes in one bulk. - journal_commit(currentcpr.c_str(), currentuser.c_str(), - host.c_str(), port, - resume.c_str(), resume.size()); -} - -std::string JournalWriter::getEntry(std::string macro) -{ - std::map< int, ResumeEntry >::iterator i = entrylist.begin(); - while(i != entrylist.end()) { - if(i->second.macro == macro) return i->second.resume; - i++; - } - return ""; -} - -void JournalWriter::removeEntry(std::string macro) -{ - std::map< int, ResumeEntry >::iterator i = entrylist.begin(); - while(i != entrylist.end()) { - if(i->second.macro == macro) { - entrylist.erase(i); - break; - } - i++; - } -} - - -#ifdef TEST_JOURNALWRITER -//deps: debug.cc journal_commit.cc -//cflags: -I.. -//libs: -#include "test.h" - -#define LONG "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do\neiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. \n\n \t"; - -TEST_BEGIN; - -TEST_EQUAL_STR(stripTrailingWhitepace - ("Lorem ipsum dolor sit amet. \n\n \t"), - "Lorem ipsum dolor sit amet.", "Test wspace remover."); - -TEST_EQUAL_STR(stripTrailingWhitepace(""), "", "Test wspace remover on empty string."); - -TEST_EQUAL_STR(stripTrailingWhitepace("\n\t "), "", "Test wspace remover on wspace-only string."); - -TEST_EQUAL_STR(stripTrailingWhitepace("\n"), "", "Test wspace remover on newline only."); -TEST_EQUAL_STR(stripTrailingWhitepace("\t"), "", "Test wspace remover on tab only."); -TEST_EQUAL_STR(stripTrailingWhitepace("\r"), "", "Test wspace remover on space only."); -TEST_EQUAL_STR(stripTrailingWhitepace(" "), "", "Test wspace remover on space only."); - -TEST_EQUAL_STR(stripTrailingWhitepace("ø "), "ø", "Test wspace remover on utf-8 char."); -TEST_EQUAL_STR(stripTrailingWhitepace("ø"), "ø", "Test wspace remover on utf-8 char only."); - -TEST_EQUAL_STR(stripTrailingWhitepace("a "), "a", "Test wspace remover on single char only."); -TEST_EQUAL_STR(stripTrailingWhitepace("a"), "a", "Test wspace remover on single char only."); - -TEST_EQUAL_STR(addNewlines - ("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do.", 60), - "Lorem ipsum dolor sit amet, consectetur adipisicing elit,\nsed do.", - "Test single linesplit."); - -TEST_EQUAL_STR(addNewlines - ("Lorem ipsum dolor sit amet, consectetur adipisicing elit, øsed do.", 60), - "Lorem ipsum dolor sit amet, consectetur adipisicing elit,\nøsed do.", - "Test single linesplit around utf-8 char."); - -TEST_EQUAL_STR(addNewlines - ("Lorem ipsum dolor sit amet, consectetur adipisicing elitø, sed do.", 60), - "Lorem ipsum dolor sit amet, consectetur adipisicing elitø,\nsed do.", - "Test single linesplit around utf-8 char."); - -TEST_EQUAL_STR(addNewlines - ("Lorem\nipsum dolor sit amet.", 12), - "Lorem\nipsum dolor\nsit amet.", - "Test single linesplit with contained newline."); - -TEST_EQUAL_STR(addNewlines - ("Lorem ipsum dolor sitan met.", 11), - "Lorem ipsum\ndolor sitan\nmet.", - "Test single linesplit on exact border."); - -TEST_EQUAL_STR(addNewlines - ("Loremipsum", 6), - "Loremi\npsum", - "Test single linesplit inside word."); - -TEST_EQUAL_STR(addNewlines - ("abc Loremipsum", 6), - "abc\nLoremi\npsum", - "Test single linesplit inside word."); - -TEST_TRUE(isInsideUTF8("ø", 1), "Test positive utf8 match."); -TEST_TRUE(isInsideUTF8("aæb", 2), "Test positive utf8 match."); -TEST_TRUE(isInsideUTF8("aøb", 2), "Test positive utf8 match."); -TEST_TRUE(isInsideUTF8("aåb", 2), "Test positive utf8 match."); -TEST_TRUE(isInsideUTF8("aÆb", 2), "Test positive utf8 match."); -TEST_TRUE(isInsideUTF8("aØb", 2), "Test positive utf8 match."); -TEST_TRUE(isInsideUTF8("aÅb", 2), "Test positive utf8 match."); -TEST_FALSE(isInsideUTF8("ø", 0), "Test negative utf8 match."); -TEST_FALSE(isInsideUTF8("aæøb", 3), "Test negative utf8 match (between two utf8 chars)."); -TEST_FALSE(isInsideUTF8("aøb", 0), "Test negative utf8 match (before utf8 char)."); - -TEST_FALSE(isInsideUTF8("𤭢", 0), "Test positive utf8 match, len 4."); -TEST_TRUE(isInsideUTF8("𤭢", 1), "Test positive utf8 match, len 4."); -TEST_TRUE(isInsideUTF8("𤭢", 2), "Test positive utf8 match, len 4."); -TEST_TRUE(isInsideUTF8("𤭢", 3), "Test positive utf8 match, len 4."); - -TEST_FALSE(isInsideUTF8("€", 0), "Test positive utf8 match, len 3."); -TEST_TRUE(isInsideUTF8("€", 1), "Test positive utf8 match, len 3."); -TEST_TRUE(isInsideUTF8("€", 2), "Test positive utf8 match, len 3."); - -TEST_FALSE(isInsideUTF8("¢", 0), "Test positive utf8 match, len 2."); -TEST_TRUE(isInsideUTF8("¢", 1), "Test positive utf8 match, len 2."); - -TEST_EQUAL_INT(UTF8Length("ø"), 1, "Test utf8 string length."); -TEST_EQUAL_INT(UTF8Length("æø"), 2, "Test utf8 string length."); -TEST_EQUAL_INT(UTF8Length(""), 0, "Test utf8 string length."); -TEST_EQUAL_INT(UTF8Length("a"), 1, "Test utf8 string length."); -TEST_EQUAL_INT(UTF8Length("aø"), 2, "Test utf8 string length."); -TEST_EQUAL_INT(UTF8Length("aøb"), 3, "Test utf8 string length."); - -TEST_EQUAL_INT(UTF8Length("a𤭢€¢ø𤭢€¢øa"), 10, "Test utf8 string length, combi."); - -TEST_EQUAL_STR(stripTrailingWhitepace(addNewlines("", 60)), "", "Test on empty input."); - -TEST_END; - -#endif/*TEST_JOURNALWRITER*/ diff --git a/server/src/journalwriter.h b/server/src/journalwriter.h deleted file mode 100644 index ea1b514..0000000 --- a/server/src/journalwriter.h +++ /dev/null @@ -1,70 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim: set et sw=2 ts=2: */ -/*************************************************************************** - * journalwriter.h - * - * Tue Jan 5 15:52:54 CET 2010 - * Copyright 2010 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_JOURNALWRITER_H__ -#define __PRACRO_JOURNALWRITER_H__ - -#include -#include - -#include "transaction.h" -#include "template.h" - -class SessionSerialiser; - -class JournalWriter { - friend class SessionSerialiser; -public: - JournalWriter(std::string host, unsigned short int port); - - void addEntry(Transaction &transaction, Commit &commit, - std::string resume, Template *templ); - - void addEntry(std::string resume, std::string macro, int index); - - void commit(); - - std::string getEntry(std::string macro); - void removeEntry(std::string macro); - -private: - std::string host; - unsigned short int port; - - std::string currentuser; - std::string currentcpr; - - class ResumeEntry { - public: - std::string resume; - std::string macro; - }; - - std::map< int, ResumeEntry > entrylist; -}; - -#endif/*__PRACRO_JOURNALWRITER_H__*/ diff --git a/server/src/session.cc b/server/src/session.cc index e53565b..31be81b 100644 --- a/server/src/session.cc +++ b/server/src/session.cc @@ -35,7 +35,7 @@ #include #include -#include "journalwriter.h" +#include "journal.h" #include "database.h" #include "configuration.h" #include "connectionpool.h" @@ -96,11 +96,11 @@ void Session::discard() } } -JournalWriter *Session::journal() +Journal *Session::journal() { if(_journal == NULL) { _journal = - new JournalWriter(Conf::journal_commit_addr, Conf::journal_commit_port); + new Journal(Conf::journal_commit_addr, Conf::journal_commit_port); } return _journal; } @@ -221,7 +221,7 @@ SessionAutolock::~SessionAutolock() } #ifdef TEST_SESSION -//deps: configuration.cc journalwriter.cc journal_commit.cc mutex.cc debug.cc sessionserialiser.cc sessionparser.cc saxparser.cc +//deps: configuration.cc journal.cc journal_commit.cc mutex.cc debug.cc sessionserialiser.cc sessionparser.cc saxparser.cc //cflags: -I.. $(PTHREAD_CFLAGS) $(EXPAT_CFLAGS) //libs: $(PTHREAD_LIBS) $(EXPAT_LIBS) #include diff --git a/server/src/session.h b/server/src/session.h index cd13aa8..0540541 100644 --- a/server/src/session.h +++ b/server/src/session.h @@ -34,7 +34,7 @@ #include "mutex.h" class Database; -class JournalWriter; +class Journal; class Session { public: @@ -49,11 +49,11 @@ public: void commit(); void discard(); - JournalWriter *journal(); + Journal *journal(); Database *database(); private: - JournalWriter *_journal; + Journal *_journal; Database *_database; std::string _id; Mutex mutex; diff --git a/server/src/sessionserialiser.cc b/server/src/sessionserialiser.cc index 2c35d2a..9b5e393 100644 --- a/server/src/sessionserialiser.cc +++ b/server/src/sessionserialiser.cc @@ -27,7 +27,7 @@ */ #include "sessionserialiser.h" -#include "journalwriter.h" +#include "journal.h" #include "sessionparser.h" #include "database.h" @@ -66,7 +66,7 @@ void SessionSerialiser::loadStr(const std::string &xml) SessionParser parser; parser.parse(xml.data(), xml.length()); - JournalWriter *j = session->journal(); + Journal *j = session->journal(); j->currentuser = XDEC(parser.userid); j->currentcpr = XDEC(parser.patientid); std::vector::iterator i = parser.entries.begin(); @@ -88,12 +88,12 @@ std::string SessionSerialiser::saveStr() xml += "id()+"\">\n"; - JournalWriter *journal = session->journal(); + Journal *journal = session->journal(); xml += " currentcpr) + "\" userid=\"" + XENC(journal->currentuser) + "\">\n"; - std::map< int, JournalWriter::ResumeEntry >::iterator i = + std::map< int, Journal::ResumeEntry >::iterator i = journal->entrylist.begin(); while(i != journal->entrylist.end()) { @@ -148,7 +148,7 @@ void SessionSerialiser::save() } #ifdef TEST_SESSIONSERIALISER -//deps: session.cc journalwriter.cc debug.cc configuration.cc mutex.cc journal_commit.cc sessionparser.cc saxparser.cc +//deps: session.cc journal.cc debug.cc configuration.cc mutex.cc journal_commit.cc sessionparser.cc saxparser.cc //cflags: -I.. $(PTHREAD_CFLAGS) $(EXPAT_CFLAGS) //libs: $(PTHREAD_LIBS) $(EXPAT_LIBS) #include "test.h" @@ -162,7 +162,7 @@ std::string xml; { Session session(SID); - JournalWriter *j = session.journal(); + Journal *j = session.journal(); j->addEntry("some text", "macro1", 0); j->addEntry("some more text", "macro2", 2); j->addEntry("yet some more text", "macro3", 1); @@ -175,7 +175,7 @@ std::string xml; { Session session(SID); - JournalWriter *j = session.journal(); + Journal *j = session.journal(); j->addEntry("some text", "macro1", 0); j->addEntry("some more text", "macro2", 2); j->addEntry("yet some more text", "macro3", 1); @@ -193,7 +193,7 @@ std::string xml; { Session session(SID); - JournalWriter *j = session.journal(); + Journal *j = session.journal(); j->addEntry("some text", "macro1", 0); j->addEntry("some more text", "macro2", 2); j->addEntry("yet some more text", "macro3", 1); diff --git a/server/src/transactionhandler.cc b/server/src/transactionhandler.cc index 1658713..967b7e9 100644 --- a/server/src/transactionhandler.cc +++ b/server/src/transactionhandler.cc @@ -36,7 +36,7 @@ #include "queryhandlerpracro.h" #include "xml_encode_decode.h" #include "widgetgenerator.h" -#include "journalwriter.h" +#include "journal.h" static std::string error_box(std::string message) { -- cgit v1.2.3