comparison data/fileio/FileSource.cpp @ 686:b4a8d8221eaf debug-output

Remove most toStdString calls (no longer needed, with debug header)
author Chris Cannam
date Thu, 12 May 2011 17:31:24 +0100
parents 948271d124ac
children 06f13a3b9e9e
comparison
equal deleted inserted replaced
685:99222d4bfc78 686:b4a8d8221eaf
86 if (m_url.toString() == "") { 86 if (m_url.toString() == "") {
87 m_url = QUrl(fileOrUrl, QUrl::TolerantMode); 87 m_url = QUrl(fileOrUrl, QUrl::TolerantMode);
88 } 88 }
89 89
90 #ifdef DEBUG_FILE_SOURCE 90 #ifdef DEBUG_FILE_SOURCE
91 std::cerr << "FileSource::FileSource(" << fileOrUrl.toStdString() << "): url <" << m_url.toString().toStdString() << ">" << std::endl; 91 std::cerr << "FileSource::FileSource(" << fileOrUrl << "): url <" << m_url.toString() << ">" << std::endl;
92 incCount(m_url.toString()); 92 incCount(m_url.toString());
93 #endif 93 #endif
94 94
95 if (!canHandleScheme(m_url)) { 95 if (!canHandleScheme(m_url)) {
96 std::cerr << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString().toStdString() << "\"" << std::endl; 96 std::cerr << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString() << "\"" << std::endl;
97 m_errorString = tr("Unsupported scheme in URL"); 97 m_errorString = tr("Unsupported scheme in URL");
98 return; 98 return;
99 } 99 }
100 100
101 init(); 101 init();
102 102
103 if (!isRemote() && 103 if (!isRemote() &&
104 !isAvailable()) { 104 !isAvailable()) {
105 #ifdef DEBUG_FILE_SOURCE 105 #ifdef DEBUG_FILE_SOURCE
106 std::cerr << "FileSource::FileSource: Failed to open local file with URL \"" << m_url.toString().toStdString() << "; trying again assuming filename was encoded" << std::endl; 106 std::cerr << "FileSource::FileSource: Failed to open local file with URL \"" << m_url.toString() << "; trying again assuming filename was encoded" << std::endl;
107 #endif 107 #endif
108 m_url = QUrl::fromEncoded(fileOrUrl.toAscii()); 108 m_url = QUrl::fromEncoded(fileOrUrl.toAscii());
109 init(); 109 init();
110 } 110 }
111 111
164 m_leaveLocalFile(false), 164 m_leaveLocalFile(false),
165 m_reporter(reporter), 165 m_reporter(reporter),
166 m_refCounted(false) 166 m_refCounted(false)
167 { 167 {
168 #ifdef DEBUG_FILE_SOURCE 168 #ifdef DEBUG_FILE_SOURCE
169 std::cerr << "FileSource::FileSource(" << url.toString().toStdString() << ") [as url]" << std::endl; 169 std::cerr << "FileSource::FileSource(" << url.toString() << ") [as url]" << std::endl;
170 incCount(m_url.toString()); 170 incCount(m_url.toString());
171 #endif 171 #endif
172 172
173 if (!canHandleScheme(m_url)) { 173 if (!canHandleScheme(m_url)) {
174 std::cerr << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString().toStdString() << "\"" << std::endl; 174 std::cerr << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString() << "\"" << std::endl;
175 m_errorString = tr("Unsupported scheme in URL"); 175 m_errorString = tr("Unsupported scheme in URL");
176 return; 176 return;
177 } 177 }
178 178
179 init(); 179 init();
196 m_leaveLocalFile(false), 196 m_leaveLocalFile(false),
197 m_reporter(rf.m_reporter), 197 m_reporter(rf.m_reporter),
198 m_refCounted(false) 198 m_refCounted(false)
199 { 199 {
200 #ifdef DEBUG_FILE_SOURCE 200 #ifdef DEBUG_FILE_SOURCE
201 std::cerr << "FileSource::FileSource(" << m_url.toString().toStdString() << ") [copy ctor]" << std::endl; 201 std::cerr << "FileSource::FileSource(" << m_url.toString() << ") [copy ctor]" << std::endl;
202 incCount(m_url.toString()); 202 incCount(m_url.toString());
203 #endif 203 #endif
204 204
205 if (!canHandleScheme(m_url)) { 205 if (!canHandleScheme(m_url)) {
206 std::cerr << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString().toStdString() << "\"" << std::endl; 206 std::cerr << "FileSource::FileSource: ERROR: Unsupported scheme in URL \"" << m_url.toString() << "\"" << std::endl;
207 m_errorString = tr("Unsupported scheme in URL"); 207 m_errorString = tr("Unsupported scheme in URL");
208 return; 208 return;
209 } 209 }
210 210
211 if (!isRemote()) { 211 if (!isRemote()) {
230 } 230 }
231 231
232 m_done = true; 232 m_done = true;
233 233
234 #ifdef DEBUG_FILE_SOURCE 234 #ifdef DEBUG_FILE_SOURCE
235 std::cerr << "FileSource::FileSource(" << m_url.toString().toStdString() << ") [copy ctor]: note: local filename is \"" << m_localFilename.toStdString() << "\"" << std::endl; 235 std::cerr << "FileSource::FileSource(" << m_url.toString() << ") [copy ctor]: note: local filename is \"" << m_localFilename << "\"" << std::endl;
236 #endif 236 #endif
237 237
238 #ifdef DEBUG_FILE_SOURCE 238 #ifdef DEBUG_FILE_SOURCE
239 std::cerr << "FileSource::FileSource(copy ctor) exiting" << std::endl; 239 std::cerr << "FileSource::FileSource(copy ctor) exiting" << std::endl;
240 #endif 240 #endif
241 } 241 }
242 242
243 FileSource::~FileSource() 243 FileSource::~FileSource()
244 { 244 {
245 #ifdef DEBUG_FILE_SOURCE 245 #ifdef DEBUG_FILE_SOURCE
246 std::cerr << "FileSource(" << m_url.toString().toStdString() << ")::~FileSource" << std::endl; 246 std::cerr << "FileSource(" << m_url.toString() << ")::~FileSource" << std::endl;
247 decCount(m_url.toString()); 247 decCount(m_url.toString());
248 #endif 248 #endif
249 249
250 cleanup(); 250 cleanup();
251 251
268 } 268 }
269 m_localFilename = QFileInfo(m_localFilename).absoluteFilePath(); 269 m_localFilename = QFileInfo(m_localFilename).absoluteFilePath();
270 270
271 #ifdef DEBUG_FILE_SOURCE 271 #ifdef DEBUG_FILE_SOURCE
272 std::cerr << "FileSource::init: URL translates to local filename \"" 272 std::cerr << "FileSource::init: URL translates to local filename \""
273 << m_localFilename.toStdString() << "\" (with literal=" << literal << ")" << std::endl; 273 << m_localFilename << "\" (with literal=" << literal << ")" << std::endl;
274 #endif 274 #endif
275 m_ok = true; 275 m_ok = true;
276 m_lastStatus = 200; 276 m_lastStatus = 200;
277 277
278 if (!QFileInfo(m_localFilename).exists()) { 278 if (!QFileInfo(m_localFilename).exists()) {
316 316
317 QString scheme = m_url.scheme().toLower(); 317 QString scheme = m_url.scheme().toLower();
318 318
319 #ifdef DEBUG_FILE_SOURCE 319 #ifdef DEBUG_FILE_SOURCE
320 std::cerr << "FileSource::init: Don't have local copy of \"" 320 std::cerr << "FileSource::init: Don't have local copy of \""
321 << m_url.toString().toStdString() << "\", retrieving" << std::endl; 321 << m_url.toString() << "\", retrieving" << std::endl;
322 #endif 322 #endif
323 323
324 if (scheme == "http") { 324 if (scheme == "http") {
325 initHttp(); 325 initHttp();
326 #ifdef DEBUG_FILE_SOURCE 326 #ifdef DEBUG_FILE_SOURCE
425 425
426 QString path = "/" + QString(m_url.toEncoded()).section('/', 3); 426 QString path = "/" + QString(m_url.toEncoded()).section('/', 3);
427 427
428 #ifdef DEBUG_FILE_SOURCE 428 #ifdef DEBUG_FILE_SOURCE
429 std::cerr << "FileSource: path is \"" 429 std::cerr << "FileSource: path is \""
430 << path.toStdString() << "\"" << std::endl; 430 << path << "\"" << std::endl;
431 #endif 431 #endif
432 432
433 if (m_preferredContentType == "") { 433 if (m_preferredContentType == "") {
434 m_http->get(path, m_localFile); 434 m_http->get(path, m_localFile);
435 } else { 435 } else {
436 #ifdef DEBUG_FILE_SOURCE 436 #ifdef DEBUG_FILE_SOURCE
437 std::cerr << "FileSource: indicating preferred content type of \"" 437 std::cerr << "FileSource: indicating preferred content type of \""
438 << m_preferredContentType.toStdString() << "\"" << std::endl; 438 << m_preferredContentType << "\"" << std::endl;
439 #endif 439 #endif
440 QHttpRequestHeader header("GET", path); 440 QHttpRequestHeader header("GET", path);
441 header.setValue("Host", m_url.host()); 441 header.setValue("Host", m_url.host());
442 header.setValue("Accept", QString("%1, */*").arg(m_preferredContentType)); 442 header.setValue("Accept", QString("%1, */*").arg(m_preferredContentType));
443 m_http->request(header, 0, m_localFile); 443 m_http->request(header, 0, m_localFile);
632 632
633 if (resp.statusCode() / 100 == 3) { 633 if (resp.statusCode() / 100 == 3) {
634 QString location = resp.value("Location"); 634 QString location = resp.value("Location");
635 #ifdef DEBUG_FILE_SOURCE 635 #ifdef DEBUG_FILE_SOURCE
636 std::cerr << "FileSource::responseHeaderReceived: redirect to \"" 636 std::cerr << "FileSource::responseHeaderReceived: redirect to \""
637 << location.toStdString() << "\" received" << std::endl; 637 << location << "\" received" << std::endl;
638 #endif 638 #endif
639 if (location != "") { 639 if (location != "") {
640 QUrl newUrl(location); 640 QUrl newUrl(location);
641 if (newUrl != m_url) { 641 if (newUrl != m_url) {
642 cleanup(); 642 cleanup();
660 if (m_lastStatus / 100 >= 4) { 660 if (m_lastStatus / 100 >= 4) {
661 m_errorString = QString("%1 %2") 661 m_errorString = QString("%1 %2")
662 .arg(resp.statusCode()).arg(resp.reasonPhrase()); 662 .arg(resp.statusCode()).arg(resp.reasonPhrase());
663 #ifdef DEBUG_FILE_SOURCE 663 #ifdef DEBUG_FILE_SOURCE
664 std::cerr << "FileSource::responseHeaderReceived: " 664 std::cerr << "FileSource::responseHeaderReceived: "
665 << m_errorString.toStdString() << std::endl; 665 << m_errorString << std::endl;
666 #endif 666 #endif
667 } else { 667 } else {
668 #ifdef DEBUG_FILE_SOURCE 668 #ifdef DEBUG_FILE_SOURCE
669 std::cerr << "FileSource::responseHeaderReceived: " 669 std::cerr << "FileSource::responseHeaderReceived: "
670 << m_lastStatus << std::endl; 670 << m_lastStatus << std::endl;
774 774
775 void 775 void
776 FileSource::deleteCacheFile() 776 FileSource::deleteCacheFile()
777 { 777 {
778 #ifdef DEBUG_FILE_SOURCE 778 #ifdef DEBUG_FILE_SOURCE
779 std::cerr << "FileSource::deleteCacheFile(\"" << m_localFilename.toStdString() << "\")" << std::endl; 779 std::cerr << "FileSource::deleteCacheFile(\"" << m_localFilename << "\")" << std::endl;
780 #endif 780 #endif
781 781
782 cleanup(); 782 cleanup();
783 783
784 if (m_localFilename == "") { 784 if (m_localFilename == "") {
811 811
812 m_fileCreationMutex.lock(); 812 m_fileCreationMutex.lock();
813 813
814 if (!QFile(m_localFilename).remove()) { 814 if (!QFile(m_localFilename).remove()) {
815 #ifdef DEBUG_FILE_SOURCE 815 #ifdef DEBUG_FILE_SOURCE
816 std::cerr << "FileSource::deleteCacheFile: ERROR: Failed to delete file \"" << m_localFilename.toStdString() << "\"" << std::endl; 816 std::cerr << "FileSource::deleteCacheFile: ERROR: Failed to delete file \"" << m_localFilename << "\"" << std::endl;
817 #endif 817 #endif
818 } else { 818 } else {
819 #ifdef DEBUG_FILE_SOURCE 819 #ifdef DEBUG_FILE_SOURCE
820 std::cerr << "FileSource::deleteCacheFile: Deleted cache file \"" << m_localFilename.toStdString() << "\"" << std::endl; 820 std::cerr << "FileSource::deleteCacheFile: Deleted cache file \"" << m_localFilename << "\"" << std::endl;
821 #endif 821 #endif
822 m_localFilename = ""; 822 m_localFilename = "";
823 } 823 }
824 824
825 m_fileCreationMutex.unlock(); 825 m_fileCreationMutex.unlock();
879 } 879 }
880 880
881 QString filepath(dir.filePath(filename)); 881 QString filepath(dir.filePath(filename));
882 882
883 #ifdef DEBUG_FILE_SOURCE 883 #ifdef DEBUG_FILE_SOURCE
884 std::cerr << "FileSource::createCacheFile: URL is \"" << m_url.toString().toStdString() << "\", dir is \"" << dir.path().toStdString() << "\", base \"" << base.toStdString() << "\", extension \"" << extension.toStdString() << "\", filebase \"" << filename.toStdString() << "\", filename \"" << filepath.toStdString() << "\"" << std::endl; 884 std::cerr << "FileSource::createCacheFile: URL is \"" << m_url.toString() << "\", dir is \"" << dir.path() << "\", base \"" << base << "\", extension \"" << extension << "\", filebase \"" << filename << "\", filename \"" << filepath << "\"" << std::endl;
885 #endif 885 #endif
886 886
887 QMutexLocker fcLocker(&m_fileCreationMutex); 887 QMutexLocker fcLocker(&m_fileCreationMutex);
888 888
889 ++m_count; 889 ++m_count;
891 if (QFileInfo(filepath).exists() || 891 if (QFileInfo(filepath).exists() ||
892 !QFile(filepath).open(QFile::WriteOnly)) { 892 !QFile(filepath).open(QFile::WriteOnly)) {
893 893
894 #ifdef DEBUG_FILE_SOURCE 894 #ifdef DEBUG_FILE_SOURCE
895 std::cerr << "FileSource::createCacheFile: Failed to create local file \"" 895 std::cerr << "FileSource::createCacheFile: Failed to create local file \""
896 << filepath.toStdString() << "\" for URL \"" 896 << filepath << "\" for URL \""
897 << m_url.toString().toStdString() << "\" (or file already exists): appending suffix instead" << std::endl; 897 << m_url.toString() << "\" (or file already exists): appending suffix instead" << std::endl;
898 #endif 898 #endif
899 899
900 if (extension == "") { 900 if (extension == "") {
901 filename = QString("%1_%2").arg(base).arg(m_count); 901 filename = QString("%1_%2").arg(base).arg(m_count);
902 } else { 902 } else {
907 if (QFileInfo(filepath).exists() || 907 if (QFileInfo(filepath).exists() ||
908 !QFile(filepath).open(QFile::WriteOnly)) { 908 !QFile(filepath).open(QFile::WriteOnly)) {
909 909
910 #ifdef DEBUG_FILE_SOURCE 910 #ifdef DEBUG_FILE_SOURCE
911 std::cerr << "FileSource::createCacheFile: ERROR: Failed to create local file \"" 911 std::cerr << "FileSource::createCacheFile: ERROR: Failed to create local file \""
912 << filepath.toStdString() << "\" for URL \"" 912 << filepath << "\" for URL \""
913 << m_url.toString().toStdString() << "\" (or file already exists)" << std::endl; 913 << m_url.toString() << "\" (or file already exists)" << std::endl;
914 #endif 914 #endif
915 915
916 return ""; 916 return "";
917 } 917 }
918 } 918 }
919 919
920 #ifdef DEBUG_FILE_SOURCE 920 #ifdef DEBUG_FILE_SOURCE
921 std::cerr << "FileSource::createCacheFile: url " 921 std::cerr << "FileSource::createCacheFile: url "
922 << m_url.toString().toStdString() << " -> local filename " 922 << m_url.toString() << " -> local filename "
923 << filepath.toStdString() << std::endl; 923 << filepath << std::endl;
924 #endif 924 #endif
925 925
926 m_localFilename = filepath; 926 m_localFilename = filepath;
927 927
928 return false; 928 return false;