OSTR.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Seven Kingdoms: Ancient Adversaries
  3. *
  4. * Copyright 1997,1998 Enlight Software Ltd.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. //Filename : OSTR.CPP
  21. //Description : Object String
  22. #include <OMISC.h>
  23. #include <ctype.h>
  24. #include <OSTR.h>
  25. //------- Define static variable --------//
  26. static char work_buf[MAX_STR_LEN+1];
  27. //-------- Begin of constructor/destructor functions --------//
  28. String::String()
  29. {
  30. str_buf[0] = NULL;
  31. }
  32. String::String( char *s )
  33. {
  34. strncpy(str_buf, s, MAX_STR_LEN );
  35. str_buf[MAX_STR_LEN] = NULL;
  36. }
  37. String::String( String& s )
  38. {
  39. memcpy(str_buf, s.str_buf, MAX_STR_LEN+1 );
  40. }
  41. //---------- End of constructor/destructor functions --------//
  42. //------- Begin of function String::substr --------//
  43. //
  44. // <int> pos = the start position of the sub string you want
  45. // start from 0, 0-aligned
  46. // [int] len = the length of the sub string
  47. // (default : till the end of the string
  48. //
  49. char* String::substr(int pos, int len)
  50. {
  51. work_buf[0] = NULL;
  52. int strLen = strlen(str_buf);
  53. if(pos >= strLen) // check for boundary errors
  54. return work_buf;
  55. if( len <= 0 )
  56. len = strLen - pos;
  57. else
  58. {
  59. if(len > strLen - pos)
  60. return work_buf;
  61. }
  62. strncpy( work_buf, str_buf+pos, len );
  63. work_buf[len] = NULL;
  64. return work_buf;
  65. }
  66. //------- End of function String::substr ------------//
  67. //------- Begin of function String::upper/lower -------//
  68. char* String::upper()
  69. {
  70. memcpy( work_buf, str_buf, len()+1 );
  71. strupr( work_buf );
  72. return work_buf;
  73. }
  74. char* String::lower(void)
  75. {
  76. memcpy( work_buf, str_buf, len()+1 );
  77. strlwr( work_buf );
  78. return work_buf;
  79. }
  80. //--------- End of function String::upper/lower --------//
  81. //-------- Begin of function String::at ---------------//
  82. //
  83. // <char*> searchStr = the string you want to search for
  84. //
  85. // return : >= 0 the position of the search str in the string
  86. // = -1 if not found
  87. //
  88. int String::at(char* searchStr)
  89. {
  90. int pos;
  91. char *tmp;
  92. if( (tmp = strstr(str_buf, searchStr)) != NULL)
  93. pos = (int)(tmp-str_buf);
  94. else
  95. pos = -1;
  96. return pos;
  97. }
  98. //---------- End of function String::at ---------------//
  99. //-------- Begin of operator= functions -----------//
  100. String& String::operator=(String& s)
  101. {
  102. memcpy(str_buf, s.str_buf, MAX_STR_LEN+1 );
  103. return *this;
  104. }
  105. String& String::operator=(char *s)
  106. {
  107. strncpy(str_buf, s, MAX_STR_LEN );
  108. str_buf[MAX_STR_LEN] = NULL;
  109. return *this;
  110. }
  111. String& String::operator=(long value)
  112. {
  113. strncpy(str_buf, m.format(value), MAX_STR_LEN );
  114. str_buf[MAX_STR_LEN] = NULL;
  115. return *this;
  116. }
  117. //---------- End of operator= functions -----------//
  118. //-------- Begin of operator+= functions -----------//
  119. String& String::operator+=(String& s)
  120. {
  121. strncat( str_buf, s.str_buf, MAX_STR_LEN );
  122. str_buf[MAX_STR_LEN] = NULL;
  123. return *this;
  124. }
  125. String& String::operator+=(char *s)
  126. {
  127. strncat( str_buf, s, MAX_STR_LEN );
  128. str_buf[MAX_STR_LEN] = NULL;
  129. return *this;
  130. }
  131. String& String::operator+=(long value)
  132. {
  133. strncat( str_buf, m.format(value), MAX_STR_LEN );
  134. str_buf[MAX_STR_LEN] = NULL;
  135. return *this;
  136. }
  137. //---------- End of operator+= functions -----------//
  138. //-------- Begin of operator*= functions -----------//
  139. String& String::operator*=(int n)
  140. {
  141. memcpy( work_buf, str_buf, len()+1 );
  142. for(int i=1; i<n; i++)
  143. strncat(str_buf, work_buf, MAX_STR_LEN);
  144. str_buf[MAX_STR_LEN] = NULL;
  145. return *this;
  146. }
  147. //---------- End of operator*= functions -----------//