ReactOS Package Manager
[reactos.git] / rosapps / packmgr / lib / download.cpp
1 ////////////////////////////////////////////////////////
2 //
3 // download.cpp
4 //
5 // Stuff related to downloading
6 //
7 //
8 // Maarten Bosma, 09.01.2004
9 // maarten.paul@bosma.de
10 //
11 ////////////////////////////////////////////////////////
12
13 #include "package.hpp"
14 #include "expat.h"
15 #include "log.h"
16 #include <wine/urlmon.h>
17
18 // Server there all the files lie
19 const char* tree_server = "http://svn.reactos.com/viewcvs/*checkout*/trunk/rosapps/packmgr/tree/";
20
21 HRESULT WINAPI URLDownloadToFileA(
22 LPUNKNOWN pCaller,
23 LPCSTR szURL,
24 LPCSTR szFileName,
25 DWORD dwReserved,
26 LPBINDSTATUSCALLBACK lpfnCB
27 );
28
29
30 // Download a file
31 char* PML_Download (const char* name, const char* local_name = "packmgr.txt", const char* server = tree_server, BOOL totemp = TRUE)
32 {
33 char url [MAX_PATH];
34 static char path [MAX_PATH];
35
36 // get temp dir
37 if(totemp)
38 GetTempPathA (200, path);
39
40 // create the local file name
41 if(local_name)
42 strcat(path, local_name);
43 else
44 strcat(path, "tmp.tmp");
45
46 // get the url
47 if(server) strcpy(url, server);
48 strcat(url, name);
49
50 // make sure there is no old file
51 DeleteFileA (path);
52
53 // download the file
54 if(URLDownloadToFileA (NULL, url, path, 0, NULL) != S_OK)
55 {
56 Log("! ERROR: Unable to download ");
57 LogAdd(url);
58
59 return NULL;
60 }
61
62 return path;
63 }
64
65 // Download and prozess a xml file
66 int PML_XmlDownload (const char* url, void* usrdata, XML_StartElementHandler start,
67 XML_EndElementHandler end, XML_CharacterDataHandler text)
68 {
69 char buffer[255];
70 int done = 0;
71
72 // logging
73 Log("* prozess the xml file: ");
74 LogAdd(url);
75
76 // download the file
77 char* filename = PML_Download(url);
78
79 if(!filename)
80 {
81 Log("! ERROR: Could not download the xml file");
82 return ERR_DOWNL;
83 }
84
85 // open the file
86 FILE* file = fopen(filename, "r");
87 if(!file)
88 {
89 Log("! ERROR: Could not open the xml file");
90 return ERR_GENERIC;
91 }
92
93 // parse the xml file
94 XML_Parser parser = XML_ParserCreate(NULL);
95 XML_SetUserData (parser, usrdata);
96 XML_SetElementHandler(parser, start, end);
97 XML_SetCharacterDataHandler(parser, text);
98
99 while (!done)
100 {
101 size_t len = fread (buffer, 1, sizeof(buffer), file);
102 done = len < sizeof(buffer);
103
104 buffer[len] = 0;
105 if(!XML_Parse(parser, buffer, len, done))
106 {
107 Log("! ERROR: Could not parse the xml file");
108 return ERR_GENERIC;
109 }
110 }
111
112 XML_ParserFree(parser);
113 fclose(file);
114
115 return ERR_OK;
116 }
117