don't use CROSS_FLAGS for executables, as -all-staic prevents Android from looking...
[gnash.git] / libcore / LoadVariablesThread.cpp
blob2f70edac32dca00bd91ec0111bef63bb762a90d1
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software 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;
105 // eof, get out !
106 if ( _stream->eof() ) break;
108 if ( cancelRequested() ) {
109 log_debug("Cancelling LoadVariables download thread...");
110 _stream.reset();
111 return;
115 if ( ! toparse.empty() ) {
116 parse(toparse);
119 try {
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();
137 setCompleted();
140 LoadVariablesThread::LoadVariablesThread(const StreamProvider& sp,
141 const URL& url, const std::string& postdata)
143 _bytesLoaded(0),
144 _bytesTotal(0),
145 _stream(sp.getStream(url, postdata)),
146 _completed(false),
147 _canceled(false)
149 if ( ! _stream.get() )
151 throw NetworkException();
155 LoadVariablesThread::LoadVariablesThread(const StreamProvider& sp,
156 const URL& url)
158 _bytesLoaded(0),
159 _bytesTotal(0),
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