Differences

This shows you the differences between two versions of the page.

Link to this comparison view

storingapplicationsettings [2015/10/03 14:54]
storingapplicationsettings [2015/10/03 14:54] (current)
Line 1: Line 1:
 +====== 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 [[http://​equinox-project.org/​api/​edelib/​classedelib_1_1Resource.html | 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 [[http://​www.freedesktop.org/​wiki/​Specifications/​basedir-spec | 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.
 +
 +<code cpp>
 +/* 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;
 +}
 +</​code>​
 +
 +===== Compiling And Running Sample Program =====
 +
 +To compile it, use:
 +<​code>​
 +g++ counter.cpp -o counter `pkg-config edelib --cflags --libs`
 +</​code>​
 +
 +By running application multiple times, you should get:
 +<​code>​
 +> ./counter
 +Application run 0 times
 +
 +> ./counter
 +Application run 1 times
 +
 +> ./counter
 +Application run 2 times
 +</​code>​
 +
 +===== 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:
 +<​code>​
 +> cat $HOME/​.config/​ede/​my-counter.conf
 +[MyCounter]
 +count=3
 +</​code>​
Print/export