9fc4d83d522795b0bfab4c1e4043527694dd1e5c
[calc.git] / workspace.c
1 #include <malloc.h>
2 #include <stdio.h>
3
4 #include "argument.h"
5 #include "debug.h"
6 #include "element.h"
7 #include "parser.h"
8 #include "stack.h"
9 #include "storage.h"
10 #include "tabular.h"
11
12 #include "workspace.h"
13
14 /* allocate workspace*/
15
16 workspace_t *alloc_ws ()
17 {
18 return (workspace_t *) callocordie (1, sizeof (workspace_t));
19 }
20
21 /* backup workspace*/
22
23 workspace_t *backup_ws (workspace_t *ws)
24 {
25 ws->answer = answer;
26 if (ws->argument) {
27 free_tab (ws->argument);
28 }
29 ws->argument = copy_tab (argument);
30 if (ws->stack) {
31 free_tab (ws->stack);
32 }
33 ws->stack = copy_tab (stack);
34 if (ws->storage) {
35 free_tab (ws->storage);
36 }
37 ws->storage = copy_tab (storage);
38 return ws;
39 }
40
41 /* clean workspace */
42
43 workspace_t *clean_ws (workspace_t *ws)
44 {
45 ws->answer = 0;
46
47 if (ws->argument) {
48 free_tab (ws->argument);
49 ws->argument = NULL;
50 }
51
52 ws->id = 0;
53
54 if (ws->root) {
55 delelement (ws->root);
56 ws->root = NULL;
57 }
58
59 if (ws->stack) {
60 free_tab (ws->stack);
61 ws->stack = NULL;
62 }
63
64 if (ws->string) {
65 free (ws->string);
66 ws->string = NULL;
67 }
68
69 if (ws->storage) {
70 free_tab (ws->storage);
71 ws->storage = NULL;
72 }
73
74 return ws;
75 }
76
77 /* free workspace */
78
79 void free_ws (workspace_t *ws)
80 {
81 if (ws) {
82 clean_ws (ws);
83 free (ws);
84 }
85 }
86
87 /* restore workspace*/
88
89 void restore_ws (workspace_t *ws)
90 {
91 answer = ws->answer;
92
93 if (argument) {
94 free_tab (argument);
95 }
96 argument = copy_tab (ws->argument);
97
98 if (stack) {
99 free_tab (stack);
100 }
101 stack = copy_tab (ws->stack);
102
103 if (storage) {
104 free_tab (storage);
105 }
106 storage = copy_tab (ws->storage);
107 }
108
109 /* vim: set ts=4 sw=4 et: */