home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / BK-SC1_4.DMS / in.adf / MUIClass.Lha / Include / Classes / TWiMUI / Misc.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-10  |  7.5 KB  |  262 lines

  1. #ifndef CPP_TWIMUI_MISC_H
  2. #define CPP_TWIMUI_MISC_H
  3.  
  4. #ifndef EXEC_MEMORY_H
  5. #include <exec/memory.h>
  6. #endif
  7.  
  8. #ifndef EXEC_TYPES_H
  9. #include <exec/types.h>
  10. #endif
  11.  
  12. #ifndef _INCLUDE_STRING_H
  13. #include <string.h>
  14. #endif
  15.  
  16. #ifndef UTILITY_TAGITEM_H
  17. #include <utility/tagitem.h>
  18. #endif
  19.  
  20. class TWiMemX
  21.     {
  22.     private:
  23.         ULONG WantedSize;
  24.         ULONG WantedFlags;
  25.     public:
  26.         TWiMemX(ULONG s, ULONG f = MEMF_ANY) : WantedSize(s), WantedFlags(f) { };
  27.         ULONG size() const { return(WantedSize); };
  28.         ULONG flags() const { return(WantedFlags); };
  29.     };
  30.  
  31. class TWiBuff
  32.     {
  33.     private:
  34.         APTR databuff;
  35.         ULONG buffsize;
  36.         BOOL privbuff;
  37.     public:
  38.         TWiBuff(const ULONG initsize = 256);
  39.         TWiBuff(const APTR, const ULONG);
  40.         TWiBuff(const TWiBuff &);
  41.         ~TWiBuff();
  42.         TWiBuff &operator= (const TWiBuff &);
  43.         APTR buffer() const { return(databuff); };
  44.         ULONG size() const { return(databuff ? buffsize : 0); };
  45.         void doubleBuff();
  46.         void setBuffSize(ULONG);
  47.     };
  48.  
  49. class TWiHelpArray
  50.     {
  51.     private:
  52.         void extend(const ULONG);
  53.         UBYTE *v;
  54.         ULONG element_size;
  55.         ULONG size;
  56.     public:
  57.         TWiHelpArray(ULONG es, ULONG s = 16);
  58.         TWiHelpArray(const TWiHelpArray &);
  59.         TWiHelpArray &operator= (const TWiHelpArray &);
  60.         ~TWiHelpArray() { delete [] v; };
  61.         operator APTR () const { return((APTR) v); };
  62.         void &operator[] (const ULONG);
  63.         ULONG esize() const { return(element_size); };
  64.         ULONG count() const { return(size); };
  65.     };
  66.  
  67. template <class T> class array : private TWiHelpArray
  68.     {
  69.     public:
  70.         array(ULONG s = 16) : TWiHelpArray(sizeof(T),s) { };
  71.         ULONG count() const { return(TWiHelpArray::count()); };
  72.         T &operator[] (ULONG i) { return((T &) TWiHelpArray::operator[] (i)); };
  73.     };
  74.  
  75. class TWiHelpArrayList : public TWiHelpArray
  76.     {
  77.     private:
  78.         ULONG top;
  79.     public:
  80.         TWiHelpArrayList(ULONG es, ULONG s = 16) : TWiHelpArray(es,s) { top = 0; };
  81.         ULONG length() const { return(top); };
  82.         void &addTail() { return(TWiHelpArray::operator[](top++)); };
  83.         void &insert(const ULONG index);
  84.         void remove(const ULONG index);
  85.         void remTail();
  86.         void clear() { top = 0; };
  87.     };
  88.  
  89. class TWiHelpArrayCursor
  90.     {
  91.     private:
  92.         TWiHelpArrayList *list;
  93.         LONG pos;
  94.     public:
  95.         TWiHelpArrayCursor(TWiHelpArrayList &);
  96.         void first() { pos = 0; };
  97.         void last() { pos = list->length() - 1; };
  98.         void next();
  99.         void prev();
  100.         void &item() { return(list->operator[](pos)); };
  101.         BOOL isDone() { return(pos >= list->length()  ||  pos < 0); };
  102.     };
  103.  
  104. template <class T> class TWiArrayList : private TWiHelpArrayList
  105.     {
  106.     friend class TWiArrayCursor<T>;
  107.     public:
  108.         TWiArrayList(ULONG s = 16) : TWiHelpArrayList(sizeof(T),s) { };
  109.         ULONG count() { return(TWiHelpArrayList::count()); };
  110.         ULONG length() const { return(TWiHelpArrayList::length()); };
  111.         T &operator[] (ULONG i) { return((T &) TWiHelpArray::operator[] (i)); };
  112.         T &addTail() { return((T &) TWiHelpArrayList::addTail()); };
  113.         T &insert(const ULONG index) { return((T &) TWiHelpArrayList::insert(index)); };
  114.         void remove(const ULONG index) { TWiHelpArrayList::remove(index); }
  115.         void remTail() { TWiHelpArrayList::remTail(); };
  116.         void clear() { TWiHelpArrayList::clear(); };
  117.     };
  118.  
  119. template <class T> class TWiArrayCursor : private TWiHelpArrayCursor
  120.     {
  121.     public:
  122.         TWiArrayCursor(TWiArrayList<T> &l) : TWiHelpArrayCursor((TWiHelpArrayList &)l) { };
  123.         void first() { TWiHelpArrayCursor::first(); };
  124.         void last() { TWiHelpArrayCursor::last(); };
  125.         void next() { TWiHelpArrayCursor::next(); };
  126.         void prev() { TWiHelpArrayCursor::prev(); };
  127.         T &item() { return((T &) TWiHelpArrayCursor::item()); };
  128.         BOOL isDone() const { return(TWiHelpArrayCursor::isDone()); };
  129.     };
  130.  
  131. class TWiStr
  132.     {
  133.     protected:
  134.         ULONG len;
  135.         TWiBuff buffer;
  136.     public:
  137.         TWiStr(const STRPTR = NULL);
  138.         TWiStr(const ULONG, const STRPTR);
  139.         TWiStr(const TWiStr &s) : len(s.len), buffer(s.buffer) { };
  140.         TWiStr(const UBYTE);
  141.         virtual ~TWiStr();
  142.         operator STRPTR() const { return((STRPTR)buffer.buffer()); };
  143.         TWiStr &operator= (const TWiStr &);
  144.         TWiStr &operator= (const STRPTR);
  145.         TWiStr &operator+= (const TWiStr &);
  146.         TWiStr &operator+= (const STRPTR);
  147.         UBYTE &operator[] (ULONG);
  148.         ULONG length() const { return(len); };
  149.         ULONG bufsize() const { return(buffer.size()); };
  150.         TWiStr left(const ULONG) const;
  151.         TWiStr right(const ULONG) const;
  152.         TWiStr mid(const ULONG, const ULONG) const;
  153.         void doubleBuff() { buffer.doubleBuff(); };
  154.         void shrinkBuff();
  155.         void setBuffSize(const ULONG);
  156.     };
  157.  
  158. TWiStr operator+ (const TWiStr &, const TWiStr &);
  159.  
  160. BOOL operator== (const TWiStr &s1, const TWiStr &s2) { return(strcmp((STRPTR)s1,(STRPTR)s2) == 0); };
  161. BOOL operator!= (const TWiStr &s1, const TWiStr &s2) { return(strcmp((STRPTR)s1,(STRPTR)s2) != 0); };
  162. BOOL operator<  (const TWiStr &s1, const TWiStr &s2) { return(strcmp((STRPTR)s1,(STRPTR)s2) <  0); };
  163. BOOL operator>  (const TWiStr &s1, const TWiStr &s2) { return(strcmp((STRPTR)s1,(STRPTR)s2) >  0); };
  164. BOOL operator<= (const TWiStr &s1, const TWiStr &s2) { return(strcmp((STRPTR)s1,(STRPTR)s2) <= 0); };
  165. BOOL operator>= (const TWiStr &s1, const TWiStr &s2) { return(strcmp((STRPTR)s1,(STRPTR)s2) >= 0); };
  166.  
  167. class ostream &operator<< (ostream &, const TWiStr &);
  168.  
  169. class istream &operator>> (istream &, TWiStr &);
  170.  
  171. class TWiStrArray
  172.     {
  173.     friend class TWiStrCursor;
  174.     private:
  175.         TWiHelpArrayList strs;
  176.     public:
  177.         TWiStrArray(STRPTR string1, ...);
  178.         TWiStrArray(STRPTR *strings = NULL);
  179.         ULONG length() const { return(strs.length()); };
  180.         STRPTR *strings();
  181.         STRPTR &operator[] (const ULONG);
  182.         void addTail(const STRPTR);
  183.         void insert(const STRPTR, const ULONG);
  184.         void remTail();
  185.         void remove(const ULONG);
  186.     };
  187.  
  188. class TWiStrCursor : public TWiHelpArrayCursor
  189.     {
  190.     public:
  191.         TWiStrCursor(TWiStrArray &);
  192.         STRPTR item();
  193.     };
  194.  
  195. class TWiFormat
  196.     {
  197.     private:
  198.         TWiBuff Buff;
  199.         TWiStr Fmt;
  200.         ULONG Index;
  201.         void TWiPutChar(UBYTE, TWiFormat *);
  202.         static void put_char(register __d0 const UBYTE, register __a3 TWiFormat *);
  203.  
  204.     public:
  205.         TWiFormat(const STRPTR pFmt = NULL) : Fmt(pFmt), Buff(0UL), Index(0UL) { };
  206.         TWiFormat(const STRPTR pStr, const ULONG pLng) : Fmt(pStr), Buff(pLng), Index(0UL) { };
  207.         TWiFormat(const ULONG pLng) : Fmt(), Buff(pLng), Index(0UL) { };
  208.         TWiFormat(const TWiFormat &pFmt) : Fmt(pFmt.Fmt), Buff(pFmt.Buff), Index(pFmt.Index) { };
  209.         ~TWiFormat() { };
  210.         void setFormat(const STRPTR pFmt) { Fmt = pFmt; };
  211.         void setBuffer(const ULONG lBuff) { Buff.setBuffSize(lBuff); };
  212.         STRPTR format(const ULONG, ...);
  213.         STRPTR format(const APTR);
  214.         STRPTR getFormat() const { return(Fmt); };
  215.         STRPTR getBuff() const { return((STRPTR)Buff.buffer()); };
  216.     };
  217.  
  218. class TWiTag
  219.     {
  220.     private:
  221.         struct TagItem *taglist;
  222.         void freetaglist();
  223.         struct TagItem *findtagend();
  224.     public:
  225.         TWiTag() : taglist(NULL) { };
  226.         TWiTag(const Tag tag1Type, ...);
  227.         TWiTag(const struct TagItem *);
  228.         TWiTag(const TWiTag &);
  229.         TWiTag &operator = (const TWiTag &);
  230.         ~TWiTag() { freetaglist(); };
  231.         struct TagItem *tags() const { return taglist };
  232.         void append(const TWiTag *);
  233.         void append(const Tag, ...);
  234.         void append(const struct TagItem *);
  235.         void set(const TWiTag *tags);
  236.         void set(const Tag tag1Type, ...);
  237.         void set(const struct TagItem *tags);
  238.         struct TagItem *find(const Tag tagType) const;
  239.         ULONG getData(const Tag tagType, const ULONG defaultData) const;
  240.         ULONG filter(const LONG logic, const Tag tagTypes[]);
  241.         ULONG filter(const LONG logic, const Tag tag1, ... );
  242.     };
  243.  
  244. class TWiTagCursor
  245.     {
  246.     private:
  247.         struct TagItem *taglist;
  248.         struct TagItem *cursor;
  249.         struct TagItem *pos;
  250.     public:
  251.         TWiTagCursor(const TWiTag &);
  252.         TWiTagCursor(struct TagItem *);
  253.         BOOL isDone() const { return(pos == NULL); };
  254.         void first();
  255.         void next();
  256.         struct TagItem *item() const { return(pos); };
  257.         Tag itemTag() const;
  258.         ULONG itemData() const;
  259.     };
  260.  
  261. #endif
  262.