Command search takes limit parameter
[pazpar2-moved-to-github.git] / src / facet_limit.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2011 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_create(const char *param)
43 {
44     int i;
45     NMEM nmem = nmem_create();
46     facet_limits_t fl = nmem_malloc(nmem, sizeof(*fl));
47     fl->nmem = nmem;
48     fl->num = 0;
49     fl->darray = 0;
50     if (param)
51         nmem_strsplit_escape(fl->nmem, ",", param, &fl->darray,
52                              &fl->num, 1, '\\');
53     /* replace = with \0 .. for each item */
54     for (i = 0; i < fl->num; i++)
55     {
56         char *cp = strchr(fl->darray[i], '=');
57         if (!cp)
58         {
59             facet_limits_destroy(fl);
60             return 0;
61         }
62         *cp = '\0';
63     }
64     return fl;
65 }
66
67 int facet_limits_num(facet_limits_t fl)
68 {
69     return fl->num;
70 }
71
72 const char *facet_limits_get(facet_limits_t fl, int idx, const char **value)
73 {
74     if (idx >= fl->num || idx < 0)
75         return 0;
76     *value = fl->darray[idx] + strlen(fl->darray[idx]) + 1;
77     return fl->darray[idx];
78 }
79
80 void facet_limits_destroy(facet_limits_t fl)
81 {
82     if (fl)
83         nmem_destroy(fl->nmem);
84 }
85
86 /*
87  * Local variables:
88  * c-basic-offset: 4
89  * c-file-style: "Stroustrup"
90  * indent-tabs-mode: nil
91  * End:
92  * vim: shiftwidth=4 tabstop=8 expandtab
93  */
94