/* Web Polygraph       http://www.web-polygraph.org/
 * (C) 2003-2006 The Measurement Factory
 * Licensed under the Apache License, Version 2.0 */

#ifndef POLYGRAPH__CLIENT_USERCRED_H
#define POLYGRAPH__CLIENT_USERCRED_H

#include "xstd/String.h"

// maintains information about user credentials
class UserCred {
	public:
		UserCred(): isValid(true) {}
		UserCred(const String &anImage): theImage(anImage), isValid(true) {}

		void reset() { theImage = String(); isValid = true; }

		bool valid() const { return isValid; }
		inline const String &image() const { return theImage; }
		inline Area name() const;
		inline Area password() const;

		inline void invalidate();

	protected:
		String theImage;
		bool isValid;
};

inline
Area UserCred::name() const {
	if (!isValid)
		return Area::Empty();

	const char *p = theImage.chr(':');
	const int len = p ? p - theImage.cstr() : theImage.len();
	return theImage.area(0, len);
}

Area UserCred::password() const {
	if (!isValid)
		return Area::Empty();

	if (const char *p = theImage.chr(':'))
		return theImage.area(p - theImage.data() + 1);
	else
		return Area::Empty();
}

inline
void UserCred::invalidate() {
	if (theImage.len() > 0)
		theImage += theImage.last();
	isValid = false;
}

#endif


syntax highlighted by Code2HTML, v. 0.9.1