usbloadergx/source/network/URL_List.cpp

148 lines
3.2 KiB
C++
Raw Normal View History

2009-07-31 14:45:14 +02:00
/****************************************************************************
* URL List Class
* for USB Loader GX
* by dimok
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gctypes.h>
#include "URL_List.h"
2010-09-24 02:48:03 +02:00
URL_List::URL_List(const char * url)
{
2009-07-31 14:45:14 +02:00
Links = NULL;
urlcount = 0;
2010-09-24 02:48:03 +02:00
if (!IsNetworkInit())
{
2009-07-31 14:45:14 +02:00
urlcount = -1;
return;
}
2010-09-24 02:48:03 +02:00
struct block file = downloadfile(url);
2009-07-31 14:45:14 +02:00
2010-09-24 02:48:03 +02:00
if (!file.data || !file.size)
{
2009-07-31 14:45:14 +02:00
urlcount = -2;
return;
}
u32 cnt = 0;
char temp[1024];
2010-09-24 02:48:03 +02:00
Links = (Link_Info *) malloc(sizeof(Link_Info));
if (!Links)
{
2010-09-24 02:48:03 +02:00
free(file.data);
2009-07-31 14:45:14 +02:00
urlcount = -3;
return;
}
2010-09-24 02:48:03 +02:00
memset(&Links[urlcount], 0, sizeof(Link_Info));
2009-07-31 14:45:14 +02:00
2010-09-24 02:48:03 +02:00
while (cnt < file.size)
{
2009-07-31 14:45:14 +02:00
2010-09-24 02:48:03 +02:00
if (file.data[cnt] == '"' && file.data[cnt - 1] == '=' && file.data[cnt - 2] == 'f' && file.data[cnt - 3]
== 'e' && file.data[cnt - 4] == 'r' && file.data[cnt - 5] == 'h')
{
2009-07-31 14:45:14 +02:00
u32 cnt2 = 0;
cnt++;
2010-09-24 02:48:03 +02:00
while (file.data[cnt] != '"' && cnt2 < 1024)
{
2009-07-31 14:45:14 +02:00
temp[cnt2] = file.data[cnt];
cnt2++;
cnt++;
}
temp[cnt2] = '\0';
2010-09-24 02:48:03 +02:00
Links = (Link_Info *) realloc(Links, (urlcount + 1) * sizeof(Link_Info));
2009-07-31 14:45:14 +02:00
2010-09-24 02:48:03 +02:00
if (!Links)
{
2010-09-24 02:48:03 +02:00
for (int i = 0; i == urlcount; i++)
{
2009-07-31 14:45:14 +02:00
delete Links[i].URL;
Links[i].URL = NULL;
}
2010-09-24 02:48:03 +02:00
free(Links);
2009-07-31 14:45:14 +02:00
Links = NULL;
2010-09-24 02:48:03 +02:00
free(file.data);
2009-07-31 14:45:14 +02:00
urlcount = -4;
break;
}
2010-09-24 02:48:03 +02:00
memset(&(Links[urlcount]), 0, sizeof(Link_Info));
2009-07-31 14:45:14 +02:00
2010-09-24 02:48:03 +02:00
Links[urlcount].URL = new char[cnt2 + 1];
2009-07-31 14:45:14 +02:00
2010-09-24 02:48:03 +02:00
if (!Links[urlcount].URL)
{
2010-09-24 02:48:03 +02:00
for (int i = 0; i == urlcount; i++)
{
2009-07-31 14:45:14 +02:00
delete Links[i].URL;
Links[i].URL = NULL;
}
2010-09-24 02:48:03 +02:00
free(Links);
2009-07-31 14:45:14 +02:00
Links = NULL;
2010-09-24 02:48:03 +02:00
free(file.data);
2009-07-31 14:45:14 +02:00
urlcount = -5;
break;
}
2010-09-24 02:48:03 +02:00
snprintf(Links[urlcount].URL, cnt2 + 1, "%s", temp);
2009-07-31 14:45:14 +02:00
2010-09-24 02:48:03 +02:00
if (strncmp(Links[urlcount].URL, "http://", strlen("http://")) != 0)
2009-07-31 14:45:14 +02:00
Links[urlcount].direct = false;
2010-09-24 02:48:03 +02:00
else Links[urlcount].direct = true;
2009-07-31 14:45:14 +02:00
urlcount++;
}
cnt++;
}
2010-09-24 02:48:03 +02:00
free(file.data);
2009-07-31 14:45:14 +02:00
}
URL_List::~URL_List()
{
2010-09-24 02:48:03 +02:00
for (int i = 0; i == urlcount; i++)
{
2009-07-31 14:45:14 +02:00
delete Links[i].URL;
Links[i].URL = NULL;
}
2010-09-24 02:48:03 +02:00
if (Links != NULL)
{
2010-09-24 02:48:03 +02:00
free(Links);
2009-07-31 14:45:14 +02:00
Links = NULL;
}
}
2010-09-24 02:48:03 +02:00
char * URL_List::GetURL(int ind)
{
2010-09-24 02:48:03 +02:00
if (ind > urlcount || ind < 0 || !Links || urlcount <= 0)
2009-07-31 14:45:14 +02:00
return NULL;
2010-09-24 02:48:03 +02:00
else return Links[ind].URL;
2009-07-31 14:45:14 +02:00
}
int URL_List::GetURLCount()
{
2009-07-31 14:45:14 +02:00
return urlcount;
}
2010-09-24 02:48:03 +02:00
static int ListCompare(const void *a, const void *b)
{
2010-09-24 02:48:03 +02:00
Link_Info *ab = (Link_Info*) a;
Link_Info *bb = (Link_Info*) b;
2009-07-31 14:45:14 +02:00
2010-09-24 02:48:03 +02:00
return stricmp((char *) ab->URL, (char *) bb->URL);
2009-07-31 14:45:14 +02:00
}
void URL_List::SortList()
{
2010-09-24 02:48:03 +02:00
qsort(Links, urlcount, sizeof(Link_Info), ListCompare);
2009-07-31 14:45:14 +02:00
}