OSDN Git Service

Use String and std::vector instead of CString, LPCTSTR and CArray
authorMarcel Gosselin <marcelgosselin@users.sourceforge.net>
Mon, 30 Jun 2008 13:37:00 +0000 (13:37 +0000)
committerMarcel Gosselin <marcelgosselin@users.sourceforge.net>
Mon, 30 Jun 2008 13:37:00 +0000 (13:37 +0000)
Src/DirColsDlg.cpp
Src/DirColsDlg.h
Src/DirViewColHandler.cpp

index 4a08faa..31ddbd4 100644 (file)
@@ -13,6 +13,7 @@
 #include "merge.h"
 #include "DirColsDlg.h"
 #include "dllver.h"
+#include <algorithm>
 
 #ifdef _DEBUG
 #define new DEBUG_NEW
@@ -91,11 +92,11 @@ BOOL CDirColsDlg::OnInitDialog()
  */
 void CDirColsDlg::LoadLists()
 {
-       for (int i = 0; i < m_cols.GetSize(); i++)
+       for (ColumnArray::iterator iter = m_cols.begin(); iter != m_cols.end(); ++iter)
        {
-               const column & c = m_cols[i];
+               const column & c = *iter;
                int x = m_listColumns.InsertItem(m_listColumns.GetItemCount(),
-                       m_cols[i].name);
+                       c.name.c_str());
                m_listColumns.SetItemData(x, c.log_col);
                if (c.phy_col >= 0)
                        m_listColumns.SetCheck(x, TRUE);
@@ -120,11 +121,11 @@ void CDirColsDlg::SelectItem(int index)
  */
 void CDirColsDlg::LoadDefLists()
 {
-       for (int i=0; i<m_cols.GetSize(); ++i)
+       for (ColumnArray::iterator iter = m_defCols.begin(); iter != m_defCols.end(); ++iter)
        {
-               const column & c = m_defCols[i];
+               const column & c = *iter;
                int x = m_listColumns.InsertItem(m_listColumns.GetItemCount(),
-                       m_cols[i].name);
+                       c.name.c_str());
                m_listColumns.SetItemData(x, c.log_col);
                if (c.phy_col >= 0)
                        m_listColumns.SetCheck(x, TRUE);
@@ -136,7 +137,7 @@ void CDirColsDlg::LoadDefLists()
  */
 void CDirColsDlg::SortArrayToLogicalOrder()
 {
-       qsort(m_cols.GetData(), m_cols.GetSize(), sizeof(m_cols[0]), &cmpcols);
+       std::sort(m_cols.begin(), m_cols.end(), &CompareColumnsByLogicalOrder);
 }
 
 /**
@@ -145,11 +146,9 @@ void CDirColsDlg::SortArrayToLogicalOrder()
  * @param [in] el2 Second column to compare.
  * @return Column order.
  */
-int __cdecl CDirColsDlg::cmpcols(const void * el1, const void * el2)
+bool CDirColsDlg::CompareColumnsByLogicalOrder( const column & el1, const column & el2 )
 {
-       const column * col1 = reinterpret_cast<const column *>(el1);
-       const column * col2 = reinterpret_cast<const column *>(el2);
-       return col1->log_col - col2->log_col;
+   return el1.log_col < el2.log_col;
 }
 
 /**
@@ -250,11 +249,11 @@ void CDirColsDlg::OnOK()
        {
                BOOL checked = m_listColumns.GetCheck(i);
                DWORD_PTR data = m_listColumns.GetItemData(i);
-               column * col1 = &m_cols[data];
+               column & col1 = m_cols[data];
                if (checked)
-                       col1->phy_col = i;
+                       col1.phy_col = i;
                else
-                       col1->phy_col = -1;
+                       col1.phy_col = -1;
        }
 
        CDialog::OnOK();
@@ -283,13 +282,13 @@ void CDirColsDlg::OnLvnItemchangedColdlgList(NMHDR *pNMHDR, LRESULT *pResult)
                int ind = m_listColumns.GetNextSelectedItem(pos);
                DWORD_PTR data = m_listColumns.GetItemData(ind);
 
-               int j = 0;
-               for (j = 0; j < m_cols.GetSize(); j++)
+               ColumnArray::size_type j;
+               for (j = 0; j < m_cols.size(); j++)
                {
-                       if (m_cols.GetAt(j).log_col == data)
+                       if (m_cols[j].log_col == data)
                                break;
                }
-               GetDlgItem(IDC_COLDLG_DESC)->SetWindowText(m_cols[j].desc);
+               GetDlgItem(IDC_COLDLG_DESC)->SetWindowText(m_cols[j].desc.c_str());
 
                // Disable Up/Down -buttons when first/last items are selected.
                if (ind == 0)
index 64c50ba..1fb6161 100644 (file)
@@ -13,6 +13,8 @@
 #define AFX_DIRCOLSDLG_H__2FCB576C_C609_4623_8C55_F3870F22CA0B__INCLUDED_
 #pragma once
 
+#include <vector>
+#include "UnicodeString.h"
 
 /////////////////////////////////////////////////////////////////////////////
 // CDirColsDlg dialog
@@ -35,22 +37,23 @@ public:
        /** @brief One column's information. */
        struct column
        {
-               CString name; /**< Column name */
-               CString desc; /**< Description for column */
+               String name; /**< Column name */
+               String desc; /**< Description for column */
                int log_col; /**< Logical (shown) order number */
                int phy_col; /**< Physical (in memory) order number */
-               column() : log_col(-1), phy_col(-1) { } /**< default constructor for use in CArray */
-               column(LPCTSTR sz, LPCTSTR dsc, int log, int phy) : name(sz), desc(dsc), log_col(log), phy_col(phy) { } 
+               column(const String & colName, const String & dsc, int log, int phy)
+                       : name(colName), desc(dsc), log_col(log), phy_col(phy)
+               { } 
        };
-       typedef CArray<column, column> ColumnArray;
+       typedef std::vector<column> ColumnArray;
 
 // Construction
 public:
        CDirColsDlg(CWnd* pParent = NULL);   // standard constructor
-       void AddColumn(LPCTSTR name, LPCTSTR desc, int log, int phy=-1)
-               { column c(name, desc, log, phy); m_cols.Add(c); }
-       void AddDefColumn(LPCTSTR name, int log, int phy=-1)
-               { column c(name, _T(""), log, phy); m_defCols.Add(c); }
+       void AddColumn(const String & name, const String & desc, int log, int phy=-1)
+               { column c(name, desc, log, phy); m_cols.push_back(c); }
+       void AddDefColumn(const String & name, int log, int phy=-1)
+               { column c(name, _T(""), log, phy); m_defCols.push_back(c); }
        const ColumnArray & GetColumns() const { return m_cols; }
 
 // Dialog Data
@@ -78,12 +81,12 @@ protected:
        void MoveItem(int index, int newIndex);
        void MoveSelectedItems(BOOL bUp);
        void SanitizeOrder();
-       static int cmpcols(const void * el1, const void * el2);
 
 // Implementation data
 private:
        ColumnArray m_cols; /**< Column list. */
        ColumnArray m_defCols; /**< Default columns. */
+       static bool CompareColumnsByLogicalOrder( const column & el1, const column & el2 );
 
        // Generated message map functions
        //{{AFX_MSG(CDirColsDlg)
index 7631cf2..e220ce8 100644 (file)
@@ -446,7 +446,7 @@ void CDirView::OnEditColumns()
        for (int col=0; col<GetListCtrl().GetHeaderCtrl()->GetItemCount(); ++col)
        {
                int l = ColPhysToLog(col);
-               dlg.AddColumn(GetColDisplayName(l).c_str(), GetColDescription(l).c_str(), l, col);
+               dlg.AddColumn(GetColDisplayName(l), GetColDescription(l), l, col);
        }
        // Now add all the columns not currently displayed
        int l=0;
@@ -454,7 +454,7 @@ void CDirView::OnEditColumns()
        {
                if (ColLogToPhys(l)==-1)
                {
-                       dlg.AddColumn(GetColDisplayName(l).c_str(), GetColDescription(l).c_str(), l);
+                       dlg.AddColumn(GetColDisplayName(l), GetColDescription(l), l);
                }
        }
 
@@ -462,7 +462,7 @@ void CDirView::OnEditColumns()
        for (l = 0; l < m_numcols; ++l)
        {
                int phy = GetColDefaultOrder(l);
-               dlg.AddDefColumn(GetColDisplayName(l).c_str(), l, phy);
+               dlg.AddDefColumn(GetColDisplayName(l), l, phy);
        }
 
        if (dlg.DoModal() != IDOK)
@@ -478,10 +478,11 @@ void CDirView::OnEditColumns()
        ClearColumnOrders();
        m_dispcols = 0;
        const int sortColumn = GetOptionsMgr()->GetInt(OPT_DIRVIEW_SORT_COLUMN);
-       for (int i=0; i<cols.GetSize(); ++i)
+       for (CDirColsDlg::ColumnArray::const_iterator iter = cols.begin();
+               iter != cols.end(); ++iter)
        {
-               int log = cols[i].log_col;
-               int phy = cols[i].phy_col;
+               int log = iter->log_col;
+               int phy = iter->phy_col;
                m_colorder[log] = phy;
                if (phy>=0)
                {