Happy new year
[pazpar2-moved-to-github.git] / src / facet_limit.c
1 /* This file is part of Pazpar2.
2    Copyright (C) Index Data
3
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 /** \file facet_limit.c
21     \brief Parse facet limit
22 */
23
24 #if HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <string.h>
29 #include <assert.h>
30
31 #include <yaz/yaz-version.h>
32 #include <yaz/nmem.h>
33
34 #include "facet_limit.h"
35
36 struct facet_limits {
37     NMEM nmem;
38     int num;
39     char **darray;
40 };
41
42 facet_limits_t facet_limits_dup(facet_limits_t fl)
43 {
44     int i;
45     NMEM nmem = nmem_create();
46     facet_limits_t fn = nmem_malloc(nmem, sizeof(*fn));
47     fn->nmem = nmem;
48     fn->num = fl->num;
49     fn->darray = 0;
50     if (fl->num)
51     {
52         fn->darray = nmem_malloc(nmem, fn->num * sizeof(*fn->darray));
53         for (i = 0; i < fn->num; i++)
54         {
55             const char *src = fl->darray[i];
56             size_t sz = strlen(src) + 2 + strlen(src + strlen(src) + 1);
57             fn->darray[i] = nmem_malloc(nmem, sz);
58             memcpy(fn->darray[i], src, sz);
59         }
60     }
61     return fn;
62 }
63
64 facet_limits_t facet_limits_create(const char *param)
65 {
66     int i;
67     NMEM nmem = nmem_create();
68     facet_limits_t fl = nmem_malloc(nmem, sizeof(*fl));
69     fl->nmem = nmem;
70     fl->num = 0;
71     fl->darray = 0;
72     if (param)
73         nmem_strsplit_escape2(fl->nmem, ",", param, &fl->darray,
74                               &fl->num, 1, '\\', 0);
75     /* replace = with \0 .. for each item */
76     for (i = 0; i < fl->num; i++)
77     {
78         char *cp = strchr(fl->darray[i], '=');
79         if (!cp)
80         {
81             facet_limits_destroy(fl);
82             return 0;
83         }
84         *cp = '\0';
85     }
86     return fl;
87 }
88
89 int facet_limits_num(facet_limits_t fl)
90 {
91     return fl->num;
92 }
93
94 const char *facet_limits_get(facet_limits_t fl, int idx, const char **value)
95 {
96     if (idx >= fl->num || idx < 0)
97         return 0;
98     *value = fl->darray[idx] + strlen(fl->darray[idx]) + 1;
99     return fl->darray[idx];
100 }
101
102 void facet_limits_destroy(facet_limits_t fl)
103 {
104     if (fl)
105         nmem_destroy(fl->nmem);
106 }
107
108 /*
109  * Local variables:
110  * c-basic-offset: 4
111  * c-file-style: "Stroustrup"
112  * indent-tabs-mode: nil
113  * End:
114  * vim: shiftwidth=4 tabstop=8 expandtab
115  */
116