1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
/********************* list.c **********************/
/********************* 1999.6.13 *******************/
#include "list.h"
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <glib.h>
#define LIST_INIT_SIZE 15
List::List()
{
items = NULL;
buffer_length = 0;
count = 0;
};
List::~List()
{
if(items) free(items);
};
void List::add_item(void * item)
{
if(!items) {
items = (void **)malloc((size_t)sizeof(void*)*LIST_INIT_SIZE);
if(!items) {
perror("class List: can not malloc mem when init:");
exit(1);
}
buffer_length = LIST_INIT_SIZE;
}
else if(buffer_length == count){
items = (void **)realloc((void*)items,sizeof(void *)*(buffer_length+=buffer_length));
if(!items){
perror("class List: mem not enought when realloc:");
exit(1);
}
}
items[count++] = item;
}
void * List::get_item(int handle)
{
if(handle >= count || handle < 0) return NULL;
return items[handle];
}
int List::reset_item(int handle, void * data)
{
if(handle >= count || handle < 0) return 1;
items[handle] = data;
return 0;
}
int List::search_item(void * item)
{
int i;
for (i = 0;i < count && items[i] != item;i++);
return (i == count)? -1:i;
}
int List::delete_item(int handle)
{
g_return_val_if_fail(handle < count && handle >= 0, 1);
if(handle != count-1) memmove(items+handle,
items+(handle+1),
(count-handle-1)*sizeof(void*));
count--;
return 0;
}
void List::insert_item(int handle, void * item)
{
int i;
if(!items) {
items = (void **)malloc(sizeof(void*)*LIST_INIT_SIZE);
if(!items) {
perror("class List: can not malloc mem when init:");
exit(1);
}
buffer_length = LIST_INIT_SIZE;
}
else if(buffer_length == count){
items = (void **)realloc((void*)items,sizeof(void *)*(buffer_length+=buffer_length));
if(!items){
perror("class List: mem not enought when realloc:");
exit(1);
}
}
if(handle >= count)
items[count++] = item;
else {
handle = handle <0? 0:handle;
for(i = count++;i>handle;i--)
items[i] = items[i-1];
items[handle] = item;
}
}
void List::delete_all()
{
count = 0;
}
int List::get_items(void * buffer)
{
memcpy((void *)buffer,(void *)items,count*sizeof(void*));
return count;
}
int List::get_size()
{
return count;
}
int List::meet_end(int handle)
{
return (handle >= count)? 1:0;
}
/********************* class Dictionary ***************/
Dictionary::Dictionary()
{
names = new List;
values = new List;
}
Dictionary::~Dictionary()
{
delete(names);
delete(values);
}
void Dictionary::add_item(char * name,void * value)
{
int i;
if((i = search_item(name)) != -1) {
values->reset_item(i,value);
}
else {
names->add_item(name);
values->add_item(value);
}
}
int Dictionary::search_item(char * name)
{
int i,j;
j = names->get_size();
for(i = 0;i<j && strcmp(name,(char*)names->get_item(i));i++);
return (i==j)? -1:i;
}
int Dictionary::have_item(char * name)
{
return search_item(name) != -1;
}
void * Dictionary::get_value(char * name)
{
int i;
i = names->search_item(name);
return (i==-1)? NULL:values->get_item(i);
}
int Dictionary::get_size()
{
return names->get_size();
}
void * Dictionary::get_value(int i)
{
return values->get_item(i);
}
char * Dictionary::get_name(int i)
{
return (char*)names->get_item(i);
}
void Dictionary::set_value(int i, void * value)
{
values->reset_item(i,value);
}
void Dictionary::delete_item(int i)
{
names->delete_item(i);
values->delete_item(i);
}
void Dictionary::display_items()
{
int i,j;
j = names->get_size();
for(i = 0;i<j;i++)
printf("%s\n",names->get_item(i));
}
|