Blame util/IMdkit/i18nOffsetCache.c

Packit 3ff832
/*
Packit 3ff832
 * Copyright (C) 2014 Peng Huang <shawn.p.huang@gmail.com>
Packit 3ff832
 * Copyright (C) 2014 Red Hat, Inc.
Packit 3ff832
 *
Packit 3ff832
 * Permission to use, copy, modify, distribute, and sell this
Packit 3ff832
 * software and its documentation for any purpose is hereby granted
Packit 3ff832
 * without fee, provided that the above copyright notice appear in
Packit 3ff832
 * all copies and that both that copyright notice and this permission
Packit 3ff832
 * notice appear in supporting documentation, and that the name of
Packit 3ff832
 * the copyright holders not be used in advertising or publicity
Packit 3ff832
 * pertaining to distribution of the software without specific,
Packit 3ff832
 * written prior permission.  The copyright holders make no
Packit 3ff832
 * representations about the suitability of this software for any
Packit 3ff832
 * purpose.  It is provided "as is" without express or implied
Packit 3ff832
 * warranty.
Packit 3ff832
 *
Packit 3ff832
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
Packit 3ff832
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
Packit 3ff832
 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
Packit 3ff832
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
Packit 3ff832
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
Packit 3ff832
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
Packit 3ff832
 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
Packit 3ff832
 * THIS SOFTWARE.
Packit 3ff832
 *
Packit 3ff832
 * Author: Klemens Baum <klemensbaum@gmail.com>
Packit 3ff832
 */
Packit 3ff832
Packit 3ff832
#include <X11/Xlib.h>
Packit 3ff832
#include <assert.h>
Packit 3ff832
#include <stddef.h>
Packit 3ff832
#include "IMdkit.h"
Packit 3ff832
#include "Xi18n.h"
Packit 3ff832
#include "Xi18n.h"
Packit 3ff832
Packit 3ff832
/*
Packit 3ff832
 * The XIM specification does not limit the number of window properties
Packit 3ff832
 * that can be used to transfer data, but Xlib uses the atom strings
Packit 3ff832
 * _client0 through _client20.
Packit 3ff832
 *
Packit 3ff832
 * So use that as a sensible initial size for the offset cache.
Packit 3ff832
 */
Packit 3ff832
#define INITIAL_OFFSET_CACHE_CAPACITY 21
Packit 3ff832
#define OFFSET_CACHE_GROWTH_FACTOR 2
Packit 3ff832
Packit 3ff832
void _Xi18nInitOffsetCache (Xi18nOffsetCache *offset_cache)
Packit 3ff832
{
Packit 3ff832
    offset_cache->size = 0;
Packit 3ff832
    offset_cache->capacity = INITIAL_OFFSET_CACHE_CAPACITY;
Packit 3ff832
    offset_cache->data = (Xi18nAtomOffsetPair *) malloc (
Packit 3ff832
        INITIAL_OFFSET_CACHE_CAPACITY * sizeof (Xi18nAtomOffsetPair));
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
unsigned long _Xi18nLookupPropertyOffset (Xi18nOffsetCache *offset_cache,
Packit 3ff832
                                          Atom key)
Packit 3ff832
{
Packit 3ff832
    Xi18nAtomOffsetPair *data;
Packit 3ff832
    size_t i;
Packit 3ff832
Packit 3ff832
    assert (offset_cache);
Packit 3ff832
    data = offset_cache->data;
Packit 3ff832
    for (i = 0; i < offset_cache->size; ++i) {
Packit 3ff832
        if (data[i].key == key) {
Packit 3ff832
            return data[i].offset;
Packit 3ff832
        }
Packit 3ff832
    }
Packit 3ff832
Packit 3ff832
    return 0;
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
void _Xi18nSetPropertyOffset (Xi18nOffsetCache *offset_cache, Atom key,
Packit 3ff832
                              unsigned long offset)
Packit 3ff832
{
Packit 3ff832
    Xi18nAtomOffsetPair *data = offset_cache->data;
Packit 3ff832
    size_t i;
Packit 3ff832
Packit 3ff832
    assert (data != NULL);
Packit 3ff832
    for (i = 0; i < offset_cache->size; ++i) {
Packit 3ff832
        if (data[i].key == key) {
Packit 3ff832
            data[i].offset = offset;
Packit 3ff832
            return;
Packit 3ff832
        }
Packit 3ff832
    }
Packit 3ff832
Packit 3ff832
    if (++offset_cache->size > offset_cache->capacity) {
Packit 3ff832
        offset_cache->capacity *= OFFSET_CACHE_GROWTH_FACTOR;
Packit 3ff832
        offset_cache->data = (Xi18nAtomOffsetPair *) realloc (data,
Packit 3ff832
                offset_cache->capacity * sizeof (Xi18nAtomOffsetPair));
Packit 3ff832
        if (offset_cache->data == NULL) {
Packit 3ff832
            offset_cache->data = data;
Packit 3ff832
            --offset_cache->size;
Packit 3ff832
        }
Packit 3ff832
        data = offset_cache->data;
Packit 3ff832
    }
Packit 3ff832
Packit 3ff832
    assert (data != NULL);
Packit 3ff832
    if (offset_cache->size > 0) {
Packit 3ff832
        data[i].key = key;
Packit 3ff832
        data[i].offset = offset;
Packit 3ff832
    }
Packit 3ff832
}
Packit 3ff832