From 8c1bb171977954d573a50d03308c9221d4c0ee8e Mon Sep 17 00:00:00 2001
From: Samo Penic <samo@altea>
Date: Tue, 16 Feb 2016 19:46:14 +0000
Subject: [PATCH] Starting with restore. Not working yet. Changes in main for debugging only

---
 src/Makefile.am |    2 
 src/main.c      |    4 
 src/snapshot.c  |    7 ++
 aclocal.m4      |   65 ++++++++++++++++++++-
 src/restore.h   |    7 ++
 src/restore.c   |   92 ++++++++++++++++++++++++++++++
 6 files changed, 168 insertions(+), 9 deletions(-)

diff --git a/aclocal.m4 b/aclocal.m4
index da47901..7657505 100644
--- a/aclocal.m4
+++ b/aclocal.m4
@@ -180,6 +180,61 @@
 fi[]dnl
 ])# PKG_CHECK_MODULES
 
+
+# PKG_INSTALLDIR(DIRECTORY)
+# -------------------------
+# Substitutes the variable pkgconfigdir as the location where a module
+# should install pkg-config .pc files. By default the directory is
+# $libdir/pkgconfig, but the default can be changed by passing
+# DIRECTORY. The user can override through the --with-pkgconfigdir
+# parameter.
+AC_DEFUN([PKG_INSTALLDIR],
+[m4_pushdef([pkg_default], [m4_default([$1], ['${libdir}/pkgconfig'])])
+m4_pushdef([pkg_description],
+    [pkg-config installation directory @<:@]pkg_default[@:>@])
+AC_ARG_WITH([pkgconfigdir],
+    [AS_HELP_STRING([--with-pkgconfigdir], pkg_description)],,
+    [with_pkgconfigdir=]pkg_default)
+AC_SUBST([pkgconfigdir], [$with_pkgconfigdir])
+m4_popdef([pkg_default])
+m4_popdef([pkg_description])
+]) dnl PKG_INSTALLDIR
+
+
+# PKG_NOARCH_INSTALLDIR(DIRECTORY)
+# -------------------------
+# Substitutes the variable noarch_pkgconfigdir as the location where a
+# module should install arch-independent pkg-config .pc files. By
+# default the directory is $datadir/pkgconfig, but the default can be
+# changed by passing DIRECTORY. The user can override through the
+# --with-noarch-pkgconfigdir parameter.
+AC_DEFUN([PKG_NOARCH_INSTALLDIR],
+[m4_pushdef([pkg_default], [m4_default([$1], ['${datadir}/pkgconfig'])])
+m4_pushdef([pkg_description],
+    [pkg-config arch-independent installation directory @<:@]pkg_default[@:>@])
+AC_ARG_WITH([noarch-pkgconfigdir],
+    [AS_HELP_STRING([--with-noarch-pkgconfigdir], pkg_description)],,
+    [with_noarch_pkgconfigdir=]pkg_default)
+AC_SUBST([noarch_pkgconfigdir], [$with_noarch_pkgconfigdir])
+m4_popdef([pkg_default])
+m4_popdef([pkg_description])
+]) dnl PKG_NOARCH_INSTALLDIR
+
+
+# PKG_CHECK_VAR(VARIABLE, MODULE, CONFIG-VARIABLE,
+# [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
+# -------------------------------------------
+# Retrieves the value of the pkg-config variable for the given module.
+AC_DEFUN([PKG_CHECK_VAR],
+[AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl
+AC_ARG_VAR([$1], [value of $3 for $2, overriding pkg-config])dnl
+
+_PKG_CONFIG([$1], [variable="][$3]["], [$2])
+AS_VAR_COPY([$1], [pkg_cv_][$1])
+
+AS_VAR_IF([$1], [""], [$5], [$4])dnl
+])# PKG_CHECK_VAR
+
 # Copyright (C) 2002-2013 Free Software Foundation, Inc.
 #
 # This file is free software; the Free Software Foundation
