home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vos2-121.zip / v / srcos2 / vslist.cpp < prev    next >
C/C++ Source or Header  |  1999-01-20  |  3KB  |  137 lines

  1. //===============================================================
  2. // vSList Class for working with C_List char string lists
  3. //
  4. // Copyright (C) 1995,1996, 1997, 1998  Bruce E. Wampler
  5. //
  6. // This file is part of the V C++ GUI Framework, and is covered
  7. // under the terms of the GNU Library General Public License,
  8. // Version 2. This library has NO WARRANTY. See the source file
  9. // vapp.cxx for more complete information about license terms.
  10. //===============================================================
  11.  
  12. #include <v/vos2.h>           // for OS/2 stuff
  13. #include <v/v_defs.h>
  14. #include <v/vslist.h>
  15. #include <v/vutil.h>
  16.  
  17. //===========================>>> vSList::vSList <<<=======================
  18.   vSList::vSList(int maxsize)
  19.   {
  20.     list = new char*[maxsize];
  21.     max = maxsize;
  22.     init();
  23.   }
  24.  
  25. //===========================>>> vSList::~vSList <<<=======================
  26.   vSList::~vSList()
  27.   {
  28.     erase();            // delete contents
  29.     delete [] list;     // delete the array itself
  30.   }
  31.  
  32. //=============================>>> vSList::= <<<==========================
  33. //  vSList& vSList::operator =(const vSList& slist)
  34. //  {
  35. //    if (this == &slist)
  36. //      return *this;
  37. //  }
  38.  
  39. //=============================>>> vSList::init <<<==========================
  40.   void vSList::init()
  41.   {
  42.     for (int ix = 0 ; ix < max ; ++ix)
  43.     list[ix] = 0;
  44.   }
  45.  
  46. //=============================>>> vsList::erase <<<==========================
  47.   void vSList::erase()
  48.   {
  49.     for (int ix = 0 ; ix < max ; ++ix)
  50.     {
  51.       if (list[ix] != 0)
  52.     delete [] list[ix];
  53.       list[ix] = 0;
  54.     }
  55.   }
  56.  
  57. //========================>>> vSList::size <<<==========================
  58.   int vSList::size() const
  59.   {
  60.     int ix;
  61.     for (ix = 0 ; ix < max && list[ix] != 0 ; ++ix)
  62.       ;
  63.     return ix;
  64.   }
  65.  
  66. //========================>>> vSList::insert <<<==========================
  67.   int vSList::insert(int insAt, const char* strn) const
  68.   {
  69.     int items = size();
  70.     int iWhere = insAt;
  71.  
  72.     if (items >= max)
  73.     return -1;
  74.  
  75.     if (iWhere < 0 || iWhere > max)    // append to end
  76.     iWhere = items;
  77.  
  78.     // First, shift the list down by one
  79.  
  80.     for (int ix = items + 1 ; ix > iWhere ; --ix)
  81.     list[ix] = list[ix-1];
  82.  
  83.     list[iWhere] = new char[strlen(strn)+1];
  84.     strcpy(list[iWhere],strn);
  85.     return items + 1;
  86.   }
  87.  
  88. //========================>>> vSList::replace <<<==========================
  89.   int vSList::replace(int repAt, const char* strn) const
  90.   {
  91.     int items = size();
  92.     int iWhere = repAt;
  93.  
  94.     if (items >= max || iWhere < 0 || iWhere >= items)
  95.     return -1;
  96.  
  97.     if (list[iWhere] == 0)
  98.     return -1;
  99.  
  100.     delete [] list[iWhere];
  101.     list[iWhere] = new char[strlen(strn)+1];
  102.     strcpy(list[iWhere],strn);
  103.     return items;
  104.   }
  105.  
  106. //========================>>> vSList::deleteItem <<<==========================
  107.   int vSList::deleteItem(int delAt) const
  108.   {
  109.     int items = size();
  110.     int iWhere = delAt;
  111.  
  112.     if (iWhere < 0 || iWhere > max)    // delete last item
  113.     iWhere = items - 1;
  114.  
  115.     if (iWhere >= items)
  116.     return -1;
  117.  
  118.     // First, delete the given item
  119.  
  120.     if (list[iWhere] == 0)    // oops!
  121.     return -1;
  122.  
  123.     delete [] list[iWhere];    // free the space!
  124.  
  125.     // Now, shift the list up by one
  126.  
  127.     for (int ix = iWhere ; ix < items ; ++ix)
  128.     list[ix] = list[ix+1];
  129.  
  130.     --items;
  131.     list[items] = 0;
  132.  
  133.     return items;
  134.   }
  135.  
  136.  
  137.