ant-http/lib/list.c

195 lines
4.0 KiB
C
Raw Permalink Normal View History

2015-11-24 17:58:32 +01:00
/*
The MIT License (MIT)
Copyright (c) 2015 LE Xuan Sang xsang.le@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
2015-10-22 11:39:11 +02:00
#include "list.h"
2019-12-15 12:10:06 +01:00
list_t list_init()
2015-10-22 11:39:11 +02:00
{
2019-12-15 12:10:06 +01:00
list_t ret = (list_t)malloc(sizeof *ret);
ret->type = LIST_TYPE_NIL;
2015-10-22 11:39:11 +02:00
ret->next = NULL;
return ret;
}
2019-12-15 12:10:06 +01:00
void list_put(list_t* l, item_t it)
2015-10-22 11:39:11 +02:00
{
2019-12-15 12:10:06 +01:00
if(*l == NULL || (*l)->type == LIST_TYPE_NIL)
2015-11-24 17:58:32 +01:00
{
2019-12-22 17:33:35 +01:00
if(*l != NULL)
free(*l);
2015-11-24 17:58:32 +01:00
*l = it;
return ;
}
2019-12-15 12:10:06 +01:00
item_t np = list_last(*l);
2015-11-24 17:58:32 +01:00
np->next = it;
2015-10-22 11:39:11 +02:00
}
2019-12-15 12:10:06 +01:00
void list_put_special(list_t* l, const char* str)
2015-10-22 11:39:11 +02:00
{
2019-12-15 12:10:06 +01:00
item_t v;
2015-11-24 17:58:32 +01:00
if(IS_INT(str))
2015-10-22 11:39:11 +02:00
{
2019-12-15 12:10:06 +01:00
v = list_item(LIST_TYPE_INT);
2015-11-24 17:58:32 +01:00
v->value.i = atoi(str);
}
else if(IS_FLOAT(str))
{
2019-12-15 12:10:06 +01:00
v = list_item(LIST_TYPE_DOUBLE);
2015-11-24 17:58:32 +01:00
v->value.d = (double)atof(str);
2015-10-22 11:39:11 +02:00
}
2015-11-24 17:58:32 +01:00
else
2015-10-22 11:39:11 +02:00
{
2019-12-22 17:33:35 +01:00
v = list_item(LIST_TYPE_POINTER);
v->value.ptr = strdup(str);
2015-10-22 11:39:11 +02:00
}
2015-11-24 17:58:32 +01:00
list_put(l,v);
2015-10-22 11:39:11 +02:00
}
2019-12-15 12:10:06 +01:00
item_t list_last(list_t l)
2015-10-22 11:39:11 +02:00
{
2019-12-15 12:10:06 +01:00
item_t p = l;
2015-11-24 17:58:32 +01:00
while(p && p->next != NULL)
p = p->next;
return p;
2015-10-22 11:39:11 +02:00
}
2019-12-22 17:33:35 +01:00
int list_remove(list_t* l,int idx)
2015-10-22 11:39:11 +02:00
{
2015-11-24 17:58:32 +01:00
if(l==NULL) return 0;
2019-12-22 17:33:35 +01:00
if(*l == NULL) return 0;
item_t it;
if(idx <0 || idx >= list_size(*l)) return 0;
2015-10-22 11:39:11 +02:00
if(idx == 0)
{
2019-12-22 17:33:35 +01:00
it = *l;
*l=(*l)->next;
it->next = NULL;
list_free(&it);
2015-10-22 11:39:11 +02:00
return 1;
}
2019-12-22 17:33:35 +01:00
item_t np = list_at(*l,idx-1);
2015-10-22 11:39:11 +02:00
if(np == NULL) return 0;
2019-12-22 17:33:35 +01:00
if(np->next != NULL)
{
item_t it = np->next;
it->next = NULL;
list_free(&it);
np->next = np->next->next;
}
2018-10-07 15:09:46 +02:00
return 1;
2015-10-22 11:39:11 +02:00
}
2019-12-15 12:10:06 +01:00
int list_size(list_t l)
2015-10-22 11:39:11 +02:00
{
2019-12-15 12:10:06 +01:00
if(l == NULL || l->type == LIST_TYPE_NIL) return 0;
2015-11-24 17:58:32 +01:00
int i=0;
2019-12-15 12:10:06 +01:00
item_t np = l;
2015-11-24 17:58:32 +01:00
while(np)
2015-10-22 11:39:11 +02:00
{
2015-11-24 17:58:32 +01:00
np = np->next;
i++;
2015-10-22 11:39:11 +02:00
}
2015-11-24 17:58:32 +01:00
return i;
2015-10-22 11:39:11 +02:00
}
2019-12-15 12:10:06 +01:00
item_t list_at(list_t l,int idx)
2015-10-22 11:39:11 +02:00
{
2015-11-24 17:58:32 +01:00
if(l == NULL || idx<0 || idx>= list_size(l))
return NULL;
int i=0;
2019-12-15 12:10:06 +01:00
item_t np = l;
2015-11-24 17:58:32 +01:00
while(np)
2015-10-22 11:39:11 +02:00
{
2015-11-24 17:58:32 +01:00
if(i==idx)
return np;
np = np->next;
i++;
2015-10-22 11:39:11 +02:00
}
2015-11-24 17:58:32 +01:00
return NULL;
}
2019-12-15 12:10:06 +01:00
item_t list_item(int type)
2015-11-24 17:58:32 +01:00
{
2019-12-15 12:10:06 +01:00
item_t ret = (item_t)malloc(sizeof *ret);
2015-11-24 17:58:32 +01:00
ret->type = type;
ret->next = NULL;
return ret;
2015-10-22 11:39:11 +02:00
}
2019-12-15 12:10:06 +01:00
list_t split(const char* str, const char* delim)
2015-10-22 11:39:11 +02:00
{
if(str == NULL || delim == NULL) return NULL;
2020-01-01 13:14:50 +01:00
char* str_cpy = (char*)str;
2015-10-22 11:39:11 +02:00
char* token;
2019-12-15 12:10:06 +01:00
list_t l = list_init();
2015-10-22 11:39:11 +02:00
while((token = strsep(&str_cpy,delim)))
{
2020-01-08 19:17:51 +01:00
trim(token, ' ');
2015-10-22 11:39:11 +02:00
if(strlen(token) > 0)
{
2020-01-08 19:17:51 +01:00
list_put_special(&l, token);
2015-10-22 11:39:11 +02:00
}
}
2019-12-15 12:10:06 +01:00
if(l->type== LIST_TYPE_NIL)
{
free(l);
2015-10-22 11:39:11 +02:00
return NULL;
2019-12-15 12:10:06 +01:00
}
2015-10-22 11:39:11 +02:00
return l;
}
2019-12-15 12:10:06 +01:00
void list_put_i(list_t* l,int v)
2015-11-24 17:58:32 +01:00
{
2019-12-15 12:10:06 +01:00
item_t it = list_item(LIST_TYPE_INT);
2015-11-24 17:58:32 +01:00
it->value.i = v;
list_put(l,it);
}
2019-12-15 12:10:06 +01:00
void list_put_d(list_t* l,double v)
2015-11-24 17:58:32 +01:00
{
2019-12-15 12:10:06 +01:00
item_t it = list_item(LIST_TYPE_DOUBLE);
2015-11-24 17:58:32 +01:00
it->value.d = v;
list_put(l,it);
}
2019-12-22 17:33:35 +01:00
void list_put_ptr(list_t* l,void* v)
2015-11-24 17:58:32 +01:00
{
2019-12-22 17:33:35 +01:00
item_t it = list_item(LIST_TYPE_POINTER);
it->value.ptr = v;
2015-11-24 17:58:32 +01:00
list_put(l,it);
}
2019-12-15 12:10:06 +01:00
void list_put_array(list_t* l,list_t v)
2015-11-24 17:58:32 +01:00
{
2019-12-15 12:10:06 +01:00
item_t it = list_item(LIST_TYPE_ARRAY);
2015-11-24 17:58:32 +01:00
it->value.array = v;
list_put(l,it);
}
2019-12-22 17:33:35 +01:00
// unliked dictionary
// user have the responsibility to free
// pointer data
2019-12-15 12:10:06 +01:00
void list_free(list_t *l)
2015-11-24 17:58:32 +01:00
{
2019-12-15 12:10:06 +01:00
item_t curr;
2015-11-24 17:58:32 +01:00
while ((curr = (*l)) != NULL) {
2018-03-02 19:04:00 +01:00
(*l) = (*l)->next;
2019-12-22 17:33:35 +01:00
if(curr->type == LIST_TYPE_ARRAY && curr->value.array)
2015-11-24 17:58:32 +01:00
list_free(&curr->value.array);
2019-12-22 17:33:35 +01:00
else if( curr->type == LIST_TYPE_POINTER && curr->value.ptr)
free(curr->value.ptr);
2015-11-24 17:58:32 +01:00
free (curr);
}
}
2019-12-15 12:10:06 +01:00
int list_empty(list_t l)
2015-11-24 17:58:32 +01:00
{
2019-12-15 12:10:06 +01:00
return l== NULL || l->type == LIST_TYPE_NIL;
2015-11-24 17:58:32 +01:00
}