Wins / tool.ml

(* Tool - Tool Object And Function
################################################################################
#    Wins is a "Point and Click" Game Motor written with OCaml                 #
#    Copyright (C) 2013    Philémon Gardet [philemon.gardet@epita.fr]          #
#                          Rafael Gozlan [rafael.gozlan@epita.fr]              #
#                                                                              #
#    This program 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 3 of the License, or         #
#    (at your option) any later version.                                       #
#                                                                              #
#    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.     #
################################################################################
*)

open Hashtbl

(* Exceptions *****************************************************************)
exception Not_found

(* Types **********************************************************************)
type ('a) binaryTree =
	| Node of 'a * ('a) binaryTree * ('a) binaryTree
	| VoidTree
;;

type 'a graph = 
	| Point of 'a * 'a graph list
	| Void
;;

(* Objects ********************************************************************)
class ['a] dictionary = 
	object (self)
		val data = Hashtbl.create 10
		
		method get key =
			find data key
		method elements () =
			let rec browser = function
				| [] -> []
				| (_,e)::q -> e::browser q
			in browser (self#sort data)
		method keys () =
			let rec browser = function
				| [] -> []
				| (k,_)::q -> k::browser q
			in browser (self#sort data)
		method is_empty () =
			length data = 0 
		method clear () = 
			clear data
		method length () =
			length data
		method set (key:string) (element:'a) =
			replace data key element
		method remove key =
			remove data key
		method check key =
			mem data key
			
		method private sort d =
			let output = ref ([]) in
			let test (ka,_) (kb,_) =
				ka <= kb
			and browser k v =
				output := (k,v)::!output
			in iter browser d;
			Sort.list test !output
	end
;;


(* Functions ******************************************************************)
let load_file file =
	let data = open_in file in
	let n = in_channel_length data in
	let s = String.create n in
	really_input data s 0 n;
	close_in data;
  (s)
;;

let cut str =
	let t = String.length str
	and (i,l) = (ref 0, ref 0)
	and o = ref ([]:string list)
	in
	while !i < t do
		(match str.[!i] with
			| ' ' | '\012' | '\n' | '\r' | '\t' -> 
				o := String.sub str !l (!i-(!l))::!o;
				l := !i+1
			| _ when !i = t-1 -> 
				o := String.sub str !l (t-(!l))::!o
			| _ -> ()
		); i := !i + 1
	done;
	
	!o
;;
	
let regexp str exp =
	let re = Str.regexp_case_fold exp in
	try 
		ignore(Str.search_forward re str 0);
		true
	with _-> false
;;

let (//) dir path =
	if not (regexp dir "/$") then
		dir^"/"^path
	else
		dir^path
;;

let print_debug msg =
	print_string ("Debug > "^msg^"\n");
	flush stdout
;;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.