FluSimpleString.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // $Id: FluSimpleString.cpp,v 1.9 2005/05/11 16:52: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. #include "FLU/FluSimpleString.H"
  13. FluSimpleString :: FluSimpleString()
  14. {
  15. str = strdup("");
  16. }
  17. FluSimpleString :: FluSimpleString( unsigned int len )
  18. {
  19. if( len > 0 )
  20. str = (char*)malloc( len );
  21. else
  22. str = strdup("");
  23. }
  24. FluSimpleString :: FluSimpleString( const char *s )
  25. {
  26. str = strdup( s );
  27. }
  28. FluSimpleString :: FluSimpleString( const FluSimpleString& s )
  29. {
  30. str = strdup( s.str );
  31. }
  32. FluSimpleString :: ~FluSimpleString()
  33. {
  34. if(str) free(str);
  35. }
  36. void FluSimpleString :: copy( const char *s, unsigned int start, unsigned int len )
  37. {
  38. if( len == 0 ) return;
  39. if( s == 0 ) return;
  40. if( start+len > strlen(s) ) return;
  41. if(str) free(str);
  42. str = (char*)malloc( len+1 );
  43. strncpy( str, s+start, len );
  44. str[len] = '\0';
  45. }
  46. int FluSimpleString :: compare( const FluSimpleString &s ) const
  47. {
  48. return strcmp( str, s.str );
  49. }
  50. int FluSimpleString :: casecompare( const FluSimpleString &s ) const
  51. {
  52. FluSimpleString s1(str), s2(s);
  53. s1.upcase();
  54. s2.upcase();
  55. return strcmp( s1.str, s2.str );
  56. }
  57. void FluSimpleString :: upcase()
  58. {
  59. unsigned int l = strlen(str);
  60. for( unsigned int i = 0; i < l; i++ )
  61. str[i] = toupper( str[i] );
  62. }
  63. void FluSimpleString :: downcase()
  64. {
  65. unsigned int l = strlen(str);
  66. for( unsigned int i = 0; i < l; i++ )
  67. str[i] = tolower( str[i] );
  68. }
  69. int FluSimpleString :: find( char c ) const
  70. {
  71. const char *i = strchr( str, c );
  72. if( !i )
  73. return -1;
  74. else
  75. return i-str;
  76. }
  77. int FluSimpleString :: rfind( char c ) const
  78. {
  79. const char *i = strrchr( str, c );
  80. if( !i )
  81. return -1;
  82. else
  83. return i-str;
  84. }
  85. FluSimpleString FluSimpleString :: substr( int pos, int len ) const
  86. {
  87. if( (pos+len) <= 0 || (pos+len) > size() )
  88. return FluSimpleString("");
  89. else
  90. {
  91. char *buf = (char*)malloc( len+1 );
  92. strncpy( buf, str+pos, len );
  93. buf[len] = '\0';
  94. FluSimpleString s = buf;
  95. free( buf );
  96. return s;
  97. }
  98. }
  99. FluSimpleString& FluSimpleString :: operator =( const char *s )
  100. {
  101. char* tmp;
  102. if(s==0)
  103. tmp = strdup("");
  104. else
  105. tmp = strdup(s);
  106. if(str)
  107. free(str);
  108. str = tmp;
  109. return *this;
  110. }
  111. FluSimpleString& FluSimpleString :: operator +=( const char *s )
  112. {
  113. if( s==0 )
  114. s = "";
  115. char *old = strdup(str);
  116. int lold = strlen(old), ls = strlen(s);
  117. free(str);
  118. str = (char*)malloc( lold + ls + 1 );
  119. memcpy( str, old, lold );
  120. memcpy( str+lold, s, ls );
  121. str[lold+ls] = '\0';
  122. free(old);
  123. return *this;
  124. }