netlist: Fix component refdes mangling
[geda-gaf.git] / tests / storage / snippets / example.c
blobbf64c3b4f4281f5d6f75c1c279017750436e6b46
1 /* Copyright (C) 2013-2020 Roland Lutz
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software Foundation,
15 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17 #include <stdio.h>
18 #include <string.h>
19 #include <xornstorage.h>
22 int main(void)
24 xorn_revision_t rev0, rev1, rev2, rev3;
25 xorn_object_t line, box, circle;
26 xorn_error_t err;
28 struct xornsch_line line_data;
29 struct xornsch_box box_data;
30 struct xornsch_circle circle_data;
31 struct xornsch_net net_data;
34 /* Create an empty revision and make it read-only. */
36 rev0 = xorn_new_revision(NULL);
38 if (rev0 == NULL)
39 goto error0;
41 xorn_finalize_revision(rev0);
44 /* Copy the revision */
46 /* You could omit the previous step and just create an empty
47 revision if you don't need to refer to the empty state later. */
49 rev1 = xorn_new_revision(rev0);
51 if (rev1 == NULL)
52 goto error1;
54 /* Prepare a xornsch_line structure */
56 memset(&line_data, 0, sizeof line_data);
57 line_data.pos.x = 0;
58 line_data.pos.y = 1;
59 line_data.size.x = 3;
60 line_data.size.y = 2;
61 line_data.color = 3;
62 line_data.line.width = 1;
64 /* Add a line object with this data to the revision */
66 line = xornsch_add_line(rev1, &line_data, &err);
68 if (line == NULL)
69 goto error2;
71 /* Finalize the revision */
73 xorn_finalize_revision(rev1);
76 /* Create a copy of the last revision, add a box and a circle
77 to it, and finalize it */
79 rev2 = xorn_new_revision(rev1);
80 if (rev2 == NULL)
81 goto error2;
83 memset(&box_data, 0, sizeof box_data);
84 box_data.pos.x = 1;
85 box_data.pos.y = 1;
86 box_data.size.x = 2;
87 box_data.size.y = 2;
88 box_data.color = 3;
89 box_data.line.width = 1;
91 box = xornsch_add_box(rev2, &box_data, &err);
92 if (box == NULL)
93 goto error3;
95 memset(&circle_data, 0, sizeof circle_data);
96 circle_data.pos.x = -1;
97 circle_data.pos.y = -1;
98 circle_data.radius = 2;
99 circle_data.color = 3;
100 circle_data.line.width = 1;
101 circle_data.fill.type = 1;
103 circle = xornsch_add_circle(rev2, &circle_data, &err);
104 if (circle == NULL)
105 goto error3;
107 xorn_finalize_revision(rev2);
110 /* Create a copy of the last revision, set the line's object
111 type to net and change its color, delete the box, and
112 finalize the revision */
114 rev3 = xorn_new_revision(rev2);
115 if (rev3 == NULL)
116 goto error3;
118 memset(&net_data, 0, sizeof net_data);
119 net_data.pos.x = 0;
120 net_data.pos.y = 1;
121 net_data.size.x = 3;
122 net_data.size.y = 2;
123 net_data.color = 4;
125 if (xornsch_set_net_data(rev3, line, &net_data, &err) == -1)
126 goto error4;
128 if (xorn_delete_object(rev3, box, &err) == -1)
129 goto error4;
131 xorn_finalize_revision(rev3);
134 /* ... you could do something with these revisions now ... */
137 /* Free the revisions (the order doesn't matter) */
139 xorn_free_revision(rev3);
140 xorn_free_revision(rev2);
141 xorn_free_revision(rev1);
142 xorn_free_revision(rev0);
144 /* No need to free objects */
146 return 0;
148 error4:
149 xorn_free_revision(rev3);
150 error3:
151 xorn_free_revision(rev2);
152 error2:
153 xorn_free_revision(rev1);
154 error1:
155 xorn_free_revision(rev0);
156 error0:
157 fprintf(stderr, "Out of memory\n");
158 return 1;