/*
  This file is part of CardWords.
  (C) 1999 Tobias Peters

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// charpointer.hh

// The class CharPointer is a converter class to and from
// char * and unsigned char *.
// The purpose of this class is to avoid lots of casts, as I want
// to use unsigned chars, otherwise the upper latin1-chars would be
// negative, but the system string.h-functions take pointers to signed
// chars.
// I did already use a lot of casts, but changing them to use these con-
// verter classes is on my fixme list.
// It is safer to use these converter classes, because explicit casts each
// time can too easily loose a const attribute.
// constCharPointer is a pointer to const chars.

#ifndef CHARPOINTER_HH
#define CHARPOINTER_HH

#include "cardwords_machinechar.hh" // My typename for unsigned chars
#include <iostream.h>

class Machine_String;

class CharPointer {
private:
  Machine_Char * pointer;
public:
  CharPointer (char * cp = 0) {pointer = (Machine_Char *)cp;}
  CharPointer (Machine_Char * mp) {pointer = mp;}
  operator char * () const {return (char *)pointer;}
  operator Machine_Char * () const {return pointer;}
  operator void * () const {return (void *) pointer;}

  // test for the 0 pointer:
  bool operator == (const char * c) const
    { return ((const Machine_Char *)c) == pointer; }
  bool operator == (const Machine_Char * c) const
    { return c == pointer;}
  bool operator == (int i) const
    { return ((i == 0) && (pointer == 0)); }

  bool operator != (const char * c) const
    { return ((const Machine_Char *)c) != pointer; }
  bool operator != (const Machine_Char * c) const
    { return c != pointer;}
  bool operator != (int i) const
    { return ((i == 0) && (pointer != 0)); }

  Machine_Char &
  operator*() const {return *pointer;}
  Machine_Char &
  operator[](int index) const {return pointer[index];}

  CharPointer &
  operator ++() {++pointer; return *this;}
  CharPointer &
  operator --() {--pointer; return *this;}
};

class constCharPointer {
private:
  const Machine_Char * pointer;
public:
  constCharPointer (const char * cp = 0) {pointer = (const Machine_Char *)cp;}
  constCharPointer (CharPointer cp) {pointer = cp;}
  constCharPointer (const Machine_Char * mp) {pointer = mp;}
  constCharPointer (const Machine_String & ms);
  operator const char * () const {return (const char *)pointer;}
  operator const Machine_Char * () const {return pointer;}
  operator void * () const {return (void *) pointer;}
  // test for the 0 pointer:
  bool operator == (const char * c) const
    { return ((const Machine_Char *)c) == pointer; }
  bool operator == (const Machine_Char * c) const
    { return c == pointer;}
  bool operator == (int i) const
    { return ((i == 0) && (pointer == 0)); }

  bool operator != (const char * c) const
    { return ((const Machine_Char *)c) != pointer; }
  bool operator != (const Machine_Char * c) const
    { return c != pointer;}
  bool operator != (int i) const
    { return ((i == 0) && (pointer != 0)); }

  const Machine_Char &
  operator*() const {return *pointer;}
  const Machine_Char &
  operator[](int index) const {return pointer[index];}

  constCharPointer &
  operator ++() {++pointer; return *this;}
  constCharPointer &
  operator --() {--pointer; return *this;}
};
inline ostream & operator << (ostream & ostr, constCharPointer ccp)
{ return ostr << (const char *)ccp;}
#endif
