protected from dereferencing of empty pointer
[pazpar2-moved-to-github.git] / src / record.c
1 /* $Id: record.c,v 1.6 2007-04-24 22:19:19 marc Exp $
2    Copyright (c) 2006-2007, Index Data.
3
4 This file is part of Pazpar2.
5
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 /* $Id: record.c,v 1.6 2007-04-24 22:19:19 marc Exp $ */
23
24
25 #include <string.h>
26
27 #include <yaz/yaz-util.h>
28 #include <yaz/nmem.h>
29
30 #if HAVE_CONFIG_H
31 #include <cconfig.h>
32 #endif
33
34 //#define CONFIG_NOEXTERNS
35 #include "config.h"
36 #include "record.h"
37
38
39
40 union data_types * data_types_assign(NMEM nmem, 
41                                      union data_types ** data1, 
42                                      union data_types data2)
43 {
44     // assert(nmem);
45
46     if (!data1)
47         return 0;
48
49     if (!*data1){
50         if (!nmem)
51             return 0;
52         else
53             *data1  = nmem_malloc(nmem, sizeof(union data_types));
54     }
55     
56     **data1 = data2;
57     return *data1;
58 }
59
60
61 struct record * record_create(NMEM nmem, int num_metadata, int num_sortkeys)
62 {
63     struct record * record = 0;
64     int i = 0;
65     
66     // assert(nmem);
67
68     record = nmem_malloc(nmem, sizeof(struct record));
69
70     record->next = 0;
71     // which client should I use for record->client = cl;  ??
72     record->client = 0;
73
74     record->metadata 
75         = nmem_malloc(nmem, 
76                       sizeof(struct record_metadata*) * num_metadata);
77     for (i = 0; i < num_metadata; i++)
78         record->metadata[i] = 0;
79     
80     record->sortkeys  
81         = nmem_malloc(nmem, 
82                       sizeof(union data_types*) * num_sortkeys);
83     for (i = 0; i < num_sortkeys; i++)
84         record->sortkeys[i] = 0;
85     
86     return record;
87 }
88
89
90 struct record_metadata * record_metadata_insert(NMEM nmem, 
91                                                 struct record_metadata ** rmd,
92                                                 union data_types data)
93 {
94     struct record_metadata * tmp_rmd = 0;
95     // assert(nmem);
96
97     if(!rmd)
98         return 0;
99
100     // construct new record_metadata
101     tmp_rmd  = nmem_malloc(nmem, sizeof(struct record_metadata));
102     tmp_rmd->data = data;
103
104
105     // insert in *rmd's place, moving *rmd one down the list
106     tmp_rmd->next = *rmd;
107     *rmd = tmp_rmd;
108
109     return *rmd;
110 }
111
112 struct record_metadata * record_add_metadata_field_id(NMEM nmem, 
113                                                      struct record * record,
114                                                      int field_id, 
115                                                      union data_types data)
116 {
117     if (field_id < 0 || !record || !record->metadata)
118         return 0;
119
120     return record_metadata_insert(nmem, &(record->metadata[field_id]), data);
121 }
122
123
124 struct record_metadata * record_add_metadata(NMEM nmem, 
125                                              struct record * record,
126                                              struct conf_service * service,
127                                              const char * name,
128                                              union data_types data)
129 {
130     int field_id = 0;
131
132     if (!record || !record->metadata || !service || !name)  
133         return 0;
134     
135     field_id = conf_service_metadata_field_id(service, name);
136
137     if (-1 == field_id)
138         return 0;
139     
140     return record_metadata_insert(nmem, &(record->metadata[field_id]), data);
141 }
142
143
144
145
146
147
148 union data_types * record_assign_sortkey_field_id(NMEM nmem, 
149                                                struct record * record,
150                                                int field_id, 
151                                                union data_types data)
152 {
153     if (field_id < 0 || !record || !record->sortkeys)
154         return 0;
155
156     return data_types_assign(nmem, &(record->sortkeys[field_id]), data);
157 }
158
159
160
161 union data_types * record_assign_sortkey(NMEM nmem, 
162                                       struct record * record,
163                                       struct conf_service * service,
164                                       const char * name,
165                                       union data_types data)
166 {
167     int field_id = 0;
168
169     if (!record || !service || !name)  
170         return 0;
171     
172     field_id = conf_service_sortkey_field_id(service, name);
173
174     if (!(-1 < field_id) || !(field_id < service->num_sortkeys))
175         return 0;
176
177     return record_assign_sortkey_field_id(nmem, record, field_id, data);
178 }
179
180
181
182 /*
183  * Local variables:
184  * c-basic-offset: 4
185  * indent-tabs-mode: nil
186  * End:
187  * vim: shiftwidth=4 tabstop=8 expandtab
188  */