i18nOffsetCache.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 2; tab-width: 2 -*- */
  2. /*
  3. * Copyright (C) 2014 Peng Huang <shawn.p.huang@gmail.com>
  4. * Copyright (C) 2014 Red Hat, Inc.
  5. *
  6. * Permission to use, copy, modify, distribute, and sell this
  7. * software and its documentation for any purpose is hereby granted
  8. * without fee, provided that the above copyright notice appear in
  9. * all copies and that both that copyright notice and this permission
  10. * notice appear in supporting documentation, and that the name of
  11. * the copyright holders not be used in advertising or publicity
  12. * pertaining to distribution of the software without specific,
  13. * written prior permission. The copyright holders make no
  14. * representations about the suitability of this software for any
  15. * purpose. It is provided "as is" without express or implied
  16. * warranty.
  17. *
  18. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
  19. * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  20. * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  22. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  23. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  24. * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
  25. * THIS SOFTWARE.
  26. *
  27. * Author: Klemens Baum <klemensbaum@gmail.com>
  28. */
  29. #include <X11/Xlib.h>
  30. #include <stddef.h>
  31. #include "Xi18n.h"
  32. /*
  33. * The XIM specification does not limit the number of window properties
  34. * that can be used to transfer data, but Xlib uses the atom strings
  35. * _client0 through _client20.
  36. *
  37. * So use that as a sensible initial size for the offset cache.
  38. */
  39. #define INITIAL_OFFSET_CACHE_CAPACITY 21
  40. #define OFFSET_CACHE_GROWTH_FACTOR 2
  41. void _Xi18nInitOffsetCache (Xi18nOffsetCache *offset_cache)
  42. {
  43. offset_cache->size = 0;
  44. offset_cache->capacity = INITIAL_OFFSET_CACHE_CAPACITY;
  45. offset_cache->data = (Xi18nAtomOffsetPair *) malloc (
  46. INITIAL_OFFSET_CACHE_CAPACITY * sizeof (Xi18nAtomOffsetPair));
  47. }
  48. unsigned long _Xi18nLookupPropertyOffset (Xi18nOffsetCache *offset_cache,
  49. Atom key)
  50. {
  51. Xi18nAtomOffsetPair *data;
  52. size_t i;
  53. data = offset_cache->data;
  54. for (i = 0; i < offset_cache->size; ++i) {
  55. if (data[i].key == key) {
  56. return data[i].offset;
  57. }
  58. }
  59. return 0;
  60. }
  61. void _Xi18nSetPropertyOffset (Xi18nOffsetCache *offset_cache, Atom key,
  62. unsigned long offset)
  63. {
  64. Xi18nAtomOffsetPair *data = offset_cache->data;
  65. size_t i;
  66. for (i = 0; i < offset_cache->size; ++i) {
  67. if (data[i].key == key) {
  68. data[i].offset = offset;
  69. return;
  70. }
  71. }
  72. if (++offset_cache->size > offset_cache->capacity) {
  73. offset_cache->capacity *= OFFSET_CACHE_GROWTH_FACTOR;
  74. offset_cache->data = (Xi18nAtomOffsetPair *) realloc (data,
  75. offset_cache->capacity * sizeof (Xi18nAtomOffsetPair));
  76. if (offset_cache->data == NULL) {
  77. offset_cache->data = data;
  78. --offset_cache->size;
  79. }
  80. data = offset_cache->data;
  81. }
  82. if (offset_cache->size > 0) {
  83. data[i].key = key;
  84. data[i].offset = offset;
  85. }
  86. }