Added check for Unix specific headers and harmonize with Win32 build.
[pazpar2-moved-to-github.git] / src / record.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2008 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 #include <string.h>
21
22 #include <yaz/yaz-util.h>
23 #include <yaz/nmem.h>
24
25 #if HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 //#define CONFIG_NOEXTERNS
30 #include "pazpar2_config.h"
31 #include "record.h"
32
33
34
35 union data_types * data_types_assign(NMEM nmem, 
36                                      union data_types ** data1, 
37                                      union data_types data2)
38 {
39     // assert(nmem);
40
41     if (!data1)
42         return 0;
43
44     if (!*data1){
45         if (!nmem)
46             return 0;
47         else
48             *data1  = nmem_malloc(nmem, sizeof(union data_types));
49     }
50     
51     **data1 = data2;
52     return *data1;
53 }
54
55
56 struct record * record_create(NMEM nmem, int num_metadata, int num_sortkeys,
57                               struct client *client, int position)
58 {
59     struct record * record = 0;
60     int i = 0;
61     
62     // assert(nmem);
63
64     record = nmem_malloc(nmem, sizeof(struct record));
65
66     record->next = 0;
67     record->client = client;
68
69     record->metadata 
70         = nmem_malloc(nmem, 
71                       sizeof(struct record_metadata*) * num_metadata);
72     for (i = 0; i < num_metadata; i++)
73         record->metadata[i] = 0;
74     
75     record->sortkeys  
76         = nmem_malloc(nmem, 
77                       sizeof(union data_types*) * num_sortkeys);
78     for (i = 0; i < num_sortkeys; i++)
79         record->sortkeys[i] = 0;
80
81     record->position = position;
82     
83     return record;
84 }
85
86 struct record_metadata * record_metadata_create(NMEM nmem)
87 {
88     struct record_metadata * rec_md 
89         = nmem_malloc(nmem, sizeof(struct record_metadata));
90     rec_md->next = 0;
91     return rec_md;
92 }
93
94
95 struct record_metadata * record_metadata_insert(NMEM nmem, 
96                                                 struct record_metadata ** rmd,
97                                                 union data_types data)
98 {
99     struct record_metadata * tmp_rmd = 0;
100     // assert(nmem);
101
102     if(!rmd)
103         return 0;
104
105     // construct new record_metadata
106     tmp_rmd  = nmem_malloc(nmem, sizeof(struct record_metadata));
107     tmp_rmd->data = data;
108
109
110     // insert in *rmd's place, moving *rmd one down the list
111     tmp_rmd->next = *rmd;
112     *rmd = tmp_rmd;
113
114     return *rmd;
115 }
116
117 struct record_metadata * record_add_metadata_field_id(NMEM nmem, 
118                                                      struct record * record,
119                                                      int field_id, 
120                                                      union data_types data)
121 {
122     if (field_id < 0 || !record || !record->metadata)
123         return 0;
124
125     return record_metadata_insert(nmem, &(record->metadata[field_id]), data);
126 }
127
128
129 struct record_metadata * record_add_metadata(NMEM nmem, 
130                                              struct record * record,
131                                              struct conf_service * service,
132                                              const char * name,
133                                              union data_types data)
134 {
135     int field_id = 0;
136
137     if (!record || !record->metadata || !service || !name)  
138         return 0;
139     
140     field_id = conf_service_metadata_field_id(service, name);
141
142     if (-1 == field_id)
143         return 0;
144     
145     return record_metadata_insert(nmem, &(record->metadata[field_id]), data);
146 }
147
148
149
150
151
152
153 union data_types * record_assign_sortkey_field_id(NMEM nmem, 
154                                                struct record * record,
155                                                int field_id, 
156                                                union data_types data)
157 {
158     if (field_id < 0 || !record || !record->sortkeys)
159         return 0;
160
161     return data_types_assign(nmem, &(record->sortkeys[field_id]), data);
162 }
163
164
165
166 union data_types * record_assign_sortkey(NMEM nmem, 
167                                       struct record * record,
168                                       struct conf_service * service,
169                                       const char * name,
170                                       union data_types data)
171 {
172     int field_id = 0;
173
174     if (!record || !service || !name)  
175         return 0;
176     
177     field_id = conf_service_sortkey_field_id(service, name);
178
179     if (!(-1 < field_id) || !(field_id < service->num_sortkeys))
180         return 0;
181
182     return record_assign_sortkey_field_id(nmem, record, field_id, data);
183 }
184
185
186
187 /*
188  * Local variables:
189  * c-basic-offset: 4
190  * indent-tabs-mode: nil
191  * End:
192  * vim: shiftwidth=4 tabstop=8 expandtab
193  */