/****************************************************************************
 * This file was derived from the CNCL library by the Pablo research group.
 *
 * The complete CNCL library is available at:
 *       http://www.comnets.rwth-aachen.de/doc/cncl.html
 *
 * The CNCL copyright notice is included here:
 *
 * |_|_|_  |_|_    |_    |_|_|_  |_               C O M M U N I C A T I O N
 *|_       |_  |_  |_  |_        |_                         N E T W O R K S
 *|_       |_  |_  |_  |_        |_                               C L A S S
 * |_|_|_  |_    |_|_    |_|_|_  |_|_|_|_                     L I B R A R Y
 *
 *
 *
 * Copyright (C) 1992-1998   Communication Networks
 *                           Aachen University of Technology
 *                           D-52056 Aachen
 *                           Germany
 *                           Email: cncl-adm@comnets.rwth-aachen.de
 *
 *
 * This library is free  software; you can  redistribute it and/or modify
 * it  under   the terms  of the GNU   Library  General Public License as
 * published by the  Free Software Foundation;  either  version 2 of  the
 * License, or  (at  your option)  any  later  version.  This  library 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 Library General Public
 * Library General Public License along with  this library; if not, write
 * to the Free Software Foundation,  675  Mass Ave, Cambridge, MA  02139,
 * USA.
 ****************************************************************************/

//   -*- C++ -*-
/*****************************************************************************
 *
 * FlClass: FlString --- Character string
 *
 *****************************************************************************/

#ifndef __String_h
#define __String_h


#include <FlObject.h>		// Base class

#include <string.h>
#include <sys/types.h>


/*
 * The class FlString
 */

class FlString : public FlObject
{
    enum { default_extra = 10 };                // extra storage allocated  

  public:	/***** Constructors ******************************************/
    FlString()          { init(); }	// Default constructor

    FlString(int extra);
    FlString(char);
    FlString(char, int);
    FlString(const char*);
    FlString(const char*, int);
    FlString(const FlString&);
    FlString(const FlString&, int);
    ~FlString();

  public:	/***** Public interface **************************************/
    void resize( unsigned );
    void to_lower();
    void to_upper();
    void capitalize();

    void strip_crlf();
    void strip_lspace();
    void strip_rspace();
    void strip_space();
    
    unsigned capacity() const   { return alloc; }
    unsigned length() const     { return len; }

    FlString  after  (int pos, int l=0) const; 
    FlString  before (int pos, int l=0) const;
    FlString& add    (char c);
    FlString& add    (const char *cs);

    FlString& del    (int pos=0, int l=1);
    FlString& del    (char c,            int pos=0);
    FlString& del    (const FlString& s, int pos=0);
    FlString& del    (const char     *s, int pos=0);

    FlString& insert (char c,            int pos=0);
    FlString& insert (const char     *s, int pos=0);
    FlString& insert (const FlString& s, int pos=0); 

    FlString& replace(char c, int pos=0);
    FlString& replace(char oldc, char newc, int pos=0); 
    FlString& replace(const FlString& s, int pos=0, int l=0);
    FlString& replace(const FlString& olds, const FlString& news, int pos=0);
    
    int downsearch(char c,            int pos=0) const;
    int downsearch(const FlString& s, int pos=0) const;
    int downsearch(const char     *s, int pos=0) const;
    int upsearch  (char c,            int pos=0) const;
    int upsearch  (const FlString& s, int pos=0) const;
    int upsearch  (const char     *s, int pos=0) const;
    
    Fl_bool matches  (const char     *s, int pos=0) const;
    Fl_bool matches  (const FlString& s, int pos=0) const;

    
    void icopy( std::istream& strm = std::cin );

    /***** Operators *********************************************************/
    operator const char * () const { return p; }

    char  operator()(int i) const;
    char& operator[](int i);

    void operator=(const FlString&);
    void operator=(const char*);
    void operator=(char);
    
    friend Fl_bool operator <  (const FlString& a, const FlString& b);
    friend Fl_bool operator >  (const FlString& a, const FlString& b);
    friend Fl_bool operator >= (const FlString& a, const FlString& b);
    friend Fl_bool operator <= (const FlString& a, const FlString& b);
    friend Fl_bool operator == (const FlString& a, const FlString& b);
    friend Fl_bool operator != (const FlString& a, const FlString& b);

    friend FlString operator + (const FlString&, const FlString&);

    FlString& operator += (const FlString&);
    FlString& operator += (const char*);


  private:	/***** Internal private members ******************************/
    size_t alloc;    // storage allocated
    				   
  protected:	/***** Internal protected members ****************************/
    char* p;           // character string
    size_t len;      // length of the string, NULL character not counted
    void init(unsigned l=0, unsigned extra=default_extra);
    void cpyn(const char *s, unsigned n);
    
  public:	/***** Member functions required by FuzzyLib *********************/
    
    // Print/debug output
    virtual void print(ostream &strm = cout) const;
    virtual void dump (ostream &strm = cout) const;
};

std::istream &operator >> (std::istream& strm, FlString& s);



/*
 * const reference to a FlString typedef
 */
typedef const FlString & FlStringR;
    

#endif /**__String_h**/
