File: cpl_pluginlist.c

package info (click to toggle)
cpl 7.3.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 15,396 kB
  • sloc: ansic: 142,902; javascript: 6,382; sh: 4,452; makefile: 661
file content (510 lines) | stat: -rw-r--r-- 12,224 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
 * This file is part of the ESO Common Pipeline Library
 * Copyright (C) 2001-2022 European Southern Observatory
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>

#include <cxlist.h>
#include <cxmemory.h>
#include <cxmessages.h>

#include "cpl_plugin.h"
#include "cpl_pluginlist.h"
#include "cpl_error_impl.h"


/**
 * @defgroup cpl_pluginlist  Plugin List
 *
 * This module implements a list container for plugin objects and provides
 * the facilities to query, to traverse and to update the container. The
 * purpose of this container is to be able to store references to available
 * plugins and to handle sets of plugins as a whole.
 *
 * Since the plugin list just stores pointers to @c cpl_plugin, a plugin
 * list may contain plugins of different kind at the same time, because
 * all context specific plugins inherit the @c cpl_plugin type (see
 * @ref cpl_plugin).
 *
 * @par Synopsis:
 * @code
 *   #include <cpl_pluginlist.h>
 * @endcode
 */

/**@{*/

struct _cpl_pluginlist_
{
    cx_list *data;
    cx_list_iterator search;
};


/**
 * @brief
 *   Creates an empty plugin list.
 *
 * @return
 *   The newly created plugin list, or @c NULL if it could not  be created.
 *
 * The function allocates memory for a plugin list object and initialises it
 * to be empty.
 */

cpl_pluginlist *
cpl_pluginlist_new(void)
{
    cpl_pluginlist *self;


    self = cx_malloc(sizeof *self);

    if (self == NULL) {
        return NULL;
    }

    self->data = cx_list_new();

    if (self->data == NULL) {
        cx_free(self);
        return NULL;
    }

    self->search = NULL;

    return self;
}


/**
 * @brief
 *    Delete a plugin list.
 *
 * @param self  The plugin list to delete.
 *
 * @return Nothing.
 *
 * The function deletes the plugin list @em self and destroys all plugins the
 * list potentially contains. If @em self is @c NULL, nothing is done and no error is set.
 */

void
cpl_pluginlist_delete(cpl_pluginlist *self)
{
    if (self) {
        if (self->data) {
            cx_list_destroy(self->data, (cx_free_func)cpl_plugin_delete);
            self->data = NULL;
        }

        self->search = NULL;

        cx_free(self);
        self = NULL;
    }

    return;
}


/**
 * @brief
 *   Get the current size of a plugin list.
 *
 * @param self  A plugin list.
 *
 * @return
 *   The plugin list's current size, or 0 if the list is empty. The function
 *   returns 0 if an error occurs and sets an appropriate error code.
 *
 * @error
 *   <table class="ec" align="center">
 *     <tr>
 *       <td class="ecl">CPL_ERROR_NULL_INPUT</td>
 *       <td class="ecr">
 *         The parameter <i>self</i> is a <tt>NULL</tt> pointer.
 *       </td>
 *     </tr>
 *   </table>
 * @enderror
 *
 * The function reports the current number of plugins stored in the plugin
 * list @em self. If @em self does not point to a valid plugin list the
 * function returns 0.
 */

int
cpl_pluginlist_get_size(cpl_pluginlist *self)
{
    if (self == NULL) {
        cpl_error_set_(CPL_ERROR_NULL_INPUT);
        return 0;
    }

    cx_assert(self->data != NULL);

    return (int)cx_list_size(self->data);
}


/**
 * @brief
 *   Append a plugin to a plugin list.
 *
 * @param self    A plugin list.
 * @param plugin  The plugin to append.
 *
 * @return
 *   The function returns @c CPL_ERROR_NONE on success or a CPL error
 *   code otherwise.
 *
 * @error
 *   <table class="ec" align="center">
 *     <tr>
 *       <td class="ecl">CPL_ERROR_NULL_INPUT</td>
 *       <td class="ecr">
 *         The parameter <i>self</i> or <i>plugin</i> is a <tt>NULL</tt>
 *         pointer.
 *       </td>
 *     </tr>
 *   </table>
 * @enderror
 *
 * The plugin @em plugin is inserted into the plugin list @em self after the
 * last element.
 *
 * If @em self does not point to a valid plugin list, or if @em plugin is not
 * a valid pointer the function returns immediately.
 */

