PackageManager: HTML Log
[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 HRESULT WINAPI URLDownloadToFileA(
19 LPUNKNOWN pCaller,
20 LPCSTR szURL,
21 LPCSTR szFileName,
22 DWORD dwReserved,
23 LPBINDSTATUSCALLBACK lpfnCB
24 );
25
26 int FindCount (string What, string Where, int start = 0, int end = -1);
27
28
29 // Download a file
30 char* PML_Download (pTree tree, const char* url, const char* server = "tree", const char* filename = "packmgr.xml")
31 {
32 UINT i;
33 static char downl [MAX_PATH]; // the full url
34 static char path [MAX_PATH]; // the full resulting Path
35
36 // It goes to the temp folder when no other path is entered (or even compleatly no filename)
37 // If server == "tree" it will be downloaded from the server speficied in option.xml
38 // File:// links are possible too
39
40 // get temp dir
41 if(!filename)
42 GetTempPathA (200, path);
43
44 else if(!strstr(filename, "\\"))
45 GetTempPathA (200, path);
46
47 else
48 strcpy(path, "");
49
50
51 // create the local file name
52 if(filename)
53 {
54 strcat(path, filename);
55 DeleteFileA (path);
56 }
57 else
58 GetTempFileNameA (path, "pml", 1, path);
59
60 // get the url
61 if (!server)
62 strcpy(downl, "");
63
64 else if(!strcmp(server, "tree"))
65 {
66 char* ret;
67 for (i=0; i<tree->sources.size(); i++)
68 {
69 ret = PML_Download(tree, url, tree->sources[i], filename);
70 if(ret)
71 return ret;
72 }
73 return NULL;
74 }
75
76 else
77 strcpy(downl, server);
78
79 strcat(downl, url);
80
81 // is this a file link ?
82 if (strstr(downl, "file://") || strstr(downl, "File://"))
83 {
84 if(!filename)
85 {
86 return &downl[7];
87 }
88
89 else
90 {
91 CopyFileA(filename, &downl[7], FALSE);
92 return (char*)filename;
93 }
94 }
95
96
97 // download the file
98 if(URLDownloadToFileA (NULL, downl, path, 0, NULL) != S_OK)
99 {
100 Log("! ERROR: Unable to download ");
101 LogAdd(downl);
102
103 return NULL;
104 }
105
106 return path;
107 }
108
109 // Download and prozess a xml file
110 int PML_XmlDownload (pTree tree, const char* url, void* usrdata,
111 XML_StartElementHandler start, XML_EndElementHandler end, XML_CharacterDataHandler text)
112 {
113 int done = 0;
114 char buffer[255];
115 char* filename = 0;
116
117 // logging
118 Log("* prozess the xml file: ");
119 LogAdd(url);
120
121 // download the file
122 if(strstr(url, "file://"))
123 filename = PML_Download(tree, url, NULL, NULL);
124
125 else
126 filename = PML_Download(tree, url);
127
128
129 if(!filename)
130 {
131 Log("! ERROR: Could not download the xml file");
132 return ERR_DOWNL;
133 }
134
135 // open the file
136 FILE* file = fopen(filename, "r");
137 if(!file)
138 {
139 Log("! ERROR: Could not open the xml file ");
140 LogAdd(filename);
141 return ERR_GENERIC;
142 }
143
144 // parse the xml file
145 XML_Parser parser = XML_ParserCreate(NULL);
146 XML_SetUserData (parser, usrdata);
147 XML_SetElementHandler(parser, start, end);
148 XML_SetCharacterDataHandler(parser, text);
149
150 while (!done)
151 {
152 size_t len = fread (buffer, 1, sizeof(buffer), file);
153 done = len < sizeof(buffer);
154
155 buffer[len] = 0;
156 if(!XML_Parse(parser, buffer, len, done))
157 {
158 Log("! ERROR: Could not parse the xml file");
159 return ERR_GENERIC;
160 }
161 }
162
163 XML_ParserFree(parser);
164 fclose(file);
165
166 return ERR_OK;
167 }
168