Commits

camlspotter committed 09cd0dd

added at macro language

  • Participants
  • Parent commits 0150d8d

Comments (0)

Files changed (5)

 OCAML_BYTE_LINK_FLAGS +=
 OCAML_NATIVE_LINK_FLAGS +=
 
-OCAMLPACKS[]= unix
+OCAMLPACKS[]= unix str
 
 # OCAMLDEPFLAGS= -syntax camlp4o -package sexplib.syntax
 # OCAMLPPFLAGS= -syntax camlp4o -package sexplib.syntax
    overload
 #   graph
 #   treeprinter
+   at
    spot
 
 LIB = spotlib
+(** Small @var@ replacement language *)
+
+(* using good old Str *)
+open Str
+
+let replace_variables f =
+  let rex = regexp "@[A-Za-z0-9_]+@" in
+  let replace = 
+    global_substitute rex (fun s ->
+      let matched = matched_string s in
+      let k = String.sub matched 1 (String.length matched - 2) in
+      f k)
+  in
+  replace
+
+let replace_file f path outpath =
+  let ic = open_in path in
+  let oc = open_out outpath in
+  let rec loop () = 
+    try 
+      let line = input_line ic in
+      output_string oc (replace_variables f line);
+      output_char oc '\n';
+      loop ()
+    with
+    | End_of_file -> close_in ic; close_out oc
+  in
+  loop ()
+(* Requires Str *)
+val replace_variables : (string -> string) -> string -> string
+val replace_file : (string -> string) -> string -> string -> unit
     else String.sub s 0 (len-1)
   else s
 
+let split_by_newline s =
+  let length = String.length s in
+  let rec aux st start_pos pos = 
+    if pos = length then List.rev st else match s.[pos] with
+    | '\r' | '\n' -> 
+        let st = String.sub s start_pos (pos - start_pos) :: st in
+        skip st (pos+1)
+    | _ -> aux st start_pos (pos+1)
+  and skip st pos = 
+    if pos = length then List.rev st else match s.[pos] with
+    | '\r' | '\n' -> skip st (pos+1)
+    | _ -> aux st pos (pos+1)
+  in
+  aux [] 0 0
+
 module Set = Xset.Make(struct type t = string let compare (x:string) y = compare x y end)
     [chop_newline "hello" = "hello"]
 *)
 
+val split_by_newline : string -> string list
+
 module Set : Xset.S with type elt = String.t