cpl_error_code
cpl_pluginlist_append(cpl_pluginlist *self, const cpl_plugin *plugin)
{
    if (self == NULL || plugin == NULL) {
        return cpl_error_set_(CPL_ERROR_NULL_INPUT);
    }

    cx_list_push_back(self->data, (cxptr)plugin);

    return CPL_ERROR_NONE;
}


/**
 * @brief
 *   Prepend a plugin to a plugin list.
 *
 * @param self    A plugin list.
 * @param plugin  The plugin to prepend.
 *
 * @return
 *   The function returns @c CPL_ERROR_NONE on success or a CPL error
 *   code otherwise.
 *
 * @error
 *   <table class="ec" align="center">
 *     <tr>
 *       <td class="ecl">CPL_ERROR_NULL_INPUT</td>
 *       <td class="ecr">
 *         The parameter <i>self</i> or <i>plugin</i> is a <tt>NULL</tt>
 *         pointer.
 *       </td>
 *     </tr>
 *   </table>
 * @enderror
 *
 * The plugin @em plugin is inserted into the plugin list @em self before the
 * first element.
 *
 * If @em self does not point to a valid plugin list, or if @em plugin is not
 * a valid pointer the function returns immediately.
 */

cpl_error_code
cpl_pluginlist_prepend(cpl_pluginlist *self, const cpl_plugin *plugin)
{
    if (self == NULL || plugin == NULL) {
        return cpl_error_set_(CPL_ERROR_NULL_INPUT);
    }

    cx_list_push_front(self->data, (cxptr)plugin);

    return CPL_ERROR_NONE;
}


/**
 * @brief
 *   Get the first plugin of a plugin list.
 *
 * @param self  A plugin list.
 *
 * @return
 *   The first plugin stored in the plugin list @em self, or @c NULL
 *   if the list is empty. The function returns @c NULL if an error
 *   occurs and sets an appropriate error code.
 *
 * @error
 *   <table class="ec" align="center">
 *     <tr>
 *       <td class="ecl">CPL_ERROR_NULL_INPUT</td>
 *       <td class="ecr">
 *         The parameter <i>self</i> is a <tt>NULL</tt> pointer.
 *       </td>
 *     </tr>
 *   </table>
 * @enderror
 *
 * The function returns a handle to the first plugin stored in the @em self.
 */


cpl_plugin *
cpl_pluginlist_get_first(cpl_pluginlist *self)
{
    if (self == NULL) {
        cpl_error_set_(CPL_ERROR_NULL_INPUT);
        return NULL;
    }

    cx_assert(self->data != NULL);

    self->search = cx_list_begin(self->data);

    if (self->search == cx_list_end(self->data)) {
        self->search = NULL;
        return NULL;
    }

    return (cpl_plugin *)cx_list_get(self->data, self->search);
}


/**
 * @brief
 *   Get the next plugin from a plugin list.
 *
 * @param self  A plugin list.
 *
 * @return
 *   The function returns the next plugin in the list, or @c NULL if
 *   the end of the list has been reached. The function returns @c NULL
 *   if an error occurs and sets an appropriate error code.
 *
 * @error
 *   <table class="ec" align="center">
 *     <tr>
 *       <td class="ecl">CPL_ERROR_NULL_INPUT</td>
 *       <td class="ecr">
 *         The parameter <i>self</i> is a <tt>NULL</tt> pointer.
 *       </td>
 *     </tr>
 *     <tr>
 *       <td class="ecl">CPL_ERROR_ILLEGAL_INPUT</td>
 *       <td class="ecr">
 *         The function was called without initialising the plugin list
 *         <i>self</i> by calling @b cpl_pluginlist_first().
 *       </td>
 *     </tr>
 *   </table>
 * @enderror
 *
 * The function returns the next plugin in @em self. To find the next plugin,
 * the plugin list caches the position of the most recently obtained plugin.
 * This requires a call to @b cpl_pluginlist_get_first() prior to calling this
 * function in order to properly initialise the internal cache.
 *
 * If the end of @em self has been reached the internal cache is reset to
 * the first plugin in the list.
 */

