41761ddbc136ab0225040a4228fcac36697ce91a
[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 = NULL)
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 // create the local file name
48 if(filename)
49 {
50 strcat(path, filename);
51 DeleteFileA (path);
52 }
53 else
54 GetTempFileNameA (path, "pml", 0, path);
55
56 // get the url
57 if (!server)
58 strcpy(downl, "");
59
60 else if(!strcmp(server, "tree"))
61 {
62 char* ret;
63 for (i=0; i<tree->sources.size(); i++)
64 {
65 ret = PML_Download(tree, url, tree->sources[i], filename);
66 if(ret)
67 return ret;
68 }
69 return NULL;
70 }
71
72 else
73 strcpy(downl, server);
74
75 strcat(downl, url);
76
77 // is this a file link ?
78 if (strstr(downl, "file://") || strstr(downl, "File://"))
79 {/*
80 if(downl[strlen(downl)] == '\')
81 downl[strlen(downl)] = '\0';
82 */
83 if(!filename)
84 return &downl[7];
85
86 else
87 {
88 CopyFileA(filename, &downl[7], FALSE);
89 return (char*)filename;
90 }
91 }
92
93 // download the file
94 if(URLDownloadToFileA (NULL, downl, path, 0, NULL) != S_OK)
95 {
96 Log("! ERROR: Unable to download ");
97 LogAdd(downl);
98
99 return NULL;
100 }
101
102 return path;
103 }
104
105 // Download and prozess a xml file
106 int PML_XmlDownload (pTree tree, const char* url, void* usrdata,
107 XML_StartElementHandler start, XML_EndElementHandler end, XML_CharacterDataHandler text)
108 {
109 int done = 0;
110 char buffer[255];
111 char* filename = 0;
112
113 // logging
114 Log("* prozess the xml file: ");
115 LogAdd(url);
116
117 // download the file
118 if(strstr(url, "file://"))
119 filename = PML_Download(tree, url, NULL);
120
121 else
122 filename = PML_Download(tree, url);
123
124
125 if(!filename)
126 {
127 Log("! ERROR: Could not download the xml file");
128 return ERR_DOWNL;
129 }
130
131 // open the file
132 FILE* file = fopen(filename, "r");
133 if(!file)
134 {
135 MessageBoxA(0,filename,0,0);
136 Log("! ERROR: Could not open the xml file \"");
137 LogAdd(filename);
138 return ERR_GENERIC;
139 }
140
141 // parse the xml file
142 XML_Parser parser = XML_ParserCreate(NULL);
143 XML_SetUserData (parser, usrdata);
144 XML_SetElementHandler(parser, start, end);
145 XML_SetCharacterDataHandler(parser, text);
146
147 while (!done)
148 {
149 size_t len = fread (buffer, 1, sizeof(buffer), file);
150 done = len < sizeof(buffer);
151
152 buffer[len] = 0;
153 if(!XML_Parse(parser, buffer, len, done))
154 {
155 Log("! ERROR: Could not parse the xml file");
156 return ERR_GENERIC;
157 }
158 }
159
160 XML_ParserFree(parser);
161 fclose(file);
162
163 return ERR_OK;
164 }
165