OSDN Git Service

Migrating to Qt platform (2)
authortkawata <tkawata@gmail.com>
Sun, 26 Feb 2012 12:11:19 +0000 (21:11 +0900)
committertkawata <tkawata@gmail.com>
Sun, 26 Feb 2012 12:11:19 +0000 (21:11 +0900)
12 files changed:
Source/DNXML.cpp
Source/DNXML.h
Source/DNXMLElement.cpp
Source/DNXMLElement.h
Source/platform/osx/OSXDNXMLImpl.mm
Source/platform/qt/qtdntimekeeperimpl.cpp
Source/platform/qt/qtdntimekeeperimpl.h
Source/platform/qt/qtdnxmlimpl.cpp
Source/platform/qt/qtdnxmlimpl.h
Source/platform/qt/qttklockimpl.cpp
Source/platform/qt/qttklockimpl.h
dennco.pro

index ec36a98..3d7a19c 100644 (file)
@@ -26,6 +26,14 @@ DNXML* DNXML::createXMLFromFile(const char *docRoot, const char *path)
     return new DNXML(DNXMLImpl::createXMLFromFileImpl(docRoot, path));
 }
 
+DNXML::~DNXML()
+{
+    if (impl)
+    {
+        delete impl;
+    }
+}
+
 DNXMLElement* DNXML::getRoot()
 {
     return impl->getRoot();
index 6a92d13..8b34eef 100644 (file)
@@ -27,6 +27,8 @@ class DNXML
 {
 public:
     static DNXML*   createXMLFromFile(const char *docRoot, const char *path);
+
+    virtual ~DNXML();
     
     DNXMLElement*   getRoot();
     
index 7e3fe20..ab9e80f 100644 (file)
@@ -26,6 +26,14 @@ name(_name),outer(NULL),inner(NULL),next(NULL),depth(0),text("")
 
 DNXMLElement::~DNXMLElement()
 {
+    DNXMLElement *e = inner;
+    while (e && e != this && depth <= e->depth)
+    {
+        while(e->inner) e = e->inner;
+        DNXMLElement *ne = e->next ? e->next : e->outer;
+        delete e;
+        e = ne;
+    }
     mAttributes.clear();
 }
 
@@ -36,6 +44,11 @@ void DNXMLElement::setAttribute(const char *name, const char *value)
     mAttributes.insert(std::map<std::string, std::string>::value_type( _key, _value));
 }
 
+void DNXMLElement::setAttribute(std::string name, std::string value)
+{
+    mAttributes.insert(std::map<std::string, std::string>::value_type( name, value));
+}
+
 std::string DNXMLElement::getAttributeValue(const char *name)
 {
     std::string key = name;
index 289bbc1..0dcd76d 100644 (file)
@@ -29,6 +29,8 @@ public:
     DNXMLElement(std::string _name);
     ~DNXMLElement();
     void         setAttribute(const char *name, const char *value);
+    void         setAttribute(std::string name, std::string value);
+
     std::string  getAttributeValue(const char *name);
     DNXMLElement *outer;
     DNXMLElement *inner;
index 5f0ada6..2a9762e 100644 (file)
@@ -62,8 +62,8 @@ mDocument(doc),mRootElement(nil)
 
 OSXDNXMLImpl::~OSXDNXMLImpl()
 {
-    //TODO 
-    //release memory.
+       if (mRootElement)
+               delete mRootElement;
 }
 
 
index 03966e7..14ee3d2 100644 (file)
 
 DNTimeKeeperImpl *DNTimeKeeperImpl::create()
 {
-    //TODO
-    return 0;
+    return new QtDNTimeKeeperImpl;
 }
 
 QtDNTimeKeeperImpl::QtDNTimeKeeperImpl()
 {
+    //TODO
+}
+
+QtDNTimeKeeperImpl::~QtDNTimeKeeperImpl()
+{
+    //TODO
+}
+
+bool  QtDNTimeKeeperImpl::setIntevalSec(float time)
+{
+    //TODO
+}
+
+bool  QtDNTimeKeeperImpl::sleepUntilNextInterval()
+{
+    //TODO
+}
+
+float QtDNTimeKeeperImpl::getTickTime()
+{
+    //TODO
+}
+
+bool QtDNTimeKeeperImpl::start()
+{
+    //TODO
+}
+
+bool QtDNTimeKeeperImpl::stop()
+{
+    //TODO
 }
index 6f916a0..d8213ae 100644 (file)
@@ -25,6 +25,14 @@ class QtDNTimeKeeperImpl : public DNTimeKeeperImpl
 {
 public:
     QtDNTimeKeeperImpl();
+    ~QtDNTimeKeeperImpl();
+
+    virtual bool    setIntevalSec(float time);
+    virtual bool    sleepUntilNextInterval();
+    virtual float   getTickTime();
+
+    virtual bool    start();
+    virtual bool    stop();
 };
 
 #endif // QTDNTIMEKEEPERIMPL_H
index 75407e0..3cfcfa6 100644 (file)
 //
 #include "qtdnxmlimpl.h"
 
-DNXMLImpl *DNXMLImpl::createXMLFromFileImpl(const char *docRoot, const char *path)
+#include "DNXMLElement.h"
+#include "TKLog.h"
+
+DNXMLImpl *DNXMLImpl::createXMLFromFileImpl(const char *docRoot, const char *docPath)
+{
+    QString qDocRoot = QString::fromLocal8Bit(docRoot);
+    QString qDocPath = QString::fromLocal8Bit(docPath);
+
+    if (qDocRoot.lastIndexOf("/") != qDocRoot.length()-1 && qDocPath.at(0) != QChar('/'))
+    {
+        qDocRoot.append("/");
+    }
+    QString filePath = qDocRoot;
+    filePath.append(qDocPath);
+
+    QFile file(filePath);
+    if (file.open(QFile::ReadOnly | QFile::Text)) {
+        QXmlInputSource xmlInputSource(&file);
+        return new QtDNXMLImpl(&xmlInputSource);
+    }
+    else
+    {
+        TKLog::printf("ERROR failed to load file.%s/%s", docRoot,docPath);
+        return NULL;
+    }
+}
+
+QtDNXMLImpl::QtDNXMLImpl(QXmlInputSource *input)
+{
+    mHandler = new QtXMLHandler();
+    QXmlSimpleReader reader;
+    reader.setContentHandler(mHandler);
+    reader.setErrorHandler(mHandler);
+
+    reader.parse(input,true);
+}
+
+QtDNXMLImpl::~QtDNXMLImpl()
+{
+    if (mHandler)
+        delete mHandler;
+}
+
+DNXMLElement* QtDNXMLImpl::getRoot()
+{
+    if (mHandler)
+    {
+        return mHandler->getRoot();
+    }
+    else
+    {
+        return NULL;
+    }
+}
+
+QtXMLHandler::QtXMLHandler() : mRootElement(NULL), mCurrentElement(NULL)
+{
+}
+
+QtXMLHandler::~QtXMLHandler()
 {
-    //TODO
-    return 0;
+    if (mRootElement)
+    {
+        delete mRootElement;
+    }
 }
 
-QtDNXMLImpl::QtDNXMLImpl()
+//QXML handler implementation
+bool QtXMLHandler::startDocument()
 {
+    return true;
 }
+
+bool QtXMLHandler::endDocument()
+{
+    return true;
+}
+
+bool QtXMLHandler::startElement(const QString & namespaceURI, const QString & localName, const QString & qName, const QXmlAttributes & atts )
+{
+    if (mRootElement  == NULL)
+    {
+        mRootElement = new DNXMLElement(qName.toStdString());
+        mCurrentElement = mRootElement;
+    }
+    else
+    {
+        if (!mCurrentElement->inner)
+        {
+            mCurrentElement->inner = new DNXMLElement(qName.toStdString());
+            DNXMLElement *outer = mCurrentElement;
+            mCurrentElement = mCurrentElement->inner;
+            mCurrentElement->outer = outer;
+            mCurrentElement->depth = outer->depth++;
+        }
+        else
+        {
+            DNXMLElement *prev = mCurrentElement->inner;
+            prev->next = new DNXMLElement(qName.toStdString());
+            mCurrentElement = prev->next;
+            mCurrentElement->outer = prev->outer;
+            mCurrentElement->depth = prev->depth;
+        }
+    }
+
+    if (mCurrentElement)
+    {
+        for(int i = 0; i < atts.count(); i++)
+        {
+            QString qname = atts.qName(i);
+            QString value = atts.value(i);
+            mCurrentElement->setAttribute(qname.toStdString(), value.toStdString());
+        }
+
+    }
+    return true;
+}
+
+bool QtXMLHandler::endElement (const QString & namespaceURI, const QString & localName, const QString & qName )
+{
+    mCurrentElement = mCurrentElement->outer;
+    return true;
+}
+
+bool QtXMLHandler::characters (const QString & ch )
+{
+    mCurrentElement->text = ch.toStdString();
+    return true;
+}
+
+bool QtXMLHandler::error (const QXmlParseException & exception )
+{
+    std::string message = exception.message().toStdString();
+    TKLog::printf("Error while reading XML file!! \n%s", message.c_str());
+    return true;
+}
+
index db44663..8d45150 100644 (file)
 #define QTDNXMLIMPL_H
 
 #include "DNXMLImpl.h"
+#include <QXmlDefaultHandler>
+
+class QtXMLHandler : public QXmlDefaultHandler
+{
+public:
+    QtXMLHandler();
+    virtual ~QtXMLHandler();
+
+    DNXMLElement *getRoot() { return mRootElement; }
+
+    //QXML handler implementation
+    virtual bool startDocument();
+    virtual bool endDocument();
+    virtual bool startElement(const QString & namespaceURI, const QString & localName, const QString & qName, const QXmlAttributes & atts );
+    virtual bool endElement (const QString & namespaceURI, const QString & localName, const QString & qName );
+    virtual bool characters (const QString & ch );
+    virtual bool error (const QXmlParseException & exception );
+
+private:
+    DNXMLElement  *mRootElement;
+    DNXMLElement  *mCurrentElement;
+
+};
 
 class QtDNXMLImpl : public DNXMLImpl
 {
 public:
-    QtDNXMLImpl();
+    QtDNXMLImpl(QXmlInputSource *input);
+    virtual ~QtDNXMLImpl();
+    virtual DNXMLElement *getRoot();
+
+private:
+    QtXMLHandler  *mHandler;
 };
 
+
 #endif // QTDNXMLIMPL_H
index 9e275f5..d00c05d 100644 (file)
 
 TKLockImpl *TKLockImpl::create()
 {
-    //TODO
-    return 0;
+    return new QtTKLockImpl;
 }
 
 QtTKLockImpl::QtTKLockImpl()
 {
 }
+
+ QtTKLockImpl::~QtTKLockImpl()
+ {
+ }
+
+ void QtTKLockImpl::lock()
+ {
+    mLock.lock();
+ }
+
+ void QtTKLockImpl::unlock()
+ {
+    mLock.unlock();
+ }
index 90cfa79..b6bd01d 100644 (file)
 
 #include "TKLockImpl.h"
 
+#include <QMutex>
+
 class QtTKLockImpl : public TKLockImpl
 {
 public:
     QtTKLockImpl();
+     virtual ~QtTKLockImpl() ;
+     virtual void lock();
+     virtual void unlock();
+
+     QMutex mLock;
 };
 
 #endif // QTTKLOCKIMPL_H
index 1db34c4..58136b6 100644 (file)
@@ -4,7 +4,7 @@
 #
 #-------------------------------------------------
 
-QT       += core gui webkit script
+QT       += core gui webkit script xml
 
 TARGET = QtDennco
 TEMPLATE = app