< index
< 7. File parser
< 7.5 Using custom data types

=====================================
7.6 Using the default parser listener
=====================================

You can run the parser with a default listener, by passing a NULL value in TCODParser::run :

C++ : myParser.run(filename,NULL)
C   : TCOD_parser_run(my_parser,filename,NULL)
Py  : parser_run(my_parser,filename,0)


In that case, a default listener will be used and the properties read from the file will be stored in the parser. The default listener does not support multiple values, each property name must appear only once in the file, else the last property will overwrite the value of the previous, so it is mean to be use with basic config files.
Using the default listener does not exempt you from declaring the structures in the parser. It still needs them to check the file validity. Once you run the parser, you can retrieve properties with following functions :

C++ : bool TCODParser::getBoolProperty(const char *name) const
      int TCODParser::getCharProperty(const char *name) const
      int TCODParser::getIntProperty(const char *name) const
      float TCODParser::getFloatProperty(const char *name) const
      TCODColor TCODParser::getColorProperty(const char *name) const
      TCOD_dice_t TCODParser::getDiceProperty(const char *name) const
      const char * TCODParser::getStringProperty(const char *name) const
      void * TCODParser::getCustomProperty(const char *name) const
      TCOD_list_t TCODParser::getListProperty(const char *name, TCOD_value_type_t type) const
C   : bool TCOD_parser_get_bool_property(TCOD_parser_t parser, const char *name)
      int TCOD_parser_get_char_property(TCOD_parser_t parser, const char *name)
      int TCOD_parser_get_int_property(TCOD_parser_t parser, const char *name)
      float TCOD_parser_get_float_property(TCOD_parser_t parser, const char *name)
      const char * TCOD_parser_get_string_property(TCOD_parser_t parser, const char *name)
      TCOD_color_t TCOD_parser_get_color_property(TCOD_parser_t parser, const char *name)
      TCOD_dice_t TCOD_parser_get_dice_property(TCOD_parser_t parser, const char *name)
      void * TCOD_parser_get_custom_property(TCOD_parser_t parser, const char *name)
      TCOD_list_t TCOD_parser_get_list_property(TCOD_parser_t parser, const char *name, TCOD_value_type_t type)
Py  : parser_get_bool_property(parser, name)
      parser_get_int_property(parser, name)
      parser_get_float_property(parser, name)
      parser_get_string_property(parser, name)
      parser_get_color_property(parser, name)
      parser_get_dice_property(parser, name)
      parser_get_list_property(parser, name, type)

ParameterDescription
parserIn the C version, the parser handler, returned by TCOD_parser_new.
nameThe full name of the property in the form <structure>.<structure>.<propertyName>.
typeFor the list properties, the type of the list's elements
Note that these function are rather slow especially if your configuration file is big, because they do a linear search in the property list, so you'd better not call them inside inner loops. If you try to get a property that was not declared in the parser, the program will exit with a fatal error. If an optional property is not defined in the file, the function will return 0 or equivalent depending on the type of the property (false for bool/flags, NULL for strings, black for color, an empty list for list properties).
Also note that TCODParser::getListProperty returns a C list structure, not a C++ template. Fortunately, TCODList has a constructor taking a TCOD_list_t as a parameter (see example below).
Example, with this config file :

video {
	mode = "800x600"
        availableModes = [ "800x600", "1024x768", "1280x1024" ]
	fullscreen = false
}
input {
	mouse {
		sensitivity = 0.5
	}
}

You can read those properties with following code :

C++ : char *mode=parser.getStringProperty("video.mode");
      TCODList<char *> availablesModes(parser.getListProperty("video.mode",TCOD_TYPE_STRING));
      bool fullscreen = parser.getBoolProperty("video.fullscreen");
      float mouseSensitivity = parser.getFloatProperty("input.mouse.sensitivity");
C   : char *mode=TCOD_parser_get_string_property(parser,"video.mode");
      TCOD_list_t availablesModes = TCOD_parser_get_list_property(parser,"video.mode",TCOD_TYPE_STRING));
      bool fullscreen = TCOD_parser_get_bool_property(parser,"video.fullscreen");
      float mouseSensitivity = TCOD_parser_get_float_property(parser,"input.mouse.sensitivity");
Py  : mode=litbcod.parser_get_string_property(parser,"video.mode")
      availablesModes = litbcod.parser_get_list_property(parser,"video.mode",litbcod.TYPE_STRING))
      fullscreen = litbcod.parser_get_bool_property(parser,"video.fullscreen")
      mouseSensitivity = litbcod.parser_get_float_property(parser,"input.mouse.sensitivity")

insert a comment