actube/src/cw/cw_type_str.c

131 lines
2.5 KiB
C
Raw Permalink Normal View History

/*
This file is part of libcapwap.
libcapwap is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libcapwap is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include "format.h"
#include "cw.h"
2022-07-31 17:15:32 +02:00
#include "val.h"
2022-07-31 17:15:32 +02:00
static void del ( struct cw_Val * data )
{
free ( data->val.str );
}
2022-07-31 17:15:32 +02:00
static struct cw_Val *get ( struct cw_Val * data, const uint8_t * src, int len )
{
uint8_t * s;
s = malloc (len+1);
if ( !s )
return NULL;
memcpy(s,src,len);
s[len]=0;
data->type = &cw_type_str;
data->val.str = (char*)s;
return data;
}
2022-07-31 17:15:32 +02:00
static int put ( const struct cw_Val *data, uint8_t * dst )
{
return cw_put_str ( dst, (uint8_t*)data->val.str );
}
2022-07-31 17:15:32 +02:00
static int to_str ( const struct cw_Val *data, char *dst, int max_len )
{
int l;
l = strlen(data->val.str);
strcpy(dst,data->val.str);
return l;
if (l<max_len){
strcpy(dst,data->val.str);
return l;
}
memcpy(dst,data->val.str,max_len);
dst[max_len]=0;
return max_len;
}
2022-07-31 17:15:32 +02:00
static struct cw_Val *from_str ( struct cw_Val * data, const char *src )
{
char * s;
s = cw_strdup(src);
if ( !s )
return NULL;
data->type = &cw_type_str;
data->val.str = s;
return data;
}
2022-07-31 17:15:32 +02:00
static int len ( struct cw_Val * data ){
return strlen (data->val.str);
}
2022-07-31 17:15:32 +02:00
static const char * get_type_name(cw_Val_t *data)
{
return CW_TYPE_STR->name;
}
2022-08-13 09:47:12 +02:00
static int bread(cw_Cfg_t *cfg, const char * key, const uint8_t *src, int len, const void *param)
{
cw_Val_t val;
int l;
char str[2048];
memset(&val,0,sizeof(cw_Val_t));
val.valguard = param;
get(&val,src,len);
to_str(&val,str,2048);
cw_cfg_set(cfg,key,str);
l = val.type->len(&val);
del(&val);
return l;
}
2022-08-17 18:41:17 +02:00
static int bwrite(cw_Cfg_t ** cfgs, const char *key, uint8_t *dst, const void * param)
{
return cw_generic_write_l(cfgs,CW_TYPE_STR,key,dst,param);
}
const struct cw_Type cw_type_str = {
"Str", /* name */
del, /* del */
put, /* put */
get, /* get */
to_str, /* to_str */
from_str, /* from_str */
len, /* len */
NULL, /* data */
2022-08-13 09:47:12 +02:00
get_type_name, /* get_type_name */
NULL,
bread,
2022-08-17 18:41:17 +02:00
bwrite,
2022-08-13 09:47:12 +02:00
};