#ifndef INTEGER_H
#define INTEGER_H

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

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

class Integer: public Object {
	private: int val;

	public: Integer(int value) {
		val = value;
	}

	public: Integer(const char *s) {
		val = getInt(s);
	}

	public: Integer &operator=(Integer *integer) {
		val = integer->val;
		delete integer;
		return *integer;
	}

	public: static int getInt(const char *s) {
		int i = 0;
		while (s[i] != 0 && s[i] == 32)
			i++;

		boolean sign = (s[i] == '-');
		if (sign)
			i++;

		int value = 0;
		while (s[i] != 0) {
			if (s[i] < '0' || s[i] > '9')
				if (s[i++] == 'o') {
					if (s[i] == 'f')
						return 0;
					if (s[i] == 'n')
						return 1;
				}
				else
				throw new Exception(
					S.e + "invalid integer <" + s + ">");
			value = 10 * value + s[i] - '0';
			i++;
		}

		if (sign)
			return -value;
		else
			return value;
	}

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

	public: int intValue() {
		return val;
	}

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

	public: static String *toString(int i) {
		return String::valueOf(i);
	}
};

#endif
