MULTI_SZ形式の文字列を扱うクラス (CMultiSzHelper)

(2004.12.09)
#pragma once
 
// WAC.com Class Liblary for Visual C++
// Copyright (C) WAC.com Inc. All rights reserved.
//
// This file is a part of the WAC.com Class Liblary.
// The use and distribution terms for this software are covered by the
// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
// which can be found in the file CPL.TXT at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by
// the terms of this license. You must not remove this notice, or
// any other, from this software.
 
#include <vector>
 
 
/////////////////////////////////////////////////////////////////////////////
// CMultiSzHelper
 
class CMultiSzHelper
{
protected:
    typedef CMultiSzHelper                        thisClass;
 
protected:
    CMultiSzHelper();                            // 実装しない
    virtual ~CMultiSzHelper();
 
public:
    static HRESULT MakeMultiSz(void* pMulitiSzBuffer, TCHAR chDelimiter = '|')
    {
        LPTSTR pch = (LPTSTR)pMulitiSzBuffer;
        while ((pch = _tcschr(pch, chDelimiter)) != NULL)
        {
            *pch++ = '\0';
        }
 
        return S_OK;
    }
 
public:
    static HRESULT AddString(void* pMulitiSzBuffer, CString strLine)
    {
#if 0
        HRESULT hr = S_OK;
 
        std::vector<CString> strLines;
        hr = MultiSzToStrings(pMulitiSzBuffer, strLines);
        if (FAILED(hr))
        {
            // 無視する。。
        }
 
        strLines.push_back(strLine);
        
        hr = StringsToMultiSz(strLines, pMulitiSzBuffer);
        if (FAILED(hr))
        {
            // 無視する。。
        }
#else
        LPTSTR pch = (LPTSTR)pMulitiSzBuffer;
        while (*pch != _T('\0'))
        {
            CString strString = pch;
            pch +=  strString.GetLength() + 1;
        }
 
        _tcscpy(pch, (LPCTSTR)strLine);
        pch += strLine.GetLength() + 1;
 
        // 終端には、Dobule NULLで終わらせる
        *pch = _T('\0');
#endif
 
        return S_OK;
    }
 
public:
    static HRESULT MultiSzToStrings(void* pMulitiSzBuffer, std::vector<CString>& strLines)
    {
        if (pMulitiSzBuffer == NULL)
        {
            return E_INVALIDARG;
        }
 
        strLines.clear();
 
        LPTSTR pch = (LPTSTR)pMulitiSzBuffer;
        while (*pch != _T('\0'))
        {
            CString strString = pch;
            pch +=  strString.GetLength() + 1;
 
            strLines.push_back(strString);
        }
 
        return S_OK;
    }
    static HRESULT StringsToMultiSz(const std::vector<CString>& strLines, void* pMulitiSzBuffer)
    {
        if (pMulitiSzBuffer == NULL)
        {
            return E_POINTER;
        }
 
        LPTSTR pch = (LPTSTR)pMulitiSzBuffer;
        for (int i = 0 ; i < (int)strLines.size() ; i++)
        {
            CString strLine = strLines[i];
 
            _tcscpy(pch, (LPCTSTR)strLine);
            pch += strLine.GetLength() + 1;
        }
 
        // 終端には、Dobule NULLで終わらせる
        *pch = _T('\0');
 
        return S_OK;
    }
};
一覧に戻る
© 2003 WAC.com All Right Reserved.