9
3

seperator.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. // This class will split up a string smartly at the div character (default is space and tab)
  17. // Seperator.arg[i] is a copy of the string chopped at the divs
  18. // Seperator.argplus[i] is a pointer to the original string so it doesnt end at the div
  19. // Written by Quagmire
  20. #ifndef SEPERATOR_H
  21. #define SEPERATOR_H
  22. #include <string.h>
  23. #include <stdlib.h>
  24. class Seperator
  25. {
  26. public:
  27. Seperator(const char* message, char div = ' ', int16 in_maxargnum = 10, int16 arglen = 100, bool iObeyQuotes = false, char div2 = '\t', char div3 = 0, bool iSkipEmpty = true) {
  28. int i;
  29. argnum = 0;
  30. msg = strdup(message);
  31. this->maxargnum = in_maxargnum;
  32. argplus = new const char *[maxargnum+1];
  33. arg = new char *[maxargnum+1];
  34. for (i=0; i<=maxargnum; i++) {
  35. argplus[i]=arg[i] = new char[arglen+1];
  36. memset(arg[i], 0, arglen+1);
  37. }
  38. int len = strlen(message);
  39. int s = 0, l = 0;
  40. bool inarg = (!iSkipEmpty || !(message[0] == div || message[0] == div2 || message[0] == div3));
  41. bool inquote = (iObeyQuotes && (message[0] == '\"' || message[0] == '\''));
  42. argplus[0] = message;
  43. if (len == 0)
  44. return;
  45. for (i=0; i<len; i++) {
  46. // cout << i << ": 0x" << hex << (int) message[i] << dec << " " << message[i] << endl;
  47. if (inarg) {
  48. if ((inquote == false && (message[i] == div || message[i] == div2 || message[i] == div3)) || (inquote && (message[i] == '\'' || message[i] == '\"') && (message[i+1] == div || message[i+1] == div2 || message[i+1] == div3 || message[i+1] == 0))) {
  49. inquote = false;
  50. l = i-s;
  51. if (l >= arglen)
  52. l = arglen;
  53. if (l){
  54. if(l > 1 && (argplus[argnum][0] == '\'' || argplus[argnum][0] == '\"')){
  55. l--;
  56. memcpy(arg[argnum], argplus[argnum]+1, l);
  57. }
  58. else
  59. memcpy(arg[argnum], argplus[argnum], l);
  60. }
  61. arg[argnum][l] = 0;
  62. argnum++;
  63. if (iSkipEmpty)
  64. inarg = false;
  65. else {
  66. s=i+1;
  67. argplus[argnum] = &message[s];
  68. }
  69. }
  70. }
  71. else if (iObeyQuotes && (message[i] == '\"' || message[i] == '\'')) {
  72. inquote = true;
  73. }
  74. else {
  75. s = i;
  76. argplus[argnum] = &message[s];
  77. if (!(message[i] == div || message[i] == div2 || message[i] == div3)) {
  78. inarg = true;
  79. }
  80. }
  81. if (argnum > maxargnum)
  82. break;
  83. }
  84. if (inarg && argnum <= maxargnum) {
  85. l = i-s;
  86. if (l >= arglen)
  87. l = arglen;
  88. if (l)
  89. memcpy(arg[argnum], argplus[argnum], l);
  90. }
  91. }
  92. ~Seperator() {
  93. for (int i=0; i<=maxargnum; i++)
  94. safe_delete_array(arg[i]);
  95. safe_delete_array(arg);
  96. safe_delete_array(argplus);
  97. if (msg)
  98. free(msg);
  99. }
  100. int16 argnum;
  101. char** arg;
  102. const char** argplus;
  103. char * msg;
  104. bool IsSet(int num) const {
  105. return IsSet(arg[num]);
  106. }
  107. bool IsNumber(int num) const {
  108. return IsNumber(arg[num]);
  109. }
  110. bool IsHexNumber(int num) const {
  111. return IsHexNumber(arg[num]);
  112. }
  113. static bool IsSet(const char *check) {
  114. return check[0] != '\0';
  115. }
  116. static bool IsNumber(const char* check) {
  117. bool SeenDec = false;
  118. int len = strlen(check);
  119. if (len == 0) {
  120. return false;
  121. }
  122. int i;
  123. for (i = 0; i < len; i++) {
  124. if (check[i] < '0' || check[i] > '9') {
  125. if (check[i] == '.' && !SeenDec) {
  126. SeenDec = true;
  127. }
  128. else if (i == 0 && (check[i] == '-' || check[i] == '+') && !check[i+1] == 0) {
  129. // this is ok, do nothin
  130. }
  131. else {
  132. return false;
  133. }
  134. }
  135. }
  136. return true;
  137. }
  138. static bool IsHexNumber(char* check) {
  139. int len = strlen(check);
  140. if (len < 3)
  141. return false;
  142. if (check[0] != '0' || (check[1] != 'x' && check[1] != 'X'))
  143. return false;
  144. for (int i=2; i<len; i++) {
  145. if ((check[i] < '0' || check[i] > '9') && (check[i] < 'A' || check[i] > 'F') && (check[i] < 'a' || check[i] > 'f'))
  146. return false;
  147. }
  148. return true;
  149. }
  150. inline int16 GetMaxArgNum() const { return maxargnum; }
  151. inline int16 GetArgNumber() const { return argnum; }
  152. private:
  153. int16 maxargnum;
  154. };
  155. #endif