summaryrefslogtreecommitdiff
path: root/server/src/journal_commit.cc
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/journal_commit.cc')
-rw-r--r--server/src/journal_commit.cc138
1 files changed, 136 insertions, 2 deletions
diff --git a/server/src/journal_commit.cc b/server/src/journal_commit.cc
index 57d8987..ce99b63 100644
--- a/server/src/journal_commit.cc
+++ b/server/src/journal_commit.cc
@@ -30,6 +30,8 @@
#include "debug.h"
+#include <string>
+
// for gethostbyname
#include <netdb.h>
@@ -47,6 +49,55 @@
#include <time.h>
#include <errno.h>
+/**
+ * Remove all spaces, tabs and newline trailing the string.
+ */
+static std::string stripTrailingWhitepace(std::string str)
+{
+ if(str == "") return str;
+
+ ssize_t end = str.size() - 1;
+
+ while(end && str[end] <= ' ') { // Every below SPACE 0x20 is consideret whitespace.
+ end--;
+ }
+ end++;
+
+ return str.substr(0, end);
+}
+
+/**
+ * Find all lines longer than 'width', and insert a newline in the
+ * first backward occurring space.
+ */
+static std::string addNewlines(std::string str, size_t width)
+{
+ std::string output;
+
+ std::string fraction;
+ size_t linelen = 0;
+ for(size_t i = 0; i < str.size(); i++) {
+
+ fraction += str[i];
+
+ if(str[i] <= ' ') {
+ if(linelen + fraction.size() > width) {
+ output[output.size() - 1] = '\n';
+ linelen = 0;
+ }
+ output += fraction;
+ linelen += fraction.size();
+ fraction = "";
+ }
+
+ if(str[i] == '\n') linelen = 0;
+
+ }
+ output += fraction;
+
+ return output;
+}
+
static int mwrite(int sock, const char *fmt, ...)
{
int l = 0;
@@ -113,7 +164,7 @@ int journal_commit(const char *cpr, const char *user,
// send header
mwrite(sock, "PUT JOURNAL PROTO1.0 \r\n");
mwrite(sock, "size : %i\r\n", size);
- mwrite(sock, "user: %s(pracro)\r\n", user);
+ mwrite(sock, "user: %s\r\n", user);
mwrite(sock, "generator: pracro\r\n");
// mwrite(sock, "password: 1234\r\n");
mwrite(sock, "cpr: %s\r\n", cpr);
@@ -121,8 +172,10 @@ int journal_commit(const char *cpr, const char *user,
mwrite(sock, "charset: utf8\r\n");
mwrite(sock, "\r\n");
+ std::string resume = stripTrailingWhitepace(addNewlines(buf, 60));
+
// send body
- if(sock != -1 && write(sock, buf, size) != (ssize_t)size) {
+ if(sock != -1 && write(sock, resume.c_str(), resume.size()) != (ssize_t)resume.size()) {
PRACRO_ERR_LOG(journal, "write did not write all the bytes in the buffer.\n");
}
PRACRO_DEBUG(journal, "%s", buf);
@@ -134,3 +187,84 @@ int journal_commit(const char *cpr, const char *user,
return 0;
}
+
+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)
+{
+ // First run - initialize username and cpr.
+ if(currentuser == "" && entrylist.size() == 0) currentuser = transaction.user;
+ if(currentcpr == "" && entrylist.size() == 0) currentcpr = transaction.cpr;
+
+ // 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();
+ }
+
+ // Strip trailing whitespace, and add newlines.
+ std::string r = stripTrailingWhitepace(addNewlines(resume, 60));
+ std::string m = commit.macro;
+
+ // If macro already exists, overwrite with this entry, otherwise, just add it
+ int idx = -1;
+ std::vector< ResumeEntry >::iterator i = entrylist.begin();
+ int cnt = 0;
+ while(i != entrylist.end()) {
+ if(i->macro == m) idx = cnt;
+ cnt++;
+ i++;
+ }
+
+ if(idx != -1) {
+ entrylist[idx].resume = r;
+ entrylist[idx].macro = m;
+ } else {
+ ResumeEntry re;
+ re.resume = r;
+ re.macro = m;
+ entrylist.push_back(re);
+ }
+}
+
+void JournalWriter::commit()
+{
+ std::string resume;
+
+ // Iterate through all resumes, and create a string containing them all.
+ std::vector< ResumeEntry >::iterator i = entrylist.begin();
+ while(i != entrylist.end()) {
+ if(resume != "") resume += "\n\n";
+ // resume += i->macro + "\n";
+ resume += i->resume;
+ i++;
+ }
+
+ // 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());
+}
+
+
+#ifdef TEST_JOURNAL_COMMIT
+
+int main()
+{
+ std::string text = "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";
+
+ std::string resume = stripTrailingWhitepace(addNewlines(text, 60));
+ printf("[%s]\n", resume.c_str());
+
+ resume = stripTrailingWhitepace(addNewlines("", 60));
+ printf("[%s]\n", resume.c_str());
+
+ return 0;
+}
+
+#endif/*TEST_JOURNAL_COMMIT*/
+