# SbString.h

    00001 #ifndef COIN_SBSTRING_H
    00002 #define COIN_SBSTRING_H
    00003
    00004 /**************************************************************************\
    00005  *
    00006  *  This file is part of the Coin 3D visualization library.
    00007  *  Copyright (C) 1998-2003 by Systems in Motion.  All rights reserved.
    00008  *
    00009  *  This library is free software; you can redistribute it and/or
    00010  *  modify it under the terms of the GNU General Public License
    00011  *  ("GPL") version 2 as published by the Free Software Foundation.
    00012  *  See the file LICENSE.GPL at the root directory of this source
    00013  *  distribution for additional information about the GNU GPL.
    00014  *
    00015  *  For using Coin with software that can not be combined with the GNU
    00016  *  GPL, and for taking advantage of the additional benefits of our
    00017  *  support services, please contact Systems in Motion about acquiring
    00018  *  a Coin Professional Edition License.
    00019  *
    00020  *  See <URL:http://www.coin3d.org> for  more information.
    00021  *
    00022  *  Systems in Motion, Teknobyen, Abels Gate 5, 7030 Trondheim, NORWAY.
    00023  *  <URL:http://www.sim.no>.
    00024  *
    00025 \**************************************************************************/
    00026
    00027 #include <Inventor/system/inttypes.h>
    00028 #include <Inventor/C/base/string.h>
    00029 #include <Inventor/lists/SbIntList.h>
    00030
    00031 #include <stdarg.h>
    00032
    00033 class COIN_DLL_API SbString {
    00034 public:
    00035   SbString(void) { cc_string_construct(&this->str); }
    00036
    00037   SbString(const char * str)
    00038   { cc_string_construct(&this->str); cc_string_set_text(&this->str, str); }
    00039
    00040   SbString(const char * str, int start, int end)
    00041   { cc_string_construct(&this->str); cc_string_set_subtext(&this->str, str, start, end); }
    00042
    00043   SbString(const SbString & str)
    00044   { cc_string_construct(&this->str); cc_string_set_string(&this->str, &str.str); }
    00045
    00046   SbString(const int digits)
    00047   { cc_string_construct(&this->str); cc_string_set_integer(&this->str, digits); }
    00048
    00049   ~SbString() { cc_string_clean(&this->str); }
    00050
    00051   uint32_t hash(void) const { return cc_string_hash(&this->str); }
    00052   static uint32_t hash(const char * s) { return cc_string_hash_text(s); }
    00053
    00054   int getLength(void) const { return cc_string_length(&this->str); }
    00055
    00056   void makeEmpty(SbBool freeold = TRUE)
    00057   {
    00058     if ( freeold ) cc_string_clear(&this->str);
    00059     else cc_string_clear_no_free(&this->str);
    00060   }
    00061
    00062   const char * getString(void) const { return cc_string_get_text(&this->str); }
    00063
    00064   SbString getSubString(int startidx, int endidx = -1) const
    00065   {
    00066     SbString s;
    00067     cc_string_set_subtext(&s.str, cc_string_get_text(&this->str), startidx, endidx);
    00068     return s;
    00069   }
    00070   void deleteSubString(int startidx, int endidx = -1)
    00071   {
    00072     cc_string_remove_substring(&this->str, startidx, endidx);
    00073   }
    00074
    00075   void addIntString(const int value) { cc_string_append_integer(&this->str, value); }
    00076
    00077   char operator[](int index) const { return this->str.pointer[index]; }
    00078
    00079   SbString & operator=(const char * str)
    00080   { cc_string_set_text(&this->str, str); return *this; }
    00081   SbString & operator=(const SbString & str)
    00082   { cc_string_set_text(&this->str, str.str.pointer); return *this; }
    00083
    00084   SbString & operator+=(const char * str)
    00085   { cc_string_append_text(&this->str, str); return *this; }
    00086   SbString & operator+=(const SbString & str)
    00087   { cc_string_append_string(&this->str, &str.str); return *this; }
    00088   SbString & operator+=(const char c)
    00089   { cc_string_append_char(&this->str, c); return *this; }
    00090
    00091   int operator!(void) const { return ! cc_string_is(&this->str); }
    00092
    00093   int compareSubString(const char * text, int offset = 0) const
    00094   { return cc_string_compare_subtext(&this->str, text, offset); }
    00095
    00096   SbString & sprintf(const char * formatstr, ...)
    00097   {
    00098     va_list args; va_start(args, formatstr);
    00099     cc_string_vsprintf(&this->str, formatstr, args);
    00100     va_end(args); return *this;
    00101   }
    00102   SbString & vsprintf(const char * formatstr, va_list args)
    00103   { cc_string_vsprintf(&this->str, formatstr, args); return *this; }
    00104
    00105   void apply(char (*func)(char input)) { cc_string_apply(&this->str, (cc_apply_f)func); }
    00106
    00107   int find(const SbString & str) const;
    00108   SbBool findAll(const SbString & str, SbIntList & found) const;
    00109
    00110   friend int operator==(const SbString & str, const char * s);
    00111   friend int operator==(const char * s, const SbString & str);
    00112   friend int operator==(const SbString & str1, const SbString & str2);
    00113   friend int operator!=(const SbString & str, const char * s);
    00114   friend int operator!=(const char * s, const SbString & str);
    00115   friend int operator!=(const SbString & str1, const SbString & str2);
    00116
    00117 private:
    00118   struct cc_string str;
    00119 };
    00120
    00121 inline int operator==(const SbString & str, const char * s)
    00122 { return (cc_string_compare_text(str.str.pointer, s) == 0); }
    00123 inline int operator==(const char * s, const SbString & str)
    00124 { return (cc_string_compare_text(s, str.str.pointer) == 0); }
    00125 inline int operator==(const SbString & str1, const SbString & str2)
    00126 { return (cc_string_compare_text(str1.str.pointer, str2.str.pointer) == 0); }
    00127
    00128 inline int operator!=(const SbString & str, const char * s)
    00129 { return (cc_string_compare_text(str.str.pointer, s) != 0); }
    00130 inline int operator!=(const char * s, const SbString & str)
    00131 { return (cc_string_compare_text(s, str.str.pointer) != 0); }
    00132 inline int operator!=(const SbString & str1, const SbString & str2)
    00133 { return (cc_string_compare_text(str1.str.pointer, str2.str.pointer) != 0); }
    00134
    00135 #ifndef COIN_INTERNAL
    00136 // For Open Inventor compatibility.
    00137 #include <Inventor/SbName.h>
    00138 #endif // !COIN_INTERNAL
    00139
    00140 #endif // !COIN_SBSTRING_H