Click here to Skip to main content
15,898,035 members
Articles / Web Development / HTML

Discover ISAPI. Working with GET-POST Data

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
15 Jul 20024 min read 105.2K   2.5K   37  
That article present a way to retrieve the GET-POST data from a form into an ISAPI extension.
///////////////////////////////////////////////////////////////////
// EnBSTR
// Copyright (C) 2001 Morten Abrahamsen (mabraha@online.no)
// All rights reserved.
//
// The code and information is provided "as-is" without
// warranty of any kind, either expressed or implied.
///////////////////////////////////////////////////////////////////
// 2001-02-25: 
//	- Initial Release
// 2001-02-28:
//	- Added upper, lower, reverse
//	- Added replace, spanInclude, spanExclude
///////////////////////////////////////////////////////////////////


#ifndef __ENBSTR_H__
#define __ENBSTR_H__

#pragma once

#ifndef __cplusplus
	#error ATL requires C++ compilation (use a .cpp suffix)
#endif

#include "comdef.h"

class CEnBSTR : public _bstr_t
{
public:
	// Constructors
	CEnBSTR() : _bstr_t() {}
	CEnBSTR(const char* s) : _bstr_t(s) {}
    CEnBSTR(const CEnBSTR& s) : _bstr_t(s) {}
    CEnBSTR(const _bstr_t& s) : _bstr_t(s) {}
    CEnBSTR(const wchar_t* s) : _bstr_t(s) {}
    CEnBSTR(const _variant_t& var) : _bstr_t(var) {}
    CEnBSTR(BSTR bstr, bool fCopy) : _bstr_t(bstr, fCopy) {}

	// Analysis
	int find(const _bstr_t& bstr) const
	{
		const wchar_t* pstr = _bstr_t::operator const wchar_t*();
		wchar_t* pdest = wcsstr(pstr, bstr);
		return pdest ? pdest - pstr : -1;
	}
	int find(const wint_t& ch) const
	{
		const wchar_t* pstr = _bstr_t::operator const wchar_t*();
		wchar_t* pdest = wcschr(pstr, ch);
		return pdest ? pdest - pstr : -1;
	}
	int find(const _bstr_t& bstr, const unsigned int nStart) const
	{
		const wchar_t* pstr = _bstr_t::operator const wchar_t*();
		if (nStart >= length()) return -1;

		wchar_t* pdest = wcsstr(pstr+nStart, bstr);
		return pdest ? pdest - pstr : -1;
	}
	int find(const wint_t& ch, const unsigned int nStart) const
	{
		const wchar_t* pstr = _bstr_t::operator const wchar_t*();

		if (nStart >= length()) return -1;

		wchar_t* pdest = wcschr(pstr+nStart, ch);
		return pdest ? pdest - pstr : -1;
	}

	/////////////////////////////////////////////////////////////////////
	// Extraction
	CEnBSTR mid(const unsigned int nFirst, unsigned int nCount = 0) const
	{
		unsigned int nLength = _bstr_t::length();
		if (nCount == 0) 
			nCount = nLength-nFirst;

		if (nFirst == 0 && nCount >= nLength)
			return *this;


		const wchar_t* pstr = _bstr_t::operator const wchar_t*();
		CEnBSTR bstr;

		if (nLength >= (nFirst+nCount))
		{
			wchar_t* pNewStr = new wchar_t[nCount+1];
			ZeroMemory(pNewStr, sizeof(wchar_t)*(nCount+1));
			wcsncpy(pNewStr, pstr+nFirst, nCount);
			bstr = pNewStr;
			delete [] pNewStr;
		}

		return bstr;
	}

	CEnBSTR left(const unsigned int nCount) const
	{
		return mid(0, nCount);
	}

	CEnBSTR right(const unsigned int nCount) const
	{
		unsigned int nLength = _bstr_t::length();
		if (nLength >= nCount)
			return mid(nLength-nCount, nCount);

		return *this;
	}

	CEnBSTR spanInclude(const _bstr_t& bstrCharset) const
	{
		const wchar_t* pstr = _bstr_t::operator const wchar_t*();
		unsigned int nLen = wcsspn(pstr, bstrCharset);
		
		return nLen ? mid(0, nLen) : "";
	}

	CEnBSTR spanExclude(const _bstr_t& bstrCharset) const
	{
		const wchar_t* pstr = _bstr_t::operator const wchar_t*();
		unsigned int nLen = wcscspn(pstr, bstrCharset);
		
		return nLen ? mid(0, nLen) : "";
	}

