MCA Logo
MCA Online Reference Documentation
Main Index

4.2. Reading values from an AttributeTree

Every module and also a part has some functions that return values from the configuration file. Lokk into the API of tPartBase and tModule for informations about the multiple individual functions. The most common possibility is a function that reads the values and stores them in specified variables. Values of the above example can be read by the following example code (within a module or a group).

Example 4-2. Reading data from an AttributeTree

  // variables that will contain values
  float value;
  char name[99];

  // define struct for reading and storing values
  tAttributesStruct attributes[]=
  {
    tAttributeFloat("value",value),
    tAttributeString(99, "name", name),
    // this is the end-marker of the field
    tAttributeFieldEnd()
  };

  // getting the attributes from base of the example_entry_1
  if (!GetAttributes(attributes,"example_entry_1",true))
     DEM("%s: could not be initialized because of missing attributes!\n", Description());
  else
     {
       UINFO("%s: value of entry 1 is:%f\n",Description(),value);
       UINFO("%s: name of entry 1 is:%s\n",Description(),name);
     }

  // getting the attributes from base of the example_entry_2
  if (!GetAttributes(attributes,"example_entry_2",true))
     DEM("%s: could not be initialized because of missing attributes!\n", Description());
  else
     {
       UINFO("%s: value of entry 2 is:%f\n",Description(),value);
       UINFO("%s: name of entry 2 is:%s\n",Description(),name);
     }

  // now a more complex example
  // variables that will contain values
  int one_integer;
  bool one_bool;
  double double_vector[3];
  char one_long_string[333];
  char one_short_string[15];
  int integer_vector[4];
  char *string_vector[5];
  int i;
  for (i=0; i<5; i++)
    string_vector[i]=new char[33];

  // define struct for reading and storing values
  tAttributesStruct my_attributes[]={
    tAttributeInt("subentry_a.my_one_integer", one_integer),
    tAttributeBool("subentry_b.my_one_bool", one_bool),
    tAttributesDouble("subentry_a.my_doubles_ly_here", double_vector, 3),
    tAttributeString(333, "my_string", one_long_string),
    // this string will be truncated because the space is not enough
    tAttributeString(15, "my_string", one_short_string),
    tAttributesInt("subentry_b.my_integer_vector", integer_vector, 4),
    // there could be also defined more values in the AttributeTree than are read by the program ...
    tAttributesString(33, "subentry_a.my_string_vector", string_vector, 5),
    tAttributeFieldEnd()
  };


  // getting the attributes
  if (!GetAttributes(my_attributes, "example_entry_3", true)) {
    // in that case we want the program not to be executed since parameters are really necessary ...
    SetStatus(eERROR);
    DEM("MyModule(%s)>> GetAttributes failed: Aborting program.\n", Description());
    // because the program exits later on we have to free memory before return
    // (especially in RT-Linux this must be done; otherwhise you'll get a memory leak here!)
    // .. deleting parameters
    for (i=0; i<5; i++)
      delete[] string_vector[i];
    return;
  }

  // .. using parameters
  UINFO("%s: value of one_integer is:%i\n",Description(),one_integer);
  UINFO("%s: value of one_bool is:%i\n",Description(),one_bool);
  UINFO("%s: value of double_vector is:(%e, %e, %e)\n",Description(), double_vector[0], double_vector[1], double_vector[2]);
  UINFO("%s: value of one_long_string is:%s\n",Description(),one_long_string);
  UINFO("%s: value of one_short_string is:%s\n",Description(),one_short_string);
  UINFO("%s: value of integer_vector is:(%i, %i, %i, %i)\n",Description(), integer_vector[0], integer_vector[1], integer_vector[2], integer_vector[3]);
  UINFO("%s: value of string_vector is:(%s, %s, %s, %s, %s)\n",Description(), string_vector[0], string_vector[1], string_vector[2], string_vector[3], string_vector[4]);

  // .. deleting parameters
  for (i=0; i<5; i++)
    delete[] string_vector[i];