Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 7 của 7
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    CD/DVD-Rom TrayManager v2.0 viết bằng C++

    Hôm qua có viết con tí hon.
    Hôm nay ngồi update lại một tí ^^

    Chương trình: Quản lý đóng mở CD/DVD v2.0
    Tác giả: Xcross87
    Project: 3 files
    [DiskTray.h - DiskTray.cpp - Program.cpp]


    Có cải tiến hơn đồ hôm qua một chút [IMG]images/smilies/biggrin.png[/IMG]

    Đây là sơ đồ UML thiết kế tạm:



    Còn đây là source:

    1. DiskTray.h


    Mã:
    /* * @author Xcross87 * @date 06-May-2009 * @file disktray.h * @note    header for DiskTray class */ #ifndef __DISKTRAY_H__#define __DISKTRAY_H__ /* prototype */#include <iostream>#include <string>#include <wtypes.h>#include <winioctl.h>#include <list> #define MAX 256  /* * @class DiskTray *      manipulate CD/DVD-Rom  */class DiskTray {    private:        std::string driveName;        int state;         /*void SetState(DiskState& _state);        DiskState GetState() const;*/    public:        // const        enum DiskState { OPEN, CLOSE };              // constructor & destructor        DiskTray();        DiskTray(const std::string& _driveName);        ~DiskTray();         // properties        void SetDriveName(const std::string& _driveName);        std::string GetDriveName() const;         // methods        bool Open();        bool Close();                // static        static bool Open(const std::string& _driveName);        static bool Close(const std::string& _driveName);        static std::list<DiskTray> GetAll(); }; #endif // __DISKTRAY_H__
    2. DiskTray.cpp


    Mã:
    /* * @author Xcross87 * @date 05-May-2009 * @file disktray.cpp * @note    definition for DiskTray class */ /* prototype */#include "DiskTray.h" /* constructor */DiskTray::DiskTray() {    this->driveName = "UNKNOWN";} DiskTray::DiskTray(const std::string &_driveName) : driveName(_driveName) { } /* destructor */DiskTray::~DiskTray() { }  /* * @property DriveName * @type std::string */voidDiskTray::SetDriveName(const std::string &_driveName) {    this->driveName = _driveName;} std::stringDiskTray::GetDriveName() const {    return this->driveName;} /* * @method  Open() *      open drive * @type bool * @param  * @return *      true    if being able to open *      false   otherwise */boolDiskTray::Open() {    // check state    if( this->state == OPEN ) {        return true;    }    const std::string Drive = std::string("\\\\.\\") + this->driveName + std::string(":");    HANDLE hDrive = INVALID_HANDLE_VALUE; // handle    // create file & att    hDrive = CreateFile( Drive.c_str(), GENERIC_READ,                         FILE_SHARE_READ | FILE_SHARE_WRITE,                         NULL, OPEN_EXISTING,                         FILE_ATTRIBUTE_NORMAL, NULL );    BOOL bRetVal = FALSE;    // state good    if (( hDrive != INVALID_HANDLE_VALUE) && (NO_ERROR == GetLastError() )) {        DWORD dwDm = 0;        // eject        bRetVal = DeviceIoControl( hDrive, IOCTL_STORAGE_EJECT_MEDIA,                                   NULL, 0, NULL, 0, &dwDm, NULL );    }    // reset state    this->state = OPEN;    // avoid warning    return (!!bRetVal);} /* * @method  Close() *      close drive * @type bool * @param  * @return *      true    if being able to close *      false   otherwise */bool DiskTray::Close() {    // check state    if( this->state == CLOSE ) {        return true;    }    const std::string Drive = std::string("\\\\.\\") + this->driveName + std::string(":");    HANDLE hDrive = INVALID_HANDLE_VALUE; // handle    // create file & att    hDrive = CreateFile( Drive.c_str(), GENERIC_READ,                         FILE_SHARE_READ | FILE_SHARE_WRITE,                         NULL, OPEN_EXISTING,                         FILE_ATTRIBUTE_NORMAL, NULL );    BOOL bRetVal = FALSE;    // state good    if (( hDrive != INVALID_HANDLE_VALUE) && (NO_ERROR == GetLastError() )) {        DWORD dwDm = 0;        // eject        bRetVal = DeviceIoControl( hDrive, IOCTL_STORAGE_LOAD_MEDIA,                                   NULL, 0, NULL, 0, &dwDm, NULL );    }    // reset state    this->state = CLOSE;    // avoid warning    return (!!bRetVal);} /* * @function Open() *      open a specific drive * @type static bool  * @param  * @return *      true    if being able to open *      false   otherwise */boolDiskTray::Open(const std::string& _driveName) {    const std::string Drive = std::string("\\\\.\\") + _driveName + std::string(":");    HANDLE hDrive = INVALID_HANDLE_VALUE; // handle    // create file & att    hDrive = CreateFile( Drive.c_str(), GENERIC_READ,                         FILE_SHARE_READ | FILE_SHARE_WRITE,                         NULL, OPEN_EXISTING,                         FILE_ATTRIBUTE_NORMAL, NULL );    BOOL bRetVal = FALSE;    // state good    if (( hDrive != INVALID_HANDLE_VALUE) && (NO_ERROR == GetLastError() )) {        DWORD dwDm = 0;        // eject        bRetVal = DeviceIoControl( hDrive, IOCTL_STORAGE_EJECT_MEDIA,                                   NULL, 0, NULL, 0, &dwDm, NULL );    }       // avoid warning    return (!!bRetVal);} /* * @function Close() *      close a specific drive * @type static bool  * @param  * @return *      true    if being able to close *      false   otherwise */boolDiskTray::Close(const std::string& _driveName) {    const std::string Drive = std::string("\\\\.\\") + _driveName + std::string(":");    HANDLE hDrive = INVALID_HANDLE_VALUE; // handle    // create file & att    hDrive = CreateFile( Drive.c_str(), GENERIC_READ,                         FILE_SHARE_READ | FILE_SHARE_WRITE,                         NULL, OPEN_EXISTING,                         FILE_ATTRIBUTE_NORMAL, NULL );    BOOL bRetVal = FALSE;    // state good    if (( hDrive != INVALID_HANDLE_VALUE) && (NO_ERROR == GetLastError() )) {        DWORD dwDm = 0;        // eject        bRetVal = DeviceIoControl( hDrive, IOCTL_STORAGE_LOAD_MEDIA,                                   NULL, 0, NULL, 0, &dwDm, NULL );    }       // avoid warning    return (!!bRetVal);} /* * @function GetAll() *      retrieve list of CD/DVD-Roms * @type static std::list<std:;string>  * @param  * @return *      std::list<std::string>    */ std::list<DiskTray>DiskTray::GetAll() {        std::list<DiskTray> Drives;     //int __drive_type = 99;    char __drive_found[MAX];    char *__pdrive = __drive_found;    // get all logicals    GetLogicalDriveStrings(MAX, __drive_found);    while( *__pdrive != NULL ) {        std::string __drive;        DiskTray disk;        // check type        switch( GetDriveType(__pdrive) ) {            case 5: // CD/DVD-ROM                __drive = *__pdrive;                disk.SetDriveName(__drive);                Drives.push_back(disk); // push                break;            default: // nothing                break;        }        // next drive        __pdrive += lstrlen(__pdrive) + 1;    }       // return list    return Drives;}
    3. Program.cpp


    Mã:
    /* * @author Xcross87 * @date 05-May-2009 * @file program.cpp * @note */ /* prototype */#include "DiskTray.h"#include <stdlib.h>#include <conio.h> /* * @class TrayManager *      manage CD/DVD tray */ class TrayManager {    private:        std::list<DiskTray> Drives;        //int CountDrives;     public:        /* constructor */        TrayManager() {            Drives = DiskTray::GetAll(); // get drive list            //CountDrives = (int)Drives.size();        }        /* destructor */        ~TrayManager() {         }        /*         * @function ShowMenu()         *      show program menu         * @type    void         * @param                     * @return         */        void ShowMenu() {            std::cout <<                 "CD/DVD Tray Manager v2.0 by Xcross87 
    "                "==================================== 
    ";            std::list<DiskTray>::iterator it = Drives.begin();            std::list<DiskTray>::iterator end = Drives.end();            for( ; it != end; ++it ) {                std::cout << "\t[ " << it->GetDriveName() << ": ] 
    ";            }            std::cout <<                "==================================== 
    ";            std::cout <<                 "Pick a drive to open by name !
    "                "Press X if you want to exit! 
    ";        }         /*         * @function Run()         *      run tray manager         * @type    void         * @param                     * @return         */        void Run() {            ShowMenu();            char _input = _getch();             while( toupper(_input) != 'X' ) {                _input = toupper(_input);                std::string _drive(1, _input);                                if( _input == 'X' ) {                    exit(-1);                } else if( isMatch(_drive) ) {                    DiskTray::Open(_drive);                }                               else {                    std::cout << "Drive not found...! 
    ";                    _getch();                }                _flushall();                system("cls");                ShowMenu();                _input = _getch();            }        }                /*         * @function isMatch()         *      match user input drive exists or not         * @type    bool         * @param   1         *      _drive      const std::string&         * @return         *      true    if match         *      false   otherwise         */        bool isMatch(const std::string& _drive) {            // iterate            std::list<DiskTray>::iterator it = Drives.begin();            std::list<DiskTray>::iterator end = Drives.end();            for( ; it != end; ++it ) {                if( _drive == it->GetDriveName() ) // return if match                    return true;            }            // not found            return false;        }};  /* entry point */int main(void){    // create tray manager    TrayManager *manager = new TrayManager();    manager->Run();    // clean heap    delete manager;     // return on success    return EXIT_SUCCESS;}
    Chúc vui vẻ ^^!

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    X mà cũng MFC nữa sao [IMG]images/smilies/wink.png[/IMG] ! Chán Linux rồi hả X ? [IMG]images/smilies/biggrin.png[/IMG] !
    Anyways, good work [IMG]images/smilies/wink.png[/IMG]

    Mã:
    - void ShowMenu() - bool isMatch( const string& )
    Hai thằng này cho nó const chắc cũng không sao nhỉ.

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    vẫn xài Linux mà, hồi đó có viết mấy ver cả Windows và Linux mà [IMG]images/smilies/biggrin.png[/IMG]
    check trong box C có bản cho Linux.

    * để const không sao cả [IMG]images/smilies/biggrin.png[/IMG]

    code ở trên quên không đặt cái end là const_iterator [IMG]images/smilies/biggrin.png[/IMG]

  4. #4
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Cả it và end đều const_iterator được hết [IMG]images/smilies/wink.png[/IMG] ! I didn't notice that too [IMG]images/smilies/biggrin.png[/IMG] !

  5. #5
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trích dẫn Gửi bởi rox_rook
    Cả it và end đều const_iterator được hết [IMG]images/smilies/wink.png[/IMG] ! I didn't notice that too [IMG]images/smilies/biggrin.png[/IMG] !
    cái it là con trỏ tăng dần trong list, không const được..

  6. #6
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Are you sure [IMG]images/smilies/wink.png[/IMG] ?

    Mã:
    #include <list>#include <iostream> class CONST_TEST {private :    std::list< int > ll;public :    CONST_TEST( const int& SIZE = 10, const int& element_value = 1 )        :ll( SIZE, element_value ) {    }    void test_list() const {        for( std::list< int >::const_iterator b = ll.begin(), e = ll.end(); b != e; ++b ) {            std::cout << *b << "->";        }    }}; int main() {    CONST_TEST a;    a.test_list();    return 0;}

  7. #7
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    sozi sozi [IMG]images/smilies/laughing.gif[/IMG]

    lẫn lộn mấy khái niệm const [IMG]images/smilies/biggrin.png[/IMG]

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •