MediaTree.cpp
资源名称:MediaTree.zip [点击查看]
上传用户:boyawuliu
上传日期:2021-10-14
资源大小:8k
文件大小:4k
源码类别:
TreeView控件
开发平台:
Visual C++
- // MediaTree.cpp : Defines the entry point for the console application.
- //
- #include "MediaTree.h"
- #include "MediaBase.h"
- #include "MediaDevice.h"
- #include "Song.h"
- #include "Video.h"
- #include "Picture.h"
- #include "File.h"
- #include <iostream>
- #include <ios>
- #include <string>
- #include <limits>
- // The one and only application object
- std::list<MediaBase*>* MediaDeviceList;
- using namespace std;
- #undef max
- template<typename T>
- bool simple_input(T& x)
- {
- if (std::cin >> std::noskipws >> x >> std::skipws && std::cin.get() == 'n')
- return true;
- x = T();
- std::cin.clear();
- std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
- return false;
- }
- bool simple_input(char* &x)
- {
- string s;
- if (std::getline(std::cin, s))
- {
- const char* all = s.c_str();
- int len = 1 + strlen(all);
- char* t = new char[len];
- strcpy(t, all);
- x = t;
- return true;
- }
- s.clear();
- std::cin.clear();
- return false;
- }
- int printMainCommand()
- {
- cout << "Please select command number:" << endl;
- cout << "1: Insert media" << endl;
- cout << "2: Print media tree" << endl;
- cout << "3: Quit" << endl;
- int command = 0;
- simple_input(command);
- return command;
- }
- MediaBase* InsertMedia()
- {
- cout << "Please type the media type:" << endl;
- cout << "0: Media device" << endl;
- cout << "1: Song" << endl;
- cout << "2: Video" << endl;
- cout << "3: Picture" << endl;
- cout << "4: File" << endl;
- int command = 0;
- simple_input(command);
- if (command >= 0 && command < 5)
- {
- MediaBase* result = NULL;
- if (command >= 1 && command < 5)
- {
- cout << "Please type the media device name of new media need to insert:" << endl;
- char* name = NULL;
- simple_input(name);
- for(list<MediaBase*>::iterator i = MediaDeviceList->begin(); i != MediaDeviceList->end(); i++)
- {
- MediaBase* child = *i;
- result = child->GetMediaByName(name);
- if (result != NULL)
- break;
- }
- if (result == NULL)
- {
- cout << "Cannot found media device name " << name << endl;
- return NULL;
- }
- }
- cout << "Please type new media name:" << endl;
- char* name = NULL;
- simple_input(name);
- char* keywords = NULL;
- if (command >= 1 && command < 5)
- {
- cout << "Please type key words for new media (seperate by comma):" << endl;
- simple_input(keywords);
- }
- MediaBase* newMedia;
- int mediaTypeIndex = command;
- switch (mediaTypeIndex)
- {
- case 0:
- newMedia = new MediaDevice();
- break;
- case 1:
- newMedia = new Song();
- break;
- case 2:
- newMedia = new Video();
- break;
- case 3:
- newMedia = new Picture();
- break;
- case 4:
- newMedia = new File();
- break;
- }
- newMedia->SetMediaName(name);
- if (command > 0)
- {
- char* keyword = strtok(keywords, ",");
- while (keyword != NULL)
- {
- newMedia->AddKeyword(keyword);
- keyword = strtok(NULL, ",");
- }
- result->GetChildList()->push_back(newMedia);
- }
- else
- {
- MediaDeviceList->push_back(newMedia);
- }
- return newMedia;
- }
- else
- {
- cout << "You choose the wrong media type." << endl;
- return NULL;
- }
- }
- void PrintMediaTree()
- {
- for(list<MediaBase*>::iterator i = MediaDeviceList->begin(); i != MediaDeviceList->end(); i++)
- {
- MediaBase* child = *i;
- child->PrintMedia();
- }
- }
- int main(int argc, char* argv[], char* envp[])
- {
- int nRetCode = 0;
- bool quit = 0;
- MediaDeviceList = new std::list<MediaBase*>();
- while (!quit)
- {
- int command = printMainCommand();
- switch (command)
- {
- case 1:
- if (InsertMedia() == NULL)
- {
- cout << "Cannot insert new media!" << endl;
- }
- break;
- case 2:
- PrintMediaTree();
- break;
- case 3:
- return 0;
- }
- }
- return nRetCode;
- }