-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
find_free_msg_id.ml
109 lines (95 loc) · 3.25 KB
/
find_free_msg_id.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
(*
* Call to Makefile.ac with the appropriate attributes from conf.xml
*
* Copyright (C) 2003-2010 ENAC
*
* This file is part of paparazzi.
*
* paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* paparazzi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with paparazzi; see the file COPYING. If not, write to
* the Free Software Foundation, 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*)
(******************************************************************************)
(* quick and dirty messages.xml free id tool *)
(* Usage: *)
(* ./find_free_msg_id *)
(* print free id of PAPARZZI_HOME/conf/_messages.xml *)
(******************************************************************************)
(** FIXME: Get file names with Arg.parse *)
let (//) = Filename.concat
let messages_xml = Env.paparazzi_home // "var" // "messages.xml"
let nb_msg = 255
(* parse the messages.xml file *)
let used_messages_id = fun xml ->
let class_xmls = Xml.children xml in
let id_of_message = fun xml ->
int_of_string (Xml.attrib xml "id")
in
let rec find_message_ids = fun xml ids ->
if Xml.tag xml = "message"
then (id_of_message xml) ::ids
else List.fold_right
(fun c l -> find_message_ids c l) (Xml.children xml) ids
in
List.map
(fun c -> ((Xml.attrib c "name"), find_message_ids c [])) class_xmls
(* useful to display grouped ids *)
let group = fun l ->
let gl = ref [] in
try
let s = ref (List.hd l) in
let n = List.length l in
for i=1 to n-1 do
let li = List.nth l i and
li1= List.nth l (i-1) in
if (li - li1 > 1)
then begin
gl :=
(
if !s = li1
then Printf.sprintf "%d" !s
else Printf.sprintf "%d-%d" !s li1) :: !gl;
s := li;
end
else if i = n -1
then
gl := (Printf.sprintf "%d-%d" !s li) :: !gl;
done;
List.rev !gl
with Not_found -> []
(* MAIN *)
let () =
(* reading files *)
let xml = ExtXml.parse_file messages_xml in
let messages =
List.map (fun c -> ((Xml.attrib c "name"), Xml.children c)) (Xml.children xml)
in
let id_list = Array.to_list (Array.init nb_msg (fun i -> i+1)) in
(* finding free IDs *)
let free_msg_id =
let umi = used_messages_id xml in
List.map (fun (c,_) ->
if List.mem_assoc c umi
then let used = List.assoc c umi in
(c,List.filter (fun i -> not (List.mem i used)) id_list)
else (c,id_list)
) messages in
(* print free IDs *)
List.iter (fun (c, l) ->
Printf.printf "Class : %s \n" c;
(*group l*)
List.iter (fun id -> Printf.printf " %d," id) l;
Printf.printf "\n\n"
) free_msg_id