view anagram/support/agstring.h @ 4:bebb2ba69e1d

maybe help with getting tex to fail properly on error
author David A. Holland
date Sat, 18 Apr 2020 17:12:17 -0400
parents 13d2b8934445
children
line wrap: on
line source

/**********************************************************

The AnaGram Class Library

The AgString Class
Copyright 1997 Parsifal Software. All Rights Reserved.
See the file COPYING for license and usage terms.

***********************************************************/

#ifndef AGSTRING_H
#define AGSTRING_H

#include <string.h>
#include "port.h"

#include "agcontainer.h"


class AgString : public AgIndexedContainer<char> {
protected:
  char *store;

  void lock();
  void unlock();
  void allocate(unsigned n);

public:
  class Cut {
    char *store;
    int index;

    void lock();
    void unlock();

  public:
    Cut() : store(0), index(0) {}
    Cut(const AgString &s, const int x);
    ~Cut() { unlock(); }
    int exists() const { return store != 0; }

    char &character() const;

    AgString leftI() const;
    AgString leftX() const;
    AgString rightI() const;
    AgString rightX() const;

    //AgString insertLeft(const AgString &s) const;
    //AgString insertLeft(const char *s) const;
    //AgString insertRight(const AgString &s) const;
    //AgString insertRight(const char *s) const;
    //String replace(const AgString &s) const;
    //String replace(const char *s) const;
  };

  inline AgString() : store(0) {}
  AgString(const unsigned n);

  AgString(const char *s);
  AgString(const char *s, const unsigned n);
  AgString(const AgString &s, const unsigned n);
  AgString(const AgString &s);
  AgString &operator = (const AgString &s);
  AgString &operator = (const char *s);

  char &operator [] (const unsigned x);

  const char &operator [] (const unsigned x) const;
  AgString &discardData();
  ~AgString();

  AgString concat(const char *s) const;
  AgString concat(const AgString s) const;

  inline int exists() const { return store != 0; }
  inline unsigned size() const { return store ? strlen(store) : 0; }

  AgString &toUpper();
  //AgString &toLower();

  inline char *pointer() const {
    return store;
  }
  inline unsigned char *unsignedPointer() const {
    return (unsigned char *) store;
  }

  int operator <  (const AgString &s) const;
  int operator <= (const AgString &s) const;
  int operator >  (const AgString &s) const;
  int operator >= (const AgString &s) const;
  int operator == (const AgString &s) const;
  int operator != (const AgString &s) const;
  int iEq(const AgString &s) const;

  int operator <  (const char *s) const;
  int operator <= (const char *s) const;
  int operator >  (const char *s) const;
  int operator >= (const char *s) const;
  int operator == (const char *s) const;
  int operator != (const char *s) const;
  int iEq(const char *s) const;

  static AgString format(const char *fs, ...) PRINTFFY(1,2);

  Cut firstCut(const char c) const;
  Cut lastCut(const char c) const;
  Cut firstCut(const char* s) const;
  Cut lastCut(const char* s) const;
};

inline AgString::AgString(const AgString &s)
  : AgIndexedContainer<char>()
{
  store = s.store;
  lock();
}

inline AgString::~AgString() {
  unlock();
}

inline AgString &AgString::discardData() {
  unlock();
  store = 0;
  return *this;
}

inline AgString &AgString::operator = (const char *s) {
  return *this = AgString(s);
}

#endif /* AGSTRING_H */