In this section we will expand the following simple program ``hello01.c'':
int main(int argc, char *argv[]) { printf("Hello world!\n"); printf("Goodbye world!\n"); exit(0); return 0; }
We change the source to hello02.c:
static char pkgname[]= { "hello" }; static char sysconfdir[] = { "/etc" }; static int success = 0; int main(int argc, char *argv[]) { dk_app_t *app = NULL; app = dkapp_open_ext1(argc, argv, pkgname, sysconfdir, 0, 0); if(app) { success = 1; printf("Hello world!\n"); printf("Goodbye world!\n"); dkapp_close(app); app = NULL; } success = (success ? 0 : 1); exit(success); return success; }
If we run
./hello02
we see the expected output
Hello world! Goodbye world!
If we run
./hello02 --/log/stdout/level=progress
we see some log messages:
Application "hello02" started. Hello world! Goodbye world! Application "hello02" finished.
The ``--/...'' arguments are preference overwrites. Here we overwrite the ``/log/stdout/level'' preference which configures logging to standard output.
If we run
./hello02 --/log/file/keep=debug
we will find a file hello02.11310.log (the number in the middle can differ) in the home directory:
Application "hello02" started. Application "hello02" finished.
The ``/log/file/keep'' preference controls whether or not a log file is removed when an application exits.
In your home directory create a subdirectory ``.defaults'' (on
Unix/Linux systems) or ``defaults'' (on Windows systems).
In this directory we need some files named ``hello02'',
``hello03'', ``hello04''...
Contents should be as follows:
/ui/lang = en /dir/app = /home/myname/src/share/DOCU/tutorial /log/file/level = debug /log/file/keep = error /log/stderr/level = progress
For ``/dir/app'' use the directory where the tutorial files are
located.
The next preferences control logging: Very detailed messages
(debug) are written to the logfile. The logfile is deleted when the
application exits unless there was at least one error message or a
higher priorized message.
Progress messages and higher priorized messages are shown on the
standard error output. Log message priority levels are ``none'',
``panic'', ``fatal'', ``error'', ``warning'', ``info'',
``progress'' and ``debug''.
We change the source to hello03.c:
static char pkgname[]= { "hello" }; static char sysconfdir[] = { "/etc" }; static int success = 0; static dk_key_value_t kv[] = { { "hello", "Hello world!" }, { "goodbye", "Goodbye world!" } }; static size_t szkv = sizeof(kv)/sizeof(dk_key_value_t); int main(int argc, char *argv[]) { dk_app_t *app = NULL; char **msg = NULL; app = dkapp_open_ext1(argc, argv, pkgname, sysconfdir, 0, 0); if(app) { msg = dkapp_find_key_value(app, kv, szkv, "h03msg"); if(msg) { success = 1; printf("%s\n", msg[0]); printf("%s\n", msg[1]); dk_delete(msg); } dkapp_close(app); app = NULL; } success = (success ? 0 : 1); exit(success); return success; }
We create a string table source file h03msg.str:
"hello" en = "Hello, I am an internationalized program." sp = "Buenos dias." de = "Moin moin." "goodbye" en = "Now it's time to say goodbye." sp = "Adios muchachos." de = "Und wech."
Each string translation consists of the string name (key) in
double quotes as used in the dk_key_value_t array in the
program source and a list of
``language[_region][.encoding]="text"''
pairs containing one text for each language/region/encoding
combination.
To convert the string table source file into the binary string tables files read by the ``dkstt.c'' or ``dkapp.c'' module we run
stc h03msg.str .
Now we see new directories ``en'', ``sp'' and ``de'' containing resources for one language each.
We change the source to hello04.c.
static char pkgname[]= { "hello" }; static char sysconfdir[] = { "/etc" }; static int success = 0; static dk_key_value_t kv[] = { { "/msg/pan", "Test panic message!" }, { "/msg/fat", "Test message for fatal error!" }, { "/msg/err", "Test error message!" }, { "/msg/wrn", "Test warning message!" }, { "/msg/inf", "Test information message." }, { "/msg/prg", "Test progress message." }, { "/msg/dbg", "Test debug message." } }; static size_t szkv = sizeof(kv)/sizeof(dk_key_value_t); static char *an_array_of_strings[] = { "Jim ", "and Joe", " are drinking beer.", }; int main(int argc, char *argv[]) { dk_app_t *app = NULL; char **msg = NULL; app = dkapp_open_ext1(argc, argv, pkgname, sysconfdir, 0, 0); if(app) { msg = dkapp_find_key_value(app, kv, szkv, "h04msg"); if(msg) { success = 1; dkapp_log_msg(app, DK_LOG_LEVEL_PANIC, &(msg[0]), 1); dkapp_log_msg(app, DK_LOG_LEVEL_FATAL, &(msg[1]), 1); dkapp_log_msg(app, DK_LOG_LEVEL_ERROR, &(msg[2]), 1); dkapp_log_msg(app, DK_LOG_LEVEL_WARNING, &(msg[3]), 1); dkapp_log_msg(app, DK_LOG_LEVEL_INFO, &(msg[4]), 1); dkapp_log_msg(app, DK_LOG_LEVEL_PROGRESS, &(msg[5]), 1); dkapp_log_msg(app, DK_LOG_LEVEL_DEBUG, &(msg[6]), 1); dkapp_log_msg(app, DK_LOG_LEVEL_INFO, an_array_of_strings, 3); dk_delete(msg); } dkapp_close(app); app = NULL; } success = (success ? 0 : 1); exit(success); return success; }
static char pkgname[]= { "hello" }; static char sysconfdir[] = { "/etc" }; static int success = 0; int main(int argc, char *argv[]) { dk_app_t *app = NULL; int i, my_argc; char **my_argv, **lfdptr; app = dkapp_open_ext1(argc, argv, pkgname, sysconfdir, 0, 0); if(app) { success = 1; my_argc = dkapp_get_argc(app); my_argv = dkapp_get_argv(app); lfdptr = my_argv; i = 0; while(i < my_argc) { if(*lfdptr) { printf("%5d \"%s\"\n", i, *lfdptr); } i++; lfdptr++; } dkapp_close(app); app = NULL; } success = (success ? 0 : 1); exit(success); return success; }
static char pkgname[]= { "hello" }; static char sysconfdir[] = { "/etc" }; static char space[] = { " " }; static int success = 0; int main(int argc, char *argv[]) { dk_app_t *app = NULL; int i, my_argc; char *cmdline, **my_argv, **lfdptr; size_t lgt; app = dkapp_open_ext1(argc, argv, pkgname, sysconfdir, 0, 0); if(app) { success = 1; my_argc = dkapp_get_argc(app); my_argv = dkapp_get_argv(app); lfdptr = my_argv; i = 0; lgt = 0; while(i < my_argc) { if(*lfdptr) { lgt += strlen(*lfdptr); } i++; lfdptr++; } lgt += my_argc; cmdline = dk_new(char,lgt); if(cmdline) { lfdptr = my_argv; i = 0; while(i < my_argc) { if(*lfdptr) { if(i > 0) { strcat(cmdline, space); strcat(cmdline, *lfdptr); } else { strcpy(cmdline, *lfdptr); } i++; lfdptr++; } } printf("Command line: %s\n", cmdline); dk_delete(cmdline); } dkapp_close(app); app = NULL; } success = (success ? 0 : 1); exit(success); return success; }