Previous Up Next

Chapter 12  Other libraries

12.1  Introduction

Cameleon offers other libraries which can be of great help to develop applications. They are described below.

12.2  Options

This library is used to manage the configuration file(s) of an application. You simply define your options and it performs the loading and saving of the options. Each option is defined from an option class (for example string_option) or from a combination of classes (for example list_option string_option).

Values of options are read and written with the two operators !! and =:=.

The format of the configuration file looks like OCaml code, and is human readable.

The library interface is described below, then you can find a commented example.

12.2.1  The Options library

Here is the interface of the Options library.

12.2.2  Example

This example is inspired from the make plug-in of Cameleon.
open Options

(* We create an option file *)
let op_file = create_options_file "my_option_file"

(* We define a new option, named "targets" in the configuration file.
   The name is a list of strings, to allows nested groups of options
   in the configuration file. Try to give an option name with more 
   than one string, and you will see that the option appears as 
   a record field in the configuration file.*)
let op_targets = define_option op_file ["targets"]
    "some help text" (* this help text will be saved in the configuration file
                        if you use the save_with_help function *)
    (list_option string_option) (* our option is a list of strings *)
    ["all" ; "byte" ; "clean" ; "depend" ; "doc" ; "opt" ; "%s"]
                                (* and the list above is its default value *)

(* We load the file, reading the values of the defined options. *)
let _ = load op_file

(* We save the file, with the help text as comments in the file. *)
let _ = save_with_help op_file

(* We can read the op_targets value with the !! operator *)
let l = !!op_targets

(* We can set the value of op_targets with the =:= operator *)
let _ = op_targets =:= [ "my" ; "new" ; "liste"]

12.3  Okey

This library contains some convenient functions to add handlers of key press events in LablGtk applications.

12.3.1  The Okey library

Here is the interface of the Okey library.

12.4  Configwin

This library offers convenient functions to make the user edit values (lists, strings, booleans, colors, fonts,...) in LablGtk applications. Here is the library interface, followed by an example.

12.4.1  The Configwin library

Here is the interface of the Configwin library.

12.4.2  Example

let _ = GMain.Main.init ()

open Configwin

let param1 = string ~help: "a string" "a string" "a value" 
let param2 = bool ~help: "bool value" "a boolean" true
let param3 = filename ~help: "a file name" "a file name" "foo" 
let param4 = strings 
    ~help: "a list of strings"
    ~eq: (fun _ -> fun _ -> false)
    ~add: (fun () -> ["another string" ; "and another string"])
    "a string list" 
    ["foo" ; "bar"] 
let param5 = color ~help: "a color" "a color" "Red" 
let param6 = font ~help: "a font" "a font" "7x13bold" 
let param7 = date ~help: "a date" "a date" (1, 0, 2002)
let n = ref 0
let param8 = list 
    ~help: "a list of int"
    ~add: (fun () -> incr n; [!n])
    ~titles: ["n" ; "n * n"]
    "an int list" (fun n -> [string_of_int n ; string_of_int (n*n)])
    [1 ; 2 ; 3]
let param9 = filenames ~help: "a list of filenames" "filenames" []
    
let structure = Section_list 
  ("Section 1", 
   [
    Section ("Section 1.1",
             [ param1 ; param2 ; param5 ; param6 ; param7 ; param9]);
    Section ("Section 1.2",
             [ param3 ; param4 ; param8])
   ] 
  )

let _ = Configwin.edit "Titre" [structure]
Figure 12.1 shows the window displayed.



Figure 12.1: Configwin example.



12.5  IoXML

This library is included in Cameleon thanks to its author Daniel de Rauglaudre. The following text comes from the README file of the IoXML distribution.

IoXML is a Camlp4 syntax extension for OCaml mli and ml files which generates XML parsers and printers for all types you define.

12.5.1  Usage

If we have two files "foo.mli" and "foo.ml":

Compilation:
ocamlc -pp "camlp4o pa_ioXML.cmo" -I +camlp4 -c foo.mli
ocamlc -pp "camlp4o pa_ioXML.cmo" -I +camlp4 -c foo.ml


Pretty print (for info):
camlp4o pa_ioXML.cmo pr_o.cmo foo.mli
camlp4o pa_ioXML.cmo pr_o.cmo foo.ml


Linking:
ocamlc -I +camlp4 ioXML.cmo ...files...

12.5.2  Details

The generated functions are, for each type "bar": The function xparse_bar takes a XML tree as parameter, defined in IoXML.ast. To get it from a file (or any stream), the library function IoXML.parse_xml_list reads XML input. Another function IoXML.parse_xml reads just one XML item.

If the type has parameters, the parser and printer functions receive as first parameters, the parser and printers for them.

The function xprint_bar takes a Format.formatter function as parameter.

12.5.3  Example

For example if the type bar is defined as:
    type ('a, 'b) bar =
      Leaf of 'a
    | Node of 'b * ('a, 'b) bar * ('a, 'b) bar
The generated functions are of types:
    xparse_bar:
       (IoXML.ast -> 'a) -> (IoXML.ast -> 'b) -> IoXML.ast -> ('a, 'b) bar   
    xprint_bar:
       (Format.formatter -> 'a -> unit) ->
       (Format.formatter -> 'b -> unit) ->
            Format.formatter -> ('a, 'b) bar -> unit
If we have e.g. values of type (int, string) bar, a typical parsing usage is:
      let ic = open_in file_name in
      let strm = Stream.of_channel ic in
      let v =
         xparse_bar IoXML.xparse_int IoXML.xparse_string
            (IoXML.parse_xml strm)
      in ...
and a typical printing usage is (see standard module Format):
      let ppf = Format.std_formatter in
      IoXML.xprint ppf "@[%a@]@."
        (xprint_bar IoXML.xprint_int IoXML.xprint_string) v;

12.6  Gpattern

This library offers convenient classes to use some LablGtk widgets. by now it only has one class, allowing to quickly build a clist with some events already bound and handling the selection of elements.

12.6.1  The Gpattern library

Here is the interface of the Gpattern library.


Previous Up Next