gwenhywfar  5.10.1
gbuilderdescr.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Mon Feb 08 2021
3  copyright : (C) 2021 by Martin Preuss
4  email : martin@libchipcard.de
5 
6  ***************************************************************************
7  * Please see toplevel file COPYING for license details *
8  ***************************************************************************/
9 
10 #ifdef HAVE_CONFIG_H
11 # include <config.h>
12 #endif
13 
14 #include "gwenbuild/builders/gbuilderdescr_p.h"
16 
17 #include <gwenhywfar/debug.h>
18 #include <gwenhywfar/memory.h>
19 #include <gwenhywfar/text.h>
20 #include <gwenhywfar/directory.h>
21 
22 
23 
24 GWEN_LIST_FUNCTIONS(GWB_GBUILDER_DESCR, GWB_GBuilderDescr)
25 
26 
27 
28 static void _readSettingsFromXml(GWB_GBUILDER_DESCR *descr);
29 GWB_GBUILDER_DESCR *_readFromFile(const char *fileName);
30 
31 
32 
33 
34 
36 {
37  GWB_GBUILDER_DESCR *descr;
38  const char *s;
39 
40  s=GWEN_XMLNode_GetProperty(xmlNode, "name", NULL);
41  if (!(s && *s)) {
42  DBG_ERROR(NULL, "No builder name in XML builder description");
43  return NULL;
44  }
45 
48  descr->builderName=strdup(s);
49  descr->xmlDescr=xmlNode;
50 
51  _readSettingsFromXml(descr);
52 
53  return descr;
54 }
55 
56 
57 
59 {
60  GWEN_XMLNODE *nInputFiles;
61 
62  nInputFiles=GWEN_XMLNode_FindFirstTag(descr->xmlDescr, "inputFiles", NULL, NULL);
63  if (nInputFiles) {
64  GWEN_XMLNODE *n;
65 
66  n=GWEN_XMLNode_FindFirstTag(nInputFiles, "acceptedInputTypes", NULL, NULL);
67  if (n)
68  descr->acceptedInputTypes=GWB_Parser_ReadXmlDataIntoStringList(NULL, n);
69 
70  n=GWEN_XMLNode_FindFirstTag(nInputFiles, "acceptedInputFiles", NULL, NULL);
71  if (n)
72  descr->acceptedInputExt=GWB_Parser_ReadXmlDataIntoStringList(NULL, n);
73  }
74 }
75 
76 
77 
79 {
80  if (descr) {
82 
83  free(descr->builderName);
84  GWEN_XMLNode_free(descr->xmlDescr);
85  GWEN_StringList_free(descr->acceptedInputTypes);
86  GWEN_StringList_free(descr->acceptedInputExt);
87 
88  GWEN_FREE_OBJECT(descr);
89  }
90 }
91 
92 
93 
95 {
96  return descr->builderName;
97 }
98 
99 
100 
102 {
103  return descr->xmlDescr;
104 }
105 
106 
107 
109 {
110  return descr->acceptedInputTypes;
111 }
112 
113 
114 
116 {
117  return descr->acceptedInputExt;
118 }
119 
120 
121 
122 
123 
124 GWB_GBUILDER_DESCR *_readFromFile(const char *fileName)
125 {
126  GWEN_XMLNODE *xmlRoot;
127  GWEN_XMLNODE *xmlDescr;
128  GWB_GBUILDER_DESCR *descr;
129  int rv;
130 
131  xmlRoot=GWEN_XMLNode_new(GWEN_XMLNodeTypeTag, "root");
133  if (rv<0) {
134  DBG_ERROR(NULL, "No builder description found in file \"%s\" (%d)", fileName, rv);
135  return NULL;
136  }
137 
138  xmlDescr=GWEN_XMLNode_FindFirstTag(xmlRoot, "GwenBuilder", NULL, NULL);
139  if (xmlDescr==NULL) {
140  DBG_ERROR(NULL, "No <GwenBuilder> element found in file \"%s\"", fileName);
141  GWEN_XMLNode_free(xmlRoot);
142  return NULL;
143  }
144  GWEN_XMLNode_UnlinkChild(xmlRoot, xmlDescr);
145  GWEN_XMLNode_free(xmlRoot);
146 
147  descr=GWB_GBuilderDescr_new(xmlDescr);
148  if (descr==NULL) {
149  DBG_ERROR(NULL, "Error in file \"%s\"", fileName);
150  GWEN_XMLNode_free(xmlDescr);
151  return NULL;
152  }
153 
154  return descr;
155 }
156 
157 
158 
159 GWB_GBUILDER_DESCR_LIST *GWB_GBuilderDescr_ReadAll(const char *folder)
160 {
161  GWEN_STRINGLIST *sl;
163  GWB_GBUILDER_DESCR_LIST *descrList;
164  int rv;
165 
166  sl=GWEN_StringList_new();
167  rv=GWEN_Directory_GetMatchingFilesRecursively(folder, sl, "*.gwb");
168  if (rv<0) {
169  DBG_ERROR(NULL, "here (%d)", rv);
171  return NULL;
172  }
173 
174  descrList=GWB_GBuilderDescr_List_new();
176  while(se) {
177  const char *s;
178 
180  if (s && *s) {
181  GWB_GBUILDER_DESCR *descr;
182 
183  DBG_INFO(NULL, "Reading builder descr file \"%s\"", s);
184  descr=_readFromFile(s);
185  if (descr==NULL) {
186  DBG_ERROR(NULL, "Error reading description from file \"%s\", ignoring", s);
187  }
188  else
189  GWB_GBuilderDescr_List_Add(descr, descrList);
190  }
191 
193  }
195 
196  return descrList;
197 }
198 
199 
200 
201 GWB_GBUILDER_DESCR *GWB_GBuilderDescr_List_GetByName(const GWB_GBUILDER_DESCR_LIST *descrList, const char *name)
202 {
203  GWB_GBUILDER_DESCR *descr;
204 
205  descr=GWB_GBuilderDescr_List_First(descrList);
206  while(descr) {
207  if (descr->builderName && strcasecmp(descr->builderName, name)==0)
208  return descr;
209  descr=GWB_GBuilderDescr_List_Next(descr);
210  }
211 
212  return NULL;
213 }
214 
215 
216 
217 
218 
219 
220 
221 
222 
223 
struct GWEN_STRINGLISTENTRYSTRUCT GWEN_STRINGLISTENTRY
Definition: stringlist.h:53
GWEN_XMLNODE * GWB_GBuilderDescr_GetXmlDescr(const GWB_GBUILDER_DESCR *descr)
GWEN_STRINGLIST * GWB_Parser_ReadXmlDataIntoStringList(GWEN_DB_NODE *db, GWEN_XMLNODE *xmlNode)
Definition: parser.c:283
#define GWEN_XML_FLAGS_DEFAULT
Definition: xml.h:117
#define GWEN_XML_FLAGS_SIMPLE
Definition: xml.h:89
const char * GWEN_XMLNode_GetProperty(const GWEN_XMLNODE *n, const char *name, const char *defaultValue)
Definition: xml.c:239
#define GWEN_FREE_OBJECT(varname)
Definition: memory.h:61
#define NULL
Definition: binreloc.c:300
GWB_GBUILDER_DESCR * GWB_GBuilderDescr_new(GWEN_XMLNODE *xmlNode)
Definition: gbuilderdescr.c:35
void GWB_GBuilderDescr_free(GWB_GBUILDER_DESCR *descr)
Definition: gbuilderdescr.c:78
GWENHYWFAR_API int GWEN_XML_ReadFile(GWEN_XMLNODE *n, const char *filepath, uint32_t flags)
Definition: xmlrw.c:1297
GWEN_XMLNODE * GWEN_XMLNode_new(GWEN_XMLNODE_TYPE t, const char *data)
Definition: xml.c:144
GWENHYWFAR_API int GWEN_Directory_GetMatchingFilesRecursively(const char *folder, GWEN_STRINGLIST *sl, const char *mask)
GWEN_STRINGLISTENTRY * GWEN_StringList_FirstEntry(const GWEN_STRINGLIST *sl)
Definition: stringlist.c:390
const char * GWEN_StringListEntry_Data(const GWEN_STRINGLISTENTRY *se)
Definition: stringlist.c:406
void GWEN_StringList_free(GWEN_STRINGLIST *sl)
Definition: stringlist.c:62
GWEN_XMLNODE * GWEN_XMLNode_FindFirstTag(const GWEN_XMLNODE *n, const char *tname, const char *pname, const char *pvalue)
Definition: xml.c:776
#define GWEN_NEW_OBJECT(typ, varname)
Definition: memory.h:55
struct GWEN_STRINGLISTSTRUCT GWEN_STRINGLIST
Definition: stringlist.h:56
GWB_GBUILDER_DESCR_LIST * GWB_GBuilderDescr_ReadAll(const char *folder)
void GWEN_XMLNode_free(GWEN_XMLNODE *n)
Definition: xml.c:160
void GWEN_XMLNode_UnlinkChild(GWEN_XMLNODE *n, GWEN_XMLNODE *child)
Definition: xml.c:570
static void _readSettingsFromXml(GWB_GBUILDER_DESCR *descr)
Definition: gbuilderdescr.c:58
#define DBG_ERROR(dbg_logger, format, args...)
Definition: debug.h:97
const char * GWB_GBuilderDescr_GetBuilderName(const GWB_GBUILDER_DESCR *descr)
Definition: gbuilderdescr.c:94
GWEN_STRINGLISTENTRY * GWEN_StringListEntry_Next(const GWEN_STRINGLISTENTRY *se)
Definition: stringlist.c:398
GWEN_STRINGLIST * GWB_GBuilderDescr_GetAcceptedInputExt(const GWB_GBUILDER_DESCR *descr)
#define DBG_INFO(dbg_logger, format, args...)
Definition: debug.h:181
#define GWEN_LIST_INIT(t, element)
Definition: list1.h:465
GWB_GBUILDER_DESCR * GWB_GBuilderDescr_List_GetByName(const GWB_GBUILDER_DESCR_LIST *descrList, const char *name)
GWEN_STRINGLIST * GWB_GBuilderDescr_GetAcceptedInputTypes(const GWB_GBUILDER_DESCR *descr)
#define GWEN_LIST_FUNCTIONS(t, pr)
Definition: list1.h:366
GWEN_STRINGLIST * GWEN_StringList_new(void)
Definition: stringlist.c:50
#define GWEN_LIST_FINI(t, element)
Definition: list1.h:474
struct GWEN__XMLNODE GWEN_XMLNODE
Definition: xml.h:156
struct GWB_GBUILDER_DESCR GWB_GBUILDER_DESCR
Definition: gbuilderdescr.h:22
GWB_GBUILDER_DESCR * _readFromFile(const char *fileName)