From 3a4838bed13b767132cbdf06364b2658da6cc356 Mon Sep 17 00:00:00 2001 From: chatty Date: Tue, 15 Dec 1992 10:55:33 +0000 Subject: Initial revision --- utils/String.cc | 240 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 240 insertions(+) create mode 100644 utils/String.cc (limited to 'utils/String.cc') diff --git a/utils/String.cc b/utils/String.cc new file mode 100644 index 0000000..3da5d7d --- /dev/null +++ b/utils/String.cc @@ -0,0 +1,240 @@ +/* + * CENA C++ Utilities + * + * by Stephane Chatty + * + * Copyright 1992 + * Centre d'Etudes de la Navigation Aerienne (CENA) + * + * CcuString allocation and copying + * + * $Id$ + * $CurLog$ + */ + +#include "String.h" +#include +#include + +/*? +\label{newstring} +Allocate a string the same size as \var{s}, +copy \var{s} in it, and return the address. \var{s} must be a null-terminated string. +If \var{s} is 0, return 0. +?*/ +char* +NewString (const char* s) +{ + char* p; + + if (! s) + return 0; + + p = new char [strlen (s) + 1]; + strcpy (p, s); + return p; +} + + +/*? +Allocate a string of \var{n} characters, +copy the \var{n} first characters of \var{s} in it, +and return the address. The returned string is null terminated. +If \var{s} is 0, return 0. +?*/ +char* +NewString (const char* s, int n) +{ + char* p; + + if (! s) + return 0; + p = NewString (n); + if (! p) + return 0; + strncpy (p, s, n); + p [n] = 0; + return p; +} + +/*? +Allocate a string of \var{n} characters, +and return its address. Actually, \var{n+1} characters are allocated, +to keep space for the terminating null character. +If \var{n} is negative, return 0. Note that \fun{NewString}~\var{0} will allocate +one character that will be able to hold an empty string. +?*/ +char* +NewString (int n) +{ + if (n < 0) + return 0; + + return new char [n+1]; +} + + +/*? +Free a string which has previously been +allocated with \fun{NewString}. +If \var{s} is 0, do nothing. +Please be careful: it is impossible to test whether \var{s} was really allocated with \fun{NewString}. +?*/ +void +FreeString (char* s) +{ + if (s) +#ifdef CPLUS_BUG4 + delete [strlen (s) + 1] s; +#else + delete [] s; +#endif +} + +/*?class CcuString +?*/ + +#ifdef DOC +/*? +Build an empty \typ{CcuString}. +?*/ +CcuString :: CcuString () +{ +} +#endif + +/*? +Build a \typ{CcuString} from \var{s}. \var{s} can be null. +?*/ +CcuString :: CcuString (const char* s) +{ + Str = NewString (s); +} + +/*? +Create a \typ{CcuString} suitable for storing at most \var{n} characters, and +initialize it to the null string. +?*/ +CcuString :: CcuString (int n) +{ + Str = NewString (n); + *Str = '\0'; +} + +/*? +Create a new \typ{CcuString} by copying \var{s}. +?*/ +CcuString :: CcuString (const CcuString& s) +{ + Str = NewString (s); +} + +/*? +Create a new \typ{CcuString} by copying \var{s}. +?*/ +CcuString :: CcuString (const CcuShadowString& s) +{ + Str = NewString (s); +} + +/*?nodoc?*/ +CcuString :: ~CcuString () +{ + if (Str) + FreeString (Str); +} + + +/*? +Copy \var{s} into this string. The old string is freed. +?*/ +CcuString& +CcuString :: operator = (const CcuString& s) +{ + if (this != &s) { + FreeString (Str); + Str = NewString (s); + } + return *this; +} + +CcuString& +CcuString :: operator = (const char* s) +{ + if (Str != s) { + FreeString (Str); + Str = NewString (s); + } + return *this; +} + +CcuString& +CcuString :: operator = (const CcuShadowString& s) +{ + if (Str != (const char*) (s)) { + FreeString (Str); + Str = NewString (s); + } + return *this; +} + +#ifdef DOC +/*? +Return the value of a string. +?*/ +CcuString :: operator const char* () const +{ +} +#endif + +int +CcuString :: Length () const +{ + return strlen (Str); +} + +#ifdef DOC +/*? +Build an empty \typ{CcuShadowString}. +?*/ +CcuShadowString :: CcuShadowString () +{ + Str = 0; +} +#endif + +/*? +?*/ +CcuShadowString :: CcuShadowString (const char* s) +{ + Str = s; +} + +/*? +Create a new \typ{CcuShadowString} by referencing \var{s}. +?*/ +CcuShadowString :: CcuShadowString (const CcuString& s) +{ + Str = (const char*) s; +} + +#ifdef DOC +CcuShadowString& +CcuShadowString :: operator = (const CcuString& s) +{ +} + +CcuShadowString& +CcuShadowString :: operator = (const char* s) +{ +} + +/*? +Return the value of a string. +?*/ +CcuShadowString :: operator const char* () const +{ +} +#endif + + -- cgit v1.1