Пример #1
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;
}
Пример #2
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;
}