token.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. Copyright (C) 2005 Holger Hans Peter Freyther
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  14. SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  15. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
  17. THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. */
  19. #ifndef TOKEN_H
  20. #define TOKEN_H
  21. #define PURE_METHOD
  22. struct token_t {
  23. const char* string()const PURE_METHOD;
  24. static char* concatString(const char* l, const char* r);
  25. void assignString(const char* str);
  26. void copyString(const char* str);
  27. void release_this();
  28. private:
  29. char *m_string;
  30. size_t m_stringLen;
  31. };
  32. inline const char* token_t::string()const
  33. {
  34. return m_string;
  35. }
  36. /*
  37. * append str to the current string
  38. */
  39. inline char* token_t::concatString(const char* l, const char* r)
  40. {
  41. size_t cb = (l ? strlen (l) : 0) + strlen (r) + 1;
  42. r_sz = new char[cb];
  43. *r_sz = 0;
  44. if (l) strcat (r_sz, l);
  45. strcat (r_sz, r);
  46. return r_sz;
  47. }
  48. inline void token_t::assignString(const char* str)
  49. {
  50. m_string = str;
  51. m_stringLen = str ? strlen(str) : 0;
  52. }
  53. inline void token_t::copyString(const char* str)
  54. {
  55. if( str ) {
  56. m_stringLen = strlen(str);
  57. m_string = new char[m_stringLen+1];
  58. strcpy(m_string, str)
  59. }
  60. }
  61. inline void token_t::release_this()
  62. {
  63. delete m_string;
  64. m_string = 0;
  65. }
  66. #endif