PackageManager: Changed download server to my own webspace. Because http://svn.reacto...
[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://maarten-online.de/xml/"; //"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 int FindCount (string What, string Where, int start = 0, int end = -1);
30
31
32 // Download a file
33 char* PML_Download (const char* url, const char* server = "tree", const char* filename = NULL)
34 {
35 char downl [MAX_PATH];
36 static char path [MAX_PATH];
37
38 // get temp dir
39 if(!filename)
40 GetTempPathA (200, path);
41
42 else if(!strstr(filename, "\\"))
43 GetTempPathA (200, path);
44
45 // create the local file name
46 if(filename)
47 {
48 strcat(path, filename);
49 DeleteFileA (path);
50 }
51 else
52 GetTempFileNameA (path, "pml", 0, path);
53
54 // get the url
55
56 if (!server)
57 strcpy(downl, "");
58
59 else if(!strcmp(server, "tree"))
60 strcpy(downl, tree_server);
61
62 else
63 strcpy(downl, server);
64
65 strcat(downl, url);
66
67 // download the file
68 if(URLDownloadToFileA (NULL, downl, path, 0, NULL) != S_OK)
69 {
70 Log("! ERROR: Unable to download ");
71 LogAdd(downl);
72
73 return NULL;
74 }
75
76 return path;
77 }
78
79 // Download and prozess a xml file
80 int PML_XmlDownload (const char* url, void* usrdata, XML_StartElementHandler start,
81 XML_EndElementHandler end, XML_CharacterDataHandler text)
82 {
83 char buffer[255];
84 int done = 0;
85
86 // logging
87 Log("* prozess the xml file: ");
88 LogAdd(url);
89
90 // download the file
91 char* filename = PML_Download(url);
92
93 if(!filename)
94 {
95 Log("! ERROR: Could not download the xml file");
96 return ERR_DOWNL;
97 }
98
99 // open the file
100 FILE* file = fopen(filename, "r");
101 if(!file)
102 {
103 Log("! ERROR: Could not open the xml file");
104 return ERR_GENERIC;
105 }
106
107 // parse the xml file
108 XML_Parser parser = XML_ParserCreate(NULL);
109 XML_SetUserData (parser, usrdata);
110 XML_SetElementHandler(parser, start, end);
111 XML_SetCharacterDataHandler(parser, text);
112
113 while (!done)
114 {
115 size_t len = fread (buffer, 1, sizeof(buffer), file);
116 done = len < sizeof(buffer);
117
118 buffer[len] = 0;
119 if(!XML_Parse(parser, buffer, len, done))
120 {
121 Log("! ERROR: Could not parse the xml file");
122 return ERR_GENERIC;
123 }
124 }
125
126 XML_ParserFree(parser);
127 fclose(file);
128
129 return ERR_OK;
130 }
131