Storing Application Settings

This tutorial will show how to store and read application specific settings.

Introduction

EDE main facility for reading and storing application settings is Resouce class from edelib. Files are stored in plain text files, by using familiar format (INI style, much like format of .desktop files) on places presribed by Desktop Base Directory Specification.

Resource class provides much more than simple read and write and details are explained in class documentation.

Writing Sample Program

Here will be shown a simple console program that will remember number of executions and will report it to the user. Each time program is executed, it will read, change and store number of executions in own settings file.

This example continues on EdelibBasics and BasicEdeApplication, so explaining some mentioned details before will be skipped. Application will be named counter and will be compiled from counter.cpp.

As application uses only base library (libedelib), there is no need to load and link against GUI facility.

/* counter.cpp */
 
#include <edelib/Resource.h>
#include <stdio.h>
 
EDELIB_NS_USING(Resource)
 
int main(int argc, char **argv) {
  int counter; /* number of counted executions */
  Resource r;
 
  /* 
   * load number or set it to zero if file does not exists
   * or format of stored file is not as expected
   */
  if(!r.load("my-counter"))
    counter = 0;
  else
    r.get("MyCounter", "count", counter, 0);
 
  printf("Application run %i times\n", counter);
 
  /* increase number and try to store it */
  counter++;
 
  r.set("MyCounter", "count", counter);
  if(!r.save("my-counter"))
    puts("Unable to store my-counter data!");
 
  return 0;
}

Compiling And Running Sample Program

To compile it, use:

g++ counter.cpp -o counter `pkg-config edelib --cflags --libs`

By running application multiple times, you should get:

> ./counter
Application run 0 times

> ./counter
Application run 1 times

> ./counter
Application run 2 times

Conclusion

If you check $HOME/.config/ede directory, you will see my-counter.conf file and when you display it, you will see stored content, like:

> cat $HOME/.config/ede/my-counter.conf
[MyCounter]
count=3
Print/export