String.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #pragma once
  2. //========================================================================
  3. // String.h : Defines some useful string utility functions
  4. //
  5. // Part of the GameCode4 Application
  6. //
  7. // GameCode4 is the sample application that encapsulates much of the source code
  8. // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
  9. // "Rez" Graham, published by Charles River Media.
  10. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
  11. //
  12. // If this source code has found it's way to you, and you think it has helped you
  13. // in any way, do the authors a favor and buy a new copy of the book - there are
  14. // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
  15. // by clicking here:
  16. // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
  17. //
  18. // There's a companion web site at http://www.mcshaffry.com/GameCode/
  19. //
  20. // The source code is managed and maintained through Google Code:
  21. // http://code.google.com/p/gamecode4/
  22. //
  23. // (c) Copyright 2012 Michael L. McShaffry and David Graham
  24. //
  25. // This program is free software; you can redistribute it and/or
  26. // modify it under the terms of the GNU Lesser GPL v3
  27. // as published by the Free Software Foundation.
  28. //
  29. // This program is distributed in the hope that it will be useful,
  30. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  32. // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
  33. //
  34. // You should have received a copy of the GNU Lesser GPL v3
  35. // along with this program; if not, write to the Free Software
  36. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  37. //
  38. //========================================================================
  39. #include <glm/glm.hpp>
  40. #define MAX_DIGITS_IN_INT 12 // max number of digits in an int (-2147483647 = 11 digits, +1 for the '\0')
  41. typedef std::vector<std::string> StringVec;
  42. // Does a classic * & ? pattern match on a file name - this is case sensitive!
  43. extern bool WildcardMatch(const char *pat, const char *str);
  44. // [munnellg] these aren't really necessary with streams
  45. extern std::string ToStr(int num, int base = 10);
  46. extern std::string ToStr(unsigned int num, int base = 10);
  47. extern std::string ToStr(unsigned long num, int base = 10);
  48. extern std::string ToStr(float num);
  49. extern std::string ToStr(double num);
  50. extern std::string ToStr(bool val);
  51. extern std::string ToStr(const glm::vec3& vec);
  52. // Splits a string by the delimeter into a vector of strings. For example, say you have the following string:
  53. // std::string test("one,two,three");
  54. // You could call Split() like this:
  55. // Split(test, outVec, ',');
  56. // outVec will have the following values:
  57. // "one", "two", "three"
  58. void Split(const std::string& str, StringVec& vec, char delimiter);
  59. // A hashed string. It retains the initial (ANSI) string in addition to the hash value for easy reference.
  60. // class HashedString - Chapter 10, page 274
  61. class HashedString
  62. {
  63. public:
  64. explicit HashedString( char const * const pIdentString )
  65. : m_ident( hash_name( pIdentString ) )
  66. , m_identStr( pIdentString )
  67. {
  68. }
  69. unsigned long getHashValue( void ) const
  70. {
  71. return m_ident;
  72. }
  73. const std::string & getStr() const
  74. {
  75. return m_identStr;
  76. }
  77. static
  78. unsigned long hash_name( char const * pIdentStr );
  79. bool operator< ( HashedString const & o ) const
  80. {
  81. bool r = ( getHashValue() < o.getHashValue() );
  82. return r;
  83. }
  84. bool operator== ( HashedString const & o ) const
  85. {
  86. bool r = ( getHashValue() == o.getHashValue() );
  87. return r;
  88. }
  89. private:
  90. // note: m_ident is stored as a void* not an int, so that in
  91. // the debugger it will show up as hex-values instead of
  92. // integer values. This is a bit more representative of what
  93. // we're doing here and makes it easy to allow external code
  94. // to assign event types as desired.
  95. unsigned long m_ident;
  96. std::string m_identStr;
  97. };