1 /* PROJECT: ReactOS Downloader
2 * LICENSE: GPL - See COPYING in the top level directory
3 * FILE: base\applications\downloader\xml.c
4 * PURPOSE: Parsing of application information xml files
5 * PROGRAMMERS: Maarten Bosma
8 #include <libs/expat/expat.h>
12 #include "structures.h"
13 #include "resources.h"
16 struct Category
* Current
;
17 struct Application
* CurrentApplication
;
18 char CurrentTag
[0x100];
19 extern WCHAR Strings
[STRING_COUNT
][MAX_STRING_LENGHT
];
21 void tag_opened (void* usrdata
, const char* tag
, const char** arg
)
25 if(!strcmp(tag
, "tree") && !CurrentApplication
)
30 else if(!strcmp(tag
, "category") && !CurrentApplication
)
34 Current
= malloc(sizeof(struct Category
));
35 memset(Current
, 0, sizeof(struct Category
));
39 Current
->Children
= malloc(sizeof(struct Category
));
40 memset(Current
->Children
, 0, sizeof(struct Category
));
41 Current
->Children
->Parent
= Current
;
42 Current
= Current
->Children
;
46 Current
->Next
= malloc(sizeof(struct Category
));
47 memset(Current
->Next
, 0, sizeof(struct Category
));
48 Current
->Next
->Parent
= Current
->Parent
;
49 Current
= Current
->Next
;
53 for (i
=0; arg
[i
]; i
+=2)
55 if(!strcmp(arg
[i
], "name"))
57 MultiByteToWideChar(CP_ACP
, 0, arg
[i
+1], -1, Current
->Name
, 0x100);
59 if(!strcmp(arg
[i
], "icon"))
61 Current
->Icon
= atoi(arg
[i
+1]);
66 else if(!strcmp(tag
, "application") && !CurrentApplication
)
70 CurrentApplication
= Current
->Apps
;
71 while(CurrentApplication
->Next
)
72 CurrentApplication
= CurrentApplication
->Next
;
73 CurrentApplication
->Next
= malloc(sizeof(struct Application
));
74 memset(CurrentApplication
->Next
, 0, sizeof(struct Application
));
75 CurrentApplication
= CurrentApplication
->Next
;
79 Current
->Apps
= malloc(sizeof(struct Application
));
80 memset(Current
->Apps
, 0, sizeof(struct Application
));
81 CurrentApplication
= Current
->Apps
;
84 for (i
=0; arg
[i
]; i
+=2)
86 if(!strcmp(arg
[i
], "name"))
88 MultiByteToWideChar(CP_ACP
, 0, arg
[i
+1], -1, CurrentApplication
->Name
, 0x100);
92 else if (CurrentApplication
)
94 strncpy(CurrentTag
, tag
, 0x100);
97 MessageBoxW(0,Strings
[IDS_XMLERROR_2
],0,0);
101 void text (void* usrdata
, const char* data
, int len
)
103 if (!CurrentApplication
)
106 // FIXME: handle newlines e.g. in Description
107 if(!strcmp(CurrentTag
, "maintainer"))
109 int currentlengt
= lstrlenW(CurrentApplication
->Location
);
110 MultiByteToWideChar(CP_ACP
, 0, data
, len
, &CurrentApplication
->Maintainer
[currentlengt
], 0x100-currentlengt
);
112 else if(!strcmp(CurrentTag
, "description"))
114 int currentlengt
= lstrlenW(CurrentApplication
->Description
);
115 MultiByteToWideChar(CP_ACP
, 0, data
, len
, &CurrentApplication
->Description
[currentlengt
], 0x400-currentlengt
);
117 else if(!strcmp(CurrentTag
, "location"))
119 int currentlengt
= lstrlenW(CurrentApplication
->Location
);
120 MultiByteToWideChar(CP_ACP
, 0, data
, len
, &CurrentApplication
->Location
[currentlengt
], 0x100-currentlengt
);
124 void tag_closed (void* tree
, const char* tag
)
128 if(!strcmp(tag
, "category"))
136 Current
= Current
->Parent
;
139 else if(!strcmp(tag
, "application"))
141 CurrentApplication
= NULL
;
145 BOOL
ProcessXML (const char* filename
, struct Category
* Root
)
158 file
= fopen(filename
, "r");
161 file
= fopen("downloader.xml", "r");
164 MessageBoxW(0,Strings
[IDS_XMLERROR_1
],0,0);
169 parser
= XML_ParserCreate(NULL
);
170 XML_SetElementHandler(parser
, tag_opened
, tag_closed
);
171 XML_SetCharacterDataHandler(parser
, text
);
175 size_t len
= fread (buffer
, 1, sizeof(buffer
), file
);
176 done
= len
< sizeof(buffer
);
179 if(!XML_Parse(parser
, buffer
, len
, done
))
181 MessageBoxW(0,Strings
[IDS_XMLERROR_2
],0,0);
186 XML_ParserFree(parser
);
192 void FreeApps (struct Application
* Apps
)
195 FreeApps(Apps
->Next
);
200 void FreeTree (struct Category
* Node
)
203 FreeTree(Node
->Children
);
206 FreeTree(Node
->Next
);
209 FreeApps(Node
->Apps
);