add worspace type
[calc.git] / workspace.c
diff --git a/workspace.c b/workspace.c
new file mode 100644 (file)
index 0000000..26bbe8a
--- /dev/null
@@ -0,0 +1,100 @@
+#include <malloc.h>
+#include <stdio.h>
+
+#include "argument.h"
+#include "debug.h"
+#include "element.h"
+#include "parser.h"
+#include "stack.h"
+#include "storage.h"
+#include "tabular.h"
+
+#include "workspace.h"
+
+/* allocate workspace*/
+
+workspace_t *alloc_ws ()
+{
+    return (workspace_t *) callocordie (1, sizeof (workspace_t));
+}
+
+/* backup workspace*/
+
+workspace_t *backup_ws (workspace_t *ws)
+{
+    ws->answer = answer;
+    ws->argument = copy_tab (argument);
+    ws->stack = copy_tab (stack);
+    ws->storage = copy_tab (storage);
+    return ws;
+}
+
+/* clean workspace */
+
+workspace_t *clean_ws (workspace_t *ws)
+{
+    ws->answer = 0;
+
+    if (ws->argument) {
+        free_tab (ws->argument);
+        ws->argument = NULL;
+    }
+
+    ws->id = 0;
+
+    if (ws->root) {
+        delelement (ws->root);
+        ws->root = NULL;
+    }
+
+    if (ws->stack) {
+        free_tab (ws->stack);
+        ws->stack = NULL;
+    }
+
+    if (ws->string) {
+        free (ws->string);
+        ws->string = NULL;
+    }
+
+    if (ws->storage) {
+        free_tab (ws->storage);
+        ws->storage = NULL;
+    }
+
+    return ws;
+}
+
+/* free workspace */
+
+void free_ws (workspace_t *ws)
+{
+    if (ws) {
+        clean_ws (ws);
+        free (ws);
+    }
+}
+
+/* restore workspace*/
+
+void restore_ws (workspace_t *ws)
+{
+    answer = ws->answer;
+
+    if (argument) {
+        free_tab (argument);
+    }
+    argument = copy_tab (ws->argument);
+
+    if (stack) {
+        free_tab (stack);
+    }
+    stack = copy_tab (ws->stack);
+
+    if (storage) {
+        free_tab (storage);
+    }
+    storage = copy_tab (ws->storage);
+}
+
+/* vim: set ts=4 sw=4 et: */