• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            你的CPP=我的CPP

            唾沫是用來數鈔票滴,不是用來講道理的

            物品分類的設計

            有一些類別,每一類都包含了一些物品,想用以下的XML保存。

            數據樣例如下:
            <Actors>
            <FileVersion>0.2</FileVersion>

            <Category>
             <CateName>未分類sdf</CateName>
             <Act>
              <ActName>地板</ActName>
              <ActId>101</ActId>
              <File>floor</File>
              <Pic></Pic>
              <Des>地板</Des>
             </Act>
             <Act>
              <ActName>怪物</ActName>
              <ActId>203</ActId>
              <File>mon31</File>
              <Pic></Pic>
              <Des>怪物</Des>
             </Act>
            </Category>

            </Actors>

            我設計了如下的類:
            //ActorCategory.hpp


            #ifndef __ACTOR_CATEGORY_HPP__
            #define __ACTOR_CATEGORY_HPP__

            #include <string>
            #include <list>
            #include <map>

            #ifndef DWORD
            #define DWORD unsigned long
            #endif

            using namespace std;

            /**
             * 每一個具體的物件信息
             */
            struct SActInfo
            {
             SActInfo():
              dwActId(0)
             {
             }

             DWORD dwActId;
             string strFileName;
             string strPicName;
             string strDescribe;  //描述
            };

            typedef pair<string, struct SActInfo*> PairAct;
            typedef map<string, struct SActInfo*> MapAct;

            typedef pair<string, MapAct*> PairCate;
            typedef map<string, MapAct*> MapCate;

            /**
             * 物件類別
             */
            class CActorCategory
            {
            public:
             CActorCategory();
             CActorCategory(const CActorCategory &rcActorCate);
             ~CActorCategory();

            // bool Init(CEditorFrame *pFrame);

             bool InsertCate(const string &strCate);  //插入物品類別strCate1
             void UpdateCateName(const string &strOldCate, const string &strNewCate);  //更新類別的名稱為strNewCate1
             bool DeleteCate(int index);
             bool DeleteCate(const string &strCateName);  //刪除類別

             //將物件strActor從類別strSrcCateName中移入類別strDestCateName中
             bool MoveTo(const string &strDestCateName, const string &strSrcCateName, const string &strActorName);
             bool InsertActor(const string &strCateName, const string &strActorName, SActInfo* pActInfo);
             //bool InsertActor(const string &strCateName, const string &strActorName, const string &strFileName=""); //將物件添加到類別中,物件指定了文件名
             bool SetActorFile(const string &strCateName, const string &strActorName, const string &strFileName); //給物件指定一個對應的文件
             void UpdateActor(const string &strCateName, const string &strOldActor, const string &strNewActor);  //更新物件的名稱
             bool DeleteActor(const string &strCateName, const string &strActorName);  //將物件strActor從類別strCate中刪除

             DWORD GetActorIdByName(const string &strActorName);

             void GetCategoryString(list<string> & lstRes);  //返回所有的分類信息名稱
             void GetActorString(const string &strCate1, list<string> &lstRes);  //返回某一分類的所有物件
            // SActorInfoRes* GetActorInfoByName(const string &strActorName);
            // SActorInfoRes* GetActorInfoByFileName(const string &strFileName);

             bool RebuldFromServer();
             bool UpdateFromServer();
             bool LoadFromXML(const string &strFileName);  //讀取分類信息文件
             bool SaveToXML(const string &strFileName = "GameActors.xml");  //保存

             CActorCategory& operator=(const CActorCategory& rcActorCate);

            private:
             void Clear();
             bool HasFile(const string& strFileName);
             map<string, map<string, struct SActInfo*>*> m_mCategoryInfo;  //類別,類別名作為索引,第二個元素用物件名作索引
            };

            #endif



            //ActorCategory.cpp

            #include "ActorCategory.hpp"

            #include <iostream>
            #include <fstream>
            #include <strstream>

            #include "Markup.h"
            #include "ActorTable.hpp"

            #define KEY_TAB (char)0x9
            #define TABLE_FILE "table/Wi_ActorInfo.csv"

            //#include "rtc2_text_xml.h"

            /**
             * the constructor
             */
            CActorCategory::CActorCategory()
            {
            }

            CActorCategory::CActorCategory(const CActorCategory &rcActorCate1)
            {
             struct SActInfo* psActorInfoCopy = new SActInfo();
             MapAct* pmActorCopy = new MapAct();

             MapAct* pmActorTemp;
             MapCate::const_iterator ite = rcActorCate1.m_mCategoryInfo.begin();
             MapCate::const_iterator end = rcActorCate1.m_mCategoryInfo.end();
             for( ; ite!=end; ++ite)
             {
              pmActorTemp = ite->second;
              for(MapAct::iterator iteAc=pmActorTemp->begin(); iteAc!=pmActorTemp->end(); ++iteAc)
              {
               psActorInfoCopy->dwActId = (iteAc->second)->dwActId;
               psActorInfoCopy->strDescribe = (iteAc->second)->strDescribe;
               psActorInfoCopy->strFileName = (iteAc->second)->strFileName;
               psActorInfoCopy->strPicName = (iteAc->second)->strPicName;
               pmActorCopy->insert(PairAct(iteAc->first, psActorInfoCopy));
              }
              this->m_mCategoryInfo.insert(PairCate(ite->first, pmActorCopy));
             }
            }

            CActorCategory& CActorCategory::operator=(const CActorCategory& rcActorCate1)
            {
             if (this == &rcActorCate1)
             {
              return *this; // identity test: if a self-assignment
             }

             MapAct* pmActorTemp;
             MapCate::const_iterator ite = rcActorCate1.m_mCategoryInfo.begin();
             MapCate::const_iterator end = rcActorCate1.m_mCategoryInfo.end();
             for( ; ite!=end; ++ite)
             { 
              MapAct* pmActorCopy = new MapAct();
              pmActorTemp = ite->second;
              for(MapAct::iterator iteAc=pmActorTemp->begin(); iteAc!=pmActorTemp->end(); ++iteAc)
              {
               struct SActInfo* psActorInfoCopy = new SActInfo();

               psActorInfoCopy->dwActId = (iteAc->second)->dwActId;
               psActorInfoCopy->strDescribe = (iteAc->second)->strDescribe;
               psActorInfoCopy->strFileName = (iteAc->second)->strFileName;
               psActorInfoCopy->strPicName = (iteAc->second)->strPicName;
               pmActorCopy->insert(PairAct(iteAc->first, psActorInfoCopy));
              }
              this->m_mCategoryInfo.insert(PairCate(ite->first, pmActorCopy));
             }
             return *this;
            }

            /**
             * the destuctor
             */
            CActorCategory::~CActorCategory()
            {
             MapAct* pmActorTemp;
             MapCate::iterator ite = m_mCategoryInfo.begin();
             MapCate::iterator end = m_mCategoryInfo.end();
             for( ; ite!=m_mCategoryInfo.end(); ++ite)
             {
              pmActorTemp = ite->second;
              for(MapAct::iterator iteAc=pmActorTemp->begin(); iteAc!=pmActorTemp->end(); ++iteAc)
              {
               delete iteAc->second;
               
              }
              delete pmActorTemp;
             }
             m_mCategoryInfo.clear();
            }

            void CActorCategory::Clear()
            {
             MapAct* pmActorTemp;
             MapCate::iterator ite = m_mCategoryInfo.begin();
             MapCate::iterator end = m_mCategoryInfo.end();
             for( ; ite!=m_mCategoryInfo.end(); ++ite)
             {
              pmActorTemp = ite->second;
              for(MapAct::iterator iteAc=pmActorTemp->begin(); iteAc!=pmActorTemp->end(); ++iteAc)
              {
               delete iteAc->second;
              }
              delete pmActorTemp;
             }
             m_mCategoryInfo.clear();
            }

            /**
             * 新增物品類別
             */
            bool CActorCategory::InsertCate(const string &strCate)  //將類別strCate1插入
            {
             m_mCategoryInfo.insert(PairCate(strCate,new MapAct()));
             return true;
            }

            /**
             * 更新類別的名稱為strNewCate1
             */
            void CActorCategory::UpdateCateName(const string &strOldCate, const string &strNewCate)
            {
                if (strNewCate != strOldCate)
                {
                    MapCate::iterator itFind = m_mCategoryInfo.find(strOldCate);
                    if (itFind != m_mCategoryInfo.end())
                    {
                        m_mCategoryInfo.insert(PairCate(strNewCate, itFind->second));
                        m_mCategoryInfo.erase(strOldCate);
                    }
                }
            }

            /**
             * 刪除類別
             */
            bool CActorCategory::DeleteCate(int index)
            {
             MapAct* pmActorTemp;
             int i = 0;
             MapCate::iterator end = m_mCategoryInfo.end();
             for(MapCate::iterator ite = m_mCategoryInfo.begin(); ite!=end; ++ite)
             {
              if(i == index)
              {
               pmActorTemp = ite->second;
               for(MapAct::iterator iteAc=pmActorTemp->begin(); iteAc!=pmActorTemp->end(); ++iteAc)
               {
                delete iteAc->second;
               }
               delete pmActorTemp;

               m_mCategoryInfo.erase(ite);
               return true;
              }
              ++i;
             }
             return false;
            }

            /**
             * 刪除類別
             */
            bool CActorCategory::DeleteCate(const string &strCateName)
            {
             MapCate::iterator ite = m_mCategoryInfo.find(strCateName);
             MapAct* pmActorTemp = ite->second;
             for(MapAct::iterator iteAc=pmActorTemp->begin(); iteAc!=pmActorTemp->end(); ++iteAc)
             {
              delete iteAc->second;
             }
             delete pmActorTemp;
             m_mCategoryInfo.erase(ite);
             return true;
            }

            /**
             * 將物件strActorName從類別strSrcCateName中移入類別strDestCateName中
             */
            bool CActorCategory::MoveTo(const string &strDestCateName, const string &strSrcCateName, const string &strActorName)
            {
             if(strDestCateName == strSrcCateName)
             {
              return false;
             }
               
                MapCate::iterator itSrcCate  = m_mCategoryInfo.find(strSrcCateName);
                MapCate::iterator itDescCate = m_mCategoryInfo.find(strDestCateName);
                MapCate::iterator itEndCate  = m_mCategoryInfo.end();
                if ((itSrcCate == itEndCate) || (itDescCate == itEndCate))
                {
                    return false;
                }

                MapAct* pmActorSrc  = itSrcCate->second;
                MapAct::iterator itSrcAct = pmActorSrc->find(strActorName);
                if (itSrcAct == pmActorSrc->end())
                {
                    return false;
                }

                itDescCate->second->insert(PairAct(strActorName, itSrcAct->second));
                pmActorSrc->erase(strActorName);

             return true;
            }

            bool CActorCategory::InsertActor(const string &strCateName, const string &strActorName, SActInfo* pActInfo)
            {
                MapCate::iterator iteCate = m_mCategoryInfo.find(strCateName);
             MapCate::iterator iteEnd = m_mCategoryInfo.end();
             if(iteCate == iteEnd)
             {
                    InsertCate(strCateName);
                }
                iteCate = m_mCategoryInfo.find(strCateName);
                assert(iteCate!=iteEnd);
             MapAct* pmActorTemp = iteCate->second;
             pmActorTemp->insert(PairAct(strActorName, pActInfo));
             return true;
            }

            /*
            bool CActorCategory::InsertActor(const string &strCate1, const string &strActorName) //將物件添加到類別中
            {
             MapActor* pmActorTemp = m_mCategoryInfo.find(strCate1)->second;
             return pmActorTemp->insert(PairActor(strActorName, new SActorInformation()));
            }
            */

            void CActorCategory::UpdateActor(const string &strCateName, const string &strOldActor, const string &strNewActor)  //更新物件的名稱
            {
             MapCate::iterator ite = m_mCategoryInfo.find(strCateName);
                if (ite != m_mCategoryInfo.end())
                {
                    MapAct* pmActorTemp = ite->second;
                    MapAct::iterator itAct = pmActorTemp->find(strOldActor);
                    if ((itAct != pmActorTemp->end()) && (strNewActor != strOldActor))
                    {
                        pmActorTemp->insert(PairAct(strNewActor, itAct->second));
                        pmActorTemp->erase(strOldActor);
                    }
                }
            }

            DWORD CActorCategory::GetActorIdByName(const string &strActorName)
            {
             MapAct* pmActorTemp;
             MapCate::iterator end = m_mCategoryInfo.end();
             for(MapCate::iterator ite = m_mCategoryInfo.begin(); ite!=end; ++ite)
             {
              pmActorTemp = ite->second;
              MapAct::iterator iteFind = pmActorTemp->find(strActorName);
              if(iteFind != pmActorTemp->end())
              {
               return (iteFind->second)->dwActId;
              }
             }
             return 0;
            }

            /**
             * 返回所有的分類信息名稱
             */
            void CActorCategory::GetCategoryString(list<string> & lstRes)
            {
             MapCate::iterator ite = m_mCategoryInfo.begin();
             MapCate::iterator end = m_mCategoryInfo.end();
             for( ; ite!=end; ++ite)
             {
              lstRes.push_back(ite->first);
             }
            }

            void CActorCategory::GetActorString(const string &strCate1, list<string> &lstRes)  //返回某一分類的所有物件
            {
             MapAct* pmActorTemp = (m_mCategoryInfo.find(strCate1))->second;
             MapAct::iterator iteA = pmActorTemp->begin();
             MapAct::iterator end = pmActorTemp->end();
             for( ; iteA!=end; ++iteA)
             {
              lstRes.push_back(iteA->first);
             }
            }

            bool CActorCategory::RebuldFromServer()
            {
                Clear();
                CActorTable actTable;
                actTable.LoadTable(TABLE_FILE);
                MapTable* pTable = actTable.GetTable();
                struct SActTable* psActTable;
                struct SActInfo* psActInfo;
                for(MapTable::iterator ite=pTable->begin(); ite!=pTable->end(); ++ite)
                {
                    psActTable = ite->second;
                    psActInfo = new SActInfo();
                    psActInfo->dwActId = psActTable->dwActId;
                    psActInfo->strFileName = psActTable->strFileName;
                    psActInfo->strDescribe = psActTable->strDescribe;
                    InsertActor("未分類", psActTable->strActName, psActInfo);
                }
                return true;
            }

            bool CActorCategory::UpdateFromServer()
            {
                CActorTable actTable;
                actTable.LoadTable(TABLE_FILE);
                MapTable* pTable = actTable.GetTable();
                struct SActTable* psActTable;
                struct SActInfo* psActInfo;
                string strFileName;
                for(MapTable::iterator ite=pTable->begin(); ite!=pTable->end(); ++ite)
                {
                    psActTable = ite->second;
                    strFileName = psActTable->strFileName;
                    if(!HasFile(strFileName))
                    {
                      psActInfo = new SActInfo();
                      psActInfo->dwActId = psActTable->dwActId;
                      psActInfo->strFileName = psActTable->strFileName;
                      psActInfo->strDescribe = psActTable->strDescribe;
                      InsertActor("未分類", psActTable->strFileName, psActInfo);
                    }
                }
                return true;
            }

            bool CActorCategory::HasFile(const string& strFileName)
            {
                SActInfo* psAct;
             MapAct* pmActorTemp;
             MapCate::iterator ite = m_mCategoryInfo.begin();
             MapCate::iterator end = m_mCategoryInfo.end();
             for( ; ite!=m_mCategoryInfo.end(); ++ite)
             {
              pmActorTemp = ite->second;
              for(MapAct::iterator iteAc=pmActorTemp->begin(); iteAc!=pmActorTemp->end(); ++iteAc)
              {
                       psAct = iteAc->second;
                       if(psAct->strFileName == strFileName)
                       {
                           return true;
                       }
                    }
                }
                return false;
            }

            /**
             * 供測試用
             *//*
            bool CActorCategory::LoadFromXML(const string &strFileName)
            {
             MapAct* pmActorTemp = new map<string, struct SActInfo*>();
             pmActorTemp->insert( PairAct("Tom",new struct SActInfo()) );
             pmActorTemp->insert( PairAct("Jim",new struct SActInfo()) );

             m_mCategoryInfo.insert(PairCate("人物",pmActorTemp));

                delete pmActorTemp;
             return true;
            }
            */

            bool CActorCategory::LoadFromXML(const string &strFileName)
            {
                Clear();
             string strInfo;
             string strFileFullName=strFileName;
             if(strFileFullName.empty())
             {
              strFileFullName = "ActCate.xml";
             }
             ifstream ifs(strFileFullName.c_str()); //


             char buf[1024] = {0};
             while(ifs.good())
             {
              ifs.getline(buf, sizeof(buf));
              strInfo.append(buf);
             }

             //cout<<strInfo.c_str()<<endl;

             CMarkup xml;
             xml.SetDoc( (strInfo.c_str()) );
             while ( xml.FindChildElem("Category") )
             {
              MapAct* pmActorTemp = new MapAct();
              xml.IntoElem();
              xml.FindChildElem( "CateName" );
              string strCateName1 = xml.GetChildData();
              while(xml.FindChildElem("Act"))
              {
               xml.IntoElem();
               xml.FindChildElem("ActName");
               string strActorName1 = xml.GetChildData();

               xml.FindChildElem("ActId");
               string strActId1 = xml.GetChildData();
               
               xml.FindChildElem("File");
               string strFileName1 = xml.GetChildData();

               xml.FindChildElem("Pic");
               string strPic1 = xml.GetChildData();
               
               xml.FindChildElem("Des");
               string strDes1 = xml.GetChildData();
               
               struct SActInfo* pSActInfo1 = new SActInfo();
               pSActInfo1->dwActId = atol(strActId1.c_str());
               pSActInfo1->strFileName = strFileName1;
               pSActInfo1->strPicName = strPic1;
               pSActInfo1->strDescribe = strDes1;

               pmActorTemp->insert( PairAct(strActorName1,pSActInfo1) );
               xml.OutOfElem();
               //cout<<strActorName1<<endl;
              }
              m_mCategoryInfo.insert( PairCate(strCateName1, pmActorTemp) );

              xml.OutOfElem();
             }

             return true;
            }

            bool CActorCategory::SaveToXML(const string &strFileName/*="GameActors.xml"*/)
            {
             ostrstream strInfo;
             string strFileFullName=strFileName;
             if(strFileFullName.empty())
             {
              strFileFullName = "GameActors.xml";
             }
             ofstream out(strFileFullName.c_str());//生成文件輸出流

             strInfo<<"<Actors>"<<endl;
             strInfo<<"<FileVersion>0.2</FileVersion>"<<endl<<endl;

             MapAct* pmActorTemp;
             MapCate::iterator ite = m_mCategoryInfo.begin();
             MapCate::iterator end = m_mCategoryInfo.end();
             for( ; ite!=end; ++ite)
             {
              strInfo<<"<Category>"<<endl;

              strInfo<<(char)0x9<<"<CateName>";
              strInfo<<ite->first.c_str();
              strInfo<<"</CateName>"<<endl;

              pmActorTemp = ite->second;
              for(MapAct::iterator ite2 = pmActorTemp->begin(); ite2!=pmActorTemp->end(); ++ite2)
              {
               strInfo<<(char)0x9<<"<Act>"<<endl;
               strInfo<<(char)0x9<<(char)0x9<<"<ActName>";
               strInfo<<ite2->first.c_str();
               strInfo<<"</ActName>"<<endl;

               strInfo<<(char)0x9<<(char)0x9<<"<ActId>";
               strInfo<<(ite2->second)->dwActId;
               strInfo<<"</ActId>"<<endl;

               strInfo<<(char)0x9<<(char)0x9<<"<File>";
               strInfo<<(ite2->second)->strFileName.c_str();
               strInfo<<"</File>"<<endl;

               strInfo<<(char)0x9<<(char)0x9<<"<Pic>";
               strInfo<<(ite2->second)->strPicName.c_str();
               strInfo<<"</Pic>"<<endl;

               strInfo<<(char)0x9<<(char)0x9<<"<Des>";
               strInfo<<(ite2->second)->strDescribe.c_str();
               strInfo<<"</Des>"<<endl;

               strInfo<<(char)0x9<<"</Act>"<<endl;
              }
              strInfo<<"</Category>"<<endl<<endl;

             }

             strInfo<<"</Actors>"<<endl;
             strInfo<<'\0';
             out<<strInfo.str();
             //cout<<strInfo.str();

             return true;
            }

             

            posted on 2007-12-22 15:34 晨風 閱讀(459) 評論(0)  編輯 收藏 引用

            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            性做久久久久久久久浪潮| 久久乐国产精品亚洲综合| 国产偷久久久精品专区| 国产一区二区精品久久 | 久久综合狠狠综合久久97色| 久久精品三级视频| 久久综合狠狠色综合伊人| 狠狠综合久久AV一区二区三区| 精品无码久久久久久午夜| 久久夜色精品国产亚洲| 久久香蕉国产线看观看猫咪?v| 精品多毛少妇人妻AV免费久久| 久久www免费人成精品香蕉| 久久福利青草精品资源站| 四虎影视久久久免费| 久久精品国产精品亚洲下载| 99久久精品国产一区二区| 久久最近最新中文字幕大全| 久久精品国产99国产精品导航| 国产ww久久久久久久久久| 亚洲国产成人久久综合一| 精品国产福利久久久| 国产成人精品久久| 久久久久亚洲av成人无码电影| 久久国产精品国产自线拍免费| 99久久99久久精品国产片果冻| 久久一区二区三区免费| 97精品伊人久久久大香线蕉| 999久久久无码国产精品| 久久亚洲欧美日本精品| 人人狠狠综合久久88成人| 狠狠久久亚洲欧美专区| 精品久久久无码21p发布| 久久精品青青草原伊人| 久久久国产亚洲精品| 无码人妻久久一区二区三区蜜桃 | 久久精品这里热有精品| 久久99国产精品久久99果冻传媒| 狠狠色丁香久久婷婷综| 久久精品成人国产午夜| 国产精品午夜久久|