| version 1.9, 2002/10/07 10:54:40 |
version 1.10, 2003/08/16 01:43:42 |
|
|
| <!-- $Platon: usage.context-manipulation.sgml,v 1.7 2002/09/02 14:12:57 nepto Exp $ --> |
<!-- $Platon: libcfg+/doc/sgml/usage.context-manipulation.sgml,v 1.9 2002/10/07 10:54:40 rajo Exp $ --> |
| |
|
| <!-- MAN: |
<!-- MAN: |
| <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook V3.1//EN" [ |
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook V3.1//EN" [ |
|
|
| <!-- MAN: <refsect1><title>SYNOPSIS | Creating context</title> --> |
<!-- MAN: <refsect1><title>SYNOPSIS | Creating context</title> --> |
| <para> |
<para> |
| |
|
| Parsing capabilities of &name; library are accessed via |
The parsing capabilities of &name; are accessed through a |
| <firstterm>context</firstterm>. It allows you to parse multiple configuration |
<firstterm>context</firstterm>, which is defined by the |
| sets. For particular command lines and/or configuration files you can have |
<structname>CFG_CONTEXT</structname> structure. By using contexts, one can |
| different contexes. They are implemented like |
parse several configuration files and the command line at the same time. |
| <structname>CFG_CONTEXT</structname> data type. |
|
| |
|
| </para> |
</para> |
| <para> |
<para> |
| |
|
| Here are functions used for creating context. Functions used for freeing |
The functions used for creating a new context are described below. Those used |
| (destroying) context are described at the end of this <!-- MAN: man -->page. |
for freeing (destroying) a context are described at the end of this |
| |
<!-- MAN: man -->page. |
| |
|
| <funcsynopsis> |
<funcsynopsis> |
| <funcsynopsisinfo><![CDATA[#include <cfg+.h>]]></funcsynopsisinfo> |
<funcsynopsisinfo><![CDATA[#include <cfg+.h>]]></funcsynopsisinfo> |
| Line 89 Here are functions used for creating con |
|
| Line 89 Here are functions used for creating con |
|
| <!-- MAN: </refsect1><refsect1><title>DESCRIPTION</title> --> |
<!-- MAN: </refsect1><refsect1><title>DESCRIPTION</title> --> |
| <para> |
<para> |
| |
|
| First function <function>cfg_get_context()</function> initializes context. |
The function <function>cfg_get_context()</function> initializes a context but |
| Although it is initialized, it also needs to be set to one from the command line |
does not associate it with either the command line or a file. It takes a |
| or configuration file type. Parameter <parameter>options</parameter> defines |
pointer to the option set <parameter>*options</parameter> and returns a new |
| options set. Options set was described in previous chapter. |
context. The association with the command line or a file can be done with |
| |
<function>cfg_set_cmdline_context()</function> and |
| |
<function>cfg_set_cfgline_context()</function> respectively. |
| |
|
| </para> |
</para> |
| <para> |
<para> |
| |
|
| For full context initialization there are two functions |
The function <function>cfg_get_cmdline_context()</function> is used for full |
| <function>cfg_get_cmdline_context()</function> and |
context initialization, which includes the initialization itself and assigning |
| <function>cfg_get_cfgfile_context()</function>. There are also functions |
the newly created context to the command line. The |
| <function>cfg_set_cmdline_context()</function> and |
<parameter>begin_pos</parameter> parameter stores the starting parsing |
| <function>cfg_set_cfgfile_context()</function> for setting up already existing |
position in the array of command line arguments. The |
| context. They have parameters with the same meanings as their equivalents with |
<parameter>size</parameter> parameter defines the number of arguments (array |
| one difference that they are modifying already existing context and nothing |
elements) to parse. If a negative value is passed, the parsing will stop only |
| returning. With these functions you can change type already existing context. So |
when an <constant>NULL</constant> is encountered. The |
| for example if you have finished parsing of command line and wanted to continue |
<parameter>argv</parameter> parameter is a pointer to a |
| with configuration file parsing relating the same option set, just change your |
<constant>NULL</constant> terminated array of strings (options + arguments) to |
| context type and parse. |
be parsed. As most of you probably know, <parameter>argv</parameter> is the |
| |
second parameter passed to the <function>main()</function> function. |
| |
|
| </para> |
</para> |
| <para> |
<para> |
| |
|
| Function <function>cfg_get_cmdline_function()</function> initializes context and |
The function <function>cfg_get_cfgfile_function()</function> initializes a new |
| sets it to command line. Parameter <parameter>begin_pos</parameter> tells |
context and associates it with a configuration file. The parameter |
| starting position in dynamic array of command line arguments. Than parameter |
<parameter>begin_pos</parameter> determines the file offset at which the |
| <parameter>size</parameter> defines number of arguments (array elements) to |
parsing will start. Whether the offset is expressed in bytes or lines depends |
| parse. If negative value is passed, it means infinite, so the parsing of command |
on the value of the context flag <constant>CFG_FILE_LINE_POS_USAGE</constant>. |
| line will finish only if ending NULL is reached. Next parameter |
If the flag is unset (the default value), <parameter>begin_pos</parameter> |
| <parameter>argv</parameter> contain NULL terminated array of strings with these |
should be interpreted as a byte offset. If the flag is set, |
| arguments. They can be passed just as they are passed to the |
<parameter>begin_pos</parameter> is a line offset. See the <xref |
| <function>main()</function> function. |
linkend="usage.context-flags" endterm="usage.context-flags.title"> section for |
| |
more information about this. Whatever the case, the offset is zero based. The |
| |
<parameter>size</parameter> parameter defines the number of bytes or lines to |
| |
parse. If the value is negative, the file will be parsed in its entirety. The |
| |
name of the file to parse is passed in <parameter>filename</parameter>. |
| |
|
| </para> |
</para> |
| <para> |
<para> |
| |
|
| Function <function>cfg_get_cfgfile_function()</function> initializes context and |
<function>cfg_set_cmdline_context()</function> and |
| sets it to configuration file. Parameter <parameter>begin_pos</parameter> tells |
<function>cfg_set_cfgfile_context()</function> work on an already existing |
| starting position in file for parsing. Type of position, byte or line, depends |
context. These functions accept the existing context in their first parameter. |
| on fact, if context flag <constant>CFG_FILE_LINE_POS_USAGE</constant> is set. By |
The rest of their arguments are the same as those of |
| default they are expected as byte possition, but they can be changed to line |
<function>cfg_get_cfgfile_function()</function> and |
| position by this context flag. See <xref linkend="usage.context-flags" |
<function>cfg_get_cmdline_context()</function>. Note that the two sets of |
| endterm="usage.context-flags.title"> section for more informations about this |
functions differ in their return values. The context setting functions return |
| issue. But anyway, all position types are counted from <constant>0</constant>. |
void while the context creating functions return a new context. Working with |
| Next parameter <parameter>size</parameter> defines number of bytes or lines to |
existing contexts may be more convenient than creating new contexts from |
| parse. If negative value is passed, it means infinite, so the parsing of confing |
scratch, especially when there is a need to switch from parsing a file to |
| file will finish only if end of file is reached. Filename to parse is passed as |
parsing the command line and vice versa. |
| the third <parameter>filename</parameter> parameter. |
|
| |
|
| </para> |
</para> |
| <para> |
<para> |
| |
|
| Please note, that these context creating and setting function are mentioned to |
Note, that while <parameter>begin_pos</parameter> of the file context defines |
| be similar for both context types. That is the reason, why also command line |
the starting parsing offset in a file, the same parameter for the command line |
| context has parameter <parameter>begin_pos</parameter>, when is also possible to |
context specifies the offset in the <parameter>argv</parameter> array. Note |
| change beginning of array parsing by passing different pointer for the parameter |
also that all offsets are zero-based. For example, the third line of a file is |
| <parameter>argv</parameter>. Also note, that all positions are counted from |
at offset <constant>2</constant>. However, the error handling |
| <constant>0</constant>. For example third line of file is at position |
routines of &name; increment all offsets by one when reporting an error, thus |
| <constant>2</constant>. Although this, standart &name; error handling functions |
human readable messages are returned. So for the first position on command |
| will already print human readable position, so for the first position on command |
line it will print something like |
| line it will print something like "on position 1", for second line in config |
<computeroutput>on position 1</computeroutput>, for second line in |
| file it will print "on line 2" and so on. |
configuration file it will print |
| |
<computeroutput>on line 2</computeroutput> and so on. |
| |
|
| </para> |
</para> |
| <para> |
<para> |
| |
|
| From version 0.5.2 there is also |
Starting from version 0.5.2, the |
| <function>cfg_get_cmdline_context_argc()</function> function. It is used for |
<function>cfg_get_cmdline_context_argc()</function> and |
| context initialization by passing <parameter>argc</parameter> and |
<function>cfg_set_cmdline_context_argc()</function> functions became |
| <parameter>argv</parameter> parameters directly from <function>main()</function> |
available. It creates a new context by passing the |
| function. It has similar parameters as |
<parameter>argc</parameter> and <parameter>argv</parameter> parameters |
| <function>cfg_get_cmdline_context()</function>, but instead of |
directly from <function>main()</function> function. Its parameters are similar |
| <parameter>begin_pos</parameter> and <parameter>size</parameter> long parameters |
to those of <function>cfg_get_cmdline_context()</function>, but instead of |
| is only one <parameter>argc</parameter> integer parameter present. Of course, |
<parameter>begin_pos</parameter> and <parameter>size</parameter>, only |
| you can also use <function>cfg_set_cmdline_context_argc()</function> equivalent. |
<parameter>argc</parameter> is present. Whether one is more convenient than |
| |
the other depends on the circumstances and it is for you to decide which one |
| |
suits you best. |
| |
|
| </para> |
</para> |
| <para> |
<para> |
| |
|
| Another macros used for initialization are |
There are also two groups of macros for managing contexts. The first group |
| <function>cfg_get_cmdline_context_pos()</function> and |
includes <function>cfg_get_cmdline_context_pos()</function> and |
| <function>cfg_get_cfgfile_context_pos()</function>. They differ from their |
<function>cfg_get_cfgfile_context_pos()</function> and is used for creating a |
| standart equivalent functions in parameter <parameter>size</parameter>, which is |
new context of the needed type. You can use the second group, which consist of |
| replaced by parameter <parameter>end_pos</parameter>. It defines the last |
<function>cfg_set_cmdline_context_pos()</function> and |
| element (array member, byte or line in file) which will be parsed. Also here you |
<function>cfg_set_cfgfile_context_pos()</function>, for manipulating an |
| can use analogicaly <function>cfg_set_cmdline_context_pos()</function> and |
existing context. The difference between these four macros and their function |
| <function>cfg_set_cfgfile_context_pos()</function> macros. |
counterparts is in the <parameter>size</parameter> parameter, which is |
| |
replaced by <parameter>end_pos</parameter> and specifies the offset of the |
| |
last element to be parsed. For a file context the value is the byte offset (or |
| |
line number) while for a command line context it is the offset in the |
| |
<parameter>argv</parameter> array. |
| |
|
| </para> |
</para> |
| <!-- MAN: </refsect1><refsect1><title>SYNOPSIS | Freeing context</title> --> |
<!-- MAN: </refsect1><refsect1><title>SYNOPSIS | Freeing context</title> --> |
| <para> |
<para> |
| |
|
| Here are further functions. They are used for reseting and freeing context: |
The following functions are used for reseting and freeing a context. |
| |
|
| <funcsynopsis> |
<funcsynopsis> |
| <funcsynopsisinfo><![CDATA[#include <cfg+.h>]]></funcsynopsisinfo> |
<funcsynopsisinfo><![CDATA[#include <cfg+.h>]]></funcsynopsisinfo> |
| Line 202 Here are further functions. They are use |
|
| Line 215 Here are further functions. They are use |
|
| <!-- MAN: </refsect1><refsect1><title>DESCRIPTION</title> --> |
<!-- MAN: </refsect1><refsect1><title>DESCRIPTION</title> --> |
| <para> |
<para> |
| |
|
| There are often situations, that reseting of context is neccessary. In command |
You may come across situations when a context reset is necessary. You can use |
| line context type current position is returned to the beginning of command line |
<function>cfg_reset_context()</function> to perform this task. When this |
| arguments specified by parameter <parameter>begin_pos</parameter>. Similary in |
function is called on a command line context, the offset in the |
| config file context type, position in file is set to |
<parameter>argv</parameter> is set to the value of |
| <parameter>begin_pos</parameter> position. Context flags and context properties |
<parameter>begin_pos</parameter>. For a file context, the offset is reset to |
| remains unchanged. For this purpose is <function>cfg_reset_context()</function> |
the value of <parameter>begin_pos</parameter>. Context flags and context |
| function provided. |
properties remain unchanged after a call to |
| |
<function>cfg_reset_context()</function>. |
| |
|
| </para> |
</para> |
| <para> |
<para> |
| |
|
| After parsing of configuration options is complete, context should be freed. The |
After you are done with a context, you should use |
| last function <function>cfg_free_context()</function> frees passed |
<function>cfg_free_context()</function> to free the resources associated with |
| <structname>CFG_CONTEXT</structname> structure. It will not frees allocated |
it. The function takes a context in its only parameter |
| arguments or argument arrays set in any <structfield>value</structfield> member |
<parameter>con</parameter>. The function does not deallocate the memory for |
| of your options set. But it will free all internal context structures including |
any arguments of argument arrays you might have allocated in your option set. |
| context flags and context properties, also with context itself. |
You need to free that memory manually. However, it will free all internal |
| |
context structures, which includes the context flags, context properties, and |
| |
the context itself. |
| |
|
| </para> |
</para> |
| </sect1> |
</sect1> |