Utilites.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #include <OgreStringConverter.h>
  2. #include "core/Utilites.h"
  3. const Ogre::String
  4. CreateAutoName(const Ogre::String prefix)
  5. {
  6. static int enumerator = 0;
  7. return prefix + Ogre::StringConverter::toString(enumerator++);
  8. }
  9. struct KeyName
  10. {
  11. Ogre::String name;
  12. OIS::KeyCode key;
  13. };
  14. KeyName key_names[] =
  15. {
  16. {"Enter", OIS::KC_RETURN},
  17. {"Escape", OIS::KC_ESCAPE},
  18. {"Space", OIS::KC_SPACE},
  19. {"LShift", OIS::KC_LSHIFT},
  20. {"RShift", OIS::KC_RSHIFT},
  21. {"LCtrl", OIS::KC_LCONTROL},
  22. {"RCtrl", OIS::KC_RCONTROL},
  23. {"LAlt", OIS::KC_LMENU},
  24. {"RAlt", OIS::KC_RMENU},
  25. {"0", OIS::KC_0},
  26. {"1", OIS::KC_1},
  27. {"2", OIS::KC_2},
  28. {"3", OIS::KC_3},
  29. {"4", OIS::KC_4},
  30. {"5", OIS::KC_5},
  31. {"6", OIS::KC_6},
  32. {"7", OIS::KC_7},
  33. {"8", OIS::KC_8},
  34. {"9", OIS::KC_9},
  35. {"A", OIS::KC_A},
  36. {"B", OIS::KC_B},
  37. {"C", OIS::KC_C},
  38. {"D", OIS::KC_D},
  39. {"E", OIS::KC_E},
  40. {"F", OIS::KC_F},
  41. {"G", OIS::KC_G},
  42. {"H", OIS::KC_H},
  43. {"I", OIS::KC_I},
  44. {"J", OIS::KC_J},
  45. {"K", OIS::KC_K},
  46. {"L", OIS::KC_L},
  47. {"M", OIS::KC_M},
  48. {"N", OIS::KC_N},
  49. {"O", OIS::KC_O},
  50. {"P", OIS::KC_P},
  51. {"Q", OIS::KC_Q},
  52. {"R", OIS::KC_R},
  53. {"S", OIS::KC_S},
  54. {"T", OIS::KC_T},
  55. {"U", OIS::KC_U},
  56. {"V", OIS::KC_V},
  57. {"W", OIS::KC_W},
  58. {"X", OIS::KC_X},
  59. {"Y", OIS::KC_Y},
  60. {"Z", OIS::KC_Z},
  61. {"Left", OIS::KC_LEFT},
  62. {"Up", OIS::KC_UP},
  63. {"Down", OIS::KC_DOWN},
  64. {"Right", OIS::KC_RIGHT},
  65. {"Num0", OIS::KC_NUMPAD0},
  66. {"Num1", OIS::KC_NUMPAD1},
  67. {"Num2", OIS::KC_NUMPAD2},
  68. {"Num3", OIS::KC_NUMPAD3},
  69. {"Num4", OIS::KC_NUMPAD4},
  70. {"Num5", OIS::KC_NUMPAD5},
  71. {"Num6", OIS::KC_NUMPAD6},
  72. {"Num7", OIS::KC_NUMPAD7},
  73. {"Num8", OIS::KC_NUMPAD8},
  74. {"Num9", OIS::KC_NUMPAD9},
  75. {"NumLock", OIS::KC_NUMLOCK},
  76. {"NumDiv", OIS::KC_DIVIDE},
  77. {"NumMult", OIS::KC_MULTIPLY},
  78. {"NumSub", OIS::KC_SUBTRACT},
  79. {"NumAdd", OIS::KC_ADD},
  80. {"NumEnter", OIS::KC_NUMPADENTER},
  81. {"NumDec", OIS::KC_DECIMAL},
  82. {"F1", OIS::KC_F1},
  83. {"F2", OIS::KC_F2},
  84. {"F3", OIS::KC_F3},
  85. {"F4", OIS::KC_F4},
  86. {"F5", OIS::KC_F5},
  87. {"F6", OIS::KC_F6},
  88. {"F7", OIS::KC_F7},
  89. {"F8", OIS::KC_F8},
  90. {"F9", OIS::KC_F9},
  91. {"F10", OIS::KC_F10},
  92. {"F11", OIS::KC_F11},
  93. {"F12", OIS::KC_F12},
  94. {"UNASSIGNED", OIS::KC_UNASSIGNED},
  95. };
  96. Ogre::String
  97. KeyToString(OIS::KeyCode key)
  98. {
  99. // find string by key
  100. for(KeyName* kn = key_names; kn->name != "UNASSIGNED"; ++kn)
  101. {
  102. if(kn->key == key)
  103. {
  104. return kn->name;
  105. }
  106. }
  107. return "UNASSIGNED";
  108. }
  109. OIS::KeyCode
  110. StringToKey(const Ogre::String& str)
  111. {
  112. if(str == "")
  113. {
  114. return OIS::KC_UNASSIGNED;
  115. }
  116. // find key by string
  117. for(KeyName* kn = key_names; kn->name != "UNASSIGNED"; ++kn)
  118. {
  119. if(kn->name == str)
  120. {
  121. return kn->key;
  122. }
  123. }
  124. return OIS::KC_UNASSIGNED;
  125. }
  126. Ogre::StringVector
  127. StringTokenise(const Ogre::String& str, const Ogre::String& delimiters, const Ogre::String& delimiters_preserve, const Ogre::String& quote, const Ogre::String& esc)
  128. {
  129. Ogre::StringVector ret;
  130. Ogre::String::size_type pos = 0; // the current position (char) in the string
  131. char ch = 0; // buffer for the current character
  132. char delimiter = 0; // the buffer for the delimiter char which
  133. // will be added to the tokens if the delimiter
  134. // is preserved
  135. char current_quote = 0; // the char of the current open quote
  136. bool quoted = false; // indicator if there is an open quote
  137. Ogre::String token; // string buffer for the token
  138. bool token_complete = false; // indicates if the current token is
  139. // read to be added to the result vector
  140. Ogre::String::size_type len = str.length(); // length of the input-string
  141. // for every char in the input-string
  142. while(len > pos)
  143. {
  144. // get the character of the string and reset the delimiter buffer
  145. ch = str.at(pos);
  146. delimiter = 0;
  147. // assume ch isn't a delimiter
  148. bool add_char = true;
  149. // check ...
  150. // ... if the delimiter is an escaped character
  151. bool escaped = false; // indicates if the next char is protected
  152. if(esc.empty() == false) // check if esc-chars are provided
  153. {
  154. if(esc.find_first_of(ch) != std::string::npos)
  155. {
  156. // get the escaped char
  157. ++pos;
  158. if(pos < len) // if there are more chars left
  159. {
  160. // get the next one
  161. ch = str.at(pos);
  162. // add the escaped character to the token
  163. add_char = true;
  164. }
  165. else // cannot get any more characters
  166. {
  167. // don't add the esc-char
  168. add_char = false;
  169. }
  170. // ignore the remaining delimiter checks
  171. escaped = true;
  172. }
  173. }
  174. // ... if the delimiter is a quote
  175. if(quote.empty() == false && escaped == false)
  176. {
  177. // if quote chars are provided and the char isn't protected
  178. if(quote.find_first_of(ch) != std::string::npos)
  179. {
  180. // if not quoted, set state to open quote and set
  181. // the quote character
  182. if(quoted == false)
  183. {
  184. quoted = true;
  185. current_quote = ch;
  186. // don't add the quote-char to the token
  187. add_char = false;
  188. }
  189. else // if quote is open already
  190. {
  191. // check if it is the matching character to close it
  192. if(current_quote == ch)
  193. {
  194. // close quote and reset the quote character
  195. quoted = false;
  196. current_quote = 0;
  197. // don't add the quote-char to the token
  198. add_char = false;
  199. }
  200. }
  201. }
  202. }
  203. // if the delimiter isn't preserved
  204. if(delimiters.empty() == false && escaped == false && quoted == false)
  205. {
  206. // if a delimiter is provided and the char isn't protected by
  207. // quote or escape char
  208. if(delimiters.find_first_of(ch) != std::string::npos)
  209. {
  210. // if ch is a delimiter and the token string isn't empty
  211. // the token is complete
  212. if(token.empty() == false)
  213. {
  214. token_complete = true;
  215. }
  216. // don't add the delimiter to the token
  217. add_char = false;
  218. }
  219. }
  220. // if the delimiter is preserved - add it as a token
  221. bool add_delimiter = false;
  222. if(delimiters_preserve.empty() == false && escaped == false && quoted == false)
  223. {
  224. // if a delimiter which will be preserved is provided and the
  225. // char isn't protected by quote or escape char
  226. if(delimiters_preserve.find_first_of(ch) != std::string::npos)
  227. {
  228. // if ch is a delimiter and the token string isn't empty the token is complete
  229. if(token.empty() == false)
  230. {
  231. token_complete = true;
  232. }
  233. // don't add the delimiter to the token
  234. add_char = false;
  235. // add the delimiter
  236. delimiter = ch;
  237. add_delimiter = true;
  238. }
  239. }
  240. // add the character to the token
  241. if(add_char == true)
  242. {
  243. // add the current char
  244. token.push_back(ch);
  245. }
  246. // add the token if it is complete
  247. if(token_complete == true && token.empty() == false)
  248. {
  249. // add the token string
  250. ret.push_back(token);
  251. // clear the contents
  252. token.clear();
  253. // build the next token
  254. token_complete = false;
  255. }
  256. // add the delimiter
  257. if(add_delimiter == true)
  258. {
  259. // the next token is the delimiter
  260. Ogre::String delim_token;
  261. delim_token.push_back(delimiter);
  262. ret.push_back(delim_token);
  263. }
  264. // repeat for the next character
  265. ++pos;
  266. }
  267. // add the final token
  268. if(token.empty() == false)
  269. {
  270. ret.push_back(token);
  271. }
  272. return ret;
  273. }