~ubuntu-branches/ubuntu/trusty/geis/trusty

« back to all changes in this revision

Viewing changes to libgeis/geis_filterable.c

  • Committer: Package Import Robot
  • Author(s): Chase Douglas
  • Date: 2012-07-30 08:51:42 UTC
  • Revision ID: package-import@ubuntu.com-20120730085142-jrc33ygjvt0ob1wl
Tags: upstream-2.2.11
ImportĀ upstreamĀ versionĀ 2.2.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @file geis_filterable.c
 
3
 * @brief internal Geis filterable entities implementation
 
4
 */
 
5
/*
 
6
 * Copyright 2011 Canonical Ltd.
 
7
 *
 
8
 * This library is free software; you can redistribute it and/or modify it under
 
9
 * the terms of the GNU Lesser General Public License as published by the Free
 
10
 * Software Foundation; either version 3 of the License, or (at your option) any
 
11
 * later version.
 
12
 *
 
13
 * This library is distributed in the hope that it will be useful, but WITHOUT
 
14
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
15
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 
16
 * details.
 
17
 *
 
18
 * You should have received a copy of the GNU Lesser General Public License
 
19
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 
20
 * 51 Franklin St, Fifth Floor#include "geis_filterable.h"
 
21
 */
 
22
#include "geis_config.h"
 
23
#include "geis_filterable.h"
 
24
 
 
25
#include "geis_logging.h"
 
26
#include <string.h>
 
27
 
 
28
 
 
29
/*
 
30
 * An internal struct to collect filterable attributes for facilities.
 
31
 */
 
32
struct FilterableAttributeBag
 
33
{
 
34
  GeisFilterableAttribute store;
 
35
  GeisSize                size;
 
36
  GeisSize                count;
 
37
};
 
38
 
 
39
 
 
40
/*
 
41
 * Constructs a new filterable attribute bag.
 
42
 */
 
43
FilterableAttributeBag
 
44
geis_filterable_attribute_bag_new()
 
45
{
 
46
  FilterableAttributeBag bag = calloc(1, sizeof(struct FilterableAttributeBag));
 
47
  if (!bag)
 
48
  {
 
49
    geis_error("failed to allocate filterable attribute bag");
 
50
    goto final_exit;
 
51
  }
 
52
 
 
53
  bag->size = 2;
 
54
  bag->count = 0;
 
55
 
 
56
  bag->store = calloc(1, sizeof(struct GeisFilterableAttribute));
 
57
  if (!bag)
 
58
  {
 
59
    geis_error("failed to allocate filterable attribute bag store");
 
60
    goto unwind_bag;
 
61
  }
 
62
  goto final_exit;
 
63
 
 
64
unwind_bag:
 
65
  free(bag);
 
66
  bag = NULL;
 
67
final_exit:
 
68
  return bag;
 
69
}
 
70
 
 
71
 
 
72
/*
 
73
 * Destroys a filterable attribute bag.
 
74
 */
 
75
void
 
76
geis_filterable_attribute_bag_delete(FilterableAttributeBag bag)
 
77
{
 
78
  if (bag)
 
79
  {
 
80
    GeisSize i;
 
81
    for (i = 0; i < bag->count; ++i)
 
82
    {
 
83
      free((char *)bag->store[i].name);
 
84
    }
 
85
    free(bag->store);
 
86
  }
 
87
  free(bag);
 
88
}
 
89
 
 
90
 
 
91
void
 
92
geis_filterable_attribute_copy(GeisFilterableAttribute src,
 
93
                               GeisFilterableAttribute dst)
 
94
{
 
95
  dst->name = strdup(src->name);
 
96
  dst->type = src->type;
 
97
  dst->add_term_callback = src->add_term_callback;
 
98
  dst->add_term_context  = src->add_term_context;
 
99
}
 
100
 
 
101
 
 
102
GeisFilterableAttributeBagIter
 
103
geis_filterable_attribute_bag_begin(FilterableAttributeBag bag)
 
104
{
 
105
  if (bag->count)
 
106
    return &bag->store[0];
 
107
  return geis_filterable_attribute_bag_end(bag);
 
108
}
 
109
 
 
110
 
 
111
GeisFilterableAttributeBagIter
 
112
geis_filterable_attribute_bag_end(FilterableAttributeBag bag GEIS_UNUSED)
 
113
{
 
114
  return NULL;
 
115
}
 
116
 
 
117
 
 
118
GeisFilterableAttributeBagIter
 
119
geis_filterable_attribute_bag_next(FilterableAttributeBag         bag,
 
120
                                   GeisFilterableAttributeBagIter iter)
 
121
{
 
122
  if (iter < bag->store + bag->count - 1)
 
123
    return ++iter;
 
124
  return geis_filterable_attribute_bag_end(bag);
 
125
}
 
126
 
 
127
 
 
128
void
 
129
geis_filterable_attribute_bag_insert(FilterableAttributeBag  bag,
 
130
                                     GeisFilterableAttribute fa)
 
131
{
 
132
  GeisSize new_count = bag->count + 1;
 
133
  if (new_count >= bag->size)
 
134
  {
 
135
    GeisSize new_size = bag->size * 2;
 
136
    GeisSize allocation_size = new_size * sizeof(struct GeisFilterableAttribute);
 
137
    GeisFilterableAttribute new_store = realloc(bag->store, allocation_size);
 
138
    if (!new_store)
 
139
    {
 
140
      geis_error("failed to reallocate filterable attribute bag store");
 
141
    }
 
142
    else
 
143
    {
 
144
      bag->store = new_store;
 
145
      bag->size = new_size;
 
146
    }
 
147
  }
 
148
  geis_filterable_attribute_copy(fa, &bag->store[bag->count]);
 
149
  bag->count = new_count;
 
150
}
 
151