//=-- MSFilename.h - Class for managing filenames ----------------------------=
//
//  This class is a superclass of the String class.  It provides all of the 
//    functionality that the String class does, but automatically dereferences
//    ~'s for users when it is converted to a const char *.
//
//=---------------------------------------------------------------------------=
//  This file is copyright (c) 1997-1999 Chris Lattner
//=---------------------------------------------------------------------------=

#ifndef MSFILENAME_H
#define MSFILENAME_H

#include "MSString.h"
class Date;

// Immutable object...
class MSAPI MSFilename : public Object {
public :
  MSFilename()                    : DecodedFilename("") {}
  MSFilename(const char *CP)      : DecodedFilename(DecodeFilename(CP)) {}
  MSFilename(const String &S)     : DecodedFilename(DecodeFilename(S )) {}
  MSFilename(const MSFilename &F) : Object(F), DecodedFilename(F.DecodedFilename ) {}

  virtual int Compare(const Object &L) const {
    return DecodedFilename.Compare(((MSFilename&)L).DecodedFilename);
  }

  virtual ostream &print(ostream &s) const {
    return s << DecodedFilename;
  }

  // Convert to char* - Return the filename as a char*...
  //
  inline operator const char *() const { 
    return DecodedFilename;
  }

  // Convert to String - Return the filename as a String &
  //
  inline operator const String &() const {
    return DecodedFilename;
  }

  // DecodeFilename - Decode filename from a possible tilde representation.
  // For example, convert ~sabre to /home/sabre or ~/.MagicStats to
  // /home/sabre/.MagicStats/ etc.
  //
  static String DecodeFilename(const char *FN);

  // EvaluateTokens - Because the output filename may take to form 
  // "OutputManager%d.html", we must now replace all occurances of %d with the 
  // current numeric date.
  //
  MSFilename EvaluateTokens(const Date &D) const;

  // FilenamePortion - Return the part of this filename that corresponds to the
  // filename of the file, without the directory prepended to it.
  //
  String FilenamePortion() const;

  // DirectoryPortion - Returns the part of this filename that corresponds to
  // the directory that the file resides in.
  //
  String DirectoryPortion() const;

  // Exists - Returns true if the file or directory corresponding to this name 
  // exists...
  //
  int Exists() const;

  // Valid - Returns true if the filename is valid.  Currently, this returns 
  // false if the filename has a zero length.
  //
  int Valid() const;

private:
  // PROTECT this, because this is an immutable object.  This method is never
  // implemented anywhere, but should not be able to be called anyways... it's 
  // private!
  MSFilename &operator=(const MSFilename &);

  // The filename, decoded, that this object refers to.
  String DecodedFilename;
};

#endif
