string.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/gtk/private/string.h
  3. // Purpose: wxGtkString class declaration
  4. // Author: Vadim Zeitlin
  5. // Created: 2006-10-19
  6. // Copyright: (c) 2006 Vadim Zeitlin <vadim@wxwindows.org>
  7. // Licence: wxWindows licence
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_GTK_PRIVATE_STRING_H_
  10. #define _WX_GTK_PRIVATE_STRING_H_
  11. // ----------------------------------------------------------------------------
  12. // Convenience class for g_freeing a gchar* on scope exit automatically
  13. // ----------------------------------------------------------------------------
  14. class wxGtkString
  15. {
  16. public:
  17. explicit wxGtkString(gchar *s) : m_str(s) { }
  18. ~wxGtkString() { g_free(m_str); }
  19. const gchar *c_str() const { return m_str; }
  20. operator gchar *() const { return m_str; }
  21. private:
  22. gchar *m_str;
  23. wxDECLARE_NO_COPY_CLASS(wxGtkString);
  24. };
  25. // ----------------------------------------------------------------------------
  26. // list for sorting collated strings
  27. // ----------------------------------------------------------------------------
  28. #include "wx/string.h"
  29. #include "wx/vector.h"
  30. #include "wx/sharedptr.h"
  31. class wxGtkCollatableString
  32. {
  33. public:
  34. wxGtkCollatableString( const wxString &label, gchar *key )
  35. {
  36. m_label = label;
  37. m_key = key;
  38. }
  39. ~wxGtkCollatableString()
  40. {
  41. if (m_key)
  42. g_free( m_key );
  43. }
  44. wxString m_label;
  45. gchar *m_key;
  46. };
  47. class wxGtkCollatedArrayString
  48. {
  49. public:
  50. wxGtkCollatedArrayString() { }
  51. int Add( const wxString &new_label )
  52. {
  53. int index = 0;
  54. gchar *new_key_lower = g_utf8_casefold( new_label.utf8_str(), -1);
  55. gchar *new_key = g_utf8_collate_key( new_key_lower, -1);
  56. g_free( new_key_lower );
  57. wxSharedPtr<wxGtkCollatableString> new_ptr( new wxGtkCollatableString( new_label, new_key ) );
  58. wxVector< wxSharedPtr<wxGtkCollatableString> >::iterator iter;
  59. for (iter = m_list.begin(); iter != m_list.end(); ++iter)
  60. {
  61. wxSharedPtr<wxGtkCollatableString> ptr = *iter;
  62. gchar *key = ptr->m_key;
  63. if (strcmp(key,new_key) >= 0)
  64. {
  65. m_list.insert( iter, new_ptr );
  66. return index;
  67. }
  68. index ++;
  69. }
  70. m_list.push_back( new_ptr );
  71. return index;
  72. }
  73. size_t GetCount()
  74. {
  75. return m_list.size();
  76. }
  77. wxString At( size_t index )
  78. {
  79. return m_list[index]->m_label;
  80. }
  81. void Clear()
  82. {
  83. m_list.clear();
  84. }
  85. void RemoveAt( size_t index )
  86. {
  87. m_list.erase( m_list.begin() + index );
  88. }
  89. private:
  90. wxVector< wxSharedPtr<wxGtkCollatableString> > m_list;
  91. };
  92. #endif // _WX_GTK_PRIVATE_STRING_H_