Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 75 lines (63 sloc) 3.759 kB
fccc685 Initial open-source release
MLstate authored
1 (*
2 Copyright © 2011 MLstate
3
513a9df @Aqua-Ye [doc] tools: changed files headers to MIT license
Aqua-Ye authored
4 This file is part of Opa.
fccc685 Initial open-source release
MLstate authored
5
513a9df @Aqua-Ye [doc] tools: changed files headers to MIT license
Aqua-Ye authored
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:
fccc685 Initial open-source release
MLstate authored
7
513a9df @Aqua-Ye [doc] tools: changed files headers to MIT license
Aqua-Ye authored
8 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
fccc685 Initial open-source release
MLstate authored
9
513a9df @Aqua-Ye [doc] tools: changed files headers to MIT license
Aqua-Ye authored
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.
fccc685 Initial open-source release
MLstate authored
11 *)
12
293dd4c [cleanup] open: remove Base in tool
Raja authored
13 (* depends *)
14 module String = BaseString
15
16 (** Simplier version of ofile + retourned type is an option (safer) *)
fccc685 Initial open-source release
MLstate authored
17
18 let extension = "of"
a9f8d34 [cleanup] Base: remove sprintf
Raja authored
19 let error s = prerr_endline (Printf.sprintf "[!] ofile : %s" s); exit 1
fccc685 Initial open-source release
MLstate authored
20 let do_verbose = ref false
21 let verbose s =
a9f8d34 [cleanup] Base: remove sprintf
Raja authored
22 if !do_verbose then prerr_endline (Printf.sprintf "ofile : %s" s)
fccc685 Initial open-source release
MLstate authored
23
24 let safe_content file =
25 try (String.escaped (File.content file))
a9f8d34 [cleanup] Base: remove sprintf
Raja authored
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)
fccc685 Initial open-source release
MLstate authored
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
a9f8d34 [cleanup] Base: remove sprintf
Raja authored
38 then error (Printf.sprintf "input file \"%s\" must have .%s extension" file extension)
fccc685 Initial open-source release
MLstate authored
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
a9f8d34 [cleanup] Base: remove sprintf
Raja authored
55 (fun buf t -> FBuffer.addln buf (Printf.sprintf " | %S -> Some \"%s\"" t (safe_content (portable_path path t) ))) buf files_list in
fccc685 Initial open-source release
MLstate authored
56 let buf = FBuffer.addln buf " | _ -> None\n" in
57 let buf = FBuffer.addln buf "let file_list =" in
a9f8d34 [cleanup] Base: remove sprintf
Raja authored
58 let buf = FBuffer.addln buf (String.concat_map ~left:"[" ~right:"]" "; " (fun f -> Printf.sprintf "%S" f) files_list) in
fccc685 Initial open-source release
MLstate authored
59 let out = (File.chop_extension file)^".ml" in
60 if File.output out (FBuffer.contents buf)
a9f8d34 [cleanup] Base: remove sprintf
Raja authored
61 then verbose (Printf.sprintf "write file \"%s\"" out)
62 else error (Printf.sprintf "cannot write file \"%s\" from \"%s\"" out (Sys.getcwd ()))
fccc685 Initial open-source release
MLstate authored
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)
9a4a8ee [enhance] help/manpages: global pass for improving help messages of O…
Mathieu Baudet authored
72 (Printf.sprintf "%s: embedded file ocaml preprocessor\nUsage: %s [options]\nOptions:" Sys.argv.(0) Sys.argv.(0));
fccc685 Initial open-source release
MLstate authored
73 if !empty
a9f8d34 [cleanup] Base: remove sprintf
Raja authored
74 then error (Printf.sprintf "no input files (give me some \"%s\" file)" extension)
Something went wrong with that request. Please try again.