ant-http/lib/list.c

248 lines
4.9 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"
2015-11-24 17:58:32 +01:00
list list_init()
2015-10-22 11:39:11 +02:00
{
2015-11-24 17:58:32 +01:00
list ret = (list)malloc(sizeof *ret);
ret->type = RPC_TYPE_NIL;
2015-10-22 11:39:11 +02:00
ret->next = NULL;
return ret;
}
2015-11-24 17:58:32 +01:00
void list_put(list* l, item it)
2015-10-22 11:39:11 +02:00
{
2015-11-24 17:58:32 +01:00
if(*l == NULL || (*l)->type == RPC_TYPE_NIL)
{
2018-03-02 19:04:00 +01:00
free(*l);
2015-11-24 17:58:32 +01:00
*l = it;
return ;
}
item np = list_last(*l);
np->next = it;
2015-10-22 11:39:11 +02:00
}
2015-11-24 17:58:32 +01:00
void list_put_special(list* l, const char* str)
2015-10-22 11:39:11 +02:00
{
2015-11-24 17:58:32 +01:00
item v;
if(IS_INT(str))
2015-10-22 11:39:11 +02:00
{
2015-11-24 17:58:32 +01:00
v = list_item(RPC_TYPE_INT);
v->value.i = atoi(str);
}
else if(IS_FLOAT(str))
{
v = list_item(RPC_TYPE_DOUBLE);
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
{
2015-11-24 17:58:32 +01:00
v = list_item(RPC_TYPE_STRING);
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
}
2015-11-24 17:58:32 +01:00
item list_last(list l)
2015-10-22 11:39:11 +02:00
{
2015-11-24 17:58:32 +01:00
item p = l;
while(p && p->next != NULL)
p = p->next;
return p;
2015-10-22 11:39:11 +02:00
}
2015-11-24 17:58:32 +01:00
int list_remove(list 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;
}
2015-11-24 17:58:32 +01:00
item 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
}
2015-11-24 17:58:32 +01:00
int list_size(list l)
2015-10-22 11:39:11 +02:00
{
2015-11-24 17:58:32 +01:00
if(l == NULL || l->type == RPC_TYPE_NIL) return 0;
int i=0;
item np = l;
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
}
2015-11-24 17:58:32 +01:00
char* as_string(list l)
2015-10-22 11:39:11 +02:00
{
2015-11-24 17:58:32 +01:00
char* str = "";
if(l != NULL && l->type != RPC_TYPE_NIL)
2015-10-22 11:39:11 +02:00
{
2015-11-24 17:58:32 +01:00
switch(l->type)
{
case RPC_TYPE_BASE64:
str = __s("b64:%s", l->value.b64);
2015-10-22 11:39:11 +02:00
break;
2015-11-24 17:58:32 +01:00
case RPC_TYPE_BOOL:
str = __s("bool:%d", l->value.b);
2015-10-22 11:39:11 +02:00
break;
2015-11-24 17:58:32 +01:00
case RPC_TYPE_DOUBLE:
str = __s("double:%lf", l->value.d);
2015-10-22 11:39:11 +02:00
break;
2015-11-24 17:58:32 +01:00
case RPC_TYPE_DATE:
str = __s("date:%s", l->value.date);
break;
case RPC_TYPE_INT:
case RPC_TYPE_I4:
str = __s("int:%d", l->value.i);
break;
case RPC_TYPE_STRING:
str = __s("string:%s", l->value.s);
break;
case RPC_TYPE_ARRAY:
str = __s("[%s]", as_string(l->value.array));
break;
default:
str = "<Unknown>";
break;
}
item np = l->next;
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
}
2015-11-24 17:58:32 +01:00
item list_at(list 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;
item np = l;
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;
}
item list_item(int type)
{
item ret = (item)malloc(sizeof *ret);
ret->type = type;
ret->next = NULL;
return ret;
2015-10-22 11:39:11 +02:00
}
list split(const char* str, const char* delim)
{
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;
2015-11-24 17:58:32 +01:00
list 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);
2015-11-24 17:58:32 +01:00
if(l->type== RPC_TYPE_NIL)
2015-10-22 11:39:11 +02:00
return NULL;
return l;
}
2015-11-24 17:58:32 +01:00
void list_put_i(list* l,int v)
{
item it = list_item(RPC_TYPE_INT);
it->value.i = v;
list_put(l,it);
}
void list_put_d(list* l,double v)
{
item it = list_item(RPC_TYPE_DOUBLE);
it->value.d = v;
list_put(l,it);
}
void list_put_b(list* l,int v)
{
item it = list_item(RPC_TYPE_BOOL);
it->value.b = v;
list_put(l,it);
}
void list_put_b64(list* l,const char* v)
{
item it = list_item(RPC_TYPE_BASE64);
it->value.b64 = strdup(v);
list_put(l,it);
}
void list_put_date(list* l,const char* v)
{
item it = list_item(RPC_TYPE_DATE);
it->value.date = strdup(v);
list_put(l,it);
}
void list_put_s(list* l,const char* v)
{
item it = list_item(RPC_TYPE_STRING);
it->value.s = strdup(v);
list_put(l,it);
}
void list_put_array(list* l,list v)
{
item it = list_item(RPC_TYPE_ARRAY);
it->value.array = v;
list_put(l,it);
}
void list_free(list *l)
{
item curr;
while ((curr = (*l)) != NULL) {
2018-03-02 19:04:00 +01:00
(*l) = (*l)->next;
2015-11-24 17:58:32 +01:00
if(curr->type == RPC_TYPE_ARRAY)
list_free(&curr->value.array);
2018-03-02 19:04:00 +01:00
else if(curr->type == RPC_TYPE_STRING)
free(curr->value.s);
else if(curr->type == RPC_TYPE_DATE)
free(curr->value.date);
else if(curr->type == RPC_TYPE_BASE64)
free(curr->value.b64);
2015-11-24 17:58:32 +01:00
free (curr);
}
}
int list_empty(list l)
{
return l== NULL || l->type == RPC_TYPE_NIL;
}