Annotation of mstools/mfc/samples/oserver/bibdoc.cpp, revision 1.1.1.1

1.1       root        1: // This is a part of the Microsoft Foundation Classes C++ library.
                      2: // Copyright (C) 1992 Microsoft Corporation
                      3: // All rights reserved.
                      4: //
                      5: // This source code is only intended as a supplement to the
                      6: // Microsoft Foundation Classes Reference and Microsoft
                      7: // QuickHelp documentation provided with the library.
                      8: // See these sources for detailed information regarding the
                      9: // Microsoft Foundation Classes product.
                     10: 
                     11: 
                     12: #include "bibref.h"
                     13: #include "bibdoc.h"
                     14: #include "bibitem.h"
                     15: 
                     16: #include <malloc.h>
                     17: 
                     18: /////////////////////////////////////////////////////////////////////////////
                     19: 
                     20: 
                     21: CBibDoc::CBibDoc(CListBox* pList,
                     22:        const char* pszFileName, const char* pszSectionName)
                     23: {
                     24:        m_pList = pList;
                     25:        m_pszFileName = pszFileName;        // Private INI file
                     26:        m_pszSectionName = pszSectionName;
                     27: 
                     28:        m_pSelectedItem = NULL;     // no item yet
                     29: }
                     30: 
                     31: BOOL CBibDoc::Load()
                     32: {
                     33:        int nDataSize;
                     34: 
                     35:        LPSTR lpBuffer = (LPSTR)_fmalloc(MAX_INI_FILE);
                     36:        if (lpBuffer == NULL)
                     37:                return FALSE;
                     38: 
                     39:        nDataSize = GetPrivateProfileString(m_pszSectionName,
                     40:                NULL, "", lpBuffer, MAX_INI_FILE, m_pszFileName);
                     41: 
                     42:        if (nDataSize == 0) 
                     43:        {
                     44:                // no initial data or data corrupt
                     45:                _ffree(lpBuffer);
                     46:                return TRUE;
                     47:        }
                     48: 
                     49:        if (nDataSize >= MAX_INI_FILE)
                     50:        {
                     51:                // no initial data or data corrupt
                     52:                _ffree(lpBuffer);
                     53:                return FALSE;
                     54:        }
                     55: 
                     56:        LPSTR lpKey = lpBuffer;
                     57:        // lpKey points to a list of string for the keys
                     58:        while (*lpKey != '\0')
                     59:        {
                     60:                char szKey[OLE_MAXNAMESIZE*2];  // more than large enough
                     61:                int cch = _fstrlen(lpKey);
                     62: 
                     63:                _fstrcpy(szKey, lpKey);
                     64:                char* pchNext = &szKey[cch];
                     65:                *pchNext++ = '\t';
                     66: 
                     67:                GetPrivateProfileString(m_pszSectionName,
                     68:                        lpKey, "", pchNext, sizeof(szKey)-2-cch, m_pszFileName);
                     69:                lpKey += cch + 1;
                     70:                m_pList->AddString(szKey);
                     71:        }
                     72: 
                     73:        _ffree(lpBuffer);
                     74:        return TRUE;
                     75: }
                     76: 
                     77: /////////////////////////////////////////////////////////////////////////////
                     78: 
                     79: void CBibDoc::AddItem(const CString& key, const CString& value)
                     80: {
                     81:        CString both = key + "\t" + value;
                     82:        m_pList->SetCurSel(m_pList->AddString(both));
                     83: 
                     84:        WritePrivateProfileString(m_pszSectionName, key, value, m_pszFileName);
                     85: }
                     86: 
                     87: void CBibDoc::DeleteItem(const CString& key, int nIndex)
                     88: {
                     89:        ASSERT(nIndex != -1);
                     90:        m_pList->DeleteString(nIndex);
                     91:        WritePrivateProfileString(m_pszSectionName, key, NULL, m_pszFileName);
                     92: }
                     93: 
                     94: /////////////////////////////////////////////////////////////////////////////
                     95: 
                     96: void CBibDoc::GetItemKeyValue(int nIndex, CString& key, CString& value)
                     97: {
                     98:        ASSERT(nIndex != -1);
                     99:        CString both;
                    100:        m_pList->GetText(nIndex, both);
                    101: 
                    102:        // tab character ('\t') separates key and value
                    103:        int i = both.Find("\t");
                    104:        if (i == -1)
                    105:        {
                    106:                // key only "<key>"
                    107:                key = both;
                    108:                value = "???";
                    109:        }
                    110:        else
                    111:        {
                    112:                // format is "<key> \t <value>"
                    113:                key = both.Left(i);
                    114:                value = both.Mid(i+1);
                    115:        }
                    116: }
                    117: 
                    118: int CBibDoc::GetItemValue(const CString& key, CString& value)
                    119:        // return index or -1 if not found
                    120: {
                    121:        // we could either look in the listbox or the file
                    122:        CString findString = key + "\t";
                    123:        int nIndex = m_pList->FindString(-1, findString);
                    124:        if (nIndex == -1)
                    125:                return -1;
                    126: 
                    127:        CString key2;
                    128:        GetItemKeyValue(nIndex, key2, value);
                    129:        ASSERT(key2.CompareNoCase(key) == 0);   // same key (case insensitive)
                    130:        return nIndex;
                    131: }
                    132: 
                    133: /////////////////////////////////////////////////////////////////////////////
                    134: // UI Specific
                    135: 
                    136: BOOL CBibDoc::ShowItem(const CString& key)
                    137: {
                    138:        CString findString = key + "\t";
                    139:        int nIndex = m_pList->FindString(-1, findString);
                    140:        if (nIndex == -1)
                    141:        {
                    142:                m_pList->SetCurSel(-1);
                    143:                return FALSE;
                    144:        }
                    145:        m_pList->SetCurSel(nIndex);
                    146:        return TRUE;
                    147: }
                    148: 
                    149: /////////////////////////////////////////////////////////////////////////////
                    150: // OLE Server functionality
                    151: 
                    152: BOOL CBibDoc::UpdateClient(const CString& string)
                    153: {
                    154:        if (m_pSelectedItem != NULL)
                    155:        {
                    156:                m_pSelectedItem->ChangeKey(string);
                    157:        }
                    158:        else
                    159:        {
                    160:                TRACE("Warning: UpdateClient with no item\n");
                    161:        }
                    162:        
                    163:        TRACE("Notifying of saved\n");
                    164:        NotifySaved();
                    165:        return TRUE;
                    166: }
                    167: 
                    168: COleServerItem* CBibDoc::OnGetDocument()
                    169: {
                    170:        TRACE("Creating an item to represent the entire (blank) document\n");
                    171:        CBibItem* pItem = new CBibItem(NULL);
                    172:        ASSERT(m_pSelectedItem == NULL);
                    173:        m_pSelectedItem = pItem;
                    174:        return pItem;
                    175: }
                    176: 
                    177: COleServerItem* CBibDoc::OnGetItem(LPCSTR lpszObjname)
                    178: {
                    179:        CString key = lpszObjname;
                    180:        TRACE("Doc::GetObject(%s)\n", (const char*)key);
                    181:        CString findString = key + "\t";
                    182:        int nIndex = m_pList->FindString(-1, findString);
                    183:        if (nIndex == -1)
                    184:        {
                    185:                TRACE("couldn't find (%Fs)\n", lpszObjname);
                    186:                return NULL;
                    187:        }
                    188:        
                    189:        CBibItem* pItem = new CBibItem(key);
                    190:        ASSERT(m_pSelectedItem == NULL);
                    191:        m_pSelectedItem = pItem;
                    192:        return pItem;
                    193: }
                    194: 
                    195: COleServerItem* CBibDoc::GetNextItem(POSITION& rPosition)
                    196: {
                    197:        if (rPosition == NULL)
                    198:        {
                    199:                rPosition = (POSITION) 1;
                    200:                return m_pSelectedItem;
                    201:        }
                    202:        else
                    203:        {
                    204:                return NULL;
                    205:        }
                    206: }
                    207: 
                    208: OLESTATUS CBibDoc::OnRelease()
                    209: {
                    210:        OLESTATUS status;
                    211:        if ((status = COleServerDoc::OnRelease()) != OLE_OK)
                    212:                return status;
                    213: 
                    214:        TRACE("Fake destroying CBibDoc\n");
                    215:        //   don't do this -> delete this;
                    216:        return OLE_OK;
                    217: }
                    218: 
                    219: OLESTATUS CBibDoc::OnSetHostNames(LPCSTR lpszHost, LPCSTR lpszHostObj)
                    220: {
                    221:        // save host names in case needed later
                    222:        m_strHost = lpszHost;
                    223:        m_strHostObj = lpszHostObj;
                    224:        return OLE_OK;
                    225: }
                    226: 
                    227: /////////////////////////////////////////////////////////////////////////////
                    228: 
                    229: #ifdef _DEBUG
                    230: void CBibDoc::AssertValid() const
                    231: {
                    232:        COleServerDoc::AssertValid();
                    233:        ASSERT(m_pList != NULL);
                    234: }
                    235: 
                    236: 
                    237: void CBibDoc::Dump(CDumpContext& dc) const
                    238: {
                    239:        COleServerDoc::Dump(dc);
                    240:        dc << "m_pList = " << m_pList << "\n";
                    241:        dc << "m_pszFileName = " << m_pszFileName << "\n";
                    242:        dc << "m_pszSectionName = " << m_pszSectionName << "\n";
                    243:        dc << "m_pSelectedItem = " << m_pSelectedItem;
                    244: }
                    245: 
                    246: #endif
                    247: 
                    248: /////////////////////////////////////////////////////////////////////////////

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.