home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / sos3-2.lha / src / smg / smg.c next >
Encoding:
C/C++ Source or Header  |  1992-01-23  |  5.3 KB  |  192 lines

  1. /* --------------------------------------------------------------------------
  2.  * Copyright 1992 by Forschungszentrum Informatik (FZI)
  3.  *
  4.  * You can use and distribute this software under the terms of the licence
  5.  * you should have received along with this program.
  6.  * If not or if you want additional information, write to
  7.  * Forschungszentrum Informatik, "STONE", Haid-und-Neu-Strasse 10-14,
  8.  * D-7500 Karlsruhe 1, Germany.
  9.  * --------------------------------------------------------------------------
  10.  */
  11. // **************************************************************************
  12. // Module smg                                                Juergen Uhl (ju)
  13. //
  14. // **************************************************************************
  15. // implements methods of classes: smg_String
  16. // **************************************************************************
  17. #include <string.h>
  18.  
  19. #include "knl_sos.h"
  20. #include "trc_smg.h"
  21.  
  22.     // AT&T does not allow initializer lists for non-public members
  23.     // (see smg_empty_string_ptr, below)
  24. #ifdef ATT
  25. #define SMG_NEED_PUBLIC_FOR_INIT
  26. #endif
  27. #include "smg.h"
  28.  
  29. /* ------------  Auxiliary Definitions  ----------------------------------- */
  30.  
  31. #define TT_smg_String_enter(name, enter_cmds) \
  32.         T_PROC(name); TT(smg_H, T_ENTER); TT(smg_M, enter_cmds);
  33.  
  34. #define TT_smg_String_leave(leave_cmds) \
  35.         TT (smg_L, TXT ("smg_String"); TP ((char*)this); \
  36.                        TXT ("smg_String_ptr"); TP ((char*)ptr);    \
  37.                TXT ("RefCtr");         TI (ptr->rc);    \
  38.                TXT ("managed");        TB (ptr->managed); \
  39.                TXT ("string");         TP (ptr->c));    \
  40.         TT (smg_H, T_LEAVE; leave_cmds);
  41.  
  42. #define TT_nil TXT("")
  43.  
  44.  
  45. smg_String_ptr smg_empty_string_ptr = { /* managed = */ 0,
  46.                     /* rc = */      1, 
  47.                     /* c = */    "" };
  48. // (The initial value of '1' for 'rc' prevents 'delete' calls)
  49.  
  50.  
  51. /* ------------  Constructors / Assignment  ------------------------------- */
  52.                     
  53. smg_String::smg_String (char *c, const smg_access access /* = SMG_BORROW */)
  54. {  TT_smg_String_enter ("smg_String::smg_String (char*, smg_access)",
  55.                 TXT ("params"); TS (c); TI (access));
  56.  
  57.    ptr = new smg_String_ptr;
  58.    ptr->rc      = 1;
  59.    ptr->managed = (access != SMG_BORROW);
  60.  
  61.    if (access == SMG_COPY)
  62.    {  ptr->c = new char [strlen (c) + 1];
  63.       strcpy (ptr->c, c);
  64.    }
  65.    else
  66.       ptr->c = c;
  67.  
  68.    TT_smg_String_leave (TT_nil);
  69. }
  70.  
  71. smg_String::smg_String (sos_String s)
  72. {  TT_smg_String_enter ("smg_String::smg_String (sos_String)",
  73.             TXT ("param (ct,of)");
  74.             TI ((int)s.container()); TU ((unsigned)s.offset()));
  75.  
  76.    ptr = new smg_String_ptr;
  77.    ptr->rc = 1;
  78.  
  79.    ptr->managed = 1;
  80.    ptr->c = s.make_Cstring();
  81.  
  82.    TT_smg_String_leave (TT_nil);
  83. }
  84.  
  85. smg_String::smg_String (const sos_Int i, const int decimal_mode /* = TRUE */)
  86. {  TT_smg_String_enter ("smg_String::smg_String (sos_Int)",
  87.             TXT ("param"); TI((int)i));
  88.    
  89.    ptr = new smg_String_ptr;
  90.    ptr->rc      = 1;
  91.    ptr->managed = 1;
  92.    ptr->c       = new char[15]; // 2^32 takes 10 digits + sign + \0 (base 10)
  93.  
  94.    sprintf(ptr->c, (decimal_mode ? "%ld" : "%lx"), (long)i);
  95.  
  96.    TT_smg_String_leave (TT_nil);
  97. }
  98.  
  99. smg_String& smg_String::operator= (const smg_String &s)
  100. {  TT_smg_String_enter ("smg_String::op= (smg_String&)",
  101.                 TXT ("param"); TP ((char*)&s));
  102.  
  103.    if (--ptr->rc == 0)
  104.    {  if (ptr->managed)
  105.      delete ptr->c;
  106.       delete ptr;
  107.    }
  108.    ptr = s.ptr;
  109.    ++ ptr->rc;
  110.  
  111.    TT_smg_String_leave (TT_nil);
  112.    return *this;
  113. }
  114.  
  115. smg_String smg_String::clone () const
  116. {  TT_smg_String_enter ("smg_String::clone", TT_nil);
  117.  
  118.    smg_String result (ptr->c, SMG_COPY);
  119.  
  120.    TT_smg_String_leave (TP ((char*)&result));
  121.    return result;
  122. }
  123.  
  124. /* ------------  Conversions  --------------------------------------------- */
  125.  
  126. char* smg_String::make_Cstring (const smg_access access /* = SMG_COPY */) const
  127. {  TT_smg_String_enter ("smg_String::make_Cstring",
  128.             TXT ("param"); TI (access));
  129.    char *result;
  130.  
  131.    if (access == SMG_COPY)
  132.    {  result = new char [length () + 1];
  133.       strcpy (result, ptr->c);
  134.    }
  135.    else
  136.    {  if (access == SMG_TRANSFER)
  137.      ptr->managed = 0;
  138.       result = ptr->c;
  139.    }
  140.  
  141.    TT_smg_String_leave (TP (result));
  142.    return result;
  143. }
  144.  
  145. sos_String smg_String::make_String (const sos_Container cnt) const
  146. {  TT_smg_String_enter ("smg_String::make_String",
  147.             TXT ("param"); TI ((int)cnt));
  148.  
  149.    sos_String s = sos_String::create (cnt);
  150.    s.assign_Cstring (this->make_Cstring(SMG_BORROW));
  151.  
  152.    TT_smg_String_leave (TXT ("(ct,of)");
  153.             TI ((int)s.container()); TU ((unsigned)s.offset()));
  154.    return s;
  155. }
  156.  
  157. /* ------------  String Manipulation  ------------------------------------- */
  158.  
  159. smg_String& smg_String::operator += (const char *c) const 
  160. {  TT_smg_String_enter ("smg_String::operator+=", TXT ("param"); TS (c));
  161.    
  162.    int  len  = length();
  163.    char *cat = new char [len + strlen (c) + 1];
  164.    strcpy (cat      , ptr->c);
  165.    strcpy (cat + len, c);
  166.    if (ptr->managed)
  167.    {  TT (smg_H, TXT ("deleting"); TP (ptr->c));
  168.       delete ptr->c;
  169.    }
  170.    ptr->c = cat;
  171.    ptr->managed = 1;
  172.  
  173.    TT_smg_String_leave (TT_nil);
  174.    return (*(smg_String*)this);
  175. }
  176.  
  177. smg_String smg_String::operator + (const char *c) const
  178. {  TT_smg_String_enter ("smg_String::operator+", TXT ("param"); TS (c));
  179.  
  180.    smg_String result(ptr->c);
  181.    result += c;
  182.  
  183.    TT_smg_String_leave (TP ((char*)&result));
  184.    return result;
  185. }
  186.  
  187. /* ------------  String I/O  ---------------------------------------------- */
  188.  
  189. ostream& operator<< (ostream& o, const smg_String &s)
  190. {  return o << s.ptr->c;
  191. }
  192.