2
// Copyright (C) 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4
// This program is free software; you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation; either version 3 of the License, or
7
// (at your option) any later version.
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
14
// You should have received a copy of the GNU General Public License
15
// along with this program; if not, write to the Free Software
16
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
#include "gnashconfig.h"
23
#include "LoadVariablesThread.h"
24
#include "IOChannel.h"
26
#include "GnashException.h"
30
#include <boost/scoped_array.hpp>
32
//#define DEBUG_LOAD_VARIABLES 1
37
LoadVariablesThread::completeLoad()
39
#ifdef DEBUG_LOAD_VARIABLES
40
log_debug("completeLoad called");
44
// TODO: how to set _bytesTotal ?
46
// this is going to override any previous setting,
47
// better do this inside a subclass (in a separate thread)
49
_bytesTotal = _stream->size();
53
const size_t chunkSize = 1024;
54
boost::scoped_array<char> buf(new char[chunkSize]);
55
unsigned int parsedLines = 0;
56
// TODO: use read_string ?
57
while ( size_t bytesRead = _stream->read(buf.get(), chunkSize) )
59
#ifdef DEBUG_LOAD_VARIABLES
60
log_debug("Read %u bytes", bytesRead);
65
std::string chunk(buf.get(), bytesRead);
70
size_t dataSize = bytesRead;
71
utf8::TextEncoding encoding;
72
char* ptr = utf8::stripBOM(buf.get(), dataSize,
74
if ( encoding != utf8::encUTF8 &&
75
encoding != utf8::encUNSPECIFIED )
77
log_unimpl("%s to utf8 conversion in "
78
"MovieClip.loadVariables "
80
utf8::textEncodingName(encoding));
82
std::string chunk(ptr, dataSize);
86
#ifdef DEBUG_LOAD_VARIABLES
87
log_debug("toparse: %s", toparse);
91
size_t lastamp = toparse.rfind('&');
92
if ( lastamp != std::string::npos )
94
std::string parseable = toparse.substr(0, lastamp);
95
#ifdef DEBUG_LOAD_VARIABLES
96
log_debug("parseable: %s", parseable);
99
toparse = toparse.substr(lastamp+1);
100
#ifdef DEBUG_LOAD_VARIABLES
101
log_debug("toparse nextline: %s", toparse);
106
_bytesLoaded += bytesRead;
107
//dispatchDataEvent();
110
if ( _stream->eof() ) break;
112
if ( cancelRequested() )
114
log_debug("Cancelling LoadVariables download thread...");
120
if ( ! toparse.empty() )
125
_stream->go_to_end();
126
_bytesLoaded = _stream->tell();
127
if ( _bytesTotal != _bytesLoaded )
129
log_error("Size of stream variables were loaded from advertised to be %d bytes long,"
130
" but turned out to be only %d bytes long",
131
_bytesTotal, _bytesLoaded);
132
_bytesTotal = _bytesLoaded;
135
_stream.reset(); // we don't need the IOChannel anymore
137
//dispatchLoadEvent();
141
LoadVariablesThread::LoadVariablesThread(const URL& url, const std::string& postdata)
143
_stream(StreamProvider::getDefaultInstance().getStream(url, postdata)),
147
if ( ! _stream.get() )
149
throw NetworkException();
153
LoadVariablesThread::LoadVariablesThread(const URL& url)
155
_stream(StreamProvider::getDefaultInstance().getStream(url)),
159
if ( ! _stream.get() )
161
throw NetworkException();
166
LoadVariablesThread::cancel()
168
boost::mutex::scoped_lock lock(_mutex);
173
LoadVariablesThread::cancelRequested()
175
boost::mutex::scoped_lock lock(_mutex);
179
LoadVariablesThread::~LoadVariablesThread()