On Tuesday 21 January 2014 16:37:20 Richard W.M. Jones wrote:
On Tue, Jan 21, 2014 at 05:18:27PM +0100, Pino Toscano wrote:
> + sv = caml_copy_string (fields->subkey ? fields->subkey :
"");
>
> Store_field (v, 1, sv);
Heh, sure would be nice if this was an option type :-)
I believe the following should work:
(1) Change CAMLlocal4 (..) at the top of the function to:
CAMLlocal5 (rv, v, sv, sv2, fv);
(2) Then the new code is:
if (fields->subkey) { /* Some subkey */
sv2 = caml_copy_string (fields->subkey);
sv = caml_alloc (1, 0);
Store_field (sv, 0, sv2);
} else /* None */
sv = Val_int (0);
Store_field (v, 1, sv);
Also you will need to make the corresponding adjustment to the field
type, ie. (string, string option, string) instead of this:
> +and field = string * string * string (* key + subkey + value *)
Indeed, they work that way. It seemed not totally clear for the
documentations I've read.
> let entries =
>
> List.map (
>
> fun (n, fields) ->
>
> + let find_elem key subkey fields =
> + match List.filter (
> + fun (iterkey, itersubkey, itervalue) ->
> + iterkey = key && itersubkey = subkey
> + ) fields with
> + | [] -> raise Not_found
> + | (_, _, value) :: _ -> value in
>
> let printable_name =
>
> - try Some (List.assoc "name" fields) with Not_found ->
> None in
> + try Some (find_elem "name" "" fields) with
> Not_found -> None in
What you could do here, which is a bit nicer, is (earlier on):
let fields = List.map (fun (k,sk,v) -> (k,sk),v) fields in
Delete the find_elem function and use instead:
- try Some (List.assoc "name" fields) with Not_found -> None in
+ try Some (List.assoc ("name",None) fields) with Not_found ->
None in
etc. since List.assoc works for any key type, not just strings.
Ah OK.
--
Pino Toscano