Move containers to standalone file
This commit is contained in:
		@@ -21,7 +21,8 @@ LOCAL_SRC_FILES:= \
 | 
			
		||||
    themes/multirom_ui_landscape.c \
 | 
			
		||||
    themes/multirom_ui_portrait.c \
 | 
			
		||||
    fstab.c \
 | 
			
		||||
    workers.c
 | 
			
		||||
    workers.c \
 | 
			
		||||
    containers.c
 | 
			
		||||
 | 
			
		||||
ifeq ($(ARCH_ARM_HAVE_NEON),true)
 | 
			
		||||
    LOCAL_SRC_FILES += col32cb16blend_neon.S
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										287
									
								
								containers.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										287
									
								
								containers.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,287 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2008 The Android Open Source Project
 | 
			
		||||
 *
 | 
			
		||||
 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
 * you may not use this file except in compliance with the License.
 | 
			
		||||
 * You may obtain a copy of the License at
 | 
			
		||||
 *
 | 
			
		||||
 *      http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
 | 
			
		||||
#include "containers.h"
 | 
			
		||||
#include "util.h"
 | 
			
		||||
 | 
			
		||||
int list_item_count(listItself list)
 | 
			
		||||
{
 | 
			
		||||
    void **l = (void**)list;
 | 
			
		||||
    int i = 0;
 | 
			
		||||
    while(l && l[i])
 | 
			
		||||
        ++i;
 | 
			
		||||
    return i;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int list_size(listItself list)
 | 
			
		||||
{
 | 
			
		||||
    return list_item_count(list)+1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void list_add(void *item, ptrToList list_p)
 | 
			
		||||
{
 | 
			
		||||
    void ***list = (void***)list_p;
 | 
			
		||||
 | 
			
		||||
    int i = 0;
 | 
			
		||||
    while(*list && (*list)[i])
 | 
			
		||||
        ++i;
 | 
			
		||||
    i += 2; // NULL and the new item
 | 
			
		||||
 | 
			
		||||
    *list = realloc(*list, i*sizeof(item));
 | 
			
		||||
 | 
			
		||||
    (*list)[--i] = NULL;
 | 
			
		||||
    (*list)[--i] = item;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int list_add_from_list(listItself src_p, ptrToList list_p)
 | 
			
		||||
{
 | 
			
		||||
    void **src = (void**)src_p;
 | 
			
		||||
    void ***list = (void***)list_p;
 | 
			
		||||
    int i, len_src = 0, len_list = 0;
 | 
			
		||||
 | 
			
		||||
    while(src && src[len_src])
 | 
			
		||||
        ++len_src;
 | 
			
		||||
 | 
			
		||||
    if(len_src == 0)
 | 
			
		||||
        return 0;
 | 
			
		||||
 | 
			
		||||
    while(*list && (*list)[len_list])
 | 
			
		||||
        ++len_list;
 | 
			
		||||
 | 
			
		||||
    ++len_src; // for NULL
 | 
			
		||||
    *list = realloc(*list, (len_list+len_src)*sizeof(void*));
 | 
			
		||||
 | 
			
		||||
    for(i = 0; i < len_src; ++i)
 | 
			
		||||
        (*list)[i+len_list] = src[i];
 | 
			
		||||
    return len_src-1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int list_rm_opt(int reorder, void *item, ptrToList list_p, callback destroy_callback_p)
 | 
			
		||||
{
 | 
			
		||||
    void ***list = (void***)list_p;
 | 
			
		||||
    callbackPtr destroy_callback = (callbackPtr)destroy_callback_p;
 | 
			
		||||
 | 
			
		||||
    int size = list_size(*list);
 | 
			
		||||
 | 
			
		||||
    int i;
 | 
			
		||||
    for(i = 0; *list && (*list)[i]; ++i)
 | 
			
		||||
    {
 | 
			
		||||
        if((*list)[i] != item)
 | 
			
		||||
            continue;
 | 
			
		||||
 | 
			
		||||
        if(destroy_callback)
 | 
			
		||||
            (*destroy_callback)(item);
 | 
			
		||||
 | 
			
		||||
        --size;
 | 
			
		||||
        if(size == 1)
 | 
			
		||||
        {
 | 
			
		||||
            free(*list);
 | 
			
		||||
            *list = NULL;
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if(i != size-1)
 | 
			
		||||
        {
 | 
			
		||||
            if(reorder)
 | 
			
		||||
                (*list)[i] = (*list)[size-1];
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                for(; *list && (*list)[i]; ++i)
 | 
			
		||||
                    (*list)[i] = (*list)[i+1];
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        *list= realloc(*list, size*sizeof(item));
 | 
			
		||||
        (*list)[size-1] = NULL;
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int list_rm(void *item, ptrToList list_p, callback destroy_callback_p)
 | 
			
		||||
{
 | 
			
		||||
    return list_rm_opt(1, item, list_p, destroy_callback_p);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int list_rm_noreorder(void *item, ptrToList list_p, callback destroy_callback_p)
 | 
			
		||||
{
 | 
			
		||||
    return list_rm_opt(0, item, list_p, destroy_callback_p);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int list_rm_at(int idx, ptrToList list_p, callback destroy_callback_p)
 | 
			
		||||
{
 | 
			
		||||
    void ***list = (void***)list_p;
 | 
			
		||||
    callbackPtr destroy_callback = (callbackPtr)destroy_callback_p;
 | 
			
		||||
 | 
			
		||||
    int size = list_size(*list);
 | 
			
		||||
    if(idx < 0 || idx >= size-1)
 | 
			
		||||
        return -1;
 | 
			
		||||
 | 
			
		||||
    void *item = (*list)[idx];
 | 
			
		||||
    if(destroy_callback)
 | 
			
		||||
        (*destroy_callback)(item);
 | 
			
		||||
 | 
			
		||||
    --size;
 | 
			
		||||
    if(size == 1)
 | 
			
		||||
    {
 | 
			
		||||
        free(*list);
 | 
			
		||||
        *list = NULL;
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    int i = idx;
 | 
			
		||||
    for(; i < size; ++i)
 | 
			
		||||
        (*list)[i] = (*list)[i+1];
 | 
			
		||||
 | 
			
		||||
    *list= realloc(*list, size*sizeof(item));
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void list_clear(ptrToList list_p, callback destroy_callback_p)
 | 
			
		||||
{
 | 
			
		||||
    void ***list = (void***)list_p;
 | 
			
		||||
    callbackPtr destroy_callback = (callbackPtr)destroy_callback_p;
 | 
			
		||||
 | 
			
		||||
    if(*list == NULL)
 | 
			
		||||
        return;
 | 
			
		||||
 | 
			
		||||
    if(destroy_callback)
 | 
			
		||||
    {
 | 
			
		||||
        int i;
 | 
			
		||||
        for(i = 0; *list && (*list)[i]; ++i)
 | 
			
		||||
            (*destroy_callback)((*list)[i]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    free(*list);
 | 
			
		||||
    *list = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int list_copy(listItself src, ptrToList dest_p)
 | 
			
		||||
{
 | 
			
		||||
    void **source = (void**)src;
 | 
			
		||||
    void ***dest = (void***)dest_p;
 | 
			
		||||
 | 
			
		||||
    if(!source)
 | 
			
		||||
        return 0;
 | 
			
		||||
 | 
			
		||||
    if(*dest)
 | 
			
		||||
        return -1;
 | 
			
		||||
 | 
			
		||||
    int size = list_size(source);
 | 
			
		||||
    *dest = calloc(size, sizeof(*source));
 | 
			
		||||
 | 
			
		||||
    int i;
 | 
			
		||||
    for(i = 0; source[i]; ++i)
 | 
			
		||||
        (*dest)[i] = source[i];
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int list_move(ptrToList source_p, ptrToList dest_p)
 | 
			
		||||
{
 | 
			
		||||
    void ***source = (void***)source_p;
 | 
			
		||||
    void ***dest = (void***)dest_p;
 | 
			
		||||
 | 
			
		||||
    if(!source)
 | 
			
		||||
        return 0;
 | 
			
		||||
 | 
			
		||||
    if(*dest)
 | 
			
		||||
        return -1;
 | 
			
		||||
 | 
			
		||||
    *dest = *source;
 | 
			
		||||
    *source = NULL;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void list_swap(ptrToList a_p, ptrToList b_p)
 | 
			
		||||
{
 | 
			
		||||
    void ***a = (void***)a_p;
 | 
			
		||||
    void ***b = (void***)b_p;
 | 
			
		||||
    void **tmp = *a;
 | 
			
		||||
    *a = *b;
 | 
			
		||||
    *b = tmp;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
map *map_create(void)
 | 
			
		||||
{
 | 
			
		||||
    map *m = mzalloc(sizeof(map));
 | 
			
		||||
    return m;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void map_destroy(map *m, void (*destroy_callback)(void*))
 | 
			
		||||
{
 | 
			
		||||
    if(!m)
 | 
			
		||||
        return;
 | 
			
		||||
 | 
			
		||||
    list_clear(&m->keys, &free);
 | 
			
		||||
    list_clear(&m->values, destroy_callback);
 | 
			
		||||
    free(m);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void map_add(map *m, char *key, void *val, void (*destroy_callback)(void*))
 | 
			
		||||
{
 | 
			
		||||
    int idx = map_find(m, key);
 | 
			
		||||
    if(idx >= 0)
 | 
			
		||||
    {
 | 
			
		||||
        if(destroy_callback)
 | 
			
		||||
            (*destroy_callback)(m->values[idx]);
 | 
			
		||||
        m->values[idx] = val;
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
        map_add_not_exist(m, key, val);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void map_add_not_exist(map *m, char *key, void *val)
 | 
			
		||||
{
 | 
			
		||||
    list_add(strdup(key), &m->keys);
 | 
			
		||||
    list_add(val, &m->values);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void map_rm(map *m, char *key, void (*destroy_callback)(void*))
 | 
			
		||||
{
 | 
			
		||||
    int idx = map_find(m, key);
 | 
			
		||||
    if(idx < 0)
 | 
			
		||||
        return;
 | 
			
		||||
 | 
			
		||||
    list_rm_at(idx, &m->keys, &free);
 | 
			
		||||
    list_rm_at(idx, &m->values, destroy_callback);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int map_find(map *m, char *key)
 | 
			
		||||
{
 | 
			
		||||
    int i;
 | 
			
		||||
    for(i = 0; m->keys && m->keys[i]; ++i)
 | 
			
		||||
        if(strcmp(m->keys[i], key) == 0)
 | 
			
		||||
            return i;
 | 
			
		||||
    return -1; 
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void *map_get_val(map *m, char *key)
 | 
			
		||||
{
 | 
			
		||||
    int idx = map_find(m, key);
 | 
			
		||||
    if(idx < 0)
 | 
			
		||||
        return NULL;
 | 
			
		||||
    return m->values[idx];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void *map_get_ref(map *m, char *key)
 | 
			
		||||
{
 | 
			
		||||
    int idx = map_find(m, key);
 | 
			
		||||
    if(idx < 0)
 | 
			
		||||
        return NULL;
 | 
			
		||||
    return &m->values[idx];
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										55
									
								
								containers.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								containers.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,55 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2010 The Android Open Source Project
 | 
			
		||||
 *
 | 
			
		||||
 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
 * you may not use this file except in compliance with the License.
 | 
			
		||||
 * You may obtain a copy of the License at
 | 
			
		||||
 *
 | 
			
		||||
 *      http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef CONTAINERS_H
 | 
			
		||||
#define CONTAINERS_H
 | 
			
		||||
 | 
			
		||||
// auto-conversion of pointer type occurs only for
 | 
			
		||||
// void*, not for void** nor void***
 | 
			
		||||
typedef void* ptrToList; // void ***
 | 
			
		||||
typedef void* listItself; // void **
 | 
			
		||||
typedef void* callback;
 | 
			
		||||
typedef void(*callbackPtr)(void*);
 | 
			
		||||
 | 
			
		||||
void list_add(void *item, ptrToList list_p);
 | 
			
		||||
int list_add_from_list(listItself src_p, ptrToList list_p);
 | 
			
		||||
int list_rm(void *item, ptrToList list_p, callback destroy_callback_p);
 | 
			
		||||
int list_rm_noreorder(void *item, ptrToList list_p, callback destroy_callback_p);
 | 
			
		||||
int list_rm_opt(int reorder, void *item, ptrToList list_p, callback destroy_callback_p);
 | 
			
		||||
int list_rm_at(int idx, ptrToList list_p, callback destroy_callback_p);
 | 
			
		||||
int list_size(listItself list);
 | 
			
		||||
int list_item_count(listItself list);
 | 
			
		||||
int list_copy(listItself src, ptrToList dest_p);
 | 
			
		||||
int list_move(ptrToList source_p, ptrToList dest_p);
 | 
			
		||||
void list_clear(ptrToList list_p, callback destroy_callback_p);
 | 
			
		||||
void list_swap(ptrToList a_p, ptrToList b_p);
 | 
			
		||||
 | 
			
		||||
typedef struct
 | 
			
		||||
{
 | 
			
		||||
    char **keys;
 | 
			
		||||
    void **values;
 | 
			
		||||
} map;
 | 
			
		||||
 | 
			
		||||
map *map_create(void);
 | 
			
		||||
void map_destroy(map *m, void (*destroy_callback)(void*));
 | 
			
		||||
void map_add(map *m, char *key, void *val, void (*destroy_callback)(void*));
 | 
			
		||||
void map_add_not_exist(map *m, char *key, void *val);
 | 
			
		||||
void map_rm(map *m, char *key, void (*destroy_callback)(void*));
 | 
			
		||||
int map_find(map *m, char *key);
 | 
			
		||||
void *map_get_val(map *m, char *key);
 | 
			
		||||
void *map_get_ref(map *m, char *key);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
@@ -36,6 +36,7 @@
 | 
			
		||||
#include "framebuffer.h"
 | 
			
		||||
#include "iso_font.h"
 | 
			
		||||
#include "util.h"
 | 
			
		||||
#include "containers.h"
 | 
			
		||||
 | 
			
		||||
// only double-buffering is implemented, this define is just
 | 
			
		||||
// for the code to know how many buffers we use
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										1
									
								
								fstab.c
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								fstab.c
									
									
									
									
									
								
							@@ -26,6 +26,7 @@
 | 
			
		||||
#include "fstab.h"
 | 
			
		||||
#include "util.h"
 | 
			
		||||
#include "log.h"
 | 
			
		||||
#include "containers.h"
 | 
			
		||||
 | 
			
		||||
// flags from system/core/fs_mgr/fs_mgr.c
 | 
			
		||||
struct flag_list {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										1
									
								
								input.c
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								input.c
									
									
									
									
									
								
							@@ -34,6 +34,7 @@
 | 
			
		||||
#include "util.h"
 | 
			
		||||
#include "log.h"
 | 
			
		||||
#include "workers.h"
 | 
			
		||||
#include "containers.h"
 | 
			
		||||
 | 
			
		||||
// for touch calculation
 | 
			
		||||
int mt_screen_res[2] = { 0 };
 | 
			
		||||
 
 | 
			
		||||
@@ -41,6 +41,7 @@
 | 
			
		||||
#include "util.h"
 | 
			
		||||
#include "version.h"
 | 
			
		||||
#include "hooks.h"
 | 
			
		||||
#include "containers.h"
 | 
			
		||||
 | 
			
		||||
#define REALDATA "/realdata"
 | 
			
		||||
#define BUSYBOX_BIN "busybox"
 | 
			
		||||
 
 | 
			
		||||
@@ -21,8 +21,8 @@
 | 
			
		||||
#include <pthread.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
#include "util.h"
 | 
			
		||||
#include "fstab.h"
 | 
			
		||||
#include "containers.h"
 | 
			
		||||
 | 
			
		||||
enum
 | 
			
		||||
{
 | 
			
		||||
 
 | 
			
		||||
@@ -37,7 +37,7 @@
 | 
			
		||||
#include "multirom_ui_themes.h"
 | 
			
		||||
#include "workers.h"
 | 
			
		||||
#include "hooks.h"
 | 
			
		||||
 | 
			
		||||
#include "containers.h"
 | 
			
		||||
 | 
			
		||||
static struct multirom_status *mrom_status = NULL;
 | 
			
		||||
static struct multirom_rom *selected_rom = NULL;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										1
									
								
								pong.c
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								pong.c
									
									
									
									
									
								
							@@ -26,6 +26,7 @@
 | 
			
		||||
#include "input.h"
 | 
			
		||||
#include "pong.h"
 | 
			
		||||
#include "util.h"
 | 
			
		||||
#include "containers.h"
 | 
			
		||||
 | 
			
		||||
#define SCORE_SPACE (75*DPI_MUL)
 | 
			
		||||
#define L 0
 | 
			
		||||
 
 | 
			
		||||
@@ -19,6 +19,7 @@
 | 
			
		||||
#include "progressdots.h"
 | 
			
		||||
#include "multirom_ui.h"
 | 
			
		||||
#include "workers.h"
 | 
			
		||||
#include "util.h"
 | 
			
		||||
 | 
			
		||||
// ms
 | 
			
		||||
#define SWITCH_SPEED 800
 | 
			
		||||
 
 | 
			
		||||
@@ -7,7 +7,8 @@ LOCAL_SRC_FILES:= \
 | 
			
		||||
    devices.c \
 | 
			
		||||
    ../util.c \
 | 
			
		||||
    adb.c \
 | 
			
		||||
    ../fstab.c
 | 
			
		||||
    ../fstab.c \
 | 
			
		||||
    ../containers.c
 | 
			
		||||
 | 
			
		||||
LOCAL_MODULE:= trampoline
 | 
			
		||||
LOCAL_MODULE_TAGS := eng
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										267
									
								
								util.c
									
									
									
									
									
								
							
							
						
						
									
										267
									
								
								util.c
									
									
									
									
									
								
							@@ -511,270 +511,3 @@ void *mzalloc(size_t size)
 | 
			
		||||
    memset(res, 0, size);
 | 
			
		||||
    return res;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int list_item_count(listItself list)
 | 
			
		||||
{
 | 
			
		||||
    void **l = (void**)list;
 | 
			
		||||
    int i = 0;
 | 
			
		||||
    while(l && l[i])
 | 
			
		||||
        ++i;
 | 
			
		||||
    return i;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int list_size(listItself list)
 | 
			
		||||
{
 | 
			
		||||
    return list_item_count(list)+1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void list_add(void *item, ptrToList list_p)
 | 
			
		||||
{
 | 
			
		||||
    void ***list = (void***)list_p;
 | 
			
		||||
 | 
			
		||||
    int i = 0;
 | 
			
		||||
    while(*list && (*list)[i])
 | 
			
		||||
        ++i;
 | 
			
		||||
    i += 2; // NULL and the new item
 | 
			
		||||
 | 
			
		||||
    *list = realloc(*list, i*sizeof(item));
 | 
			
		||||
 | 
			
		||||
    (*list)[--i] = NULL;
 | 
			
		||||
    (*list)[--i] = item;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int list_add_from_list(listItself src_p, ptrToList list_p)
 | 
			
		||||
{
 | 
			
		||||
    void **src = (void**)src_p;
 | 
			
		||||
    void ***list = (void***)list_p;
 | 
			
		||||
    int i, len_src = 0, len_list = 0;
 | 
			
		||||
 | 
			
		||||
    while(src && src[len_src])
 | 
			
		||||
        ++len_src;
 | 
			
		||||
 | 
			
		||||
    if(len_src == 0)
 | 
			
		||||
        return 0;
 | 
			
		||||
 | 
			
		||||
    while(*list && (*list)[len_list])
 | 
			
		||||
        ++len_list;
 | 
			
		||||
 | 
			
		||||
    ++len_src; // for NULL
 | 
			
		||||
    *list = realloc(*list, (len_list+len_src)*sizeof(void*));
 | 
			
		||||
 | 
			
		||||
    for(i = 0; i < len_src; ++i)
 | 
			
		||||
        (*list)[i+len_list] = src[i];
 | 
			
		||||
    return len_src-1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int list_rm_opt(int reorder, void *item, ptrToList list_p, callback destroy_callback_p)
 | 
			
		||||
{
 | 
			
		||||
    void ***list = (void***)list_p;
 | 
			
		||||
    callbackPtr destroy_callback = (callbackPtr)destroy_callback_p;
 | 
			
		||||
 | 
			
		||||
    int size = list_size(*list);
 | 
			
		||||
 | 
			
		||||
    int i;
 | 
			
		||||
    for(i = 0; *list && (*list)[i]; ++i)
 | 
			
		||||
    {
 | 
			
		||||
        if((*list)[i] != item)
 | 
			
		||||
            continue;
 | 
			
		||||
 | 
			
		||||
        if(destroy_callback)
 | 
			
		||||
            (*destroy_callback)(item);
 | 
			
		||||
 | 
			
		||||
        --size;
 | 
			
		||||
        if(size == 1)
 | 
			
		||||
        {
 | 
			
		||||
            free(*list);
 | 
			
		||||
            *list = NULL;
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if(i != size-1)
 | 
			
		||||
        {
 | 
			
		||||
            if(reorder)
 | 
			
		||||
                (*list)[i] = (*list)[size-1];
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                for(; *list && (*list)[i]; ++i)
 | 
			
		||||
                    (*list)[i] = (*list)[i+1];
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        *list= realloc(*list, size*sizeof(item));
 | 
			
		||||
        (*list)[size-1] = NULL;
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int list_rm(void *item, ptrToList list_p, callback destroy_callback_p)
 | 
			
		||||
{
 | 
			
		||||
    return list_rm_opt(1, item, list_p, destroy_callback_p);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int list_rm_noreorder(void *item, ptrToList list_p, callback destroy_callback_p)
 | 
			
		||||
{
 | 
			
		||||
    return list_rm_opt(0, item, list_p, destroy_callback_p);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int list_rm_at(int idx, ptrToList list_p, callback destroy_callback_p)
 | 
			
		||||
{
 | 
			
		||||
    void ***list = (void***)list_p;
 | 
			
		||||
    callbackPtr destroy_callback = (callbackPtr)destroy_callback_p;
 | 
			
		||||
 | 
			
		||||
    int size = list_size(*list);
 | 
			
		||||
    if(idx < 0 || idx >= size-1)
 | 
			
		||||
        return -1;
 | 
			
		||||
 | 
			
		||||
    void *item = (*list)[idx];
 | 
			
		||||
    if(destroy_callback)
 | 
			
		||||
        (*destroy_callback)(item);
 | 
			
		||||
 | 
			
		||||
    --size;
 | 
			
		||||
    if(size == 1)
 | 
			
		||||
    {
 | 
			
		||||
        free(*list);
 | 
			
		||||
        *list = NULL;
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    int i = idx;
 | 
			
		||||
    for(; i < size; ++i)
 | 
			
		||||
        (*list)[i] = (*list)[i+1];
 | 
			
		||||
 | 
			
		||||
    *list= realloc(*list, size*sizeof(item));
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void list_clear(ptrToList list_p, callback destroy_callback_p)
 | 
			
		||||
{
 | 
			
		||||
    void ***list = (void***)list_p;
 | 
			
		||||
    callbackPtr destroy_callback = (callbackPtr)destroy_callback_p;
 | 
			
		||||
 | 
			
		||||
    if(*list == NULL)
 | 
			
		||||
        return;
 | 
			
		||||
 | 
			
		||||
    if(destroy_callback)
 | 
			
		||||
    {
 | 
			
		||||
        int i;
 | 
			
		||||
        for(i = 0; *list && (*list)[i]; ++i)
 | 
			
		||||
            (*destroy_callback)((*list)[i]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    free(*list);
 | 
			
		||||
    *list = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int list_copy(listItself src, ptrToList dest_p)
 | 
			
		||||
{
 | 
			
		||||
    void **source = (void**)src;
 | 
			
		||||
    void ***dest = (void***)dest_p;
 | 
			
		||||
 | 
			
		||||
    if(!source)
 | 
			
		||||
        return 0;
 | 
			
		||||
 | 
			
		||||
    if(*dest)
 | 
			
		||||
        return -1;
 | 
			
		||||
 | 
			
		||||
    int size = list_size(source);
 | 
			
		||||
    *dest = calloc(size, sizeof(*source));
 | 
			
		||||
 | 
			
		||||
    int i;
 | 
			
		||||
    for(i = 0; source[i]; ++i)
 | 
			
		||||
        (*dest)[i] = source[i];
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int list_move(ptrToList source_p, ptrToList dest_p)
 | 
			
		||||
{
 | 
			
		||||
    void ***source = (void***)source_p;
 | 
			
		||||
    void ***dest = (void***)dest_p;
 | 
			
		||||
 | 
			
		||||
    if(!source)
 | 
			
		||||
        return 0;
 | 
			
		||||
 | 
			
		||||
    if(*dest)
 | 
			
		||||
        return -1;
 | 
			
		||||
 | 
			
		||||
    *dest = *source;
 | 
			
		||||
    *source = NULL;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void list_swap(ptrToList a_p, ptrToList b_p)
 | 
			
		||||
{
 | 
			
		||||
    void ***a = (void***)a_p;
 | 
			
		||||
    void ***b = (void***)b_p;
 | 
			
		||||
    void **tmp = *a;
 | 
			
		||||
    *a = *b;
 | 
			
		||||
    *b = tmp;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
map *map_create(void)
 | 
			
		||||
{
 | 
			
		||||
    map *m = mzalloc(sizeof(map));
 | 
			
		||||
    return m;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void map_destroy(map *m, void (*destroy_callback)(void*))
 | 
			
		||||
{
 | 
			
		||||
    if(!m)
 | 
			
		||||
        return;
 | 
			
		||||
 | 
			
		||||
    list_clear(&m->keys, &free);
 | 
			
		||||
    list_clear(&m->values, destroy_callback);
 | 
			
		||||
    free(m);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void map_add(map *m, char *key, void *val, void (*destroy_callback)(void*))
 | 
			
		||||
{
 | 
			
		||||
    int idx = map_find(m, key);
 | 
			
		||||
    if(idx >= 0)
 | 
			
		||||
    {
 | 
			
		||||
        if(destroy_callback)
 | 
			
		||||
            (*destroy_callback)(m->values[idx]);
 | 
			
		||||
        m->values[idx] = val;
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
        map_add_not_exist(m, key, val);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void map_add_not_exist(map *m, char *key, void *val)
 | 
			
		||||
{
 | 
			
		||||
    list_add(strdup(key), &m->keys);
 | 
			
		||||
    list_add(val, &m->values);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void map_rm(map *m, char *key, void (*destroy_callback)(void*))
 | 
			
		||||
{
 | 
			
		||||
    int idx = map_find(m, key);
 | 
			
		||||
    if(idx < 0)
 | 
			
		||||
        return;
 | 
			
		||||
 | 
			
		||||
    list_rm_at(idx, &m->keys, &free);
 | 
			
		||||
    list_rm_at(idx, &m->values, destroy_callback);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int map_find(map *m, char *key)
 | 
			
		||||
{
 | 
			
		||||
    int i;
 | 
			
		||||
    for(i = 0; m->keys && m->keys[i]; ++i)
 | 
			
		||||
        if(strcmp(m->keys[i], key) == 0)
 | 
			
		||||
            return i;
 | 
			
		||||
    return -1; 
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void *map_get_val(map *m, char *key)
 | 
			
		||||
{
 | 
			
		||||
    int idx = map_find(m, key);
 | 
			
		||||
    if(idx < 0)
 | 
			
		||||
        return NULL;
 | 
			
		||||
    return m->values[idx];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void *map_get_ref(map *m, char *key)
 | 
			
		||||
{
 | 
			
		||||
    int idx = map_find(m, key);
 | 
			
		||||
    if(idx < 0)
 | 
			
		||||
        return NULL;
 | 
			
		||||
    return &m->values[idx];
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										36
									
								
								util.h
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								util.h
									
									
									
									
									
								
							@@ -47,40 +47,4 @@ inline int imax(int a, int b);
 | 
			
		||||
inline int in_rect(int x, int y, int rx, int ry, int rw, int rh);
 | 
			
		||||
inline void *mzalloc(size_t size); // alloc and fill with 0s
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// auto-conversion of pointer type occurs only for
 | 
			
		||||
// void*, not for void** nor void***
 | 
			
		||||
typedef void* ptrToList; // void ***
 | 
			
		||||
typedef void* listItself; // void **
 | 
			
		||||
typedef void* callback;
 | 
			
		||||
typedef void(*callbackPtr)(void*);
 | 
			
		||||
 | 
			
		||||
void list_add(void *item, ptrToList list_p);
 | 
			
		||||
int list_add_from_list(listItself src_p, ptrToList list_p);
 | 
			
		||||
int list_rm(void *item, ptrToList list_p, callback destroy_callback_p);
 | 
			
		||||
int list_rm_noreorder(void *item, ptrToList list_p, callback destroy_callback_p);
 | 
			
		||||
int list_rm_opt(int reorder, void *item, ptrToList list_p, callback destroy_callback_p);
 | 
			
		||||
int list_rm_at(int idx, ptrToList list_p, callback destroy_callback_p);
 | 
			
		||||
int list_size(listItself list);
 | 
			
		||||
int list_item_count(listItself list);
 | 
			
		||||
int list_copy(listItself src, ptrToList dest_p);
 | 
			
		||||
int list_move(ptrToList source_p, ptrToList dest_p);
 | 
			
		||||
void list_clear(ptrToList list_p, callback destroy_callback_p);
 | 
			
		||||
void list_swap(ptrToList a_p, ptrToList b_p);
 | 
			
		||||
 | 
			
		||||
typedef struct
 | 
			
		||||
{
 | 
			
		||||
    char **keys;
 | 
			
		||||
    void **values;
 | 
			
		||||
} map;
 | 
			
		||||
 | 
			
		||||
map *map_create(void);
 | 
			
		||||
void map_destroy(map *m, void (*destroy_callback)(void*));
 | 
			
		||||
void map_add(map *m, char *key, void *val, void (*destroy_callback)(void*));
 | 
			
		||||
void map_add_not_exist(map *m, char *key, void *val);
 | 
			
		||||
void map_rm(map *m, char *key, void (*destroy_callback)(void*));
 | 
			
		||||
int map_find(map *m, char *key);
 | 
			
		||||
void *map_get_val(map *m, char *key);
 | 
			
		||||
void *map_get_ref(map *m, char *key);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user