PageRenderTime 38ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 1ms

/thirdparty/breakpad/common/mac/SimpleStringDictionary.mm

http://github.com/tomahawk-player/tomahawk
Objective C++ | 133 lines | 70 code | 20 blank | 43 comment | 24 complexity | 94a35209e77e7232c32fde6fda18744a MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, GPL-2.0
  1. // Copyright (c) 2007, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. //
  30. // SimpleStringDictionary.mm
  31. // Simple string dictionary that does not allocate memory
  32. //
  33. #include <assert.h>
  34. #import "SimpleStringDictionary.h"
  35. namespace google_breakpad {
  36. //==============================================================================
  37. const KeyValueEntry *SimpleStringDictionary::GetEntry(int i) const {
  38. return (i >= 0 && i < MAX_NUM_ENTRIES) ? &entries_[i] : NULL;
  39. }
  40. //==============================================================================
  41. int SimpleStringDictionary::GetCount() const {
  42. int count = 0;
  43. for (int i = 0; i < MAX_NUM_ENTRIES; ++i) {
  44. if (entries_[i].IsActive() ) {
  45. ++count;
  46. }
  47. }
  48. return count;
  49. }
  50. //==============================================================================
  51. const char *SimpleStringDictionary::GetValueForKey(const char *key) const {
  52. assert(key);
  53. if (!key)
  54. return NULL;
  55. for (int i = 0; i < MAX_NUM_ENTRIES; ++i) {
  56. const KeyValueEntry &entry = entries_[i];
  57. if (entry.IsActive() && !strcmp(entry.GetKey(), key)) {
  58. return entry.GetValue();
  59. }
  60. }
  61. return NULL;
  62. }
  63. //==============================================================================
  64. void SimpleStringDictionary::SetKeyValue(const char *key,
  65. const char *value) {
  66. if (!value) {
  67. RemoveKey(key);
  68. return;
  69. }
  70. // key must not be NULL
  71. assert(key);
  72. if (!key)
  73. return;
  74. // key must not be empty string
  75. assert(key[0] != '\0');
  76. if (key[0] == '\0')
  77. return;
  78. int free_index = -1;
  79. // check if key already exists
  80. for (int i = 0; i < MAX_NUM_ENTRIES; ++i) {
  81. KeyValueEntry &entry = entries_[i];
  82. if (entry.IsActive()) {
  83. if (!strcmp(entry.GetKey(), key)) {
  84. entry.SetValue(value);
  85. return;
  86. }
  87. } else {
  88. // Make a note of an empty slot
  89. if (free_index == -1) {
  90. free_index = i;
  91. }
  92. }
  93. }
  94. // check if we've run out of space
  95. assert(free_index != -1);
  96. // Put new key into an empty slot (if found)
  97. if (free_index != -1) {
  98. entries_[free_index].SetKeyValue(key, value);
  99. }
  100. }
  101. //==============================================================================
  102. void SimpleStringDictionary::RemoveKey(const char *key) {
  103. assert(key);
  104. if (!key)
  105. return;
  106. for (int i = 0; i < MAX_NUM_ENTRIES; ++i) {
  107. if (!strcmp(entries_[i].GetKey(), key)) {
  108. entries_[i].Clear();
  109. return;
  110. }
  111. }
  112. }
  113. } // namespace google_breakpad