@@ -263,10 +318,9 @@
 # configured tree to be moved without reconfiguration.
 
 AC_DEFUN([AM_AUX_DIR_EXPAND],
-[dnl Rely on autoconf to set up CDPATH properly.
-AC_PREREQ([2.50])dnl
-# expand $ac_aux_dir to an absolute path
-am_aux_dir=`cd $ac_aux_dir && pwd`
+[AC_REQUIRE([AC_CONFIG_AUX_DIR_DEFAULT])dnl
+# Expand $ac_aux_dir to an absolute path.
+am_aux_dir=`cd "$ac_aux_dir" && pwd`
 ])
 
 # AM_CONDITIONAL                                            -*- Autoconf -*-
@@ -733,7 +787,8 @@
 END
     AC_MSG_ERROR([Your 'rm' program is bad, sorry.])
   fi
-fi])
+fi
+])
 
 dnl Hook into '_AC_COMPILER_EXEEXT' early to learn its expansion.  Do not
 dnl add the conditional right here, as _AC_COMPILER_EXEEXT may be further
diff --git a/src/Makefile.am b/src/Makefile.am
index 4517cb2..f56d139 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,5 +1,5 @@
 bin_PROGRAMS = trisurf
-trisurf_SOURCES = general.c vertex.c bond.c triangle.c cell.c vesicle.c initial_distribution.c io.c dumpstate.c frame.c energy.c timestep.c vertexmove.c bondflip.c main.c poly.c stats.c sh.c shcomplex.c constvol.c snapshot.c
+trisurf_SOURCES = general.c vertex.c bond.c triangle.c cell.c vesicle.c initial_distribution.c io.c dumpstate.c frame.c energy.c timestep.c vertexmove.c bondflip.c main.c poly.c stats.c sh.c shcomplex.c constvol.c snapshot.c restore.c
 GITVERSION:=$(shell git --no-pager describe --tags --always --dirty)
 AM_CFLAGS = -Wall -Werror -DTS_VERSION=\"$(GITVERSION)\"
 AM_CPPFLAGS = ${libxml2_CFLAGS}
diff --git a/src/main.c b/src/main.c
index c55d30a..3407c65 100644
--- a/src/main.c
+++ b/src/main.c
@@ -15,7 +15,7 @@
 #include "sh.h"
 #include "shcomplex.h"
 #include "dumpstate.h"
