FluSimpleString.H 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // $Id: FluSimpleString.h,v 1.12 2003/11/27 01:09:04 jbryan Exp $
  2. /***************************************************************
  3. * FLU - FLTK Utility Widgets
  4. * Copyright (C) 2002 Ohio Supercomputer Center, Ohio State University
  5. *
  6. * This file and its content is protected by a software license.
  7. * You should have received a copy of this license with this file.
  8. * If not, please contact the Ohio Supercomputer Center immediately:
  9. * Attn: Jason Bryan Re: FLU 1224 Kinnear Rd, Columbus, Ohio 43212
  10. *
  11. ***************************************************************/
  12. #ifndef _FLU_SIMPLE_STRING_H
  13. #define _FLU_SIMPLE_STRING_H
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <ctype.h>
  18. #include "FLU/Flu_Enumerations.H"
  19. //! A simple string object that works a lot like the STL string object
  20. class FLU_EXPORT FluSimpleString
  21. {
  22. public:
  23. //! Default constructor: sets to empty string ("")
  24. FluSimpleString();
  25. //! Allocates the string to be of length \b len. Does not set the buffer to any initial value
  26. FluSimpleString( unsigned int len );
  27. //! String copy constructor
  28. FluSimpleString( const char *s );
  29. //! Copy constructor
  30. FluSimpleString( const FluSimpleString& s );
  31. //! Default destructor
  32. ~FluSimpleString();
  33. //! \return a c-style nul terminated pointer to the string
  34. inline const char* c_str() const { return str; }
  35. //! \return how long the string is
  36. inline int size() const { return strlen(str); }
  37. //! Array operator
  38. inline char& operator [](int i) { return str[i]; }
  39. //! Array operator
  40. inline char operator [](int i) const { return str[i]; }
  41. //! \return the indicated substring of this string
  42. FluSimpleString substr( int pos, int len ) const;
  43. //! Convert this string to uppercase
  44. void upcase();
  45. //! Convert this string to lowercase
  46. void downcase();
  47. //! \return the first index of character \b c in this string, or -1 if \c is not in this string
  48. int find( char c ) const;
  49. //! \return the last index of character \b c in this string, or -1 if \c is not in this string
  50. int rfind( char c ) const;
  51. //! \return 0 if this string is equal to \b s, -1 if this string is lexigraphically less than \b s, 1 if this string is lexigraphically greater than \b s (uses c-std function \b strcmp )
  52. int compare( const FluSimpleString &s ) const;
  53. //! Same as compare(), except ignores the case of the string
  54. int casecompare( const FluSimpleString &s ) const;
  55. //! \return 0 if this string is equal to \b s, -1 if this string is lexigraphically less than \b s, 1 if this string is lexigraphically greater than \b s (uses c-std function \b strcmp )
  56. inline friend int compare( const FluSimpleString &s1, const FluSimpleString &s2 )
  57. { return s1.compare( s2 ); }
  58. //! Same as compare(), except ignores the case of the string
  59. inline friend int casecompare( const FluSimpleString &s1, const FluSimpleString &s2 )
  60. { return s1.casecompare( s2 ); }
  61. //! Add character \b c to the end of the string
  62. inline void push_back( char c )
  63. { char s[2] = { c, '\0' }; *this += s; }
  64. //! Alias for the \c = operator
  65. inline void copy( const FluSimpleString& s )
  66. { *this = s; }
  67. //! Alias for the \c = operator
  68. inline void copy( const char *s )
  69. { *this = s; }
  70. //! Copy the substring of \b s to this string
  71. inline void copy( const FluSimpleString& s, unsigned int start, unsigned int len )
  72. { copy( s.c_str(), start, len ); }
  73. //! Copy the substring of \b s to this string
  74. void copy( const char *s, unsigned int start, unsigned int len );
  75. //! Copy operator
  76. FluSimpleString& operator =( const char *s );
  77. //! Copy operator
  78. inline FluSimpleString& operator =( FluSimpleString s ) { return( *this = s.str ); }
  79. //! Less-than operator
  80. inline bool operator <( const FluSimpleString& s ) const { return( strcmp( str, s.str ) < 0 ); }
  81. //! Greater-than operator
  82. inline bool operator >( const FluSimpleString& s ) const { return( strcmp( str, s.str ) > 0 ); }
  83. //! Equality operator
  84. inline bool operator ==( const FluSimpleString& s ) const { return( strcmp( str, s.str ) == 0 ); }
  85. //! Inequality operator
  86. inline bool operator !=( const FluSimpleString& s ) const { return( strcmp( str, s.str ) != 0 ); }
  87. //! Concatenate operator
  88. inline friend FluSimpleString operator +( const char *s1, FluSimpleString s2 ) { FluSimpleString s = s1; s += s2; return s; }
  89. //! Concatenate operator
  90. inline friend FluSimpleString operator +( FluSimpleString s1, const char *s2 ) { FluSimpleString s = s1; s += s2; return s; }
  91. //! Concatenate operator
  92. inline friend FluSimpleString operator +( FluSimpleString s1, FluSimpleString s2 ) { FluSimpleString s = s1; s += s2; return s; }
  93. //! Concatenate assignment operator
  94. inline FluSimpleString& operator +=( const FluSimpleString& s ) { *this += s.str; return *this; }
  95. //! Concatenate assignment operator
  96. FluSimpleString& operator +=( const char *s );
  97. private:
  98. char *str;
  99. };
  100. #endif