Fix test for bug #32625
[gnash.git] / libcore / LoadVariablesThread.cpp
blobaf04fc5d851bb175e65ca2671735ec140bd53923
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
3 // Foundation, Inc
4 //
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.
9 //
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"
22 #include "log.h"
23 #include "GnashException.h"
24 #include "utf8.h"
26 #include <string>
27 #include <boost/scoped_array.hpp>
29 //#define DEBUG_LOAD_VARIABLES 1
31 namespace gnash {
33 void
34 LoadVariablesThread::completeLoad()
36 #ifdef DEBUG_LOAD_VARIABLES
37 log_debug("completeLoad called");
38 #endif
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)
45 _bytesLoaded = 0;
46 _bytesTotal = _stream->size();
48 std::string toparse;
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);
58 #endif
60 if ( _bytesLoaded )
62 std::string chunk(buf.get(), bytesRead);
63 toparse += chunk;
65 else
67 size_t dataSize = bytesRead;
68 utf8::TextEncoding encoding;
69 char* ptr = utf8::stripBOM(buf.get(), dataSize,
70 encoding);
71 if ( encoding != utf8::encUTF8 &&
72 encoding != utf8::encUNSPECIFIED )
74 log_unimpl("%s to utf8 conversion in "
75 "MovieClip.loadVariables "
76 "input parsing",
77 utf8::textEncodingName(encoding));
79 std::string chunk(ptr, dataSize);
80 toparse += chunk;
83 #ifdef DEBUG_LOAD_VARIABLES
84 log_debug("toparse: %s", toparse);
85 #endif
87 // parse remainder
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);
94 #endif
95 parse(parseable);
96 toparse = toparse.substr(lastamp+1);
97 #ifdef DEBUG_LOAD_VARIABLES
98 log_debug("toparse nextline: %s", toparse);
99 #endif
100 ++parsedLines;
103 _bytesLoaded += bytesRead;
104 //dispatchDataEvent();
106 // eof, get out !
107 if ( _stream->eof() ) break;
109 if ( cancelRequested() )
111 log_debug("Cancelling LoadVariables download thread...");
112 _stream.reset();
113 return;
117 if ( ! toparse.empty() )
119 parse(toparse);
122 try {
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();
141 setCompleted();
144 LoadVariablesThread::LoadVariablesThread(const StreamProvider& sp,
145 const URL& url, const std::string& postdata)
147 _stream(sp.getStream(url, postdata)),
148 _completed(false),
149 _canceled(false)
151 if ( ! _stream.get() )
153 throw NetworkException();
157 LoadVariablesThread::LoadVariablesThread(const StreamProvider& sp,
158 const URL& url)
160 _stream(sp.getStream(url)),
161 _completed(false),
162 _canceled(false)
164 if ( ! _stream.get() )
166 throw NetworkException();
170 void
171 LoadVariablesThread::cancel()
173 boost::mutex::scoped_lock lock(_mutex);
174 _canceled = true;
177 bool
178 LoadVariablesThread::cancelRequested()
180 boost::mutex::scoped_lock lock(_mutex);
181 return _canceled;
184 LoadVariablesThread::~LoadVariablesThread()
186 if ( _thread.get() )
188 cancel();
189 _thread->join();
190 _thread.reset();
195 } // namespace gnash