-
+#include "restore.h"
 /** Entrance function to the program
   * @param argv is a number of parameters used in program call (including the program name
   * @param argc is a pointer to strings (character arrays) which holds the arguments
@@ -29,7 +29,7 @@
 	force_from_tape=0;
 	parse_args(argv,argc); // sets global variable command_line_args (defined in io.h)
 	ts_fprintf(stdout,"Starting program...\n\n");
-
+	parseDump("timestep_000000.vtu");
     if(command_line_args.dump_from_vtk[0]!=0){
 		ts_fprintf(stdout,"************************************************\n");
 		ts_fprintf(stdout,"***** Dumping vesicle from VTK points list *****\n");
diff --git a/src/restore.c b/src/restore.c
new file mode 100644
index 0000000..f7725b4
--- /dev/null
+++ b/src/restore.c
@@ -0,0 +1,92 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <libxml/xmlmemory.h>
+#include <libxml/parser.h>
+#include <general.h>
+#include <restore.h>
+#include <snapshot.h>
+#include <zlib.h>
+#include "vesicle.h"
+ts_bool parseDump(char *dumpfname) {
+	xmlDocPtr doc;
+	xmlNodePtr cur;
+	ts_vesicle *vesicle;
+
+	doc = xmlParseFile(dumpfname);
+	
+	if (doc == NULL ) {
+		fatal("Dump file could not be found or parsed. It is correct file?",1);
+	}
+	
+	cur = xmlDocGetRootElement(doc);
+	
+	if (cur == NULL) {
+		fatal("Dump file is empty.",1);
+	}
+	
+	if (xmlStrcmp(cur->name, (const xmlChar *) "VTKFile")) {
+		fatal("document of the wrong type, root node != story",1);
+	}
+	
+	cur = cur->xmlChildrenNode;
+	while (cur != NULL) {
+		if ((!xmlStrcmp(cur->name, (const xmlChar *)"trisurf"))){
+			*vesicle=parseTrisurfTag(doc, cur);
+		}
+		 
+	cur = cur->next;
+	}
+	
+	xmlFreeDoc(doc);
+	fprintf(stderr,"Restoration completed\n");
+	exit(0);
+	return TS_SUCCESS;
+}
+
+ts_vesicle *parseTrisurfTag(xmlDocPtr doc, xmlNodePtr cur){
+	fprintf(stderr,"Parsing trisurf tag\n");
+	/* base64decode */
+	size_t cLen;
+	/*size_t tLen;
+	const unsigned char test[]="Test";
+	char *cTest=base64_encode(test, 4,&tLen);
+	unsigned char *cuTest=base64_decode((char *)cTest,tLen,&tLen);
+	cuTest[tLen]=0;
+	fprintf(stderr,"%s\n",cuTest);
+	*/
+	xmlChar *b64=xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	unsigned char *compressed=base64_decode((char *)b64,strlen((char *)b64)-1,&cLen);
+	/* uncompress */
+	unsigned char *subtree=(unsigned char *)malloc(512000*sizeof(unsigned char)); /* TODO: again, the uncompressed string must not exceed this */
+	z_stream infstream;
+	infstream.zalloc = Z_NULL;
+	infstream.zfree = Z_NULL;
+	infstream.opaque = Z_NULL;
+	infstream.avail_in = (ts_uint)cLen; // size of input
+    	infstream.next_in = compressed; // input char array
+    	infstream.avail_out = (ts_uint)512000; // size of output
+    	infstream.next_out = subtree; // output char array
+     
+    	// the actual DE-compression work.
+    	inflateInit(&infstream);
+    	inflate(&infstream, Z_NO_FLUSH);
+    	inflateEnd(&infstream);	
+	fprintf(stderr,"%lu\n",cLen);
+	subtree[infstream.total_out]='\0'; //zero terminate string	
+	fprintf(stderr,"%s\n",subtree);
+	
+	free(subtree);
+	/*parse xml subtree */
+	xmlChar *nvtx, *npoly, *nfono;
+	nvtx = xmlGetProp(cur, (xmlChar *)"nvtx");
+	npoly=xmlGetProp(cur, (xmlChar *)"npoly");
+	nfono=xmlGetProp(cur, (xmlChar *)"nfono");
+	fprintf(stderr,"nvtx=%u\n",atoi((char *)nvtx));
+	ts_vesicle *vesicle=init_vesicle(atoi((char *)nvtx),10,10,10,0.1);
+	//vesicle->poly_list=init_poly_list(atoi((char *)npoly),atoi((char *)nmono), vesicle->vlist, vesicle);
+	xmlFree(nvtx);
+	xmlFree(npoly);
+	xmlFree(nfono);
+	return vesicle;
+}
diff --git a/src/restore.h b/src/restore.h
new file mode 100644
index 0000000..9118568
--- /dev/null
+++ b/src/restore.h
@@ -0,0 +1,7 @@
+#ifndef _H_RESTORE
+#define _H_RESTORE
+
+ts_bool parseDump(char *dumpfname);
+ts_vesicle *parseTrisurfTag(xmlDocPtr doc, xmlNodePtr cur);
+
+#endif
diff --git a/src/snapshot.c b/src/snapshot.c
index 50dc139..f279dd0 100644
--- a/src/snapshot.c
+++ b/src/snapshot.c
@@ -117,6 +117,7 @@
 
 /* zlib compression base64 encoded */
 /* compressed must not be pre-malloced */
+/* taken from https://gist.github.com/arq5x/5315739 */
 ts_uint ts_compress_string64(char *data, ts_uint data_len, char **compressed){
 	z_stream defstream;
 	defstream.zalloc = Z_NULL;
@@ -138,6 +139,10 @@
 	return nbase;
 }
 
+ts_uint ts_decompress_string64(char *b64, ts_uint data_len, char **decompressed){
+return TS_SUCCESS;
+
+}
 
 /* base64 encoding, taken from http://stackoverflow.com/questions/342409/how-do-i-base64-encode-decode-in-c */
 static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
@@ -214,7 +219,7 @@
         	if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
         	if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
     }
-
+	if(decoding_table !=NULL) free(decoding_table);
     return decoded_data;
 }
 

--
Gitblit v1.9.3