2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "LoadVariablesThread.h"
21 #include "IOChannel.h"
23 #include "GnashException.h"
27 #include <boost/scoped_array.hpp>
29 //#define DEBUG_LOAD_VARIABLES 1
34 LoadVariablesThread::completeLoad()
36 #ifdef DEBUG_LOAD_VARIABLES
37 log_debug("completeLoad called");
41 // TODO: how to set _bytesTotal ?
43 // this is going to override any previous setting,
44 // better do this inside a subclass (in a separate thread)
46 _bytesTotal
= _stream
->size();
50 const size_t chunkSize
= 1024;
51 boost::scoped_array
<char> buf(new char[chunkSize
]);
52 unsigned int parsedLines
= 0;
53 // TODO: use read_string ?
54 while ( size_t bytesRead
= _stream
->read(buf
.get(), chunkSize
) )
56 #ifdef DEBUG_LOAD_VARIABLES
57 log_debug("Read %u bytes", bytesRead
);
62 std::string
chunk(buf
.get(), bytesRead
);
67 size_t dataSize
= bytesRead
;
68 utf8::TextEncoding encoding
;
69 char* ptr
= utf8::stripBOM(buf
.get(), dataSize
,
71 if ( encoding
!= utf8::encUTF8
&&
72 encoding
!= utf8::encUNSPECIFIED
)
74 log_unimpl(_("%s to UTF8 conversion in "
75 "MovieClip.loadVariables "
77 utf8::textEncodingName(encoding
));
79 std::string
chunk(ptr
, dataSize
);
83 #ifdef DEBUG_LOAD_VARIABLES
84 log_debug("toparse: %s", toparse
);
88 size_t lastamp
= toparse
.rfind('&');
89 if ( lastamp
!= std::string::npos
)
91 std::string parseable
= toparse
.substr(0, lastamp
);
92 #ifdef DEBUG_LOAD_VARIABLES
93 log_debug("parseable: %s", parseable
);
96 toparse
= toparse
.substr(lastamp
+1);
97 #ifdef DEBUG_LOAD_VARIABLES
98 log_debug("toparse nextline: %s", toparse
);
103 _bytesLoaded
+= bytesRead
;
106 if ( _stream
->eof() ) break;
108 if ( cancelRequested() ) {
109 log_debug("Cancelling LoadVariables download thread...");
115 if ( ! toparse
.empty() ) {
120 _stream
->go_to_end();
122 catch (IOException
& ex
) {
123 log_error(_("Stream couldn't seek to end: %s"), ex
.what());
126 _bytesLoaded
= _stream
->tell();
127 if ( _bytesTotal
!= _bytesLoaded
) {
128 log_error(_("Size of 'variables' stream advertised to be %d bytes,"
129 " but turned out to be %d bytes."),
130 _bytesTotal
, _bytesLoaded
);
131 _bytesTotal
= _bytesLoaded
;
134 _stream
.reset(); // we don't need the IOChannel anymore
136 //dispatchLoadEvent();
140 LoadVariablesThread::LoadVariablesThread(const StreamProvider
& sp
,
141 const URL
& url
, const std::string
& postdata
)
145 _stream(sp
.getStream(url
, postdata
)),
149 if ( ! _stream
.get() )
151 throw NetworkException();
155 LoadVariablesThread::LoadVariablesThread(const StreamProvider
& sp
,
160 _stream(sp
.getStream(url
)),
164 if ( ! _stream
.get() )
166 throw NetworkException();
171 LoadVariablesThread::cancel()
173 boost::mutex::scoped_lock
lock(_mutex
);
178 LoadVariablesThread::cancelRequested()
180 boost::mutex::scoped_lock
lock(_mutex
);
184 LoadVariablesThread::~LoadVariablesThread()