home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / GENMAN.ZIP / STRLOC.CXX < prev    next >
C/C++ Source or Header  |  1993-03-13  |  4KB  |  179 lines

  1. //------------------------------------------------------------
  2. //
  3. //            strloc.cxx
  4. //
  5. // Purpose:
  6. //    Implements the strloc class
  7.  
  8. #include <string.h>
  9. #include <util/list.h>
  10. #include <util/strloc.h>
  11.  
  12. //------------------------------------------------------------
  13. //
  14. //            strloc
  15. //
  16. // Description:
  17. //    Normal constructor.
  18. // Arguments:
  19. //    Passed a pointer to a character string it copies it
  20. //    to new memory and sets the reference count to 1.
  21.  
  22. strloc::strloc( const char *newstr )
  23.     {
  24.     // get a structure to hold the string pointer and ref count
  25.     st_com= new strloc_data;
  26.     st_com->std_refcnt= 1;
  27.  
  28.     if( newstr == 0 )
  29.         newstr= "";
  30.  
  31.     // get the length of the string
  32.     int len= strlen( newstr ) +1;
  33.  
  34.     // get some space for the string
  35.     st_com->std_ptr= new char[len];
  36.  
  37.     // copy the data
  38.     strcpy( st_com->std_ptr, newstr );
  39.     }
  40.  
  41. //------------------------------------------------------------
  42. //
  43. //            strloc
  44. //
  45. // Description:
  46. //    Constructor called when strloc a= b.
  47. // Arguments:
  48. //    "this" is "a" and "orig" is a pointer to "b".
  49.  
  50. strloc::strloc( strloc &orig )
  51.     {
  52.     // have "a" point to the same string structure as "b"
  53.     st_com= orig.st_com;
  54.  
  55.     // update the reference count
  56.     st_com->std_refcnt += 1;
  57.     }
  58.  
  59. //------------------------------------------------------------
  60. //
  61. //            ~strloc
  62. //
  63. // Description:
  64. //    Destructor for the strloc class.
  65. //    The refernce count is decrimented and when it goes to
  66. //    zero the string is deleted.
  67.  
  68. strloc::~strloc()
  69.     {
  70.     // decriment the reference count
  71.     st_com->std_refcnt -= 1;
  72.  
  73.     // see if nothing else pointing to the string
  74.     if( st_com->std_refcnt == 0 )
  75.         {
  76.         delete st_com->std_ptr;    // delete the string
  77.         delete st_com;             // delete the string structure
  78.         }
  79.     }
  80.  
  81. //------------------------------------------------------------
  82. //
  83. //            operator=
  84. //
  85. // Description:
  86. //    Implements copy function.
  87. //    Frees up old string and copies a pointer to the new one.
  88. // Arguments:
  89.  
  90. strloc & strloc::operator=( strloc &rvalue )
  91.     {
  92.     // incriment the source string reference count
  93.     rvalue.st_com->std_refcnt += 1;
  94.  
  95.     // decriment the orig reference count
  96.     st_com->std_refcnt -= 1;
  97.  
  98.     // see if nothing else pointing to the string
  99.     if( st_com->std_refcnt == 0 )
  100.         {
  101.         delete st_com->std_ptr;    // delete the string
  102.         delete st_com;             // delete the string structure
  103.         }
  104.  
  105.     // point this to the rvalue string structure
  106.     st_com= rvalue.st_com;
  107.  
  108.     return *this;
  109.     }
  110.  
  111. //------------------------------------------------------------
  112. //
  113. //            operator=
  114. //
  115. // Description:
  116. //    Implements set function: strloc a; a= "foo";
  117. //    Frees up old string and copies a pointer to the new one.
  118.  
  119. strloc & strloc::operator=( const char *newstr )
  120.     {
  121.     // decriment the orig reference count
  122.     st_com->std_refcnt -= 1;
  123.  
  124.     // see if nothing else pointing to the string
  125.     if( st_com->std_refcnt == 0 )
  126.         {
  127.         delete st_com->std_ptr;    // delete the string
  128.         delete st_com;             // delete the string structure
  129.         }
  130.  
  131.     // get a structure to hold the string pointer and ref count
  132.     st_com= new strloc_data;
  133.     st_com->std_refcnt= 1;
  134.  
  135.     if( newstr == 0 )
  136.         newstr= "";
  137.  
  138.     // get the length of the string
  139.     int len= strlen( newstr ) +1;
  140.  
  141.     // get some space for the string
  142.     st_com->std_ptr= new char[len];
  143.  
  144.     // copy the data
  145.     strcpy( st_com->std_ptr, newstr );
  146.  
  147.     return *this;
  148.     }
  149.  
  150. //-----------------------------------------------------------
  151. //
  152. //                    operator+=
  153. //
  154. // Description:
  155. //  Appends the specified string onto the end of the current string.
  156.  
  157. void strloc::operator+=( const char *string )
  158.     {
  159.     // just return if no string specified
  160.     if( (string == NULL) || (*string == '\0') )
  161.         return;
  162.  
  163.     // determine the length of the new string
  164.     int len= strlen( st_com->std_ptr ) + strlen( string ) +1;
  165.  
  166.     // get some space for the string
  167.     char *ptr= new char[len];
  168.  
  169.     // copy the data
  170.     strcpy( ptr, st_com->std_ptr );
  171.     strcat( ptr, string );
  172.  
  173.     // delete the original data
  174.     delete st_com->std_ptr;
  175.  
  176.     // save the pointer to the new data
  177.     st_com->std_ptr= ptr;
  178.     }
  179.