/* * CENA C++ Utilities * * by Stephane Chatty * * Copyright 1992, 1993 * Centre d'Etudes de la Navigation Aerienne (CENA) * * String 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. If it is shorter than \var{n} characters, the new string is padded with null characters. ?*/ 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 IvlString ?*/ #ifdef DOC /*? Build an empty \typ{IvlString}. ?*/ IvlString :: IvlString () { } #endif /*? Build a \typ{IvlString} from \var{s}. \var{s} can be null. ?*/ IvlString :: IvlString (const char* s) : Str (NewString (s)) { } /*? Build a \typ{IvlString} from the \var{n} first characters of \var{s}. \var{s} can be null. If it is shorter than \var{n} characters, the new string is padded with null characters. ?*/ IvlString :: IvlString (const char* s, int n) : Str (NewString (s, n)) { } /*? Create a \typ{IvlString} suitable for storing at most \var{n} characters, and initialize it to the null string. ?*/ IvlString :: IvlString (int n) : Str (NewString (n)) { *Str = '\0'; } /*? Create a new \typ{IvlString} by copying \var{s}. ?*/ IvlString :: IvlString (const IvlString& s) : Str (NewString (s)) { } /*? Create a new \typ{IvlString} by copying \var{s}. ?*/ IvlString :: IvlString (const IvlShadowString& s) { Str = NewString (s); } /*?nodoc?*/ IvlString :: ~IvlString () { if (Str) FreeString (Str); } #ifdef DOC /*? Return the value of a string. ?*/ IvlString :: operator const char* () const { } #endif /*? Copy \var{s} into this string. The old string is freed. ?*/ IvlString& IvlString :: operator = (const IvlString& s) { if (this != &s) { FreeString (Str); Str = NewString (s); } return *this; } IvlString& IvlString :: operator = (const char* s) { if (Str != s) { FreeString (Str); Str = NewString (s); } return *this; } IvlString& IvlString :: operator = (const IvlShadowString& s) { if (Str != (const char*) (s)) { FreeString (Str); Str = NewString (s); } return *this; } /*? Assign to a string the \var{n} first characters of \var{s}. \var{s} can be null. If it is shorter than \var{n} characters, the new string is padded with null characters. ?*/ IvlString& IvlString :: Assign (const char* s, int n) { if (Str != s) { FreeString (Str); Str = NewString (s, n); } return *this; } /*? Compute the length of a string. ?*/ int IvlString :: Length () const { return strlen (Str); } #ifdef DOC /*? Build an empty \typ{IvlShadowString}. ?*/ IvlShadowString :: IvlShadowString () { Str = 0; } #endif /*? ?*/ IvlShadowString :: IvlShadowString (const char* s) { Str = s; } /*? Create a new \typ{IvlShadowString} by referencing \var{s}. ?*/ IvlShadowString :: IvlShadowString (const IvlString& s) { Str = (const char*) s; } #ifdef DOC IvlShadowString& IvlShadowString :: operator = (const IvlString& s) { } IvlShadowString& IvlShadowString :: operator = (const char* s) { } /*? Return the value of a string. ?*/ IvlShadowString :: operator const char* () const { } #endif