• <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

            唾沫是用來數(shù)鈔票滴,不是用來講道理的

            物品分類的設計

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

            數(shù)據(jù)樣例如下:
            <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 晨風 閱讀(458) 評論(0)  編輯 收藏 引用

            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            色8久久人人97超碰香蕉987| 中文精品久久久久国产网址| 亚洲精品无码久久不卡| 亚洲欧美一区二区三区久久| 伊人 久久 精品| 国产精品禁18久久久夂久| 日韩一区二区久久久久久| 亚洲国产一成久久精品国产成人综合 | 精品无码久久久久国产动漫3d| 久久久亚洲欧洲日产国码二区| 国产成人精品久久亚洲| 免费无码国产欧美久久18| 国产精品久久永久免费| 久久人人爽人人爽人人片AV东京热| 国产成人无码久久久精品一| 久久伊人影视| 国产精品99久久久久久猫咪 | 天天爽天天狠久久久综合麻豆| 久久精品无码一区二区三区| 日本五月天婷久久网站| 久久综合九色综合久99| 国产精品久久精品| 人妻无码中文久久久久专区| 狠狠色丁香久久婷婷综合图片| 国产毛片久久久久久国产毛片| 久久精品一本到99热免费| 国产精品乱码久久久久久软件| 国内精品久久久久久久久电影网 | 亚洲欧美成人久久综合中文网 | 国产69精品久久久久观看软件 | 久久精品中文字幕有码| 久久综合丁香激情久久| 成人综合伊人五月婷久久| 日韩精品久久久肉伦网站| AV无码久久久久不卡蜜桃| 国内精品久久久久影院薰衣草| 久久久久久综合网天天| 中文字幕乱码人妻无码久久| 亚洲AV无码久久精品蜜桃| 久久夜色精品国产欧美乱| 久久久无码精品亚洲日韩蜜臀浪潮|