home *** CD-ROM | disk | FTP | other *** search
- /* --------------------------------------------------------------------------
- * Copyright 1992 by Forschungszentrum Informatik (FZI)
- *
- * You can use and distribute this software under the terms of the licence
- * you should have received along with this program.
- * If not or if you want additional information, write to
- * Forschungszentrum Informatik, "STONE", Haid-und-Neu-Strasse 10-14,
- * D-7500 Karlsruhe 1, Germany.
- * --------------------------------------------------------------------------
- */
- // **************************************************************************
- // Module smg Juergen Uhl (ju)
- //
- // **************************************************************************
- // implements methods of classes: smg_String
- // **************************************************************************
- #include <string.h>
-
- #include "knl_sos.h"
- #include "trc_smg.h"
-
- // AT&T does not allow initializer lists for non-public members
- // (see smg_empty_string_ptr, below)
- #ifdef ATT
- #define SMG_NEED_PUBLIC_FOR_INIT
- #endif
- #include "smg.h"
-
- /* ------------ Auxiliary Definitions ----------------------------------- */
-
- #define TT_smg_String_enter(name, enter_cmds) \
- T_PROC(name); TT(smg_H, T_ENTER); TT(smg_M, enter_cmds);
-
- #define TT_smg_String_leave(leave_cmds) \
- TT (smg_L, TXT ("smg_String"); TP ((char*)this); \
- TXT ("smg_String_ptr"); TP ((char*)ptr); \
- TXT ("RefCtr"); TI (ptr->rc); \
- TXT ("managed"); TB (ptr->managed); \
- TXT ("string"); TP (ptr->c)); \
- TT (smg_H, T_LEAVE; leave_cmds);
-
- #define TT_nil TXT("")
-
-
- smg_String_ptr smg_empty_string_ptr = { /* managed = */ 0,
- /* rc = */ 1,
- /* c = */ "" };
- // (The initial value of '1' for 'rc' prevents 'delete' calls)
-
-
- /* ------------ Constructors / Assignment ------------------------------- */
-
- smg_String::smg_String (char *c, const smg_access access /* = SMG_BORROW */)
- { TT_smg_String_enter ("smg_String::smg_String (char*, smg_access)",
- TXT ("params"); TS (c); TI (access));
-
- ptr = new smg_String_ptr;
- ptr->rc = 1;
- ptr->managed = (access != SMG_BORROW);
-
- if (access == SMG_COPY)
- { ptr->c = new char [strlen (c) + 1];
- strcpy (ptr->c, c);
- }
- else
- ptr->c = c;
-
- TT_smg_String_leave (TT_nil);
- }
-
- smg_String::smg_String (sos_String s)
- { TT_smg_String_enter ("smg_String::smg_String (sos_String)",
- TXT ("param (ct,of)");
- TI ((int)s.container()); TU ((unsigned)s.offset()));
-
- ptr = new smg_String_ptr;
- ptr->rc = 1;
-
- ptr->managed = 1;
- ptr->c = s.make_Cstring();
-
- TT_smg_String_leave (TT_nil);
- }
-
- smg_String::smg_String (const sos_Int i, const int decimal_mode /* = TRUE */)
- { TT_smg_String_enter ("smg_String::smg_String (sos_Int)",
- TXT ("param"); TI((int)i));
-
- ptr = new smg_String_ptr;
- ptr->rc = 1;
- ptr->managed = 1;
- ptr->c = new char[15]; // 2^32 takes 10 digits + sign + \0 (base 10)
-
- sprintf(ptr->c, (decimal_mode ? "%ld" : "%lx"), (long)i);
-
- TT_smg_String_leave (TT_nil);
- }
-
- smg_String& smg_String::operator= (const smg_String &s)
- { TT_smg_String_enter ("smg_String::op= (smg_String&)",
- TXT ("param"); TP ((char*)&s));
-
- if (--ptr->rc == 0)
- { if (ptr->managed)
- delete ptr->c;
- delete ptr;
- }
- ptr = s.ptr;
- ++ ptr->rc;
-
- TT_smg_String_leave (TT_nil);
- return *this;
- }
-
- smg_String smg_String::clone () const
- { TT_smg_String_enter ("smg_String::clone", TT_nil);
-
- smg_String result (ptr->c, SMG_COPY);
-
- TT_smg_String_leave (TP ((char*)&result));
- return result;
- }
-
- /* ------------ Conversions --------------------------------------------- */
-
- char* smg_String::make_Cstring (const smg_access access /* = SMG_COPY */) const
- { TT_smg_String_enter ("smg_String::make_Cstring",
- TXT ("param"); TI (access));
- char *result;
-
- if (access == SMG_COPY)
- { result = new char [length () + 1];
- strcpy (result, ptr->c);
- }
- else
- { if (access == SMG_TRANSFER)
- ptr->managed = 0;
- result = ptr->c;
- }
-
- TT_smg_String_leave (TP (result));
- return result;
- }
-
- sos_String smg_String::make_String (const sos_Container cnt) const
- { TT_smg_String_enter ("smg_String::make_String",
- TXT ("param"); TI ((int)cnt));
-
- sos_String s = sos_String::create (cnt);
- s.assign_Cstring (this->make_Cstring(SMG_BORROW));
-
- TT_smg_String_leave (TXT ("(ct,of)");
- TI ((int)s.container()); TU ((unsigned)s.offset()));
- return s;
- }
-
- /* ------------ String Manipulation ------------------------------------- */
-
- smg_String& smg_String::operator += (const char *c) const
- { TT_smg_String_enter ("smg_String::operator+=", TXT ("param"); TS (c));
-
- int len = length();
- char *cat = new char [len + strlen (c) + 1];
- strcpy (cat , ptr->c);
- strcpy (cat + len, c);
- if (ptr->managed)
- { TT (smg_H, TXT ("deleting"); TP (ptr->c));
- delete ptr->c;
- }
- ptr->c = cat;
- ptr->managed = 1;
-
- TT_smg_String_leave (TT_nil);
- return (*(smg_String*)this);
- }
-
- smg_String smg_String::operator + (const char *c) const
- { TT_smg_String_enter ("smg_String::operator+", TXT ("param"); TS (c));
-
- smg_String result(ptr->c);
- result += c;
-
- TT_smg_String_leave (TP ((char*)&result));
- return result;
- }
-
- /* ------------ String I/O ---------------------------------------------- */
-
- ostream& operator<< (ostream& o, const smg_String &s)
- { return o << s.ptr->c;
- }
-