From 73baf50a4d8db3ea461f73758f169cbdbd835a1c Mon Sep 17 00:00:00 2001 From: ikemo Date: Fri, 2 Jan 2004 14:58:19 +0000 Subject: [PATCH] use pointer git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/kita/kita/trunk@771 56b19765-1e22-0410-a548-a0f45d66c51a --- kita/src/libkita/favoritethreads.cpp | 256 +++++++++++++++++----------------- kita/src/libkita/tests/threadtest.cpp | 51 +++---- kita/src/libkita/tests/threadtest.h | 2 +- kita/src/libkita/thread.cpp | 7 +- kita/src/libkita/thread.h | 6 +- 5 files changed, 161 insertions(+), 161 deletions(-) diff --git a/kita/src/libkita/favoritethreads.cpp b/kita/src/libkita/favoritethreads.cpp index 701d343..8071594 100644 --- a/kita/src/libkita/favoritethreads.cpp +++ b/kita/src/libkita/favoritethreads.cpp @@ -1,12 +1,12 @@ /*************************************************************************** - * Copyright (C) 2003 by Hideki Ikemoto * - * ikemo@users.sourceforge.jp * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - ***************************************************************************/ +* Copyright (C) 2003 by Hideki Ikemoto * +* ikemo@users.sourceforge.jp * +* * +* This program is free software; you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation; either version 2 of the License, or * +* (at your option) any later version. * +***************************************************************************/ #include "favoritethreads.h" @@ -17,174 +17,172 @@ FavoriteThreads* FavoriteThreads::instance = 0; FavoriteThreads::FavoriteThreads() -{ -} +{} FavoriteThreads::~FavoriteThreads() -{ -} +{} FavoriteThreads* FavoriteThreads::getInstance() { - if( ! instance ) { - instance = new FavoriteThreads(); - } - return instance; + if ( ! instance ) { + instance = new FavoriteThreads(); + } + return instance; } void FavoriteThreads::insert( const Kita::Thread* thread ) { - if( ! m_threadDict.find( thread->datURL() ) ) { - m_threadDict.insert( thread->datURL(), thread ); - } + if ( ! m_threadDict.find( thread->datURL() ) ) { + m_threadDict.insert( thread->datURL(), thread ); + } } void FavoriteThreads::remove( const QString& datURL ) { - m_threadDict.remove( datURL ); + m_threadDict.remove( datURL ); } bool FavoriteThreads::contains( const QString& datURL ) const { - if( m_threadDict.find( datURL ) ) { - return true; - } else { - return false; - } + if ( m_threadDict.find( datURL ) ) { + return true; + } else { + return false; + } } FavoriteThreads* FavoriteThreads::fromXml( const QString& xml ) { - FavoriteThreads* instance = FavoriteThreads::getInstance(); - - FavoritesXmlParser parser; - QXmlSimpleReader reader; - QXmlInputSource source; - source.setData( xml ); - reader.setContentHandler( &parser ); - reader.parse( &source ); - - if( parser.isValid() ) { - QPtrList threads = parser.getThreads(); - - instance->m_threadDict.clear(); - for( Kita::Thread* thread = threads.first(); thread; thread = threads.next() ) { - instance->insert( thread ); + FavoriteThreads * instance = FavoriteThreads::getInstance(); + + FavoritesXmlParser parser; + QXmlSimpleReader reader; + QXmlInputSource source; + source.setData( xml ); + reader.setContentHandler( &parser ); + reader.parse( &source ); + + if ( parser.isValid() ) { + QPtrList threads = parser.getThreads(); + + instance->m_threadDict.clear(); + for ( Kita::Thread * thread = threads.first(); thread; thread = threads.next() ) { + instance->insert( thread ); + } + } else { + kdError() << "inValid" << endl; } - } else { - kdError() << "inValid" << endl; - } - return instance; + return instance; } const QString FavoriteThreads::toXml() const { - QString ret; + QString ret; - ret += "\n"; + ret += "\n"; -// FavoriteThreads::const_iterator it; - QDictIterator it( threads() ); - for( ; it.current(); ++it ) { - ret += it.current()->toXmlFragment(); - } - ret += "\n"; + // FavoriteThreads::const_iterator it; + QDictIterator it( threads() ); + for ( ; it.current(); ++it ) { + ret += it.current() ->toXmlFragment(); + } + ret += "\n"; - return ret; + return ret; } bool FavoriteThreads::test() { - kdDebug() << "FavoriteThreads::test()" << endl; - - QString xml = "\n" - "\n" - "http://pc.2ch.net/linux/dat/1022744633.dat\n" - "2ch browser thread\n" - "\n" - "http://pc.2ch.net/linux/\n" - "Linux\n" - "\n" - "\n" - "\n" - "http://pc.2ch.net/unix/dat/956134538.dat\n" - "NeXT\n" - "\n" - "http://pc.2ch.net/unix/\n" - "Unix\n" - "\n" - "\n" - "\n"; - - FavoriteThreads* favorites = FavoriteThreads::fromXml( xml ); - QDict threads = favorites->threads(); - - if( threads.size() != 2 ) kdError() << "inValid: line=" << __LINE__ << endl; - if( favorites->contains( "http://pc.2ch.net/linux/dat/1022744633.dat" ) != true ) { - kdError() << "inValid: line=" << __LINE__; - } - if( favorites->contains( "http://pc.2ch.net/unix/dat/956134538.dat" ) != true ) { - kdError() << "inValid: line=" << __LINE__; - } - return true; + kdDebug() << "FavoriteThreads::test()" << endl; + + QString xml = "\n" + "\n" + "http://pc.2ch.net/linux/dat/1022744633.dat\n" + "2ch browser thread\n" + "\n" + "http://pc.2ch.net/linux/\n" + "Linux\n" + "\n" + "\n" + "\n" + "http://pc.2ch.net/unix/dat/956134538.dat\n" + "NeXT\n" + "\n" + "http://pc.2ch.net/unix/\n" + "Unix\n" + "\n" + "\n" + "\n"; + + FavoriteThreads* favorites = FavoriteThreads::fromXml( xml ); + QDict threads = favorites->threads(); + + if ( threads.size() != 2 ) kdError() << "inValid: line=" << __LINE__ << endl; + if ( favorites->contains( "http://pc.2ch.net/linux/dat/1022744633.dat" ) != true ) { + kdError() << "inValid: line=" << __LINE__; + } + if ( favorites->contains( "http://pc.2ch.net/unix/dat/956134538.dat" ) != true ) { + kdError() << "inValid: line=" << __LINE__; + } + return true; } bool FavoritesXmlParser::startElement( const QString& namespaceURI, const QString& localName, const QString& qName, const QXmlAttributes& atts ) { - if( m_inThread ) { - m_threadParser->startElement( namespaceURI, localName, qName, atts ); - } else if( m_inFavorites ) { - if( localName == "thread" ) { - m_inThread = true; - m_threadParser = new Kita::ThreadXmlParser(); - m_threadParser->startElement( namespaceURI, localName, qName, atts ); + if ( m_inThread ) { + m_threadParser->startElement( namespaceURI, localName, qName, atts ); + } else if ( m_inFavorites ) { + if ( localName == "thread" ) { + m_inThread = true; + m_threadParser = new Kita::ThreadXmlParser(); + m_threadParser->startElement( namespaceURI, localName, qName, atts ); + } else { + // error + return false; + } } else { - // error - return false; + if ( localName == "favorites" ) { + m_inFavorites = true; + } else { + // error + return false; + } } - } else { - if( localName == "favorites" ) { - m_inFavorites = true; - } else { - // error - return false; - } - } - return true; + return true; } bool FavoritesXmlParser::endElement( const QString& namespaceURI, const QString& localName, const QString& qName ) { - if( localName == "thread" ) { - m_inThread = false; - m_threadParser->endElement( namespaceURI, localName, qName ); - Kita::Thread thread = m_threadParser->getThread(); - Kita::Thread::setName( thread.datURL(), thread.name() ); - m_threadList.append( Kita::Thread::getByURL( thread.datURL() ) ); - KitaThreadInfo::setResNum( thread.datURL(), thread.resNum() ); - - delete m_threadParser; - m_threadParser = 0; - } else if( m_inThread ) { - m_threadParser->endElement( namespaceURI, localName, qName ); - } else if( localName == "favorites" ) { - m_inFavorites = false; - m_isValid = true; - } else { - // error - return false; - } - return true; + if ( localName == "thread" ) { + m_inThread = false; + m_threadParser->endElement( namespaceURI, localName, qName ); + Kita::Thread* thread = m_threadParser->getThread(); + Kita::Thread::setName( thread->datURL(), thread->name() ); + m_threadList.append( Kita::Thread::getByURL( thread->datURL() ) ); + KitaThreadInfo::setResNum( thread->datURL(), thread->resNum() ); + + delete m_threadParser; + m_threadParser = 0; + } else if ( m_inThread ) { + m_threadParser->endElement( namespaceURI, localName, qName ); + } else if ( localName == "favorites" ) { + m_inFavorites = false; + m_isValid = true; + } else { + // error + return false; + } + return true; } bool FavoritesXmlParser::characters( const QString& ch ) { - if( m_inThread ) { - m_threadParser->characters( ch ); - } else { - m_characters = ch; - } - return true; + if ( m_inThread ) { + m_threadParser->characters( ch ); + } else { + m_characters = ch; + } + return true; } diff --git a/kita/src/libkita/tests/threadtest.cpp b/kita/src/libkita/tests/threadtest.cpp index dfb9996..7be0d01 100644 --- a/kita/src/libkita/tests/threadtest.cpp +++ b/kita/src/libkita/tests/threadtest.cpp @@ -29,7 +29,7 @@ void ThreadTest::setUp() "\n" "\n" ); - m_fromxml2 = Thread::fromXml( m_fromxml.toXmlFragment() ); + m_fromxml2 = Thread::fromXml( m_fromxml->toXmlFragment() ); m_fromxml3 = Thread::fromXml( "\n" " http://pc.2ch.net/linux/dat/1022744633.dat\n" @@ -52,11 +52,11 @@ void ThreadTest::testDatURL() CPPUNIT_ASSERT_EQUAL( QString( "http://pc.2ch.net/linux/dat/1069738960.dat"), m_normal->datURL() ); CPPUNIT_ASSERT_EQUAL( QString( "http://pc.2ch.net/linux/dat/1022744633.dat"), - m_fromxml.datURL() ); + m_fromxml->datURL() ); CPPUNIT_ASSERT_EQUAL( QString( "http://pc.2ch.net/linux/dat/1022744633.dat"), - m_fromxml2.datURL() ); + m_fromxml2->datURL() ); CPPUNIT_ASSERT_EQUAL( QString( "http://pc.2ch.net/linux/dat/1022744633.dat"), - m_fromxml3.datURL() ); + m_fromxml3->datURL() ); } void ThreadTest::testURL() @@ -64,27 +64,27 @@ void ThreadTest::testURL() CPPUNIT_ASSERT_EQUAL( QString( "http://pc.2ch.net/test/read.cgi/linux/1069738960/"), m_normal->url() ); CPPUNIT_ASSERT_EQUAL( QString( "http://pc.2ch.net/test/read.cgi/linux/1022744633/"), - m_fromxml.url() ); + m_fromxml->url() ); CPPUNIT_ASSERT_EQUAL( QString( "http://pc.2ch.net/test/read.cgi/linux/1022744633/"), - m_fromxml2.url() ); + m_fromxml2->url() ); CPPUNIT_ASSERT_EQUAL( QString( "http://pc.2ch.net/test/read.cgi/linux/1022744633/"), - m_fromxml3.url() ); + m_fromxml3->url() ); } void ThreadTest::testDatID() { CPPUNIT_ASSERT_EQUAL( QString( "1069738960" ), m_normal->datID() ); - CPPUNIT_ASSERT_EQUAL( QString( "1022744633" ), m_fromxml.datID() ); - CPPUNIT_ASSERT_EQUAL( QString( "1022744633" ), m_fromxml2.datID() ); - CPPUNIT_ASSERT_EQUAL( QString( "1022744633" ), m_fromxml3.datID() ); + CPPUNIT_ASSERT_EQUAL( QString( "1022744633" ), m_fromxml->datID() ); + CPPUNIT_ASSERT_EQUAL( QString( "1022744633" ), m_fromxml2->datID() ); + CPPUNIT_ASSERT_EQUAL( QString( "1022744633" ), m_fromxml3->datID() ); } void ThreadTest::testResNum() { CPPUNIT_ASSERT_EQUAL( 0, m_normal->resNum() ); - CPPUNIT_ASSERT_EQUAL( 700, m_fromxml.resNum() ); - CPPUNIT_ASSERT_EQUAL( 700, m_fromxml2.resNum() ); - CPPUNIT_ASSERT_EQUAL( 700, m_fromxml3.resNum() ); + CPPUNIT_ASSERT_EQUAL( 700, m_fromxml->resNum() ); + CPPUNIT_ASSERT_EQUAL( 700, m_fromxml2->resNum() ); + CPPUNIT_ASSERT_EQUAL( 700, m_fromxml3->resNum() ); } void ThreadTest::testName() @@ -92,33 +92,34 @@ void ThreadTest::testName() CPPUNIT_ASSERT_EQUAL( QString( "Kita - 2ch client for KDE" ), m_normal->name() ); CPPUNIT_ASSERT_EQUAL( QString( "2ch browser thread" ), - m_fromxml.name() ); + m_fromxml->name() ); CPPUNIT_ASSERT_EQUAL( QString( "2ch browser thread" ), - m_fromxml2.name() ); + m_fromxml2->name() ); CPPUNIT_ASSERT_EQUAL( QString( "2ch browser thread" ), - m_fromxml3.name() ); + m_fromxml3->name() ); } void ThreadTest::testBoardName() { CPPUNIT_ASSERT_EQUAL( QString( "Linux" ), m_normal->boardName() ); - CPPUNIT_ASSERT_EQUAL( QString( "Linux" ), m_fromxml.boardName() ); - CPPUNIT_ASSERT_EQUAL( QString( "Linux" ), m_fromxml2.boardName() ); - CPPUNIT_ASSERT_EQUAL( QString( "Linux" ), m_fromxml3.boardName() ); + CPPUNIT_ASSERT_EQUAL( QString( "Linux" ), m_fromxml->boardName() ); + CPPUNIT_ASSERT_EQUAL( QString( "Linux" ), m_fromxml2->boardName() ); + CPPUNIT_ASSERT_EQUAL( QString( "Linux" ), m_fromxml3->boardName() ); } void ThreadTest::testBoardURL() { CPPUNIT_ASSERT_EQUAL( QString( "http://pc.2ch.net/linux/" ), m_normal->boardURL() ); - CPPUNIT_ASSERT_EQUAL( QString( "http://pc.2ch.net/linux/" ), m_fromxml.boardURL() ); - CPPUNIT_ASSERT_EQUAL( QString( "http://pc.2ch.net/linux/" ), m_fromxml2.boardURL() ); - CPPUNIT_ASSERT_EQUAL( QString( "http://pc.2ch.net/linux/" ), m_fromxml3.boardURL() ); + CPPUNIT_ASSERT_EQUAL( QString( "http://pc.2ch.net/linux/" ), m_fromxml->boardURL() ); + CPPUNIT_ASSERT_EQUAL( QString( "http://pc.2ch.net/linux/" ), m_fromxml2->boardURL() ); + CPPUNIT_ASSERT_EQUAL( QString( "http://pc.2ch.net/linux/" ), m_fromxml3->boardURL() ); } void ThreadTest::testBoardID() { CPPUNIT_ASSERT_EQUAL( QString( "linux" ), m_normal->boardID() ); - CPPUNIT_ASSERT_EQUAL( QString( "linux" ), m_fromxml.boardID() ); - CPPUNIT_ASSERT_EQUAL( QString( "linux" ), m_fromxml2.boardID() ); - CPPUNIT_ASSERT_EQUAL( QString( "linux" ), m_fromxml3.boardID() ); + CPPUNIT_ASSERT_EQUAL( QString( "linux" ), m_fromxml->boardID() ); + CPPUNIT_ASSERT_EQUAL( QString( "linux" ), m_fromxml2->boardID() ); + CPPUNIT_ASSERT_EQUAL( QString( "linux" ), m_fromxml3->boardID() ); } + diff --git a/kita/src/libkita/tests/threadtest.h b/kita/src/libkita/tests/threadtest.h index 8296110..8225987 100644 --- a/kita/src/libkita/tests/threadtest.h +++ b/kita/src/libkita/tests/threadtest.h @@ -29,7 +29,7 @@ class ThreadTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE_END(); Kita::Thread *m_normal; - Kita::Thread m_fromxml, m_fromxml2, m_fromxml3; // FIXME: pointer¤Ë¤¹¤Ù¤­ + Kita::Thread *m_fromxml, *m_fromxml2, *m_fromxml3; public: void setUp(); void tearDown(); diff --git a/kita/src/libkita/thread.cpp b/kita/src/libkita/thread.cpp index d9942be..8d61b1f 100644 --- a/kita/src/libkita/thread.cpp +++ b/kita/src/libkita/thread.cpp @@ -111,9 +111,9 @@ const QString Thread::toXmlFragment() const return ret; } -Thread Thread::fromXml( const QString& xml ) +Thread* Thread::fromXml( const QString& xml ) { - Thread thread; + Thread * thread; ThreadXmlParser parser; QXmlSimpleReader reader; @@ -207,7 +207,8 @@ bool ThreadXmlParser::endElement( const QString& namespaceURI, const QString& lo m_boardParser->endElement( namespaceURI, localName, qName ); } else if ( localName == "thread" ) { m_inThread = false; - m_thread = Kita::Thread( m_datUrlStr, m_threadName ); + Kita::Thread::setName( m_datUrlStr, m_threadName ); + m_thread = Kita::Thread::getByURL( m_datUrlStr ); KitaThreadInfo::setResNum( m_datUrlStr, m_resNum ); m_isValid = true; // create thread; diff --git a/kita/src/libkita/thread.h b/kita/src/libkita/thread.h index 6162efe..705e95a 100644 --- a/kita/src/libkita/thread.h +++ b/kita/src/libkita/thread.h @@ -50,7 +50,7 @@ namespace Kita const QString boardID() const; const QString toXmlFragment() const; - static Thread fromXml( const QString& xml ); + static Thread* fromXml( const QString& xml ); static Thread* getByURL( const QString& datURL ); static void setName( const QString& datURL, const QString& threadName ); @@ -74,13 +74,13 @@ namespace Kita const QString& qName ); bool characters( const QString& ch ); bool isValid() const { return m_isValid; } - Thread getThread() const { return m_thread; } + Thread* getThread() const { return m_thread; } private: QString m_datUrlStr; QString m_threadName; int m_resNum; - Thread m_thread; + Thread* m_thread; BoardXmlParser* m_boardParser; QString m_characters; -- 2.11.0