Skip to content
This repository
Newer
Older
100644 74 lines (63 sloc) 3.759 kb
fccc6851 » MLstate
2011-06-21 Initial open-source release
1 (*
2 Copyright © 2011 MLstate
3
513a9dfe » Aqua-Ye
2012-07-09 [doc] tools: changed files headers to MIT license
4 This file is part of Opa.
fccc6851 » MLstate
2011-06-21 Initial open-source release
5
513a9dfe » Aqua-Ye
2012-07-09 [doc] tools: changed files headers to MIT license
6 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
fccc6851 » MLstate
2011-06-21 Initial open-source release
7
513a9dfe » Aqua-Ye
2012-07-09 [doc] tools: changed files headers to MIT license
8 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
fccc6851 » MLstate
2011-06-21 Initial open-source release
9
513a9dfe » Aqua-Ye
2012-07-09 [doc] tools: changed files headers to MIT license
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
fccc6851 » MLstate
2011-06-21 Initial open-source release
11 *)
12
293dd4cd » Raja
2011-06-28 [cleanup] open: remove Base in tool
13 (* depends *)
14 module String = BaseString
15
16 (** Simplier version of ofile + retourned type is an option (safer) *)
fccc6851 » MLstate
2011-06-21 Initial open-source release
17
18 let extension = "of"
a9f8d34c » Raja
2011-06-28 [cleanup] Base: remove sprintf
19 let error s = prerr_endline (Printf.sprintf "[!] ofile : %s" s); exit 1
fccc6851 » MLstate
2011-06-21 Initial open-source release
20 let do_verbose = ref false
21 let verbose s =
a9f8d34c » Raja
2011-06-28 [cleanup] Base: remove sprintf
22 if !do_verbose then prerr_endline (Printf.sprintf "ofile : %s" s)
fccc6851 » MLstate
2011-06-21 Initial open-source release
23
24 let safe_content file =
25 try (String.escaped (File.content file))
a9f8d34c » Raja
2011-06-28 [cleanup] Base: remove sprintf
26 with Unix.Unix_error(e,s0,s1) -> error (Printf.sprintf "cannot load \"%s\" from \"%s\" [%s,%s,%s]" file (Sys.getcwd ()) (Unix.error_message e) s0 s1)
fccc6851 » MLstate
2011-06-21 Initial open-source release
27
28 let win2unix f =
29 let res = String.copy f in
30 let n = String.length res in
31 for i = 0 to n-1 do
32 if res.[i] = '\\' then res.[i] <- '/'
33 done;
34 res
35
36 let iter_of path file =
37 if File.extension file <> extension
a9f8d34c » Raja
2011-06-28 [cleanup] Base: remove sprintf
38 then error (Printf.sprintf "input file \"%s\" must have .%s extension" file extension)
fccc6851 » MLstate
2011-06-21 Initial open-source release
39 else
40 if Sys.file_exists file && not (Sys.is_directory file)
41 then
42 let buf = FBuffer.create 1024 in
43 let buf = FBuffer.addln buf "let get_file = function" in
44 let files_list = List.rev
45 (File.lines_fold
46 (fun acc line -> let t = String.trim line in if t = "" then acc else t::acc)
47 [] file) in
48 let portable_path _path file =
49 let path_file =(*_path^*)file in
50 (Mlstate_platform.platform_dependent
51 ~unix:PathTransform.string_to_unix
52 ~windows:PathTransform.string_to_windows ()) path_file
53 in
54 let buf = List.fold_left
a9f8d34c » Raja
2011-06-28 [cleanup] Base: remove sprintf
55 (fun buf t -> FBuffer.addln buf (Printf.sprintf " | %S -> Some \"%s\"" t (safe_content (portable_path path t) ))) buf files_list in
fccc6851 » MLstate
2011-06-21 Initial open-source release
56 let buf = FBuffer.addln buf " | _ -> None\n" in
57 let buf = FBuffer.addln buf "let file_list =" in
a9f8d34c » Raja
2011-06-28 [cleanup] Base: remove sprintf
58 let buf = FBuffer.addln buf (String.concat_map ~left:"[" ~right:"]" "; " (fun f -> Printf.sprintf "%S" f) files_list) in
fccc6851 » MLstate
2011-06-21 Initial open-source release
59 let out = (File.chop_extension file)^".ml" in
60 if File.output out (FBuffer.contents buf)
a9f8d34c » Raja
2011-06-28 [cleanup] Base: remove sprintf
61 then verbose (Printf.sprintf "write file \"%s\"" out)
62 else error (Printf.sprintf "cannot write file \"%s\" from \"%s\"" out (Sys.getcwd ()))
fccc6851 » MLstate
2011-06-21 Initial open-source release
63
64 let _ =
65 (** please, keep the possibility of this option for other potential users of this generic application *)
66 let path = ref "" and empty = ref true in
67 Arg.parse
68 [ "-v", Arg.Set do_verbose, " verbose";
69 "-path", Arg.Set_string path, "<dir> prefix directory for files"
70 ]
71 (fun t -> iter_of !path t; empty := false)
9a4a8eed » Mathieu Baudet
2011-12-19 [enhance] help/manpages: global pass for improving help messages of O…
72 (Printf.sprintf "%s: embedded file ocaml preprocessor\nUsage: %s [options]\nOptions:" Sys.argv.(0) Sys.argv.(0));
fccc6851 » MLstate
2011-06-21 Initial open-source release
73 if !empty
a9f8d34c » Raja
2011-06-28 [cleanup] Base: remove sprintf
74 then error (Printf.sprintf "no input files (give me some \"%s\" file)" extension)
Something went wrong with that request. Please try again.