#ifndef BOOLEAN_H
#define BOOLEAN_H

/*
 * @(#)Boolean.h
 *
 * This file is part of webCDwriter - Network CD Writing.
 *
 * Copyright (C) 2002 Jörg P. M. Haeger
 *
 * webCDwriter is free software. See CDWserver.cpp for details.
 */

#include "Exception.h"
#include "Object.h"

class Boolean: public Object {
	private: boolean val;

	public: Boolean(boolean value) {
		val = value;
	}

	public: Boolean(const char *s) {
		val = getBoolean(s);
	}

	public: Boolean(String &s) {
		val = getBoolean(s);
	}

	public: Boolean &operator=(Boolean *b) {
		val = b->val;
		delete b;
		return *this;
	}

	public: boolean booleanValue() {
		return val;
	}

	public: static boolean getBoolean(const char *name) {
		// skip initial whitespace
		while (*name == '\t' || *name == ' ')
			name++;

		// ignore trailing whitespace
		const char *str2 = name + strlen(name);
		while (str2 > name && (*str2 == '\t' || *str2 == ' '))
			str2--;
		int n = str2 - name;
		char str3[n + 1];
		strncpy(str3, name, n);
		str3[n] = 0;

		if (strcasecmp(str3, "1") == 0
			|| strcasecmp(str3, "on") == 0
			|| strcasecmp(str3, "true") == 0)
			return true;
		else
		if (strcasecmp(str3, "0") == 0
			|| strcasecmp(str3, "off") == 0
			|| strcasecmp(str3, "false") == 0)
			return false;
		else
			throw new Exception("invalid boolean");
	}

	public: static boolean getBoolean(String &name) {
		return getBoolean(name.getBytes());
	};

	public: boolean instanceof(const char *objectName) {
		return equal(objectName, "Boolean")
			|| Object::instanceof(objectName);
	}

	public: String *toString() {
		return toString(val);
	}

	public: static String *toString(boolean b) {
		if (b)
			return new String("true");
		else
			return new String("false");
	}
};

#endif
