xrdp/common/list.c

227 lines
5.1 KiB
C
Raw Normal View History

/**
* xrdp: A Remote Desktop Protocol server.
*
* Copyright (C) Jay Sorg 2004-2012
*
* 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.
*
* simple list
*/
2005-06-28 07:04:36 +04:00
#include "arch.h"
#include "os_calls.h"
#include "list.h"
/*****************************************************************************/
struct list *APP_CC
2005-06-28 07:04:36 +04:00
list_create(void)
{
struct list *self;
2005-06-28 07:04:36 +04:00
self = (struct list *)g_malloc(sizeof(struct list), 1);
self->grow_by = 10;
self->alloc_size = 10;
self->items = (tbus *)g_malloc(sizeof(tbus) * 10, 1);
return self;
2005-06-28 07:04:36 +04:00
}
/*****************************************************************************/
void APP_CC
list_delete(struct list *self)
2005-06-28 07:04:36 +04:00
{
int i;
if (self == 0)
{
return;
}
if (self->auto_free)
2005-06-28 07:04:36 +04:00
{
for (i = 0; i < self->count; i++)
{
g_free((void *)self->items[i]);
self->items[i] = 0;
}
2005-06-28 07:04:36 +04:00
}
g_free(self->items);
g_free(self);
2005-06-28 07:04:36 +04:00
}
/*****************************************************************************/
void APP_CC
list_add_item(struct list *self, tbus item)
2005-06-28 07:04:36 +04:00
{
tbus *p;
int i;
if (self->count >= self->alloc_size)
{
i = self->alloc_size;
self->alloc_size += self->grow_by;
p = (tbus *)g_malloc(sizeof(tbus) * self->alloc_size, 1);
g_memcpy(p, self->items, sizeof(tbus) * i);
g_free(self->items);
self->items = p;
}
self->items[self->count] = item;
self->count++;
2005-06-28 07:04:36 +04:00
}
/*****************************************************************************/
2007-11-03 09:03:46 +03:00
tbus APP_CC
list_get_item(struct list *self, int index)
2005-06-28 07:04:36 +04:00
{
if (index < 0 || index >= self->count)
{
return 0;
}
return self->items[index];
2005-06-28 07:04:36 +04:00
}
/*****************************************************************************/
void APP_CC
list_clear(struct list *self)
2005-06-28 07:04:36 +04:00
{
int i;
2005-06-28 07:04:36 +04:00
if (self->auto_free)
2005-06-28 07:04:36 +04:00
{
for (i = 0; i < self->count; i++)
{
g_free((void *)self->items[i]);
self->items[i] = 0;
}
2005-06-28 07:04:36 +04:00
}
g_free(self->items);
self->count = 0;
self->grow_by = 10;
self->alloc_size = 10;
self->items = (tbus *)g_malloc(sizeof(tbus) * 10, 1);
2005-06-28 07:04:36 +04:00
}
/*****************************************************************************/
int APP_CC
list_index_of(struct list *self, tbus item)
2005-06-28 07:04:36 +04:00
{
int i;
2005-06-28 07:04:36 +04:00
for (i = 0; i < self->count; i++)
2005-06-28 07:04:36 +04:00
{
if (self->items[i] == item)
{
return i;
}
2005-06-28 07:04:36 +04:00
}
return -1;
2005-06-28 07:04:36 +04:00
}
/*****************************************************************************/
void APP_CC
list_remove_item(struct list *self, int index)
2005-06-28 07:04:36 +04:00
{
int i;
2005-06-28 07:04:36 +04:00
if (index >= 0 && index < self->count)
2005-06-28 07:04:36 +04:00
{
if (self->auto_free)
{
g_free((void *)self->items[index]);
self->items[index] = 0;
}
for (i = index; i < (self->count - 1); i++)
{
self->items[i] = self->items[i + 1];
}
self->count--;
2005-06-28 07:04:36 +04:00
}
}
/*****************************************************************************/
void APP_CC
list_insert_item(struct list *self, int index, tbus item)
2005-06-28 07:04:36 +04:00
{
tbus *p;
int i;
if (index == self->count)
2005-06-28 07:04:36 +04:00
{
list_add_item(self, item);
return;
2005-06-28 07:04:36 +04:00
}
if (index >= 0 && index < self->count)
2005-06-28 07:04:36 +04:00
{
self->count++;
if (self->count > self->alloc_size)
{
i = self->alloc_size;
self->alloc_size += self->grow_by;
p = (tbus *)g_malloc(sizeof(tbus) * self->alloc_size, 1);
g_memcpy(p, self->items, sizeof(tbus) * i);
g_free(self->items);
self->items = p;
}
for (i = (self->count - 2); i >= index; i--)
{
self->items[i + 1] = self->items[i];
}
self->items[index] = item;
2005-06-28 07:04:36 +04:00
}
}
2006-09-18 08:35:10 +04:00
/*****************************************************************************/
/* append one list to another using strdup for each item in the list */
/* begins copy at start_index, a zero based index on the soure list */
void APP_CC
list_append_list_strdup(struct list *self, struct list *dest, int start_index)
2006-09-18 08:35:10 +04:00
{
int index;
tbus item;
char *dup;
for (index = start_index; index < self->count; index++)
{
item = list_get_item(self, index);
dup = g_strdup((char *)item);
list_add_item(dest, (tbus)dup);
}
2006-09-18 08:35:10 +04:00
}
2009-08-05 10:16:05 +04:00
/*****************************************************************************/
void APP_CC
list_dump_items(struct list *self)
2009-08-05 10:16:05 +04:00
{
int index;
if (self->count == 0)
{
g_writeln("List is empty");
}
for (index = 0; index < self->count; index++)
{
g_writeln("%d: %s", index, list_get_item(self, index));
}
2009-08-05 10:16:05 +04:00
}