more records constructor work, no yet finished
[pazpar2-moved-to-github.git] / src / record.c
1 /* $Id: record.c,v 1.2 2007-04-23 08:48:50 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.2 2007-04-23 08:48:50 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 struct record * record_create(NMEM nmem, int num_metadata, int num_sortkeys)
40 {
41     struct record * record = 0;
42     
43     // assert(nmem);
44
45     record = nmem_malloc(nmem, sizeof(struct record));
46
47     record->next = 0;
48     // which client should I use for record->client = cl;  ??
49     record->client = 0;
50
51     record->metadata 
52         = nmem_malloc(nmem, 
53                       sizeof(struct record_metadata*) * num_metadata);
54     memset(record->metadata, 0, 
55            sizeof(struct record_metadata*) * num_metadata);
56     
57     record->sortkeys  
58         = nmem_malloc(nmem, 
59                       sizeof(union data_types*) * num_sortkeys);
60     memset(record->metadata, 0, 
61            sizeof(union data_types*) * num_sortkeys);
62     
63     
64     return record;
65 }
66
67
68 struct record_metadata * record_metadata_insert(NMEM nmem, 
69                                                 struct record_metadata ** rmd,
70                                                 union data_types data)
71 {
72     struct record_metadata * tmp_rmd = 0;
73     // assert(nmem);
74
75     if(!rmd)
76         return 0;
77
78     // construct new record_metadata
79     tmp_rmd  = nmem_malloc(nmem, sizeof(struct record_metadata));
80     tmp_rmd->data = data;
81
82
83     // insert in *rmd's place
84     tmp_rmd->next = *rmd;
85     *rmd = tmp_rmd;
86
87     return tmp_rmd;
88 }
89
90
91 struct record_metadata * record_add_metadata(NMEM nmem, 
92                                              struct record * record,
93                                              struct conf_service * service,
94                                              const char * name,
95                                              union data_types data)
96 {
97     int field_id = 0;
98
99     if (!record || !record->metadata || !service || !name)  
100         return 0;
101     
102     field_id = conf_service_field_id(service, name);
103
104     if (-1 == field_id)
105         return 0;
106     
107     return record_metadata_insert(nmem, &(record->metadata[field_id]), data);
108 }
109
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 /*
125  * Local variables:
126  * c-basic-offset: 4
127  * indent-tabs-mode: nil
128  * End:
129  * vim: shiftwidth=4 tabstop=8 expandtab
130  */