	/////////////////////////////////////////////////////////////////////
	// Manipulation
	void upper()
	{
		_bstr_t bstr(*this, true);
		_wcsupr((wchar_t*)bstr);
		*this = bstr;
	}

	void lower()
	{
		_bstr_t bstr(*this, true);
		_wcslwr((wchar_t*)bstr);
		*this = bstr;
	}

	void reverse()
	{
		_bstr_t bstr(*this, true);
		_wcsrev((wchar_t*)bstr);
		*this = bstr;
	}

	unsigned int replace(const wint_t& chFind, const wint_t& chReplace)
	{
		const wchar_t* pstr = _bstr_t::operator const wchar_t*();
		if (pstr == NULL) return 0;
		if (chFind == chReplace) return 0;

		// Make a copy of the string to avoid reference counting problems.
		_bstr_t bstrNew(*this, true);
		pstr = (wchar_t*)bstrNew;


		unsigned int nCount = 0;
		wchar_t* pSearchPos = 0;
		while (pSearchPos = wcschr(pSearchPos ? pSearchPos : pstr, chFind))
		{
			*pSearchPos = chReplace;
			++nCount;			
		}

		// Assign the new string (avoids refcount and deletes cached char-string)
		*this = bstrNew;
		return nCount;
	}

	unsigned int replace(const _bstr_t& bstrFind, const _bstr_t& bstrReplace)
	{
		const wchar_t* pSrcStr = _bstr_t::operator const wchar_t*();
		if (pSrcStr == NULL) return 0;

		unsigned int nCount = 0;
		unsigned int nSearchStrLen = bstrFind.length();

		// Calculate new length
		wchar_t* pSearchPos = 0;
		while (pSearchPos = wcsstr(pSearchPos ? pSearchPos : pSrcStr, bstrFind))
		{
			pSearchPos += nSearchStrLen;
			++nCount;			
		}

		// Replace bstrFind substrings with bstrReplace substrings
		if (nCount)
		{
			unsigned int nReplaceStrLen = bstrReplace.length();
			unsigned int nOrgStrLen = length();
			unsigned int nNewStrLength = nOrgStrLen - (nSearchStrLen - nReplaceStrLen)*nCount;
			nCount = 0;

			wchar_t* pNewStr = SysAllocStringLen(0, nNewStrLength);
			wchar_t* pSrcPos = (wchar_t*)pSrcStr;
			wchar_t* pDestPos = pNewStr;

			wchar_t* pNextSrcPos = 0;
			while (pNextSrcPos = wcsstr(pSrcPos, bstrFind))
			{
				if (pSrcPos != pNextSrcPos)
				{
					unsigned int nCopyLength = pNextSrcPos-pSrcPos;
					wcsncpy(pDestPos, pSrcPos, nCopyLength);
					pDestPos += nCopyLength;

					pSrcPos = pNextSrcPos;
				}

				wcsncpy(pDestPos, bstrReplace, nReplaceStrLen);
				pDestPos += nReplaceStrLen;
				pSrcPos += nSearchStrLen;
				++nCount;
			}

			wcsncpy(pDestPos, pSrcPos, nOrgStrLen - (pSrcPos-pSrcStr));

			*this = bstr_t(pNewStr, false);
		}
		return nCount;
	}

	unsigned int replace(const _bstr_t& bstrFind, const _bstr_t& bstrReplace, unsigned int nFirst)
	{
		unsigned int nCount = 0;
		unsigned int nnn = 0;

		CEnBSTR		bstrLeft(bstrFind);
		CEnBSTR		bstrRight(bstrFind);
		
		bstrLeft	= bstrLeft.left(nFirst);
		bstrRight	= bstrRight.mid(nFirst);

		nCount		= replace(bstrRight, bstrReplace);

		nnn			= bstrLeft.length() + bstrRight.length();

		wchar_t* pNewStr = SysAllocStringLen(0, nnn);

		bstrLeft 	= bstrLeft + bstrRight;
		
		wcsncpy(pNewStr, bstrLeft, nnn);

		*this = bstr_t(pNewStr, false);
		return nCount;
	}

};

#endif (__ENBSTR_H__)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
Web Developer
Romania Romania
I make programming for over 4 years and extensive experience in C++, ASP, Pascal, MFC, COM+, ATL, TCP/IP, HTTP protocols, XML, XSL, SOAP and SQL.
For the last 2 years i working extensively at the background of financial sites (databases, n tier architecture).

I’m available for contracts and/or outsourcing (<Adrian Bacaianu>adrian_bacaianu@yahoo.com).

Comments and Discussions