8d450f318a153106f2795c95620537e4fd016d47
[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 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
58 else if(!strcmp(server, "tree"))
59 strcpy(downl, tree_server);
60
61 else
62 strcpy(downl, server);
63
64 strcat(downl, url);
65
66 // download the file
67 if(URLDownloadToFileA (NULL, downl, path, 0, NULL) != S_OK)
68 {
69 Log("! ERROR: Unable to download ");
70 LogAdd(downl);
71
72 return NULL;
73 }
74
75 return path;
76 }
77
78 // Download and prozess a xml file
79 int PML_XmlDownload (const char* url, void* usrdata, XML_StartElementHandler start,
80 XML_EndElementHandler end, XML_CharacterDataHandler text)
81 {
82 char buffer[255];
83 int done = 0;
84
85 // logging
86 Log("* prozess the xml file: ");
87 LogAdd(url);
88
89 // download the file
90 char* filename = PML_Download(url);
91
92 if(!filename)
93 {
94 Log("! ERROR: Could not download the xml file");
95 return ERR_DOWNL;
96 }
97
98 // open the file
99 FILE* file = fopen(filename, "r");
100 if(!file)
101 {
102 Log("! ERROR: Could not open the xml file");
103 return ERR_GENERIC;
104 }
105
106 // parse the xml file
107 XML_Parser parser = XML_ParserCreate(NULL);
108 XML_SetUserData (parser, usrdata);
109 XML_SetElementHandler(parser, start, end);
110 XML_SetCharacterDataHandler(parser, text);
111
112 while (!done)
113 {
114 size_t len = fread (buffer, 1, sizeof(buffer), file);
115 done = len < sizeof(buffer);
116
117 buffer[len] = 0;
118 if(!XML_Parse(parser, buffer, len, done))
119 {
120 Log("! ERROR: Could not parse the xml file");
121 return ERR_GENERIC;
122 }
123 }
124
125 XML_ParserFree(parser);
126 fclose(file);
127
128 return ERR_OK;
129 }
130