/*********************************************************************
 * This file is part of the cpplibs suite.
 *
 * Copyright (C) 2001 Topi Mäenpää
 * All rights reserved.
 *
 * This program is free software. You can redistribute and/or modify
 * it under the terms of the free software licence found in the
 * accompanying file "COPYING". The licence terms must always be
 * redistributed with this source file. The above copyright notice
 * must be reproduced in all modified and unmodified copies of this
 * source file.
 *
 * $Revision: 1.6 $
 *********************************************************************/

#ifndef _GENERALFILE_H
#define _GENERALFILE_H

#include <stdio.h>
#include <unistd.h>
#include <string>

#include "IO.h"

#define MAXLEN 64

namespace util { namespace io {
	/**
	 * GeneralFile is a common base class for all types of files. As
	 * just about everything in unix is a file, this class can be used
	 * as a front-end for device and socket communications.
	 **/
	class GeneralFile
	{
	public:
		/**
		 * Construct a new GeneralFile object. By default, the file
		 * descriptor of the file will be set to 0 (stdin).
		 **/
		GeneralFile(void) { fd = 0; }

		/**
		 * Destroy the file object.
		 **/
		virtual ~GeneralFile() {}

		/**
		 * Write a null-terminated character array to a file. The null
		 * character will not be written.
		 * @exception IOException if the write does not succeed
		 **/
		void write(const char* msg) throw (IOException&) { write(msg,strlen(msg)); }
		/**
		 * Write a string to a file.
		 * @exception IOException if the write does not succeed
		 **/
		void write(const std::string& msg) throw (IOException&) { write(msg.data(),msg.size()); }
		/**
		 * Write data to a file. The other write methods call this method
		 * to implement the actual write operation. It can be overridden
		 * by subclasses if needed.
		 * @param msg the data to be written
		 * @param len the number of bytes to write
		 * @exception IOException if the write does not succeed
		 **/
		virtual void write(const void* msg, int len) throw (IOException&);

		//  		void write(char msg);
		void write(int msg) throw (IOException&);
		void write(float msg) throw (IOException&);
		//  		void write(double msg);

		/**
		 * Read a line from a file. This method will read bytes from a
		 * file until a newline is encountered. If the last character
		 * before the newline is carriage return (\r), it will be stripped
		 * off.
		 * @return a line read from a file
		 * @exception IOException if the read does not succeed
		 **/
		std::string readLine(void) throw (IOException&);

		/**
		 * Read data from a file. The other read methods call this method
		 * to implement the actual read operation. It can be overridden by
		 * subclasses if needed.
		 * @param buf a place for the read data
		 * @param len the number of bytes to read
		 * @return buf
		 * @exception IOException if the read does not succeed
		 **/
		virtual void* read(void* buf, int len) throw (IOException&);

		/**
		 * Open a file.
		 * @exception IOException if the file cannot be opened
		 **/
		virtual void open(void) throw (IOException&) {};

		/**
		 * Close a file.
		 * @exception IOException if the file cannot be closed
		 **/
		virtual void close(void) throw (IOException&);
		
	protected:
		/**
		 * The file descriptor.
		 **/
		int fd;
	};
}}

#endif
