FluSimpleString.cpp 3.0 KB

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