2010-09-19 01:16:05 +02:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2010
|
|
|
|
* by Dimok
|
|
|
|
*
|
|
|
|
* This software is provided 'as-is', without any express or implied
|
|
|
|
* warranty. In no event will the authors be held liable for any
|
|
|
|
* damages arising from the use of this software.
|
|
|
|
*
|
|
|
|
* Permission is granted to anyone to use this software for any
|
|
|
|
* purpose, including commercial applications, and to alter it and
|
|
|
|
* redistribute it freely, subject to the following restrictions:
|
|
|
|
*
|
|
|
|
* 1. The origin of this software must not be misrepresented; you
|
|
|
|
* must not claim that you wrote the original software. If you use
|
|
|
|
* this software in a product, an acknowledgment in the product
|
|
|
|
* documentation would be appreciated but is not required.
|
|
|
|
*
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and
|
|
|
|
* must not be misrepresented as being the original software.
|
|
|
|
*
|
|
|
|
* 3. This notice may not be removed or altered from any source
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* HTML_Stream Class
|
|
|
|
*
|
|
|
|
* for WiiXplorer 2010
|
|
|
|
***************************************************************************/
|
2010-05-30 09:00:29 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "HTML_Stream.h"
|
|
|
|
#include "networkops.h"
|
|
|
|
#include "http.h"
|
|
|
|
|
|
|
|
#define htmlstringcompare(text, cmp, pos) strncasecmp((const char*) &text[pos], (const char*) cmp, strlen((const char*) cmp))
|
|
|
|
|
|
|
|
|
|
|
|
HTML_Stream::HTML_Stream()
|
|
|
|
{
|
|
|
|
HTML_File = NULL;
|
|
|
|
position = 0;
|
|
|
|
filesize = 0;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
HTML_Stream::HTML_Stream( const char * url )
|
2010-05-30 09:00:29 +02:00
|
|
|
{
|
|
|
|
HTML_File = NULL;
|
|
|
|
position = 0;
|
|
|
|
filesize = 0;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
LoadLink( url );
|
2010-05-30 09:00:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HTML_Stream::~HTML_Stream()
|
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( HTML_File )
|
|
|
|
free( HTML_File );
|
2010-05-30 09:00:29 +02:00
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
bool HTML_Stream::LoadLink( const char * url )
|
2010-05-30 09:00:29 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !IsNetworkInit() )
|
2010-05-30 09:00:29 +02:00
|
|
|
return false;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
struct block file = downloadfile( url );
|
2010-05-30 09:00:29 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !file.data || !file.size )
|
2010-05-30 09:00:29 +02:00
|
|
|
return false;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( HTML_File )
|
|
|
|
free( HTML_File );
|
2010-05-30 09:00:29 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
HTML_File = ( char * ) file.data;
|
2010-05-30 09:00:29 +02:00
|
|
|
filesize = file.size;
|
|
|
|
position = 0;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
const char * HTML_Stream::FindStringStart( const char * string )
|
2010-05-30 09:00:29 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !HTML_File )
|
2010-05-30 09:00:29 +02:00
|
|
|
return NULL;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
while ( ( u32 ) position < filesize )
|
2010-05-30 09:00:29 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( htmlstringcompare( HTML_File, string, position ) == 0 )
|
2010-05-30 09:00:29 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
position++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &HTML_File[position];
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
const char * HTML_Stream::FindStringEnd( const char * string )
|
2010-05-30 09:00:29 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !HTML_File )
|
2010-05-30 09:00:29 +02:00
|
|
|
return NULL;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
while ( ( u32 ) position < filesize )
|
2010-05-30 09:00:29 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( htmlstringcompare( HTML_File, string, position ) == 0 )
|
2010-05-30 09:00:29 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
position++;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( ( u32 ) position >= filesize )
|
2010-05-30 09:00:29 +02:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
position += strlen( string );
|
2010-05-30 09:00:29 +02:00
|
|
|
|
|
|
|
return &HTML_File[position];
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
char * HTML_Stream::CopyString( const char * stopat )
|
2010-05-30 09:00:29 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !stopat || !HTML_File )
|
2010-05-30 09:00:29 +02:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
u32 blocksize = 1024;
|
|
|
|
u32 counter = 0;
|
|
|
|
u32 allocatedsize = 0;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
char * outtext = ( char* ) malloc( blocksize );
|
|
|
|
if ( !outtext )
|
2010-05-30 09:00:29 +02:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
allocatedsize = blocksize;
|
2010-09-19 01:16:05 +02:00
|
|
|
memset( outtext, 0, blocksize );
|
2010-05-30 09:00:29 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
while ( ( htmlstringcompare( HTML_File, stopat, position ) != 0 ) && ( position + strlen( stopat ) < filesize ) )
|
2010-05-30 09:00:29 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( counter > blocksize )
|
2010-05-30 09:00:29 +02:00
|
|
|
{
|
|
|
|
blocksize += 1024;
|
2010-09-19 01:16:05 +02:00
|
|
|
char * tmpblock = ( char* ) realloc( outtext, blocksize );
|
|
|
|
if ( !tmpblock )
|
2010-05-30 09:00:29 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
free( outtext );
|
2010-05-30 09:00:29 +02:00
|
|
|
outtext = NULL;
|
2010-09-19 01:16:05 +02:00
|
|
|
free( tmpblock );
|
2010-05-30 09:00:29 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
outtext = tmpblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
outtext[counter] = HTML_File[position];
|
|
|
|
position++;
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
outtext[counter] = '\0';
|
2010-09-19 01:16:05 +02:00
|
|
|
outtext = ( char* ) realloc( outtext, counter + 1 );
|
2010-05-30 09:00:29 +02:00
|
|
|
|
|
|
|
return outtext;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
int HTML_Stream::Seek( u32 pos, int origin )
|
2010-05-30 09:00:29 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !HTML_File )
|
2010-05-30 09:00:29 +02:00
|
|
|
return -1;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
switch ( origin )
|
2010-05-30 09:00:29 +02:00
|
|
|
{
|
|
|
|
case SEEK_SET:
|
|
|
|
position = pos;
|
|
|
|
break;
|
|
|
|
case SEEK_CUR:
|
|
|
|
position += pos;
|
|
|
|
break;
|
|
|
|
case SEEK_END:
|
2010-09-19 01:16:05 +02:00
|
|
|
position = filesize + pos;
|
2010-05-30 09:00:29 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTML_Stream::Rewind()
|
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !HTML_File )
|
2010-05-30 09:00:29 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
position = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int HTML_Stream::GetPosition()
|
|
|
|
{
|
|
|
|
return position;
|
|
|
|
}
|