ant-http/lib/list.c

251 lines
5.1 KiB
C
Raw 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
{
2018-03-02 19:04:00 +01:00
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-15 12:10:06 +01:00
v = list_item(LIST_TYPE_STRING);
2015-11-24 17:58:32 +01:00
v->value.s = 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-15 12:10:06 +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;
if(idx <0 || idx >= list_size(l)) return 0;
2015-10-22 11:39:11 +02:00
if(idx == 0)
{
2015-11-24 17:58:32 +01:00
l=l->next;
2015-10-22 11:39:11 +02:00
return 1;
}
2019-12-15 12:10:06 +01:00
item_t np = list_at(l,idx-1);
2015-10-22 11:39:11 +02:00
if(np == NULL) return 0;
if(np->next == NULL) return 1;
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
char* as_string(list_t l)
2015-10-22 11:39:11 +02:00
{
2015-11-24 17:58:32 +01:00
char* str = "";
2019-12-15 12:10:06 +01:00
if(l != NULL && l->type != LIST_TYPE_NIL)
2015-10-22 11:39:11 +02:00
{
2015-11-24 17:58:32 +01:00
switch(l->type)
{
2019-12-15 12:10:06 +01:00
case LIST_TYPE_BASE64:
2015-11-24 17:58:32 +01:00
str = __s("b64:%s", l->value.b64);
2015-10-22 11:39:11 +02:00
break;
2015-11-24 17:58:32 +01:00
2019-12-15 12:10:06 +01:00
case LIST_TYPE_BOOL:
2015-11-24 17:58:32 +01:00
str = __s("bool:%d", l->value.b);
2015-10-22 11:39:11 +02:00
break;
2015-11-24 17:58:32 +01:00
2019-12-15 12:10:06 +01:00
case LIST_TYPE_DOUBLE:
2015-11-24 17:58:32 +01:00
str = __s("double:%lf", l->value.d);
2015-10-22 11:39:11 +02:00
break;
2015-11-24 17:58:32 +01:00
2019-12-15 12:10:06 +01:00
case LIST_TYPE_DATE:
2015-11-24 17:58:32 +01:00
str = __s("date:%s", l->value.date);
break;
2019-12-15 12:10:06 +01:00
case LIST_TYPE_INT:
case LIST_TYPE_I4:
2015-11-24 17:58:32 +01:00
str = __s("int:%d", l->value.i);
break;
2019-12-15 12:10:06 +01:00
case LIST_TYPE_STRING:
2015-11-24 17:58:32 +01:00
str = __s("string:%s", l->value.s);
break;
2019-12-15 12:10:06 +01:00
case LIST_TYPE_ARRAY:
2015-11-24 17:58:32 +01:00
str = __s("[%s]", as_string(l->value.array));
break;
default:
str = "<Unknown>";
break;
}
2019-12-15 12:10:06 +01:00
item_t np = l->next;
2015-11-24 17:58:32 +01:00
if(np)
{
str = __s("%s,\n%s", str, as_string(np));
}
return str;
2015-10-22 11:39:11 +02:00
}
2015-11-24 17:58:32 +01:00
return "[empty]";
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;
char* str_cpy = strdup(str);
2018-03-02 19:04:00 +01:00
char* org_str = str_cpy;
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)))
{
if(strlen(token) > 0)
{
2015-11-24 17:58:32 +01:00
list_put_special(&l,token);
2015-10-22 11:39:11 +02:00
}
}
2018-03-02 19:04:00 +01:00
free(org_str);
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-15 12:10:06 +01:00
void list_put_b(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_BOOL);
2015-11-24 17:58:32 +01:00
it->value.b = v;
list_put(l,it);
}
2019-12-15 12:10:06 +01:00
void list_put_b64(list_t* l,const char* v)
2015-11-24 17:58:32 +01:00
{
2019-12-15 12:10:06 +01:00
item_t it = list_item(LIST_TYPE_BASE64);
2015-11-24 17:58:32 +01:00
it->value.b64 = strdup(v);
list_put(l,it);
}
2019-12-15 12:10:06 +01:00
void list_put_date(list_t* l,const char* v)
2015-11-24 17:58:32 +01:00
{
2019-12-15 12:10:06 +01:00
item_t it = list_item(LIST_TYPE_DATE);
2015-11-24 17:58:32 +01:00
it->value.date = strdup(v);
list_put(l,it);
}
2019-12-15 12:10:06 +01:00
void list_put_s(list_t* l,const char* v)
2015-11-24 17:58:32 +01:00
{
2019-12-15 12:10:06 +01:00
item_t it = list_item(LIST_TYPE_STRING);
2015-11-24 17:58:32 +01:00
it->value.s = strdup(v);
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-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-15 12:10:06 +01:00
if(curr->type == LIST_TYPE_ARRAY)
2015-11-24 17:58:32 +01:00
list_free(&curr->value.array);
2019-12-15 12:10:06 +01:00
else if(curr->type == LIST_TYPE_STRING)
2018-03-02 19:04:00 +01:00
free(curr->value.s);
2019-12-15 12:10:06 +01:00
else if(curr->type == LIST_TYPE_DATE)
2018-03-02 19:04:00 +01:00
free(curr->value.date);
2019-12-15 12:10:06 +01:00
else if(curr->type == LIST_TYPE_BASE64)
2018-03-02 19:04:00 +01:00
free(curr->value.b64);
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
}