home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / ctlhtmlc / uireg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-12  |  2.5 KB  |  91 lines

  1. /*---------------------------------------------------------------------------*\
  2.  | UIReg.cpp    - DevUI(tm) string list profile functions                    |
  3.  |                From Developing User Interfaces for Microsoft Windows      |
  4.  |                Copyright (c) 1999, Everett N. McKay                       |
  5. \*---------------------------------------------------------------------------*/
  6.  
  7. #include "stdafx.h"
  8. #include "DevUI.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. // effect:  Similar to WriteProfileString, but for CStringLists.
  17. HRESULT McSetProfileStringList (LPCTSTR section, LPCTSTR entryPrefix,  
  18.                                 const CStringList &list)
  19. {
  20.    CWinApp *pApp = AfxGetApp();
  21.    CString  entry, ep;
  22.     POSITION pos;
  23.    int      i, length, oldLength, retVal;
  24.  
  25.    ASSERT_VALID(pApp);
  26.    ASSERT_VALID(&list);
  27.     ASSERT(section != 0);
  28.     ASSERT(entryPrefix != 0);
  29.  
  30.    // get old list length and set new length
  31.    ep        = (CString)entryPrefix;
  32.    entry     = ep + _T("Count");
  33.    oldLength = pApp->GetProfileInt(section, entry, 0);
  34.    length    = list.GetCount();
  35.    retVal = pApp->WriteProfileInt(section, entry, length);
  36.  
  37.    // set new items
  38.    for (i = 0, pos = list.GetHeadPosition();
  39.         pos != 0;
  40.         i++, list.GetNext(pos))
  41.    {
  42.       entry.Format(_T("%s%d"), ep, i + 1);
  43.       retVal = pApp->WriteProfileString(section, entry, list.GetAt(pos));
  44.    }
  45.  
  46.    // remove unused old items
  47.    if (oldLength > length)
  48.    {
  49.       for (i = length; i < oldLength; i++)
  50.       {
  51.          entry.Format(_T("%s%d"), ep, i + 1);
  52.          pApp->WriteProfileString(section, entry, 0);
  53.       }
  54.    }
  55.  
  56.    return (retVal > 0) ? NO_ERROR : E_FAIL;
  57. }
  58.  
  59. // effect:  Similar to GetProfileString, but for CStringLists.
  60. HRESULT McGetProfileStringList (LPCTSTR section, LPCTSTR entryPrefix, 
  61.                                 CStringList &list)
  62. {
  63.    CWinApp *pApp = AfxGetApp();
  64.    CString  entry, ep, s;
  65.    int      i, length;
  66.  
  67.    ASSERT_VALID(pApp);
  68.    ASSERT_VALID(&list);
  69.     ASSERT(section != 0);
  70.     ASSERT(entryPrefix != 0);
  71.  
  72.    // get length
  73.    ep     = (CString)entryPrefix;
  74.    entry  = ep + _T("Count");
  75.    length = pApp->GetProfileInt(section, entry, 0);
  76.  
  77.    // clear list
  78.    list.RemoveAll();
  79.  
  80.    // load list
  81.    for (i = 0; i < length; i++)
  82.    {
  83.       entry.Format(_T("%s%d"), ep, i + 1);
  84.       s = pApp->GetProfileString(section, entry, 0);
  85.       list.AddTail(s);
  86.    }
  87.  
  88.    return NO_ERROR;
  89. }
  90.  
  91.