/*
 * Utility functions for the basis library.
 *
 * Copyright (c) 1997 David L Parsons 
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice and this paragraph are
 * duplicated in all such forms and that any documentation,
 * advertising materials, and other materials related to such
 * distribution and use acknowledge that the software was developed
 * by David L Parsons (orc@pell.chi.il.us).  My name may not be used
 * to endorse or promote products derived from this software without
 * specific prior written permission.  THIS SOFTWARE IS PROVIDED
 * ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
 * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.
 */
#ifndef _UTIL_D
#define _UTIL_D

#include <stdio.h>

extern "C" {
#include <malloc.h>
#include <netdb.h>

extern char *haddrtostring(struct hostent *p, int which);
extern int Runlevel();
extern void make_header(FILE*, char*, ...);

extern int backup(char*);

#define MULTIUSER	'm'
}

/*
 * a heap to store interesting things in
 */
class Heap {
private:
    char *bfr;
    int used, alloc;
public:
    Heap();
    Heap(char *string);
    ~Heap();

    void add(char *string);
    void add(char *data, int size);

    inline char* operator () (void)	{ return bfr; }
    inline int size()			{ return used; }
} ;

/*
 * buffered input
 */
class Buffer {
private:
    char *bfr;
    int bfrlen;
    int curr, size;
    int fd;
    int _bytes_read;
    int _bytes_written;
    int _timeout;
public:
    Buffer(int filedesc, int timeout=0);
    inline ~Buffer()	{ free(bfr); }
    unsigned int readchar();
    void readline(char *line, int len);
    int read(char *data, int len);
    int write(char *data, int len);
    inline int bytes_read()	{ return _bytes_read; }
    inline int bytes_written()	{ return _bytes_written; }
    inline int buffered() 	{ return size-curr; }
    void alarm(int to);
    void pushback(char *data, int size);
    int flush(Buffer &to);
    int flush(int to);
} ;

#endif/*_UTIL_D*/
