Ejemplo n.º 1
0
/** function to generate a People "object" from a preferences description */
static NftResult _people_from_prefs(NftPrefs *p, void **newObj, NftPrefsNode *node, void *userptr)
{
        people.people_count = sizeof(persons)/sizeof(struct Person);

        /* call toObj() of child objects */
        NftPrefsNode *child;
        size_t i = 0;
        for(child = nft_prefs_node_get_first_child(node);
            child;
            child = nft_prefs_node_get_next(child))
        {
                if(i >= sizeof(persons)/sizeof(struct Person))
                {
                        NFT_LOG(L_ERROR, "more persons in prefs file than expected");
                        return NFT_FAILURE;
                }

                /* call toObj function of child node (should be a <person> node) */
                if(!(people.people[i++] = nft_prefs_obj_from_node(p, child, userptr)))
                {
                        NFT_LOG(L_ERROR, "Failed to create object from preference node");
                        return NFT_FAILURE;
                }
        }

        /* save pointer to new object */
        *newObj = &people;

        return NFT_SUCCESS;
}
Ejemplo n.º 2
0
/**
 * generate LedHardware from LedPrefsNode
 *
 * @param p LedPrefs context
 * @param n LedPrefsNode
 * @result newly created LedHardware
 */
LedHardware *led_prefs_hardware_from_node(LedPrefs * p, LedPrefsNode * n)
{
        if(!p || !n)
                NFT_LOG_NULL(NULL);

        /* check if node is of expected class */
        if(strcmp(nft_prefs_node_get_name(n), LED_HARDWARE_NAME) != 0)
        {
                NFT_LOG(L_ERROR,
                        "got wrong LedPrefsNode class. Expected \"%s\" but got \"%s\"",
                        LED_HARDWARE_NAME, nft_prefs_node_get_name(n));
                return NULL;
        }

        return nft_prefs_obj_from_node(p, n, NULL);
}
Ejemplo n.º 3
0
/**
 * generate LedChain from LedPrefsNode
 *
 * @param p LedPrefs context
 * @param n LedPrefsNode 
 * @result newly created LedChain
 */
LedChain *led_prefs_chain_from_node(LedPrefs * p, LedPrefsNode * n)
{
        if(!p || !n)
                NFT_LOG_NULL(NULL);

        /* check if node is of expected class */
        if(!led_prefs_is_chain_node(n))
        {
                NFT_LOG(L_ERROR,
                        "got wrong LedPrefsNode class. Expected \"%s\" but got \"%s\"",
                        LED_CHAIN_NAME, nft_prefs_node_get_name(n));
                return NULL;
        }

        return nft_prefs_obj_from_node(p, n, NULL);
}
Ejemplo n.º 4
0
        /* save pointer to new object */
        *newObj = &persons[i++];


        return NFT_SUCCESS;
}


//~ /** create object from preferences definition */
int main(int argc, char *argv[])
{
    	/* do preliminary version checks */
    	NFT_PREFS_CHECK_VERSION

        /* fail per default */
        int result = EXIT_FAILURE;


        /* initialize libniftyprefs */
        NftPrefs *prefs;
        if(!(prefs = nft_prefs_init()))
		{
				NFT_LOG(L_ERROR, "initialize prefs");
				goto _deinit;
		}


        /* register "people" class to niftyprefs */
        if(!(nft_prefs_class_register(prefs, PEOPLE_NAME, &_people_from_prefs, NULL)))
		{
				NFT_LOG(L_ERROR, "failed to register class");
				goto _deinit;
		}

        /* register "person" class to niftyprefs */
        if(!(nft_prefs_class_register(prefs, PERSON_NAME, &_person_from_prefs, NULL)))
        {
				NFT_LOG(L_ERROR, "failed to register class");
				goto _deinit;
		}


    	/* parse file to prefs node */
    	NftPrefsNode *node;
    	if(!(node = nft_prefs_node_from_file("test-prefs.xml")))
		{
				NFT_LOG(L_ERROR, "failed to parse prefs file \"test-prefs.xml\"");
				goto _deinit;
		}

        /* create object from node */
        struct People *people;
        if(!(people = nft_prefs_obj_from_node(prefs, node, NULL)))
        {
				NFT_LOG(L_ERROR, "failed to create object from prefs node");
				goto _deinit;
		}

    	/* free node */
    	nft_prefs_node_free(node);

        /* process all persons */
        size_t n;
        for(n=0; n < people->people_count; n++)
        {
                /* print info */
                printf("\tperson(name=\"%s\",email=\"%s\", age=\"%d\", vitality=\"%s\")\n",
                    	people->people[n]->name, 
                       	people->people[n]->email, 
                       	people->people[n]->age,
                       	people->people[n]->alive ? "alive" : "dead");
        }

        /* we should get what we put in */
        if((strcmp(people->people[0]->name, "Bob") != 0) ||
           (strcmp(people->people[0]->email, "*****@*****.**") != 0) ||
           (people->people[0]->age != 30) ||
           (people->people[0]->alive != true) ||
           (strcmp(people->people[1]->name, "Alice") != 0) ||
           (strcmp(people->people[1]->email, "*****@*****.**") != 0) ||
           (people->people[1]->age != 30) ||
           (people->people[1]->alive != false))
        {
                NFT_LOG(L_ERROR, "Input from 01_obj-to-prefs.c doesn't match output!");
                goto _deinit;
        }

		/** @todo check xml against DTD */

        /* all went fine */
        result = EXIT_SUCCESS;

_deinit:
        nft_prefs_deinit(prefs);

        return result;
}
Ejemplo n.º 5
0
/**
 * Config-to-Object function.
 * Creates a LedSetup model from a prefs node
 * @note you shouldn't call this function directly
 * It's used by nft_prefs_obj_from_node() etc. 
 */