cpl_plugin *
cpl_pluginlist_get_next(cpl_pluginlist *self)
{
    cx_list_iterator next;


    if (self == NULL) {
        cpl_error_set_(CPL_ERROR_NULL_INPUT);
        return NULL;
    }

    if (self->search == NULL) {
        cpl_error_set_(CPL_ERROR_ILLEGAL_INPUT);
        return NULL;
    }

    cx_assert(self->data != NULL);

    next = cx_list_next(self->data, self->search);

    if (next == cx_list_end(self->data)) {
        self->search = cx_list_begin(self->data);
        return NULL;
    }

    self->search = next;

    return (cpl_plugin *)cx_list_get(self->data, self->search);
}


/**
 * @brief
 *   Get the last plugin of a plugin list.
 *
 * @param self  A plugin list.
 *
 * @return The last plugin stored in the plugin list @em self, or @c NULL
 *   if the list is empty. The function returns @c NULL if an error
 *   occurs and sets an appropriate error code.
 *
 * @error
 *   <table class="ec" align="center">
 *     <tr>
 *       <td class="ecl">CPL_ERROR_NULL_INPUT</td>
 *       <td class="ecr">
 *         The parameter <i>self</i> is a <tt>NULL</tt> pointer.
 *       </td>
 *     </tr>
 *   </table>
 * @enderror
 *
 * The function returns a pointer to the last plugin stored in the plugin
 * list @em self.
 */

cpl_plugin *
cpl_pluginlist_get_last(cpl_pluginlist *self)
{
    if (self == NULL) {
        cpl_error_set_(CPL_ERROR_NULL_INPUT);
        return NULL;
    }

    cx_assert(self->data != NULL);

    if (cx_list_empty(self->data)) {
        return NULL;
    }

    return (cpl_plugin *)cx_list_back(self->data);
}


/**
 * @brief
 *   Find a plugin with a given name in a plugin list.
 *
 * @param self  The plugin list to query.
 * @param name  The plugin's unique name to look for.
 *
 * @return
 *   The first plugin with the given name @em name, or @c NULL if it
 *   no plugin with this name could be found. The function returns
 *   @c NULL if an error occurs and sets an appropriate error code.
 *
 * @error
 *   <table class="ec" align="center">
 *     <tr>
 *       <td class="ecl">CPL_ERROR_NULL_INPUT</td>
 *       <td class="ecr">
 *         The parameter <i>self</i> or <i>name</i> is a <tt>NULL</tt>
 *         pointer.
 *       </td>
 *     </tr>
 *   </table>
 * @enderror
 *
 * This function searches the plugin list @em self for a plugin with the
 * unique name @em name and returns a pointer to the first one found.
 * If no plugin with the given name is found the function returns @c NULL.
 */

cpl_plugin *
cpl_pluginlist_find(cpl_pluginlist *self, const char *name)
{
    cx_list_iterator first, last;


    if (self == NULL) {
        cpl_error_set_(CPL_ERROR_NULL_INPUT);
        return NULL;
    }

    cx_assert(self->data != NULL);

    first = cx_list_begin(self->data);
    last = cx_list_end(self->data);

    while (first != last) {
        cpl_plugin *p = cx_list_get(self->data, first);

        if (strcmp(cpl_plugin_get_name(p), name) == 0) {
            return p;
        }
        first = cx_list_next(self->data, first);
    }

    return NULL;
}


/**
 * @brief
 *   Dump the contents of a plugin list to the given stream.
 *
 * @param self    The plugin list.
 * @param stream  The output stream to use.
 *
 * @return Nothing.
 *
 * The function dumps the debugging information for each plugin found in
 * the plugin list @em self to the output stream @em stream. The debugging
 * information for each individual plugin is dumped using
 * @b cpl_plugin_dump(). If @em self is @c NULL the function does nothing.
 *
 * @see cpl_plugin_dump()
 */

void
cpl_pluginlist_dump(const cpl_pluginlist *self, FILE *stream)
{
    if (self != NULL) {
        cx_list_const_iterator pos = cx_list_begin(self->data);

        if (stream == NULL) {
            stream = stdout;
        }

        while (pos != cx_list_end(self->data)) {
            const cpl_plugin *p = cx_list_get(self->data, pos);

            cpl_plugin_dump(p, stream);
            pos = cx_list_next(self->data, pos);
        }
    }

    return;
}
/**@}*/