Results 1 to 6 of 6

Thread: How to load strings in ClanLib 2.0?

  1. #1
    Lesser Knight
    Join Date
    Jan 2010
    Posts
    52

    Question How to load strings in ClanLib 2.0?

    Hello!
    Long time ClanLib user here :wheelchair:. I've branched my project in order to try to switch to ClanLib 2.1 from 1.0. I've been using the little guide on 1.0->2.1 conversion, but... I've run into a snag.

    How do I load a string from a resource? It used to be done with the string class, like CL_String::load(). But I don't see any equivalent in the 2.1 API.

    What's the new hip way to pull in a string from a resource file?

  2. #2
    ClanLib Developer
    Join Date
    May 2007
    Posts
    1,824

    Default

    I may be wrong, but this is how you can do it:

    For a value:

    Code:
    int to_int(const std::string &resource_name, CL_ResourceManager *gfx)
    {
    	return gfx->get_integer_resource(resource_name, 0);
    }
    For a bool:

    Code:
    int to_bool(const std::string &resource_name, CL_ResourceManager *gfx)
    {    
    	return gfx->get_boolean_resource(ressource_name, false);
    }
    For a string:
    Code:
    CL_String to_string(const CL_String &resource_id, const CL_String &default_value)
    {
    	if (!resource_exists(resource_id))
    		return default_value;
    
    	CL_Resource resource = get_resource(resource_id);
    	resource.get_element().get_attribute(cl_text("string"));
    }

  3. #3
    Lesser Knight
    Join Date
    Jan 2010
    Posts
    52

    Default

    Quote Originally Posted by rombust View Post
    I may be wrong, but this is how you can do it:

    For a value:

    Code:
    int to_int(const std::string &resource_name, CL_ResourceManager *gfx)
    {
    	return gfx->get_integer_resource(resource_name, 0);
    }
    For a bool:

    Code:
    int to_bool(const std::string &resource_name, CL_ResourceManager *gfx)
    {    
    	return gfx->get_boolean_resource(ressource_name, false);
    }
    For a string:
    Code:
    CL_String to_string(const CL_String &resource_id, const CL_String &default_value)
    {
    	if (!resource_exists(resource_id))
    		return default_value;
    
    	CL_Resource resource = get_resource(resource_id);
    	resource.get_element().get_attribute(cl_text("string"));
    }
    Hmm, if I'm understanding that, I think in my case it might be actually cl_text("value") since my XML looks like:

    Code:
                <string name="general" value="sb_black"/>
    I used to be able to do, like CL_String::load("Settings/Configuration/general",resources);
    and it would return a string with "sb_black" in this case.... So now I would do
    Code:
    CL_Resource resource = get_resource("Settings/Configuration/general");
    CL_String value = resource.get_element().get_attribute(cl_text("value"));
    Is that right?

  4. #4
    Lesser Knight
    Join Date
    Mar 2009
    Posts
    54

    Default

    Taken straight from my project

    Code:
            CL_String loadString(const CL_String &path, CL_ResourceManager &man)
            {
                CL_Resource res = man.get_resource(path);
                if (res.get_type() != cl_text("string"))
                    throw CL_Exception(cl_text("Resource is not string type!"));
    
                return res.get_element().get_text();
            }
    
            int loadInteger(const CL_String &path, CL_ResourceManager &man)
            {
                CL_Resource res = man.get_resource(path);
                if (res.get_type() != cl_text("integer"))
                    throw CL_Exception(cl_text("Resource is not integer type!"));
    
                return CL_StringHelp::text_to_int(res.get_element().get_text());
            }
    
            bool loadBool(const CL_String &path, CL_ResourceManager &man)
            {
                CL_Resource res = man.get_resource(path);
                if (res.get_type() != cl_text("bool"))
                    throw CL_Exception(cl_text("Resource is not bool type!"));
    
                return CL_StringHelp::text_to_lower(res.get_element().get_text()) == _ct("true") ? true : false;
            }
    example
    Code:
    <resources>
    	<section name="network">
    		<string name="ip">127.0.0.1</string>
    		<integer name="port">6666</integer>
    	</section>
    </resources>
    loadString("network/ip", resourceManager) returns "127.0.0.1"
    loadInteger("network/port", resourceManager) returns 6666

  5. #5
    Lesser Knight
    Join Date
    Jan 2010
    Posts
    52

    Default

    Thank you Hunter!!!

  6. #6
    Lesser Knight
    Join Date
    Jan 2010
    Posts
    52

    Default

    Ok so I have to move my values out into the text part of the xml tag to use huntercool's solution. No problem, I kind of prefer that

Similar Threads

  1. Novashell won't load
    By Ainiaria in forum Novashell Game Creation System
    Replies: 11
    Last Post: 07-13-2009, 11:10 PM
  2. Patch : load method for CL_CollisionOutline
    By sandelz in forum Official ClanLib SDK Forums
    Replies: 2
    Last Post: 05-27-2009, 06:35 PM
  3. Can't load Dinkedit
    By cathadink in forum Dink Smallwood HD
    Replies: 2
    Last Post: 12-10-2006, 02:19 AM

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •