Exemple #1
0
Array_item *add_to_array (Array *array, Array_item *item)
{
	int count;
	int location = 0;
	Array_item *ret = (void *) 0 ;
	u_32int_t       mask;    

	if (array->hash == HASH_INSENSITIVE)
		item->hash = ci_alist_hash(item->name, &mask);
	else
		item->hash = cs_alist_hash(item->name, &mask);

	check_array_size(array);
	if (array->max)
	{
		find_array_item(array, item->name, &count, &location);
		if (count < 0)
		{
			ret = ((Array_item *) (( array ) -> list [ (  location ) ])) ;
			array->max--;
		}
		else
			move_array_items(array, location, array->max, 1);
	}

	array->list[location] = item;
	array->max++;
	return ret;
}
Exemple #2
0
/*
 * Returns an entry that has been displaced, if any.
 */
Array_item *BX_add_to_array (Array *array, Array_item *item)
{
	int count;
	int location = 0;
	Array_item *ret = NULL;
	u_32int_t       mask;   /* Dummy var */

	if (array->hash == HASH_INSENSITIVE)
		item->hash = ci_alist_hash(item->name, &mask);
	else
		item->hash = cs_alist_hash(item->name, &mask);

	check_array_size(array);
	if (array->max)
	{
		find_array_item(array, item->name, &count, &location);
		if (count < 0)
		{
			ret = ARRAY_ITEM(array, location);
			array->max--;
		}
		else
			move_array_items(array, location, array->max, 1);
	}

	array->list[location] = item;
	array->max++;
	return ret;
}
Exemple #3
0
void add_new_fset(char *name, char *args)
{
	if (args && *args)
	{
		IrcVariable *tmp = NULL;
		int cnt, loc;
		tmp = (IrcVariable *)find_array_item((Array *)&ext_fset_list, name, &cnt, &loc);
		if (!tmp || cnt >= 0)
		{
			tmp = new_malloc(sizeof(IrcVariable));
			tmp->name = m_strdup(name);
			tmp->type = STR_TYPE_VAR;
			add_to_array((Array *)&ext_fset_list, (Array_item *)tmp);
		}
		malloc_strcpy(&tmp->string, args);
	}
	else 
	{
		IrcVariable *tmp;
		if ((tmp = (IrcVariable *)remove_from_array((Array *)&ext_fset_list, name)))
		{
			new_free(&tmp->name);
			new_free(&tmp->string);
			new_free(&tmp);
		}
	}
}
Exemple #4
0
IrcVariable *find_ext_fset_var(char *name)
{
IrcVariable *tmp = NULL;
int loc, cnt;
	tmp = (IrcVariable *)find_array_item((Array *)&ext_fset_list, name, &cnt, &loc);
	if (tmp && cnt < 0)
		return tmp;
	return NULL;
}
Exemple #5
0
array_item *array_lookup (array *a, const char *name, int wild, int rem)
{
	int 	count, 
		location;

	if (rem)
		return remove_from_array(a, name);
	else
		return find_array_item(a, name, &count, &location);
}
Exemple #6
0
/*
 * Returns the entry that has been removed, if any.
 */
Array_item *BX_remove_from_array (Array *array, char *name)
{
	int count, location = 0;

	if (array->max)
	{
		find_array_item(array, name, &count, &location);
		if (count >= 0)
			return NULL;

		return array_pop(array, location);
	}
	return NULL;	/* Cant delete whats not there */
}
Exemple #7
0
Array_item *remove_from_array (Array *array, char *name)
{
	int count, location = 0;

	if (array->max)
	{
		find_array_item(array, name, &count, &location);
		if (count >= 0)
			return (void *) 0 ;

		return array_pop(array, location);
	}
	return (void *) 0 ;	 
}
Exemple #8
0
/*
 * Returns the entry that has been removed, if any.
 */
