PageRenderTime 47ms CodeModel.GetById 40ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  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
 34#include <assert.h>
 35
 36#import "SimpleStringDictionary.h"
 37
 38namespace google_breakpad {
 39
 40//==============================================================================
 41const KeyValueEntry *SimpleStringDictionary::GetEntry(int i) const {
 42  return (i >= 0 && i < MAX_NUM_ENTRIES) ? &entries_[i] : NULL;
 43}
 44
 45//==============================================================================
 46int SimpleStringDictionary::GetCount() const {
 47  int count = 0;
 48  for (int i = 0; i < MAX_NUM_ENTRIES; ++i) {
 49    if (entries_[i].IsActive() ) {
 50      ++count;
 51    }
 52  }
 53  
 54  return count;
 55}
 56
 57//==============================================================================
 58const char *SimpleStringDictionary::GetValueForKey(const char *key) const {
 59  assert(key);
 60  if (!key)
 61    return NULL;
 62
 63  for (int i = 0; i < MAX_NUM_ENTRIES; ++i) {
 64    const KeyValueEntry &entry = entries_[i];
 65    if (entry.IsActive() && !strcmp(entry.GetKey(), key)) {
 66      return entry.GetValue();
 67    }
 68  }
 69
 70  return NULL;
 71}
 72
 73//==============================================================================
 74void SimpleStringDictionary::SetKeyValue(const char *key,
 75                                         const char *value) {
 76  if (!value) {
 77    RemoveKey(key);
 78    return;
 79  }
 80
 81  // key must not be NULL
 82  assert(key);
 83  if (!key)
 84    return;
 85  
 86  // key must not be empty string
 87  assert(key[0] != '\0');
 88  if (key[0] == '\0')
 89    return;
 90  
 91  int free_index = -1;
 92  
 93  // check if key already exists
 94  for (int i = 0; i < MAX_NUM_ENTRIES; ++i) {
 95    KeyValueEntry &entry = entries_[i];
 96    
 97    if (entry.IsActive()) {
 98      if (!strcmp(entry.GetKey(), key)) {
 99        entry.SetValue(value);
100        return;
101      }
102    } else {
103      // Make a note of an empty slot
104      if (free_index == -1) {
105        free_index = i;
106      }
107    }
108  }
109  
110  // check if we've run out of space
111  assert(free_index != -1);
112  
113  // Put new key into an empty slot (if found)
114  if (free_index != -1) {
115    entries_[free_index].SetKeyValue(key, value);
116  }
117}
118
119//==============================================================================
120void SimpleStringDictionary::RemoveKey(const char *key) {
121  assert(key);
122  if (!key)
123    return;
124
125  for (int i = 0; i < MAX_NUM_ENTRIES; ++i) {
126    if (!strcmp(entries_[i].GetKey(), key)) {
127      entries_[i].Clear();
128      return;
129    }
130  }
131}
132
133}  // namespace google_breakpad