2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
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
;
104 //dispatchDataEvent();
107 if ( _stream
->eof() ) break;
109 if ( cancelRequested() )
111 log_debug("Cancelling LoadVariables download thread...");
117 if ( ! toparse
.empty() )
123 _stream
->go_to_end();
125 catch (IOException
& ex
) {
126 log_error("Stream couldn't seek to end: %s", ex
.what());
129 _bytesLoaded
= _stream
->tell();
130 if ( _bytesTotal
!= _bytesLoaded
)
132 log_error("Size of 'variables' stream advertised to be %d bytes,"
133 " but turned out to be %d bytes.",
134 _bytesTotal
, _bytesLoaded
);
135 _bytesTotal
= _bytesLoaded
;
138 _stream
.reset(); // we don't need the IOChannel anymore
140 //dispatchLoadEvent();
144 LoadVariablesThread::LoadVariablesThread(const StreamProvider
& sp
,
145 const URL
& url
, const std::string
& postdata
)
147 _stream(sp
.getStream(url
, postdata
)),
151 if ( ! _stream
.get() )
153 throw NetworkException();
157 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()