Xorn
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
object.cc
Go to the documentation of this file.
1 /* Copyright (C) 2013-2016 Roland Lutz
2 
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.
7 
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.
12 
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. */
16 
17 #include "internal.h"
18 #include <stdlib.h>
19 #include <algorithm>
20 #include "key_iterator.h"
21 
22 
26 {
27  return rev->obstates.find(ob) != rev->obstates.end();
28 }
29 
36 {
37  std::map<xorn_object_t, obstate *>::const_iterator i
38  = rev->obstates.find(ob);
39 
40  if (i == rev->obstates.end())
41  return xorn_obtype_none;
42 
43  return i->second->type;
44 }
45 
65  xorn_obtype_t type)
66 {
67  std::map<xorn_object_t, obstate *>::const_iterator i
68  = rev->obstates.find(ob);
69 
70  if (i == rev->obstates.end() || i->second->type != type)
71  return NULL;
72 
73  return i->second->data;
74 }
75 
94  xorn_object_t *attached_to_return,
95  unsigned int *position_return)
96 {
97  std::map<xorn_object_t, xorn_object_t>::const_iterator i
98  = rev->parent.find(ob);
99  if (i == rev->parent.end())
100  return -1;
101 
102  if (attached_to_return != NULL)
103  *attached_to_return = i->second;
104 
105  if (position_return != NULL) {
106  std::vector<xorn_object_t> const &children
107  = rev->children[i->second];
108  *position_return = find(children.begin(),
109  children.end(), ob) - children.begin();
110  }
111  return 0;
112 }
113 
114 static void dump_children(xorn_revision_t rev, xorn_object_t attached_to,
115  xorn_object_t **objects_return, size_t *count_return)
116 {
117  std::map<xorn_object_t, std::vector<xorn_object_t> >::const_iterator i
118  = rev->children.find(attached_to);
119 
120  if (i == rev->children.end())
121  return;
122 
123  for (std::vector<xorn_object_t>::const_iterator j
124  = i->second.begin(); j != i->second.end(); ++j) {
125  (*objects_return)[(*count_return)++] = *j;
126  dump_children(rev, *j, objects_return, count_return);
127  }
128 }
129 
150  xorn_revision_t rev,
151  xorn_object_t **objects_return, size_t *count_return)
152 {
153  *objects_return = (xorn_object_t *) malloc(
154  rev->obstates.size() * sizeof(xorn_object_t));
155  *count_return = 0;
156  if (*objects_return == NULL && !rev->obstates.empty())
157  return -1;
158 
159  dump_children(rev, NULL, objects_return, count_return);
160  return 0;
161 }
162 
178  xorn_object_t **objects_return, size_t *count_return)
179 {
180  if (ob != NULL && rev->obstates.find(ob) == rev->obstates.end())
181  return -1;
182  std::map<xorn_object_t, std::vector<xorn_object_t> >::const_iterator i
183  = rev->children.find(ob);
184  if (objects_return == NULL) {
185  *count_return =
186  i == rev->children.end() ? 0 : i->second.size();
187  return 0;
188  }
189  if (i == rev->children.end()) {
190  *objects_return = NULL;
191  *count_return = 0;
192  return 0;
193  }
194 
195  *objects_return = (xorn_object_t *) malloc(
196  i->second.size() * sizeof(xorn_object_t));
197  *count_return = 0;
198  if (*objects_return == NULL && !i->second.empty())
199  return -1;
200 
201  for (std::vector<xorn_object_t>::const_iterator j
202  = i->second.begin(); j != i->second.end(); ++j)
203  (*objects_return)[(*count_return)++] = *j;
204  return 0;
205 }
206 
217  xorn_object_t **objects_return, size_t *count_return)
218 {
219  *objects_return = (xorn_object_t *) malloc(
220  std::min(rev->obstates.size(),
221  sel->size()) * sizeof(xorn_object_t));
222  *count_return = 0;
223  if (*objects_return == NULL && !rev->obstates.empty() && !sel->empty())
224  return -1;
225 
226  xorn_object_t *ptr = set_intersection(
227  iterate_keys(rev->obstates.begin()),
228  iterate_keys(rev->obstates.end()),
229  sel->begin(), sel->end(), *objects_return);
230 
231  *count_return = ptr - *objects_return;
232  *objects_return = (xorn_object_t *) realloc(
233  *objects_return, *count_return * sizeof(xorn_object_t));
234  return 0;
235 }
236 
247  xorn_revision_t from_rev, xorn_revision_t to_rev,
248  xorn_object_t **objects_return, size_t *count_return)
249 {
250  *objects_return = (xorn_object_t *) malloc(
251  to_rev->obstates.size() * sizeof(xorn_object_t));
252  *count_return = 0;
253  if (*objects_return == NULL && !to_rev->obstates.empty())
254  return -1;
255 
256  xorn_object_t *ptr = set_difference(
257  iterate_keys(to_rev->obstates.begin()),
258  iterate_keys(to_rev->obstates.end()),
259  iterate_keys(from_rev->obstates.begin()),
260  iterate_keys(from_rev->obstates.end()), *objects_return);
261 
262  *count_return = ptr - *objects_return;
263  *objects_return = (xorn_object_t *) realloc(
264  *objects_return, *count_return * sizeof(xorn_object_t));
265  return 0;
266 }
267 
278  xorn_revision_t from_rev, xorn_revision_t to_rev,
279  xorn_object_t **objects_return, size_t *count_return)
280 {
281  *objects_return = (xorn_object_t *) malloc(
282  from_rev->obstates.size() * sizeof(xorn_object_t));
283  *count_return = 0;
284  if (*objects_return == NULL && !from_rev->obstates.empty())
285  return -1;
286 
287  xorn_object_t *ptr = set_difference(
288  iterate_keys(from_rev->obstates.begin()),
289  iterate_keys(from_rev->obstates.end()),
290  iterate_keys(to_rev->obstates.begin()),
291  iterate_keys(to_rev->obstates.end()), *objects_return);
292 
293  *count_return = ptr - *objects_return;
294  *objects_return = (xorn_object_t *) realloc(
295  *objects_return, *count_return * sizeof(xorn_object_t));
296  return 0;
297 }
298 
308  xorn_revision_t from_rev, xorn_revision_t to_rev,
309  xorn_object_t **objects_return, size_t *count_return)
310 {
311  *objects_return = (xorn_object_t *) malloc(
312  std::min(from_rev->obstates.size(),
313  to_rev->obstates.size()) * sizeof(xorn_object_t));
314  *count_return = 0;
315  if (*objects_return == NULL && !from_rev->obstates.empty()
316  && !to_rev->obstates.empty())
317  return -1;
318 
319  std::map<xorn_object_t, obstate *>::const_iterator i
320  = from_rev->obstates.begin();
321  std::map<xorn_object_t, obstate *>::const_iterator j
322  = to_rev->obstates.begin();
323 
324  while (i != from_rev->obstates.end() && j != to_rev->obstates.end())
325  if (i->first < j->first)
326  ++i;
327  else if (i->first > j->first)
328  ++j;
329  else {
330  if (i->second != j->second)
331  (*objects_return)[(*count_return)++] =
332  i->first;
333  ++i;
334  ++j;
335  }
336 
337  *objects_return = (xorn_object_t *) realloc(
338  *objects_return, *count_return * sizeof(xorn_object_t));
339  return 0;
340 }
int xorn_get_objects_attached_to(xorn_revision_t rev, xorn_object_t ob, xorn_object_t **objects_return, size_t *count_return)
Return a list of objects in a revision which are attached to a certain object.
Definition: object.cc:176
bool xorn_object_exists_in_revision(xorn_revision_t rev, xorn_object_t ob)
Return whether an object exists in a revision.
Definition: object.cc:25
struct xorn_revision * xorn_revision_t
Opaque type representing the contents of a file.
Definition: xornstorage.h:54
int xorn_get_objects(xorn_revision_t rev, xorn_object_t **objects_return, size_t *count_return)
Return a list of all objects in a revision.
Definition: object.cc:149
void const * xorn_get_object_data(xorn_revision_t rev, xorn_object_t ob, xorn_obtype_t type)
Get a pointer to an object's data in a given revision.
Definition: object.cc:64
int xorn_get_object_location(xorn_revision_t rev, xorn_object_t ob, xorn_object_t *attached_to_return, unsigned int *position_return)
Get the location of an object in the object structure.
Definition: object.cc:93
int xorn_get_added_objects(xorn_revision_t from_rev, xorn_revision_t to_rev, xorn_object_t **objects_return, size_t *count_return)
Return a list of objects which are in a revision but not in another.
Definition: object.cc:246
struct xorn_selection * xorn_selection_t
Opaque type representing the identity of a set of objects.
Definition: xornstorage.h:56
int xorn_get_modified_objects(xorn_revision_t from_rev, xorn_revision_t to_rev, xorn_object_t **objects_return, size_t *count_return)
Return a list of objects which exist in two revisions but have different type or data.
Definition: object.cc:307
int xorn_get_removed_objects(xorn_revision_t from_rev, xorn_revision_t to_rev, xorn_object_t **objects_return, size_t *count_return)
Return a list of objects which are in a revision but not in another.
Definition: object.cc:277
xorn_obtype_t xorn_get_object_type(xorn_revision_t rev, xorn_object_t ob)
Get the type of an object in a given revision.
Definition: object.cc:35
int xorn_get_selected_objects(xorn_revision_t rev, xorn_selection_t sel, xorn_object_t **objects_return, size_t *count_return)
Return a list of objects which are in a revision as well as in a selection.
Definition: object.cc:215
struct xorn_object * xorn_object_t
Opaque type representing the identity of an object.
Definition: xornstorage.h:55
xorn_obtype_t
Object types, each corresponding to a data structure.
Definition: xornstorage.h:27