//--------------------- CSPParameters.h -----------------------

//*************************************************************
//*****       Copyright ©2002 - 2004 Micronovae Ltd       *****
//*****                All rights reserved                *****
//*************************************************************


#ifndef _CSPPARAMETERS
#define _CSPPARAMETERS

#include "CSPConf.h"
#include "CSPLib.h"
#include <httpext.h>
#include "CSPList.h"
#include "CSPString.h"
#include "CSPBuffer.h"
#include "CSPMap.h"

class CSPOBJ_CLASS_EXPORT CSPRequest;



class CSPOBJ_CLASS_EXPORT CSPParam : public CSPObject
{
	typedef CSPList<CSPString> CSPValueList;
	friend class CSPParamSet;

	public:
		CSPParam();
		virtual ~CSPParam();

		const CSPString& GetName() const;
		const CSPString& GetValue(unsigned long x = 0) const;
		const CSPString& operator[](unsigned long x) const;
		const CSPString& operator()(unsigned long x) const;
		operator const CSPString& () const;
		unsigned long GetCount() const;

	protected:
		CSPString strName;
		CSPValueList ValueList;
		CSPString strNull;

		void AddValue(const CSPString& strValue);
};

//------------------------ CSPParamSet -----------------------

class CSPOBJ_CLASS_EXPORT CSPParamSet : public CSPObject
{
		typedef CSPMap< CSPString, CSPParam > CSPParamMap;

	public:
		CSPParamSet( const CSPRequest& Request );
		virtual ~CSPParamSet();
		const CSPParam& GetParam( const CSPString& strParamName ) const;
		const CSPParam& GetParam( unsigned long ulIndex ) const;
		const CSPParam& operator () ( const CSPString& strParamName ) const;
		const CSPParam& operator [] ( unsigned long ulIndex ) const;
		unsigned long GetCount() const;
			
	protected:
		CSPParamMap ParamMap;
		const CSPParam NullParam;
		bool bParsed;
		const CSPRequest& ParentRequest;

		void MakeLists( const char* lpszParamData, int len = -1 );
		virtual bool PrepareParams() = 0;
};


//---------------------- CSPUploadFile -----------------------

class CSPOBJ_CLASS_EXPORT CSPUploadFile : public CSPObject
{
public:
	CSPUploadFile();
	virtual ~CSPUploadFile();

	CSPString Filename;
	CSPString ContentType;
	CSPBuffer Data;
};


//---------------------- CSPUploadFileGroup -----------------------

class CSPOBJ_CLASS_EXPORT CSPUploadFileGroup : public CSPObject
{
	friend class CSPOBJ_CLASS_EXPORT CSPUploadFileSet;

public:
	CSPUploadFileGroup();
	virtual ~CSPUploadFileGroup();
	unsigned long GetCount() const;
	const CSPString& GetName() const;
	const CSPUploadFile& GetFile( unsigned long ulIndex ) const;
	const CSPUploadFile& operator ()( unsigned long ulIndex ) const;
	const CSPUploadFile& operator []( unsigned long ulIndex ) const;

protected:
	CSPString strName;
	CSPList<CSPUploadFile> FileList;
	CSPUploadFile NullFile;
};

//---------------------- CSPUploadFileSet ----------------------

class CSPOBJ_CLASS_EXPORT CSPUploadFileSet : public CSPObject
{
	friend class CSPOBJ_CLASS_EXPORT CSPForm;
public:
	CSPUploadFileSet( const CSPRequest& Request );
	virtual ~CSPUploadFileSet();

	const CSPUploadFileGroup& GetGroup( const CSPString& strName ) const;
	const CSPUploadFileGroup& GetGroup( unsigned long ulIndex ) const;
	const CSPUploadFileGroup& operator () ( const CSPString& strName ) const;
	const CSPUploadFileGroup& operator [] ( unsigned long ulIndex ) const;
	unsigned long GetCount() const;

protected:
	const CSPRequest& ParentRequest;
	CSPMap<CSPString,CSPUploadFileGroup> FileGroupMap;
	CSPUploadFileGroup NullFileGroup;
	bool bParsed;

	bool PrepareFiles();
	static unsigned long Find( const CSPString& strBody, const CSPString& strFind, unsigned long ulStart = 0 );
};



//---------------------- CSPForm --------------------------


class CSPOBJ_CLASS_EXPORT CSPForm : public CSPParamSet
{
	friend class CSPOBJ_CLASS_EXPORT CSPUploadFileSet;
	public:
		CSPUploadFileSet Files;
		CSPForm( const CSPRequest& Request );
		virtual ~CSPForm();	
			
	protected:
		bool PrepareParams();
};





//---------------------- CSPQueryString --------------------------

class CSPOBJ_CLASS_EXPORT CSPQueryString : public CSPParamSet
{
	public:

		CSPQueryString( const CSPRequest& Request );
		virtual ~CSPQueryString();
			
	protected:
		bool PrepareParams();
};

#endif