static NftResult _prefs_to_setup(LedPrefs * p, void **newObj,
                                 NftPrefsNode * n, void *userptr)
{
        if(!p || !newObj)
                NFT_LOG_NULL(NFT_FAILURE);


        /* new setup */
        LedSetup *s;

        /* new setup */
        if(!(s = led_setup_new()))
        {
                NFT_LOG(L_ERROR, "Failed to create new LedSetup object");
                return NFT_FAILURE;
        }

        /* walk all child nodes and process them */
        NftPrefsNode *child;
        for(child = nft_prefs_node_get_first_child(n);
            child; child = nft_prefs_node_get_next(child))
        {
                /* check if node describes a LedHardware object */
                if(!led_prefs_is_hardware_node(child))
                {
                        NFT_LOG(L_ERROR,
                                "\"%s\" object may only contain \"%s\" children but got \"%s\"",
                                LED_SETUP_NAME, LED_HARDWARE_NAME,
                                nft_prefs_node_get_name(child));
                        goto _pts_error;
                }

                /* call toObj function of child node */
                LedHardware *hw;
                if(!(hw = nft_prefs_obj_from_node(p, child, userptr)))
                {
                        NFT_LOG(L_ERROR,
                                "Failed to create object from preference node");
                        return NFT_FAILURE;
                }

                /* register first hardware */
                if(!led_setup_get_hardware(s))
                {
                        led_setup_set_hardware(s, hw);
                }
                /* attach hardware to list */
                else
                {
                        if(!led_hardware_list_append_head
                           (led_setup_get_hardware(s), hw))
                        {
                                NFT_LOG(L_ERROR,
                                        "Failed to append LedHardware as sibling");
                                return NFT_FAILURE;
                        }
                }
        }

        /* save new setup-object to "newObj" pointer */
        *newObj = s;

        return NFT_SUCCESS;


_pts_error:
        led_setup_destroy(s);
        return NFT_FAILURE;
}
Ejemplo n.º 6
0
int main(int argc, char *argv[])
{
    int result = EXIT_FAILURE;

    /* do preliminary version checks */
    if(!NFT_PREFS_CHECK_VERSION)
    		return EXIT_FAILURE;

	/* set loglevel */
	nft_log_level_set(L_MAX);
	
	/* initialize libniftyprefs */
	NftPrefs *prefs;
	if(!(prefs = nft_prefs_init(1)))
	{
			NFT_LOG(L_ERROR, "initialize prefs");
			goto _deinit;
	}

	/* register "people" class to niftyprefs */
	if(!
	   (nft_prefs_class_register
		(prefs, PEOPLE_NAME, &_people_from_prefs, NULL)))
	{
			NFT_LOG(L_ERROR, "failed to register class");
			goto _deinit;
	}

	/* register "person" class to niftyprefs */
	if(!
	   (nft_prefs_class_register
		(prefs, PERSON_NAME, &_person_from_prefs, NULL)))
	{
			NFT_LOG(L_ERROR, "failed to register class");
			goto _deinit;
	}

	/* register updater for v0 "person" nodes */
	if(!nft_prefs_updater_register(prefs, _update_person, PERSON_NAME, 0, NULL))
	{
			NFT_LOG(L_ERROR, "failed to register updater");
			goto _deinit;
	}


	                               
	/* parse file to prefs node */
	NftPrefsNode *node;
	if(!(node = nft_prefs_node_from_file(prefs, "test-prefs.xml")))
	{
			NFT_LOG(L_ERROR,
					"failed to parse prefs file \"test-prefs.xml\"");
			goto _deinit;
	}

	/* dump updated node to file */
	if(!nft_prefs_node_to_file(prefs, node, "test-prefs_updated.xml", true))
	{
			NFT_LOG(L_ERROR, 
			        "failed to save prefs file \"test-prefs_updated.xml\"");
			goto _deinit;
	}
	
	/* create object from node */
	struct People *people;
	if(!(people = nft_prefs_obj_from_node(prefs, node, NULL)))
	{
			NFT_LOG(L_ERROR, "failed to create object from prefs node");
			goto _deinit;
	}

	/* free node */
	nft_prefs_node_free(node);

	/* process all persons */
	size_t n;
	for(n = 0; n < people->people_count; n++)
	{
			/* print info */
			printf("\tperson(name=\"%s\", email=\"%s@%s\", age=\"%d\", vitality=\"%s\")\n", 
			       	people->people[n]->name, 
			       	people->people[n]->email_user, 
			       	people->people[n]->email_host, 
			       	people->people[n]->age, 
			       	people->people[n]->alive ? "alive" : "dead");
	}

	/* we should get what we put in */
	if((strcmp(people->people[0]->name, "Bob") != 0) ||
	   (strcmp(people->people[0]->email_user, "bob") != 0) ||
	   (strcmp(people->people[0]->email_host, "example.com") != 0) ||
	   (people->people[0]->age != 30) ||
	   (people->people[0]->alive != true) ||
	   (strcmp(people->people[1]->name, "Alice") != 0) ||
	   (strcmp(people->people[1]->email_user, "alice") != 0) ||
	   (strcmp(people->people[1]->email_host, "example.com") != 0) ||
	   (people->people[1]->age != 30) ||
	   (people->people[1]->alive != false))
	{
			NFT_LOG(L_ERROR,
					"Updated input doesn't match output!");
			goto _deinit;
	}


	/* all good */
	result = EXIT_SUCCESS;

_deinit:
	nft_prefs_deinit(prefs);

	return result;
}