1 /* libcomps - C alternative to yum.comps library
2  * Copyright (C) 2013 Jindrich Luza
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License 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  Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
17  * USA
18  */
19 
20 #include "comps_docgroupid.h"
comps_docgroupid_create(COMPS_DocGroupId * groupid,COMPS_Object ** args)21 void comps_docgroupid_create(COMPS_DocGroupId* groupid, COMPS_Object **args) {
22 
23     (void)args;
24     groupid->name = NULL;
25     groupid->def = 0;
26     groupid->arches = NULL;
27 }
COMPS_CREATE_u(docgroupid,COMPS_DocGroupId)28 COMPS_CREATE_u(docgroupid, COMPS_DocGroupId)
29 
30 void comps_docgroupid_copy(COMPS_DocGroupId *gid_dst,
31                            COMPS_DocGroupId *gid_src) {
32     gid_dst->name = (COMPS_Str*)comps_object_copy((COMPS_Object*)gid_src->name);
33     gid_dst->arches = (COMPS_ObjList*)comps_object_copy((COMPS_Object*)gid_src->arches);
34     gid_dst->def = gid_src->def;
35 }
COMPS_COPY_u(docgroupid,COMPS_DocGroupId)36 COMPS_COPY_u(docgroupid, COMPS_DocGroupId)    /*comps_utils.h macro*/
37 
38 void comps_docgroupid_destroy(COMPS_DocGroupId *gid) {
39     COMPS_OBJECT_DESTROY(gid->name);
40     COMPS_OBJECT_DESTROY(gid->arches);
41 }
COMPS_DESTROY_u(docgroupid,COMPS_DocGroupId)42 COMPS_DESTROY_u(docgroupid, COMPS_DocGroupId) /*comps_utils.h macro*/
43 
44 
45 COMPS_Object* comps_docgroupid_get_name(COMPS_DocGroupId *gid) {
46     return comps_object_incref((COMPS_Object*)gid->name);
47 }
comps_docgroupid_set_name(COMPS_DocGroupId * gid,char * name,char copy)48 void comps_docgroupid_set_name(COMPS_DocGroupId *gid, char *name, char copy) {
49     (void)copy;
50     if (gid->name)
51         COMPS_OBJECT_DESTROY(gid->name);
52     gid->name = comps_str(name);
53 }
54 
comps_docgroupid_get_default(COMPS_DocGroupId * gid)55 COMPS_Object* comps_docgroupid_get_default(COMPS_DocGroupId *gid) {
56     return (COMPS_Object*)comps_num(gid->def);
57 }
comps_docgroupid_set_default(COMPS_DocGroupId * gid,int def)58 void comps_docgroupid_set_default(COMPS_DocGroupId *gid, int def) {
59     gid->def = (def != 0);
60 }
61 
comps_docgroupid_arches(COMPS_DocGroupId * gid)62 COMPS_ObjList* comps_docgroupid_arches(COMPS_DocGroupId *gid) {
63     return (COMPS_ObjList*)comps_object_incref((COMPS_Object*)gid->arches);
64 }
comps_docgroupid_set_arches(COMPS_DocGroupId * gid,COMPS_ObjList * arches)65 void comps_docgroupid_set_arches(COMPS_DocGroupId *gid,
66                                  COMPS_ObjList *arches) {
67     COMPS_OBJECT_DESTROY(gid->arches);
68     gid->arches = arches;
69 }
70 
comps_docgroupid_cmp_u(COMPS_Object * gid1,COMPS_Object * gid2)71 signed char comps_docgroupid_cmp_u(COMPS_Object *gid1, COMPS_Object *gid2) {
72     #define _gid1 ((COMPS_DocGroupId*)gid1)
73     #define _gid2 ((COMPS_DocGroupId*)gid2)
74 
75     if (!COMPS_OBJECT_CMP(_gid1->name, _gid2->name)) {
76         //printf("gid fail str %s != %s\n", _gid1->name, _gid2->name);
77         return 0;
78     }
79     if (_gid1->def != _gid2->def) { return 0;
80         //printf("gid fail %d != %d\n", _gid1->def, _gid2->def);
81     }
82     return 1;
83 
84     #undef _gid1
85     #undef _gid2
86 }
87 
__comps_docgroupid_cmp_set(void * gid1,void * gid2)88 char __comps_docgroupid_cmp_set(void *gid1, void *gid2) {
89     return comps_object_cmp((COMPS_Object*)((COMPS_DocGroupId*)gid1)->name,
90                             (COMPS_Object*)((COMPS_DocGroupId*)gid2)->name);
91 }
comps_docgroupid_str_u(COMPS_Object * docgroupid)92 char* comps_docgroupid_str_u(COMPS_Object* docgroupid) {
93     const int len = strlen("<COMPS_DocGroupId name='' default=''>");
94     char *name = comps_object_tostr((COMPS_Object*)((COMPS_DocGroupId*)docgroupid)->name);
95     char *def = ((COMPS_DocGroupId*)docgroupid)->def?"True":"False";
96     char *ret = malloc(sizeof(char)*(len+strlen(name)+strlen(def)+1));
97     sprintf(ret, "<COMPS_DocGroupId name='%s' default='%s'>", name, def);
98     free(name);
99     return ret;
100 }
101 
comps_docgroupid_xml(COMPS_DocGroupId * groupid,xmlTextWriterPtr writer,COMPS_Log * log,COMPS_XMLOptions * options,COMPS_DefaultsOptions * def_options)102 signed char comps_docgroupid_xml(COMPS_DocGroupId *groupid,
103                                   xmlTextWriterPtr writer,
104                                   COMPS_Log *log, COMPS_XMLOptions *options,
105                                   COMPS_DefaultsOptions *def_options) {
106     char *str;
107     bool default_def = false;
108     int ret;
109     (void)def_options;
110 
111     ret = xmlTextWriterStartElement(writer, BAD_CAST "groupid");
112     COMPS_XMLRET_CHECK()
113     if (options->arch_output) {
114         COMPS_Object *obj = (COMPS_Object*)groupid->arches;
115         //printf("obj %d\n", obj);
116         ret = __comps_xml_arch(obj, writer);
117         COMPS_XMLRET_CHECK()
118     }
119     if (options->gid_default_explicit) {
120         if (groupid->def)
121             ret = xmlTextWriterWriteAttribute(writer, BAD_CAST "default",
122                                               BAD_CAST "true");
123         else
124             ret = xmlTextWriterWriteAttribute(writer, BAD_CAST "default",
125                                               BAD_CAST "false");
126         COMPS_XMLRET_CHECK()
127     } else if (groupid->def != default_def) {
128         if (groupid->def)
129             ret = xmlTextWriterWriteAttribute(writer, BAD_CAST "default",
130                                               BAD_CAST "true");
131         else
132             ret = xmlTextWriterWriteAttribute(writer, BAD_CAST "default",
133                                               BAD_CAST "false");
134     }
135     str = comps_object_tostr((COMPS_Object*)groupid->name);
136     ret = xmlTextWriterWriteString(writer, BAD_CAST str);
137     free(str);
138     COMPS_XMLRET_CHECK()
139     ret = xmlTextWriterEndElement(writer);
140     COMPS_XMLRET_CHECK()
141     return 0;
142 }
143 
144 COMPS_ObjectInfo COMPS_DocGroupId_ObjInfo = {
145     .obj_size = sizeof(COMPS_DocGroupId),
146     .constructor = &comps_docgroupid_create_u,
147     .destructor = &comps_docgroupid_destroy_u,
148     .copy = &comps_docgroupid_copy_u,
149     .obj_cmp = &comps_docgroupid_cmp_u,
150     .to_str = &comps_docgroupid_str_u
151 };
152 
153 COMPS_ValRuleGeneric* COMPS_DocGroupId_ValidateRules[] = {
154     (COMPS_ValRuleGeneric*)&(COMPS_ValRuleProp){COMPS_VAL_RULE_PROP,
155                          .verbose_msg = "GroupId name check: ",
156                          .get_f = (COMPS_VAL_GETF) &comps_docgroupid_get_name,
157                          .check_f = &comps_empty_check},
158     NULL
159 };
160