Array_item *BX_remove_all_from_array (Array *array, char *name)
{
	int count, location = 0;
	Array_item *ret = NULL;

	if (array->max)
	{
		find_array_item(array, name, &count, &location);
		if (count == 0)
			return NULL;
		ret = ARRAY_ITEM(array, location);
		move_array_items(array, location + 1, array->max, -1);
		array->max--;
		return ret;
	}
	return NULL;	/* Cant delete whats not there */
}
Exemple #9
0
Array_item *remove_all_from_array (Array *array, char *name)
{
	int count, location = 0;
	Array_item *ret = (void *) 0 ;

	if (array->max)
	{
		find_array_item(array, name, &count, &location);
		if (count == 0)
			return (void *) 0 ;
		ret = ((Array_item *) (( array ) -> list [ (  location ) ])) ;
		move_array_items(array, location + 1, array->max, -1);
		array->max--;
		return ret;
	}
	return (void *) 0 ;	 
}
Exemple #10
0
int    
confparser_parse(ConfParser* parser)
{
    FILE *f;

    f = fopen(parser->filename, "r");
    if (NULL == f)
        return -1;
    
    char buf[4096];
    char *pstart;
    int  lineno = 0;
    char key[100] = {0};
    while (fgets(buf, 4096, f) != NULL) {
        pstart = buf;
        lineno++;

        while (isblank(*pstart)) pstart++;
        if (pstart[0] == '#' || pstart[0] == '\r' || pstart[0] == '\n') {
            continue;
        }

        if (*pstart == '=') {
            DERROR("config file parse error! not key at line:%d\n", lineno);
            fclose(f);
            return -1;
        }

        char *sp = strchr(pstart, '=');
        if (sp == NULL) {
            if (*key == '\0') {
                DERROR("config file parse error! \"=\" not found at line:%d\n", lineno);
                fclose(f);
                return -1;
            }
        } else {
            char *end = sp - 1;
            while (end > pstart && isblank(*end)) {
                end--;
            }
            *(end + 1) = 0;
            strcpy(key, pstart);

            pstart = sp + 1;
            while (isblank(*pstart)) pstart++;
            if (*pstart == '\r' || *pstart == '\n')
                continue;
        }
        clean_end(pstart);
            

        
        ConfParam *param = parser->params;
        char *tmp = NULL, *item = NULL;
        while (param) {
            if (strcmp(param->name, key) == 0) {
            //    lastname = param->name;
                if (param->arraysize && param->dsti >= param->arraysize) {
                    DERROR("config parser error, too many items: %s line:%d\n", param->name, lineno);
                    fclose(f);
                    return -1;
                }
                switch(param->type) {
                case CONF_INT:
                    if (param->arraysize) {
                        while ((item = find_array_item(pstart, &tmp)) != NULL) {
                            ((int*)param->dst)[param->dsti] = atoi(item);
                            //DERROR("%d\n", ((int*)param->dst)[param->dsti]);
                            param->dsti++;
                        }
                    }else{
                        *(int*)param->dst = atoi(pstart);
                    }
                    break;
                case CONF_FLOAT:
                    if (param->arraysize) {
                        while ((item = find_array_item(pstart, &tmp)) != NULL) {
                            ((float*)param->dst)[param->dsti] = atof(item);
                            param->dsti++;
                        }
                    }else{
                        *(float*)param->dst = atof(pstart);
                    }
                    break;
                case CONF_STRING: 
                    if (param->arraysize) {
                        while ((item = find_array_item(pstart, &tmp)) != NULL) {
                            snprintf(((char**)param->dst)[param->dsti], 1024, "%s", item);
                            param->dsti++;
                        }
                    }else{
                        snprintf((char*)param->dst, 1024, "%s", pstart);
                    }
                    break;
                case CONF_BOOL: {
                    int v = 0;    
                    if (param->arraysize) {
                        while ((item = find_array_item(pstart, &tmp)) != NULL) {
                            v = bool_check(item);
                            if (v < 0) {
                                DERROR("conf error, %s must yes/no true/false at line:%d\n", 
                                            param->name, lineno);
                                fclose(f);
                                return -1;
                            }

                            ((int*)param->dst)[param->dsti] = v;
                            param->dsti++;
                        }
                    }else{
                        v = bool_check(pstart);
                        if (v < 0) {
                            DERROR("conf error, %s must yes/no true/false at line:%d\n", 
                                        param->name, lineno);
                            fclose(f);
                            return -1;
                        }
                        *(int*)param->dst = v;
                    }
                    break;
                }
                case CONF_ENUM: { // 枚举只能是整型
                    int v = 0;
                    if (param->arraysize) {
                        while ((item = find_array_item(pstart, &tmp)) != NULL) {
                            if (enum_check(param, pstart, &v) == FALSE) {
                                DERROR("conf error, %s value error at line:%d\n", param->name, lineno);
                                fclose(f);
                                return -1;
                            }
                            ((int*)param->dst)[param->dsti] = v;
                            param->dsti++;
                        }
                    }else{
                        if (enum_check(param, pstart, &v) == FALSE) {
                            DERROR("conf error, %s value error at line:%d\n", param->name, lineno);
                            fclose(f);
                            return -1;
                        }
                        *(int*)param->dst = v;
                    }
                    break;
                }
                case CONF_USER: {
                    if (param->arraysize) {
                        while ((item = find_array_item(pstart, &tmp)) != NULL) {
                            if (param->param.userfunc(param->dst, item, param->dsti) == FALSE) {
                                DERROR("conf error, %s value error at line:%d\n", param->name, lineno);
                                fclose(f);
                                return -1;
                            }
                            //((int*)param->dst)[param->dsti] = v;
                            param->dsti++;
                        }
                    }else{
                        if (param->param.userfunc(param->dst, pstart, 0) == FALSE) {
                            DERROR("conf error, %s value error at line:%d\n", param->name, lineno);
                            fclose(f);
                            return -1;
                        }
                        //*(int*)param->dst = v;
                    }

                    break;
                }
                default:
                    DERROR("conf param type error at %s with type:%d\n", param->name, param->type);
                    fclose(f);
                    return -1;
                }
                break;
            }
            param = param->next;
        }
    }

    fclose(f);
    return 0;
}