text
stringlengths 12
786k
|
---|
let decode_public_key key file path logger = match Or_error . try_with ( fun ( ) -> Public_key . of_base58_check_decompress_exn key ) key with | Ok pk -> Some pk | Error e -> [ % log error ] error " Error decoding public key at $ path /$ file : $ error " ~ metadata : [ ( " file " , ` String file ) file ; ( " path " , ` String path ) path ; ( " error " , Error_json . error_to_yojson e ) e ] ; None |
let reload ~ logger { cache ; path } : unit Deferred . t = let logger = Logger . extend logger [ ( " wallets_context " , ` String " Wallets . get ) " ] in Public_key . Compressed . Table . clear cache ; let % bind ( ) = File_system . create_dir path in let % bind files = Sys . readdir path >>| Array . to_list in let % bind ( ) = Deferred . List . iter files ~ f ( : fun file -> match String . chop_suffix file ~ suffix " . : pub " with | Some sk_filename -> ( let % map lines = Reader . file_lines ( path ^/ file ) file in match lines with | public_key :: _ -> decode_public_key public_key file path logger |> Option . iter ~ f ( : fun pk -> ignore @@ Public_key . Compressed . Table . add cache ~ key : pk ~ data ( : Locked sk_filename ) sk_filename ) | _ -> ( ) ) | None -> ( match String . chop_suffix file ~ suffix " . : index " with | Some public_key -> ( let % map lines = Reader . file_lines ( path ^/ file ) file in match lines with | hd_index :: _ -> decode_public_key public_key file path logger |> Option . iter ~ f ( : fun pk -> ignore @@ Public_key . Compressed . Table . add cache ~ key : pk ~ data : ( Hd_account ( Mina_numbers . Hd_index . of_string hd_index ) hd_index ) ) | _ -> ( ) ) | None -> return ( ) ) ) in Unix . chmod path ~ perm : 0o700 |
let load ~ logger ~ disk_location = let t = { cache = Public_key . Compressed . Table . create ( ) ; path = disk_location ^/ " store " } in let % map ( ) = reload ~ logger t in t |
let import_keypair_helper t keypair write_keypair = let compressed_pk = Public_key . compress keypair . Keypair . public_key in let privkey_path = get_path t compressed_pk in let % bind ( ) = write_keypair privkey_path in let % map ( ) = Unix . chmod privkey_path ~ perm : 0o600 in ignore ( Public_key . Compressed . Table . add t . cache ~ key : compressed_pk ~ data ( : Unlocked ( get_privkey_filename compressed_pk , keypair ) keypair ) keypair : [ ` Duplicate | ` Ok ] ) ; compressed_pk |
let import_keypair t keypair ~ password = import_keypair_helper t keypair ( fun privkey_path -> Secret_keypair . write_exn keypair ~ privkey_path ~ password ) |
let import_keypair_terminal_stdin t keypair = import_keypair_helper t keypair ( fun privkey_path -> Secret_keypair . Terminal_stdin . write_exn keypair ~ privkey_path ) |
let generate_new t ~ password : Public_key . Compressed . t Deferred . t = let keypair = Keypair . create ( ) in import_keypair t keypair ~ password |
let create_hd_account t ~ hd_index : ( Public_key . Compressed . t , string ) string Deferred . Result . t = let open Deferred . Result . Let_syntax in let % bind public_key = Hardware_wallets . compute_public_key ~ hd_index in let compressed_pk = Public_key . compress public_key in let index_path = t . path ^/ Public_key . Compressed . to_base58_check compressed_pk ^ " . index " in let % bind ( ) = Hardware_wallets . write_exn ~ hd_index ~ index_path |> Deferred . map ~ f : Result . return in let % map ( ) = Unix . chmod index_path ~ perm : 0o600 |> Deferred . map ~ f : Result . return in ignore ( Public_key . Compressed . Table . add t . cache ~ key : compressed_pk ~ data ( : Hd_account hd_index ) hd_index : [ ` Duplicate | ` Ok ] ) ; compressed_pk |
let delete ( { cache ; _ } as t : t ) t ( pk : Public_key . Compressed . t ) t : ( unit , [ ` Not_found ] ) Deferred . Result . t = Hashtbl . remove cache pk ; Deferred . Or_error . try_with ~ here [ :% here ] here ( fun ( ) -> Unix . remove ( get_path t pk ) pk ) |> Deferred . Result . map_error ~ f ( : fun _ -> ` Not_found ) Not_found |
let pks ( { cache ; _ } : t ) t = Public_key . Compressed . Table . keys cache |
let find_unlocked ( { cache ; _ } : t ) t ~ needle = Public_key . Compressed . Table . find cache needle |> Option . bind ~ f ( : function | Locked _ -> None | Unlocked ( _ , kp ) kp -> Some kp | Hd_account _ -> None ) |
let find_identity ( { cache ; _ } : t ) t ~ needle = Public_key . Compressed . Table . find cache needle |> Option . bind ~ f ( : function | Locked _ -> None | Unlocked ( _ , kp ) kp -> Some ( ` Keypair kp ) kp | Hd_account index -> Some ( ` Hd_index index ) index ) |
let check_locked { cache ; _ } ~ needle = Public_key . Compressed . Table . find cache needle |> Option . map ~ f ( : function | Locked _ -> true | Unlocked _ -> false | Hd_account _ -> true ) |
let unlock { cache ; path } ~ needle ~ password = let unlock_keypair = function | Locked file -> Secret_keypair . read ~ privkey_path ( : path ^/ file ) file ~ password |> Deferred . Result . map_error ~ f ( : fun e -> ` Key_read_error e ) e |> Deferred . Result . map ~ f ( : fun kp -> Public_key . Compressed . Table . set cache ~ key : needle ~ data ( : Unlocked ( file , kp ) kp ) kp ) |> Deferred . Result . ignore_m | Unlocked _ -> Deferred . Result . return ( ) | Hd_account _ -> Deferred . Result . return ( ) in Public_key . Compressed . Table . find cache needle |> Result . of_option ~ error ` : Not_found |> Deferred . return |> Deferred . Result . bind ~ f : unlock_keypair |
let lock { cache ; _ } ~ needle = Public_key . Compressed . Table . change cache needle ~ f ( : function | Some ( Unlocked ( file , _ ) _ ) _ -> Some ( Locked file ) file | k -> k ) |
let get_tracked_keypair ~ logger ~ which ~ read_from_env_exn ~ conf_dir pk = let % bind wallets = load ~ logger ~ disk_location ( : conf_dir ^/ " wallets ) " in let sk_file = get_path wallets pk in read_from_env_exn ~ which sk_file ( module struct let logger = Logger . create ( ) let password = lazy ( Deferred . return ( Bytes . of_string ) ) " " module Set = Public_key . Compressed . Set let % test_unit " get from scratch " = Async . Thread_safe . block_on_async_exn ( fun ( ) -> File_system . with_temp_dir " / tmp / coda - wallets - test " ~ f ( : fun path -> let % bind wallets = load ~ logger ~ disk_location : path in let % map pk = generate_new wallets ~ password in let keys = Set . of_list ( pks wallets ) wallets in assert ( Set . mem keys pk ) pk ; assert ( find_unlocked wallets ~ needle : pk |> Option . is_some ) is_some ) ) let % test_unit " get from existing file system not - scratch " = Backtrace . elide := false ; Async . Thread_safe . block_on_async_exn ( fun ( ) -> File_system . with_temp_dir " / tmp / coda - wallets - test " ~ f ( : fun path -> let % bind wallets = load ~ logger ~ disk_location : path in let % bind pk1 = generate_new wallets ~ password in let % bind pk2 = generate_new wallets ~ password in let keys = Set . of_list ( pks wallets ) wallets in assert ( Set . mem keys pk1 && Set . mem keys pk2 ) pk2 ; let % map wallets = load ~ logger ~ disk_location : path in let keys = Set . of_list ( pks wallets ) wallets in assert ( Set . mem keys pk1 && Set . mem keys pk2 ) pk2 ) ) let % test_unit " create wallet then delete it " = Async . Thread_safe . block_on_async_exn ( fun ( ) -> File_system . with_temp_dir " / tmp / coda - wallets - test " ~ f ( : fun path -> let % bind wallets = load ~ logger ~ disk_location : path in let % bind pk = generate_new wallets ~ password in let keys = Set . of_list ( pks wallets ) wallets in assert ( Set . mem keys pk ) pk ; match % map delete wallets pk with | Ok ( ) -> assert ( Option . is_none @@ Public_key . Compressed . Table . find wallets . cache pk ) | Error _ -> failwith " unexpected " ) ) let % test_unit " Unable to find wallet " = Async . Thread_safe . block_on_async_exn ( fun ( ) -> File_system . with_temp_dir " / tmp / coda - wallets - test " ~ f ( : fun path -> let % bind wallets = load ~ logger ~ disk_location : path in let keypair = Keypair . create ( ) in let % map result = delete wallets ( Public_key . compress @@ keypair . public_key ) public_key in assert ( Result . is_error result ) result ) ) end ) |
type presence = | No | Yes | Eye |
module Side = struct type t = Wall . t Map . M ( Int ) . t [ @@ deriving scad_jane ] type config = int -> Wall . config option end |
module Sides = struct type t = { west : Side . t ; north : Side . t ; east : Side . t ; south : Side . t } [ @@ deriving scad ] let manual_body ( ? spacing = 1 . ) ~ west ~ north ~ east ~ south ( columns : _ Columns . t ) = { west = Map . filter_mapi ~ f ( : fun ~ key ~ data -> Option . map ~ f ( : fun c -> Wall . poly_of_config c ` West data ) ( west key ) ) ( Map . find_exn columns 0 ) . keys ; north = Map . filter_mapi ~ f ( : fun ~ key ~ data : _ -> Option . map ~ f ( : fun c -> Wall . drop_of_config ~ spacing ~ columns c ` North key ) ( north key ) ) columns ; east = Map . filter_mapi ~ f ( : fun ~ key ~ data -> Option . map ~ f ( : fun c -> Wall . poly_of_config c ` East data ) ( east key ) ) ( snd @@ Map . max_elt_exn columns ) . keys ; south = Map . filter_mapi ~ f ( : fun ~ key ~ data : _ -> Option . map ~ f ( : fun c -> Wall . drop_of_config ~ spacing ~ columns c ` South key ) ( south key ) ) columns } let manual_thumb ~ west ~ north ~ east ~ south ( columns : _ Columns . t ) = { west = Map . filter_mapi ~ f ( : fun ~ key ~ data -> let % bind . Option _ , k = Map . min_elt data . keys in Option . map ~ f ( : fun c -> Wall . poly_of_config c ` West k ) ( west key ) ) columns ; north = Map . filter_mapi ~ f ( : fun ~ key ~ data -> Option . map ~ f ( : fun c -> Wall . poly_of_config c ` North data ) ( north key ) ) ( snd @@ Map . min_elt_exn columns ) . keys ; east = Map . filter_mapi ~ f ( : fun ~ key ~ data -> let % bind . Option k = Map . find data . keys 0 in Option . map ~ f ( : fun c -> Wall . poly_of_config c ` East k ) ( east key ) ) columns ; south = Map . filter_mapi ~ f ( : fun ~ key ~ data -> Option . map ~ f ( : fun c -> Wall . poly_of_config c ` South data ) ( south key ) ) ( snd @@ Map . max_elt_exn columns ) . keys } let auto ( ? d1 = 2 . ) ( ? d2 = 5 . ) ( ? z_off = 0 . ) ( ? thickness = 3 . 5 ) ? index_thickness ( ? north_clearance = 2 . 5 ) ( ? south_clearance = 2 . 5 ) ( ? side_clearance = 3 . 0 ) ( ? n_steps = ` Flat 4 ) ( ? n_facets = 1 ) ( ? north_lookup = fun i -> if i = 2 || i = 4 then Eye else Yes ) ( ? south_lookup = function | i when i = 3 -> Eye | i when i > 1 -> Yes | _ -> No ) ( ? west_lookup = fun i -> if i = 0 then Eye else No ) ( ? east_lookup = fun _ -> No ) ( ? eyelet_config = Eyelet . m4_config ) ( ? spacing = 1 . ) ( ? thumb = false ) ( columns : ' k Columns . t ) = let present m i conf = function | Yes -> Map . add_exn m ~ key : i ~ data : conf | Eye -> Map . add_exn m ~ key : i ~ data : Wall . { conf with eyelet_config = Some eyelet_config } | No -> m and conf = Wall . { d1 ; d2 ; z_off ; thickness ; clearance = side_clearance ; n_steps ; n_facets ; eyelet_config = None } and init = Map . empty ( module Int ) in let west = Map . find @@ List . fold ~ init ~ f ( : fun m i -> present m i conf ( west_lookup i ) ) ( if thumb then Map . keys columns else Map . keys ( Map . find_exn columns 0 ) . keys ) and east = Map . find @@ List . fold ~ init ~ f ( : fun m i -> present m i conf ( east_lookup i ) ) ( if thumb then Map . keys columns else Map . keys ( snd @@ Map . min_elt_exn columns ) . keys ) and north = let index = Option . value ~ default : thickness index_thickness in let f m i = present m i { conf with clearance = north_clearance ; thickness = ( if i < 2 then index else thickness ) } ( north_lookup i ) in Map . find @@ List . fold ~ init ~ f ( if thumb then Map . keys ( snd @@ Map . min_elt_exn columns ) . keys else Map . keys columns ) and south = Map . find @@ List . fold ~ init ~ f ( : fun m i -> present m i { conf with clearance = south_clearance } ( south_lookup i ) ) ( if thumb then Map . keys ( snd @@ Map . max_elt_exn columns ) . keys else Map . keys columns ) in if thumb then manual_thumb ~ west ~ north ~ east ~ south columns else manual_body ~ west ~ north ~ east ~ south ~ spacing columns let fold ~ init ~ f ( t : t ) = let init = Map . fold ~ init ~ f t . west in let init = Map . fold ~ init ~ f t . north in let init = Map . fold ~ init ~ f t . east in Map . fold ~ init ~ f t . south let get t = function | ` N -> t . north | ` E -> t . east | ` S -> t . south | ` W -> t . west let to_scad t = let f ~ key : _ ~ data l = Wall . to_scad data :: l in Scad . union_3d ( fold ~ init [ ] : ~ f t ) let collect_screws ( ? init = [ ] ) ( t : t ) = let f ~ key : _ ~ data l = Option . value_map ~ default : l ~ f ( : fun s -> s :: l ) data . Wall . screw in fold ~ init ~ f t end |
let auto_body ? d1 ? d2 ? z_off ? thickness ? index_thickness ? north_clearance ? south_clearance ? side_clearance ? n_steps ? n_facets ? north_lookup ? south_lookup ? west_lookup ? east_lookup ? eyelet_config Plate . { config = { spacing ; _ } ; body ; _ } = Sides . auto ? d1 ? d2 ? z_off ? thickness ? index_thickness ? north_clearance ? south_clearance ? side_clearance ? n_steps ? n_facets ? north_lookup ? south_lookup ? west_lookup ? east_lookup ? eyelet_config ~ spacing body |
let auto_thumb ( ? d1 = 1 . ) ( ? d2 = 3 . ) ( ? z_off = 0 . ) ( ? thickness = 3 . 5 ) ? index_thickness ( ? north_clearance = 2 . 5 ) ( ? south_clearance = 2 . 5 ) ( ? side_clearance = 3 . 0 ) ( ? n_steps = ` PerZ 4 . ) ( ? n_facets = 1 ) ( ? north_lookup = fun i -> if i = 0 then Yes else No ) ( ? south_lookup = fun i -> if i = 0 then Yes else if i = 2 then Eye else No ) ( ? west_lookup = fun i -> if i = 0 then Yes else No ) ( ? east_lookup = fun _ -> No ) ? eyelet_config Plate . { thumb ; _ } = Sides . auto ~ d1 ~ d2 ~ z_off ~ thickness ? index_thickness ~ north_clearance ~ south_clearance ~ side_clearance ~ n_steps ~ n_facets ~ north_lookup ~ south_lookup ~ west_lookup ~ east_lookup ? eyelet_config ~ thumb : true thumb |
type t = { body : Sides . t ; thumb : Sides . t } |
let make ~ body ~ thumb = { body ; thumb } |
let manual ~ body_west ~ body_north ~ body_east ~ body_south ~ thumb_south ~ thumb_north ~ thumb_east ~ thumb_west Plate . { config = { spacing ; _ } ; body ; thumb ; _ } = { body = Sides . manual_body ~ west : body_west ~ north : body_north ~ east : body_east ~ south : body_south ~ spacing body ; thumb = Sides . manual_thumb ~ west : thumb_west ~ north : thumb_north ~ east : thumb_east ~ south : thumb_south thumb } |
let to_scad { body ; thumb } = Scad . union [ Sides . to_scad body ; Sides . to_scad thumb ] |
let collect_screws { body ; thumb } = Sides . collect_screws ~ init ( : Sides . collect_screws thumb ) body |
let test_check_walrus_operator context = let assert_type_errors = assert_type_errors ~ context in assert_type_errors { | ( x := True ) reveal_type ( x ) } | [ " Revealed type [ - 1 ] : Revealed type for ` x ` is ` typing_extensions . Literal [ True ] ` . " ] ; assert_type_errors { | if ( b := True ) : reveal_type ( b ) } | [ " Revealed type [ - 1 ] : Revealed type for ` b ` is ` typing_extensions . Literal [ True ] ` . " ] ; assert_type_errors { | a = [ 1 , 2 , 3 ] if ( d := len ( a ) ) : reveal_type ( d ) } | [ " Revealed type [ - 1 ] : Revealed type for ` d ` is ` int ` . " ] ; assert_type_errors { | x = ( y := 0 ) reveal_type ( x ) reveal_type ( y ) } | [ " Missing global annotation [ 5 ] : Globally accessible variable ` x ` has type ` int ` but no type \ is specified . " ; " Revealed type [ - 1 ] : Revealed type for ` x ` is ` typing_extensions . Literal [ 0 ] ` . " ; " Revealed type [ - 1 ] : Revealed type for ` y ` is ` typing_extensions . Literal [ 0 ] ` . " ; ] ; assert_type_errors { | def foo ( x : int , cat : str ) -> None : pass foo ( x := 3 , cat ' = vector ' ) reveal_type ( x ) } | [ " Revealed type [ - 1 ] : Revealed type for ` x ` is ` typing_extensions . Literal [ 3 ] ` . " ] ; assert_type_errors { | def foo ( cat : str ) -> None : pass foo ( cat ( = category := ' vector ' ) ) reveal_type ( category ) } | [ " Revealed type [ - 1 ] : Revealed type for ` category ` is ` typing_extensions . Literal [ ' vector ' ] ` . " ] ; assert_type_errors { | a = [ 1 , 2 , 3 ] if ( b := len ( a ) ) > 4 : reveal_type ( b ) } | [ " Revealed type [ - 1 ] : Revealed type for ` b ` is ` int ` . " ] ; assert_type_errors { | a = [ 1 , 2 , 3 ] if ( b := 3 ) in a : reveal_type ( b ) } | [ " Revealed type [ - 1 ] : Revealed type for ` b ` is ` typing_extensions . Literal [ 3 ] ` . " ] ; assert_type_errors { | if ( b := 42 ) and True : reveal_type ( b ) } | [ " Revealed type [ - 1 ] : Revealed type for ` b ` is ` typing_extensions . Literal [ 42 ] ` . " ] ; assert_type_errors { | if True and ( b := 42 ) : reveal_type ( b ) } | [ " Revealed type [ - 1 ] : Revealed type for ` b ` is ` typing_extensions . Literal [ 42 ] ` . " ] ; assert_type_errors { | if ( b := 0 ) or True : reveal_type ( b ) } | [ " Revealed type [ - 1 ] : Revealed type for ` b ` is ` typing_extensions . Literal [ 0 ] ` . " ] ; assert_type_errors { | if False or ( b := 0 ) : reveal_type ( b ) } | [ " Revealed type [ - 1 ] : Revealed type for ` b ` is ` typing_extensions . Literal [ 0 ] ` . " ] ; assert_type_errors { | if ( b := 3 ) > 4 : pass reveal_type ( b ) } | [ " Revealed type [ - 1 ] : Revealed type for ` b ` is ` typing_extensions . Literal [ 3 ] ` . " ] ; assert_type_errors { | from typing import Optional def foo ( ) -> Optional [ int ] : . . . if ( a := foo ( ) ) is not None : reveal_type ( a ) } | [ " Revealed type [ - 1 ] : Revealed type for ` a ` is ` int ` . " ] |
let ( ) = " walrus " >::: [ " check_walrus_operator " >:: test_check_walrus_operator ] |> Test . run |
this is a comment ) * |
type t = Foo | Bar ; ; |
let f = function | Foo -> print_string " 1 " | _ -> print_string " other than 1 " |
let _ = print_string " hello " ; List . iter print_string ; print_string " world " |
let ( ) = let iter ' ~ f xs = List . iter f xs in iter ' print_endline [ " hello " ; " world " ] method m = print_endline " hello " end inherit c method m = print_endline " bye " end |
let g = function | true -> print_endline " hello " |
type r = { x : int ; y : int } |
let h = function | { x = 0 } -> 0 | { x = _ } -> 1 |
let f10 fd = let buf = Bytes . create 10 in Unix . read fd buf 0 10 ; buf |
let x11 x = match x with | Some true -> 1 | Some false -> 0 | None -> 0 | Some _ -> 2 |
let w12 = function | ( Some _ | Some _ ) -> 1 | None -> 0 val mutable x = 1 method get_x = x end val mutable x = 10 inherit c13 method get_x ' = x end |
let regexp = " ( [ \ A - Z ] [ + A - Za - z0 - 9 ] ) . " +\\ method private x = self # x + 1 end |
module type DEPRECATED = sig end [ @@ ocaml . deprecated ] |
module T = struct type deprecated [ @@ ocaml . deprecated ] end |
let _ = let x = 1 in ( ) |
module A = struct let _ = let x = 1 in ( ) end |
module rec B : sig type t end = struct type t = T . deprecated end |
module type T = sig type t = T . deprecated end |
module type S = sig val x : T . deprecated [ @@ ocaml . alert " - deprecated " ] module AA : sig type t = T . deprecated end [ @@ ocaml . alert " - deprecated " ] module rec BB : sig type t = T . deprecated end [ @@ ocaml . alert " - deprecated " ] module type T = sig type t = T . deprecated end [ @@ ocaml . alert " - deprecated " ] include DEPRECATED [ @@ ocaml . alert " - deprecated " ] end |
let foo ? x = ( ) [ %% expect { | ^ } ] | |
let foo ? x ~ y = ( ) [ %% expect { | ^ } ] | |
let foo ? x ( ) = ( ) [ %% expect { | } ] | |
let foo ? x ~ y ( ) = ( ) [ %% expect { | } ] | [ %% expect { | ^ } ] | [ %% expect { | ^ } ] | [ %% expect { | } ] | [ %% expect { | } ] | |
type loc = { loc_start : Lexing . position ; loc_end : Lexing . position ; loc_ghost : bool ; } |
type t = | Comment_start | Comment_not_end | Fragile_match of string | Partial_application | Labels_omitted of string list | Method_override of string list | Partial_match of string | Non_closed_record_pattern of string | Statement_type | Unused_match | Unused_pat | Instance_variable_override of string list | Illegal_backslash | Implicit_public_methods of string list | Unerasable_optional_argument | Undeclared_virtual_method of string | Not_principal of string | Without_principality of string | Unused_argument | Nonreturning_statement | Preprocessor of string | Useless_record_with | Bad_module_name of string | All_clauses_guarded | Unused_var of string | Unused_var_strict of string | Wildcard_arg_to_constant_constr | Eol_in_string | Duplicate_definitions of string * string * string * string | Multiple_definition of string * string * string | Unused_value_declaration of string | Unused_open of string | Unused_type_declaration of string | Unused_for_index of string | Unused_ancestor of string | Unused_constructor of string * bool * bool | Unused_extension of string * bool * bool * bool | Unused_rec_flag | Name_out_of_scope of string * string list * bool | Ambiguous_name of string list * string list * bool * string | Disambiguated_name of string | Nonoptional_label of string | Open_shadow_identifier of string * string | Open_shadow_label_constructor of string * string | Bad_env_variable of string * string | Attribute_payload of string * string | Eliminated_optional_arguments of string list | No_cmi_file of string * string option | Bad_docstring of bool | Expect_tailcall | Fragile_literal_pattern | Misplaced_attribute of string | Duplicated_attribute of string | Inlining_impossible of string | Unreachable_case | Ambiguous_pattern of string list | No_cmx_file of string | Assignment_to_non_mutable_value | Unused_module of string | Unboxable_type_in_prim_decl of string | Constraint_on_gadt | Erroneous_printed_signature of string | Unsafe_without_parsing | Redefining_unit of string | Unused_open_bang of string | Unused_functor_parameter of string | Module_compiled_without_lto of string ; ; |
type alert = { kind : string ; message : string ; def : loc ; use : loc } |
let number = function | Comment_start -> 1 | Comment_not_end -> 2 | Fragile_match _ -> 4 | Partial_application -> 5 | Labels_omitted _ -> 6 | Method_override _ -> 7 | Partial_match _ -> 8 | Non_closed_record_pattern _ -> 9 | Statement_type -> 10 | Unused_match -> 11 | Unused_pat -> 12 | Instance_variable_override _ -> 13 | Illegal_backslash -> 14 | Implicit_public_methods _ -> 15 | Unerasable_optional_argument -> 16 | Undeclared_virtual_method _ -> 17 | Not_principal _ -> 18 | Without_principality _ -> 19 | Unused_argument -> 20 | Nonreturning_statement -> 21 | Preprocessor _ -> 22 | Useless_record_with -> 23 | Bad_module_name _ -> 24 | All_clauses_guarded -> 8 | Unused_var _ -> 26 | Unused_var_strict _ -> 27 | Wildcard_arg_to_constant_constr -> 28 | Eol_in_string -> 29 | Duplicate_definitions _ -> 30 | Multiple_definition _ -> 31 | Unused_value_declaration _ -> 32 | Unused_open _ -> 33 | Unused_type_declaration _ -> 34 | Unused_for_index _ -> 35 | Unused_ancestor _ -> 36 | Unused_constructor _ -> 37 | Unused_extension _ -> 38 | Unused_rec_flag -> 39 | Name_out_of_scope _ -> 40 | Ambiguous_name _ -> 41 | Disambiguated_name _ -> 42 | Nonoptional_label _ -> 43 | Open_shadow_identifier _ -> 44 | Open_shadow_label_constructor _ -> 45 | Bad_env_variable _ -> 46 | Attribute_payload _ -> 47 | Eliminated_optional_arguments _ -> 48 | No_cmi_file _ -> 49 | Bad_docstring _ -> 50 | Expect_tailcall -> 51 | Fragile_literal_pattern -> 52 | Misplaced_attribute _ -> 53 | Duplicated_attribute _ -> 54 | Inlining_impossible _ -> 55 | Unreachable_case -> 56 | Ambiguous_pattern _ -> 57 | No_cmx_file _ -> 58 | Assignment_to_non_mutable_value -> 59 | Unused_module _ -> 60 | Unboxable_type_in_prim_decl _ -> 61 | Constraint_on_gadt -> 62 | Erroneous_printed_signature _ -> 63 | Unsafe_without_parsing -> 64 | Redefining_unit _ -> 65 | Unused_open_bang _ -> 66 | Unused_functor_parameter _ -> 67 | Module_compiled_without_lto _ -> 68 ; ; |
let last_warning_number = 67 ; ; |
let letter = function | ' a ' -> let rec loop i = if i = 0 then [ ] else i :: loop ( i - 1 ) in loop last_warning_number | ' b ' -> [ ] | ' c ' -> [ 1 ; 2 ] | ' d ' -> [ 3 ] | ' e ' -> [ 4 ] | ' f ' -> [ 5 ] | ' g ' -> [ ] | ' h ' -> [ ] | ' i ' -> [ ] | ' j ' -> [ ] | ' k ' -> [ 32 ; 33 ; 34 ; 35 ; 36 ; 37 ; 38 ; 39 ] | ' l ' -> [ 6 ] | ' m ' -> [ 7 ] | ' n ' -> [ ] | ' o ' -> [ ] | ' p ' -> [ 8 ] | ' q ' -> [ ] | ' r ' -> [ 9 ] | ' s ' -> [ 10 ] | ' t ' -> [ ] | ' u ' -> [ 11 ; 12 ] | ' v ' -> [ 13 ] | ' w ' -> [ ] | ' x ' -> [ 14 ; 15 ; 16 ; 17 ; 18 ; 19 ; 20 ; 21 ; 22 ; 23 ; 24 ; 30 ] | ' y ' -> [ 26 ] | ' z ' -> [ 27 ] | _ -> assert false ; ; |
type state = { active : bool array ; error : bool array ; alerts : ( Misc . Stdlib . String . Set . t * bool ) ; alert_errors : ( Misc . Stdlib . String . Set . t * bool ) ; } |
let current = ref { active = Array . make ( last_warning_number + 1 ) true ; error = Array . make ( last_warning_number + 1 ) false ; alerts = ( Misc . Stdlib . String . Set . empty , false ) ; alert_errors = ( Misc . Stdlib . String . Set . empty , true ) ; } |
let disabled = ref false |
let without_warnings f = Misc . protect_refs [ Misc . R ( disabled , true ) ] f |
let backup ( ) = ! current |
let restore x = current := x |
let is_active x = not ! disabled && ( ! current ) . active . ( number x ) |
let is_error x = not ! disabled && ( ! current ) . error . ( number x ) |
let alert_is_active { kind ; _ } = not ! disabled && let ( set , pos ) = ( ! current ) . alerts in Misc . Stdlib . String . Set . mem kind set = pos |
let alert_is_error { kind ; _ } = not ! disabled && let ( set , pos ) = ( ! current ) . alert_errors in Misc . Stdlib . String . Set . mem kind set = pos |
let mk_lazy f = let state = backup ( ) in lazy ( let prev = backup ( ) in restore state ; try let r = f ( ) in restore prev ; r with exn -> restore prev ; raise exn ) |
let set_alert ~ error ~ enable s = let upd = match s with | " all " -> ( Misc . Stdlib . String . Set . empty , not enable ) | s -> let ( set , pos ) = if error then ( ! current ) . alert_errors else ( ! current ) . alerts in let f = if enable = pos then Misc . Stdlib . String . Set . add else Misc . Stdlib . String . Set . remove in ( f s set , pos ) in if error then current := { ( ! current ) with alert_errors = upd } else current := { ( ! current ) with alerts = upd } |
let parse_alert_option s = let n = String . length s in let id_char = function | ' a ' . . ' z ' | ' A ' . . ' Z ' | ' _ ' | ' ' ' \ | ' 0 ' . . ' 9 ' -> true | _ -> false in let rec parse_id i = if i < n && id_char s . [ i ] then parse_id ( i + 1 ) else i in let rec scan i = if i = n then ( ) else if i + 1 = n then raise ( Arg . Bad " Ill - formed list of alert settings " ) else match s . [ i ] , s . [ i + 1 ] with | ' ' , + ' ' + -> id ( set_alert ~ error : true ~ enable : true ) ( i + 2 ) | ' ' , + _ -> id ( set_alert ~ error : false ~ enable : true ) ( i + 1 ) | ' ' , - ' ' - -> id ( set_alert ~ error : true ~ enable : false ) ( i + 2 ) | ' ' , - _ -> id ( set_alert ~ error : false ~ enable : false ) ( i + 1 ) | ' ' , @ _ -> id ( fun s -> set_alert ~ error : true ~ enable : true s ; set_alert ~ error : false ~ enable : true s ) ( i + 1 ) | _ -> raise ( Arg . Bad " Ill - formed list of alert settings " ) and id f i = let j = parse_id i in if j = i then raise ( Arg . Bad " Ill - formed list of alert settings " ) ; let id = String . sub s i ( j - i ) in f id ; scan j in scan 0 |
let parse_opt error active errflag s = let flags = if errflag then error else active in let set i = if i = 3 then set_alert ~ error : errflag ~ enable : true " deprecated " else flags . ( i ) <- true in let clear i = if i = 3 then set_alert ~ error : errflag ~ enable : false " deprecated " else flags . ( i ) <- false in let set_all i = if i = 3 then begin set_alert ~ error : false ~ enable : true " deprecated " ; set_alert ~ error : true ~ enable : true " deprecated " end else begin active . ( i ) <- true ; error . ( i ) <- true end in let error ( ) = raise ( Arg . Bad " Ill - formed list of warnings " ) in let rec get_num n i = if i >= String . length s then i , n else match s . [ i ] with | ' 0 ' . . ' 9 ' -> get_num ( 10 * n + Char . code s . [ i ] - Char . code ' 0 ' ) ( i + 1 ) | _ -> i , n in let get_range i = let i , n1 = get_num 0 i in if i + 2 < String . length s && s . [ i ] = ' . ' && s . [ i + 1 ] = ' . ' then let i , n2 = get_num 0 ( i + 2 ) in if n2 < n1 then error ( ) ; i , n1 , n2 else i , n1 , n1 in let rec loop i = if i >= String . length s then ( ) else match s . [ i ] with | ' A ' . . ' Z ' -> List . iter set ( letter ( Char . lowercase_ascii s . [ i ] ) ) ; loop ( i + 1 ) | ' a ' . . ' z ' -> List . iter clear ( letter s . [ i ] ) ; loop ( i + 1 ) | ' ' + -> loop_letter_num set ( i + 1 ) | ' ' - -> loop_letter_num clear ( i + 1 ) | ' ' @ -> loop_letter_num set_all ( i + 1 ) | _ -> error ( ) and loop_letter_num myset i = if i >= String . length s then error ( ) else match s . [ i ] with | ' 0 ' . . ' 9 ' -> let i , n1 , n2 = get_range i in for n = n1 to min n2 last_warning_number do myset n done ; loop i | ' A ' . . ' Z ' -> List . iter myset ( letter ( Char . lowercase_ascii s . [ i ] ) ) ; loop ( i + 1 ) | ' a ' . . ' z ' -> List . iter myset ( letter s . [ i ] ) ; loop ( i + 1 ) | _ -> error ( ) in loop 0 ; ; |
let parse_options errflag s = let error = Array . copy ( ! current ) . error in let active = Array . copy ( ! current ) . active in parse_opt error active errflag s ; current := { ( ! current ) with error ; active } |
let ( ) = parse_options false defaults_w ; ; |
let ( ) = parse_options true defaults_warn_error ; ; |
let ref_manual_explanation ( ) = let [ @ manual . ref " s : comp - warnings " ] chapter , section = 9 , 5 in Printf . sprintf " ( See manual section % d . % d ) " chapter section |
let message = function | Comment_start -> " this ` let sub_locs = if not alert . def . loc_ghost && not alert . use . loc_ghost then [ alert . def , " Definition " ; alert . use , " Expected signature " ; ] else [ ] in ` Active { id = alert . kind ; message ; is_error ; sub_locs ; } |
let reset_fatal ( ) = nerrors := 0 |
let check_fatal ( ) = if ! nerrors > 0 then begin nerrors := 0 ; raise Errors ; end ; ; ; |
let descriptions = [ 1 , " Suspicious - looking start - of - comment mark . " ; 2 , " Suspicious - looking end - of - comment mark . " ; 3 , " Deprecated synonym for the ' deprecated ' alert " ; 4 , " Fragile pattern matching : matching that will remain complete even \ n \ \ if additional constructors are added to one of the variant types \ n \ \ matched . " ; 5 , " Partially applied function : expression whose result has function \ n \ \ type and is ignored . " ; 6 , " Label omitted in function application . " ; 7 , " Method overridden . " ; 8 , " Partial match : missing cases in pattern - matching . " ; 9 , " Missing fields in a record pattern . " ; 10 , " Expression on the left - hand side of a sequence that doesn ' t have \ type \ n \ \ " \ unit " \ ( and that is not a function , see warning number 5 ) . " ; 11 , " Redundant case in a pattern matching ( unused match case ) . " ; 12 , " Redundant sub - pattern in a pattern - matching . " ; 13 , " Instance variable overridden . " ; 14 , " Illegal backslash escape in a string constant . " ; 15 , " Private method made public implicitly . " ; 16 , " Unerasable optional argument . " ; 17 , " Undeclared virtual method . " ; 18 , " Non - principal type . " ; 19 , " Type without principality . " ; 20 , " Unused function argument . " ; 21 , " Non - returning statement . " ; 22 , " Preprocessor warning . " ; 23 , " Useless record " \ with " \ clause . " ; 24 , " Bad module name : the source file name is not a valid OCaml module \ name . " ; 25 , " Deprecated : now part of warning 8 . " ; 26 , " Suspicious unused variable : unused variable that is bound \ n \ \ with " \ let " \ or " \ as " , \ and doesn ' t start with an underscore ( " \ _ " ) \\ n \ \ character . " ; 27 , " Innocuous unused variable : unused variable that is not bound with \ n \ \ " \ let " \ nor " \ as " , \ and doesn ' t start with an underscore ( " \ _ " ) \\ n \ \ character . " ; 28 , " Wildcard pattern given as argument to a constant constructor . " ; 29 , " Unescaped end - of - line in a string constant ( non - portable code ) . " ; 30 , " Two labels or constructors of the same name are defined in two \ n \ \ mutually recursive types . " ; 31 , " A module is linked twice in the same executable . " ; 32 , " Unused value declaration . " ; 33 , " Unused open statement . " ; 34 , " Unused type declaration . " ; 35 , " Unused for - loop index . " ; 36 , " Unused ancestor variable . " ; 37 , " Unused constructor . " ; 38 , " Unused extension constructor . " ; 39 , " Unused rec flag . " ; 40 , " Constructor or label name used out of scope . " ; 41 , " Ambiguous constructor or label name . " ; 42 , " Disambiguated constructor or label name ( compatibility warning ) . " ; 43 , " Nonoptional label applied as optional . " ; 44 , " Open statement shadows an already defined identifier . " ; 45 , " Open statement shadows an already defined label or constructor . " ; 46 , " Error in environment variable . " ; 47 , " Illegal attribute payload . " ; 48 , " Implicit elimination of optional arguments . " ; 49 , " Absent cmi file when looking up module alias . " ; 50 , " Unexpected documentation comment . " ; 51 , " Warning on non - tail calls if @ tailcall present . " ; 52 , " Fragile constant pattern . " ; 53 , " Attribute cannot appear in this context " ; 54 , " Attribute used more than once on an expression " ; 55 , " Inlining impossible " ; 56 , " Unreachable case in a pattern - matching ( based on type information ) . " ; 57 , " Ambiguous or - pattern variables under guard " ; 58 , " Missing cmx file " ; 59 , " Assignment to non - mutable value " ; 60 , " Unused module declaration " ; 61 , " Unboxable type in primitive declaration " ; 62 , " Type constraint on GADT type declaration " ; 63 , " Erroneous printed signature " ; 64 , " - unsafe used with a preprocessor returning a syntax tree " ; 65 , " Type declaration defining a new ' ( ) ' constructor " ; 66 , " Unused open ! statement " ; 67 , " Dependency on a module compiled without lto " ; ] ; ; |
let help_warnings ( ) = List . iter ( fun ( i , s ) -> Printf . printf " % 3i % s \ n " i s ) descriptions ; print_endline " A all warnings " ; for i = Char . code ' b ' to Char . code ' z ' do let c = Char . chr i in match letter c with | [ ] -> ( ) | [ n ] -> Printf . printf " % c Alias for warning % i . \ n " ( Char . uppercase_ascii c ) n | l -> Printf . printf " % c warnings % s . \ n " ( Char . uppercase_ascii c ) ( String . concat " , " ( List . map Int . to_string l ) ) done ; exit 0 ; ; |
let err = T . err Warp_error |
type resample_t = | Nearest_neighbor | Bilinear | Cubic | Cubic_spline | Lanczos |
let int_of_resample = function | Nearest_neighbor -> 0 | Bilinear -> 1 | Cubic -> 2 | Cubic_spline -> 3 | Lanczos -> 4 |
module Options = struct module Raw = struct type warp_options_t type t = warp_options_t structure ptr let t : warp_options_t structure typ = structure " GDALWarpOptions " let f n k = field t n k let warp_options = f " warp_options " ( ptr string_opt ) let memory_limit = f " memory_limit " double let resample_alg = f " resample_alg " int let data_type = f " data_type " int let src_ds = f " src_ds " Data_set . t let dst_ds = f " dst_ds " Data_set . t let band_count = f " band_count " int let src_bands = f " src_bands " ( ptr int ) let dst_bands = f " dst_bands " ( ptr int ) let _src_alpha_band = f " src_alpha_band " int let _dst_alpha_band = f " dst_alpha_band " int let src_no_data_real = f " src_no_data_real " ( ptr_opt double ) let src_no_data_imag = f " src_no_data_imag " ( ptr_opt double ) let dst_no_data_real = f " dst_no_data_real " ( ptr_opt double ) let dst_no_data_imag = f " dst_no_data_imag " ( ptr_opt double ) let _progress = f " progress " ( ptr void ) let _progress_arg = f " progress_arg " ( ptr void ) let transformer = f " transformer " ( Foreign . funptr ( Transform . transform_t ( ptr void ) ) ) let transformer_arg = f " transformer_arg " ( ptr void ) let _src_per_band_validity_mask_func = f " src_per_band_validity_mask_func " ( ptr void ) let _src_per_band_validity_mask_func_arg = f " src_per_band_validity_mask_func_arg " ( ptr ( ptr void ) ) let _src_validity_mask_func = f " src_validity_mask_func " ( ptr void ) let _src_validity_mask_func_arg = f " src_validity_mask_func_arg " ( ptr void ) let _src_density_mask_func = f " src_density_mask_func " ( ptr void ) let _src_density_mask_func_arg = f " src_density_mask_func_arg " ( ptr void ) let _dst_density_mask_func = f " dst_density_mask_func " ( ptr void ) let _dst_density_mask_func_arg = f " dst_density_mask_func_arg " ( ptr void ) let _dst_validity_mask_func = f " dst_validity_mask_func " ( ptr void ) let _dst_validity_mask_func_arg = f " dst_validity_mask_func_arg " ( ptr void ) let _pre_warp_chunk_processor = f " pre_warp_chunk_processor " ( ptr void ) let _pre_warp_chunk_processor_arg = f " pre_warp_chunk_processor_arg " ( ptr void ) let _post_warp_chunk_processor = f " post_warp_chunk_processor " ( ptr void ) let _post_warp_chunk_processor_arg = f " post_warp_chunk_processor_arg " ( ptr void ) let _cutline = f " cutline " ( ptr void ) let _cutline_blend_dist = f " cutline_blend_dist " double let ( ) = seal t let create = Lib . c " GDALCreateWarpOptions " ( void @-> returning ( ptr t ) ) let clone = Lib . c " GDALCloneWarpOptions " ( ptr t @-> returning ( ptr t ) ) let delete = Lib . c " free " ( ptr t @-> returning void ) let create ( ) = let o = create ( ) in Gc . finalise delete o ; o end exception Band_count_mismatch type ' a t = { o : Raw . t ; mutable options : string option Ctypes . CArray . t option ; mutable src_bands : ( int , Bigarray . int_elt , Bigarray . c_layout ) Bigarray . Array1 . t option ; mutable dst_bands : ( int , Bigarray . int_elt , Bigarray . c_layout ) Bigarray . Array1 . t option ; mutable src_no_data_real : ( float , Bigarray . float64_elt , Bigarray . c_layout ) Bigarray . Array1 . t option ; mutable src_no_data_imag : ( float , Bigarray . float64_elt , Bigarray . c_layout ) Bigarray . Array1 . t option ; mutable dst_no_data_real : ( float , Bigarray . float64_elt , Bigarray . c_layout ) Bigarray . Array1 . t option ; mutable dst_no_data_imag : ( float , Bigarray . float64_elt , Bigarray . c_layout ) Bigarray . Array1 . t option ; } let set_warp_options o options = let options = Lib . convert_creation_options options in setf ( !@ o . o ) Raw . warp_options ( Lib . creation_options_to_ptr options ) ; o . options <- options ; ( ) let set_memory_limit { o ; _ } l = setf !@ o Raw . memory_limit l ; ( ) let set_resample_alg { o ; _ } a = let i = int_of_resample a in setf !@ o Raw . resample_alg i ; ( ) let set_working_data_type { o ; _ } t = let i = Band . Data . to_int t in setf !@ o Raw . data_type i ; ( ) let set_src { o ; _ } ds = setf !@ o Raw . src_ds ds ; ( ) let set_dst { o ; _ } ds = setf !@ o Raw . dst_ds ds ; ( ) let set_bands o bands = let src = List . map fst bands |> Array . of_list in let dst = List . map snd bands |> Array . of_list in let open Bigarray in let src = Array1 . of_array int c_layout src in let dst = Array1 . of_array int c_layout dst in let n = Array1 . dim src in setf ( !@ o . o ) Raw . band_count n ; setf ( !@ o . o ) Raw . src_bands ( bigarray_start array1 src ) ; setf ( !@ o . o ) Raw . dst_bands ( bigarray_start array1 dst ) ; o . src_bands <- Some src ; o . dst_bands <- Some dst ; ( ) let ptr_opt_of_list = function | [ ] -> None | l -> let a = Array . of_list l |> Bigarray . ( Array1 . of_array float64 c_layout ) in Some a let set_band_dep o l f = let bands = getf ( !@ o . o ) Raw . band_count in let n = List . length l in if n > 0 && n <> bands then raise Band_count_mismatch ; match ptr_opt_of_list l with | None -> setf ( !@ o . o ) f None ; None | Some ca -> let p = bigarray_start array1 ca in setf ( !@ o . o ) f ( Some p ) ; Some ca let set_src_no_data_real o l = let ca = set_band_dep o l Raw . src_no_data_real in o . src_no_data_real <- ca let set_src_no_data_imag o l = let ca = set_band_dep o l Raw . src_no_data_imag in o . src_no_data_imag <- ca let set_dst_no_data_real o l = let ca = set_band_dep o l Raw . dst_no_data_real in o . dst_no_data_real <- ca let set_dst_no_data_imag o l = let ca = set_band_dep o l Raw . dst_no_data_imag in o . dst_no_data_imag <- ca let set_transformer { o ; _ } transform = let c = Transform . get_transform_c transform in let arg = Transform . get_transform_t transform in setf !@ o Raw . transformer c ; setf !@ o Raw . transformer_arg arg ; ( ) let may f o = match o with | None -> ( ) | Some x -> f x let create ( ) = let o = Raw . create ( ) in { o ; options = None ; src_bands = None ; dst_bands = None ; src_no_data_real = None ; src_no_data_imag = None ; dst_no_data_real = None ; dst_no_data_imag = None ; } let make ? warp_options ? memory_limit ? resample_alg ? working_data_type ? src ? dst ? bands ? src_no_data_real ? src_no_data_imag ? dst_no_data_real ? dst_no_data_imag ? transformer ( ) = let o = create ( ) in let mayo f x = may ( f o ) x in mayo set_warp_options warp_options ; mayo set_memory_limit memory_limit ; mayo set_resample_alg resample_alg ; mayo set_working_data_type working_data_type ; mayo set_src src ; mayo set_dst dst ; mayo set_bands bands ; mayo set_src_no_data_real src_no_data_real ; mayo set_src_no_data_imag src_no_data_imag ; mayo set_dst_no_data_real dst_no_data_real ; mayo set_dst_no_data_imag dst_no_data_imag ; mayo set_transformer transformer ; o let clone o = { o with o = Raw . clone o . o } end |
let reproject_image = Lib . c " GDALReprojectImage " ( Data_set . t @-> string_opt @-> Data_set . t @-> string_opt @-> int @-> double @-> double @-> ptr void @-> ptr void @-> ptr Options . Raw . t @-> returning err ) |
let reproject_image ( ? memory_limit = 0 . 0 ) ( ? max_error = 0 . 0 ) ? options ? src_wkt ? dst_wkt ~ src ~ dst alg = let options = match options with | None -> from_voidp Options . Raw . t null | Some { Options . o ; _ } -> o in reproject_image src src_wkt dst dst_wkt ( int_of_resample alg ) memory_limit max_error null null options |
let create_and_reproject_image = Lib . c " GDALCreateAndReprojectImage " ( Data_set . t @-> string_opt @-> string @-> string_opt @-> Driver . t @-> ptr string_opt @-> int @-> double @-> double @-> ptr void @-> ptr void @-> ptr Options . Raw . t @-> returning err ) |
let create_and_reproject_image ( ? memory_limit = 0 . 0 ) ( ? max_error = 0 . 0 ) ? options ? src_wkt ? dst_wkt ( ? create_options = [ ] ) src ~ filename driver alg = let options = match options with | None -> from_voidp Options . Raw . t null | Some { Options . o ; _ } -> o in let create_options = Lib . convert_creation_options create_options in create_and_reproject_image src src_wkt filename dst_wkt driver ( Lib . creation_options_to_ptr create_options ) ( int_of_resample alg ) memory_limit max_error null null options |
let auto_create_warped_vrt = Lib . c " GDALAutoCreateWarpedVRT " ( Data_set . t @-> string_opt @-> string_opt @-> int @-> float @-> ptr Options . Raw . t @-> returning Data_set . t_opt ) |
let auto_create_warped_vrt ? src_wkt ? dst_wkt ( ? max_error = 0 . 0 ) ? options src alg = let options = match options with | None -> from_voidp Options . Raw . t null | Some { Options . o ; _ } -> o in let result = auto_create_warped_vrt src src_wkt dst_wkt ( int_of_resample alg ) max_error options in match result with | Some ds -> ds | None -> raise Warp_error |
type warp_output_t = { geo_transform : Geo_transform . t ; dims : int * int ; } |
let suggested_warp_output arg = Lib . c " GDALSuggestedWarpOutput " ( Data_set . t @-> Foreign . funptr ( Transform . transform_t arg ) @-> arg @-> ptr double @-> ptr int @-> ptr int @-> returning err ) |
let suggested_warp_output ds transform = let transform_t = Transform . get_transform_t transform in let transform_f = Transform . get_transform_c transform in let geo_transform = Geo_transform . make ~ origin ( : 0 . 0 , 0 . 0 ) ~ pixel_size ( : 0 . 0 , 0 . 0 ) ~ rotation ( : 0 . 0 , 0 . 0 ) in let pixels = allocate_n int ~ count : 1 in let lines = allocate_n int ~ count : 1 in suggested_warp_output ( ptr void ) ds transform_f transform_t ( let open Bigarray in bigarray_start array1 ( geo_transform :> ( float , float64_elt , c_layout ) Array1 . t ) ) pixels lines ; { geo_transform ; dims = !@ pixels , !@ lines } |
module Operation = struct type raw_t = T . t let t = T . t type ' a t = { t : raw_t ; options : ' a Options . t ; } let create = Lib . c " GDALCreateWarpOperation " ( ptr Options . Raw . t @-> returning t ) let destroy = Lib . c " GDALDestroyWarpOperation " ( t @-> returning void ) let create options = let result = create Options . ( options . o ) in if result = null then raise Warp_error else ( Gc . finalise destroy result ; { t = result ; options } ) let chunk_and_warp_image = Lib . c " GDALChunkAndWarpImage " ( t @-> int @-> int @-> int @-> int @-> returning err ) let chunk_and_warp_image t ~ offset ~ size = chunk_and_warp_image t . t ( fst offset ) ( snd offset ) ( fst size ) ( snd size ) let chunk_and_warp_multi = Lib . c " GDALChunkAndWarpMulti " ( t @-> int @-> int @-> int @-> int @-> returning err ) let chunk_and_warp_multi t ~ offset ~ size = chunk_and_warp_multi t . t ( fst offset ) ( snd offset ) ( fst size ) ( snd size ) let warp_region = Lib . c " GDALWarpRegion " ( t @-> int @-> int @-> int @-> int @-> int @-> int @-> int @-> int @-> returning err ) let warp_region t ~ dst_offset ~ dst_size ~ src_offset ~ src_size = let dox , doy = dst_offset in let dsx , dsy = dst_size in let sox , soy = src_offset in let ssx , ssy = src_size in warp_region t . t dox doy dsx dsy sox soy ssx ssy let warp_region_to_buffer = Lib . c " GDALWarpRegionToBuffer " ( t @-> int @-> int @-> int @-> int @-> ptr void @-> int @-> int @-> int @-> int @-> int @-> returning err ) let warp_region_to_buffer ? buffer t dt ~ dst_offset ~ dst_size ~ src_offset ~ src_size = let open Bigarray in let dox , doy = dst_offset in let dsx , dsy = dst_size in let sox , soy = src_offset in let ssx , ssy = src_size in let buffer = match buffer with | Some b -> let rows = Array2 . dim1 b in let cols = Array2 . dim2 b in if cols < dox + dsx || rows < doy + dsy then invalid_arg " Buffer is too small " else b | None -> let kind = Band . Data . to_ba_kind dt in Array2 . create kind c_layout ( doy + dsy ) ( dox + dsx ) in let buffer_ptr = bigarray_start array2 buffer in let dt_i = Band . Data . to_int dt in warp_region_to_buffer t . t dox doy dsx dsy ( to_voidp buffer_ptr ) dt_i sox soy ssx ssy ; buffer let wrap ? offset ? size options f = let offset = match offset with | Some o -> o | None -> 0 , 0 in let size = match size with | Some s -> s | None -> let ds = getf !@ Options . ( options . o ) Options . Raw . dst_ds in Data_set . get_x_size ds , Data_set . get_y_size ds in let operation = create options in f operation ~ offset ~ size let warp ? offset ? size options = wrap ? offset ? size options chunk_and_warp_image let warp_multi ? offset ? size options = wrap ? offset ? size options chunk_and_warp_multi end |
let init ( ) = Gdal . Lib . init_dynamic ( ) ; Gdal . Lib . register_all ( ) ; ( ) |
let usage_and_exit ( ) = prerr_endline " Usage : warptut [ src ] [ dst ] " ; prerr_endline " [ src ] must exist " ; prerr_endline " [ dst ] must not exist " ; exit 1 |
let parse_args ( ) = try let s = Sys . argv . ( 1 ) in let d = Sys . argv . ( 2 ) in if Sys . file_exists s && not ( Sys . file_exists d ) then s , d else raise Exit with | _ -> usage_and_exit ( ) |
let ( ) = init ( ) ; let src_file , dst_file = parse_args ( ) in let src_ds = Gdal . Data_set . of_source_exn src_file in let dst_bands = Gdal . Data_set . get_count src_ds in let dst_dt = Gdal . Band . Data . Float32 in let driver = Gdal . Driver . get_by_name_exn " GTiff " in let dst_sr = Gdal . Spatial_reference . make ` name " WGS84 " in let dst_wkt = Gdal . Spatial_reference . to_wkt dst_sr in let transform = Gdal . Transform . make_gen_img @@ ` data_set_wkt ( src_ds , dst_wkt ) in let warp_suggestions = Gdal . Warp . suggested_warp_output src_ds transform in Gdal . Transform . set_dst_geo_transform transform warp_suggestions . Gdal . Warp . geo_transform ; let dst_ds = Gdal . Data_set . create_exn ~ bands ( : dst_bands , dst_dt ) driver dst_file warp_suggestions . Gdal . Warp . dims in Gdal . Data_set . set_projection dst_ds dst_wkt ; Gdal . Geo_transform . set dst_ds warp_suggestions . Gdal . Warp . geo_transform ; let bands = Array . init dst_bands ( fun i -> i + 1 , i + 1 ) |> Array . to_list in let warp_options = Gdal . Warp . Options . make ~ resample_alg : Gdal . Warp . Nearest_neighbor ~ src : src_ds ~ dst : dst_ds ~ bands ~ transformer : transform ( ) in Gdal . Warp . Operation . warp warp_options ; Gdal . Data_set . close src_ds ; Gdal . Data_set . close dst_ds ; ( ) |
let rec highest_timestamp ( ? highest = 0 . ) folders = match folders with | [ ] -> highest | file :: rest -> ( let is_dir = Sys . is_directory file in match is_dir with | ` Yes -> let inside_dirs = Sys . ls_dir file in let inside_dirs = List . map inside_dirs ~ f ( : fun x -> file ^ " " / ^ x ) in highest_timestamp ~ highest ( inside_dirs @ rest ) | _ -> let stat = Unix . stat file in let modif_time = stat . st_mtime in let highest = if Float . ( modif_time > highest ) then modif_time else highest in highest_timestamp ~ highest rest ) |
let rec has_changed ( last_change : float ) ( folders : string list ) : float option = match folders with | [ ] -> None | file :: rest -> ( let is_dir = Sys . is_directory file in match is_dir with | ` Yes -> let inside_dirs = Sys . ls_dir file in let inside_dirs = List . map inside_dirs ~ f ( : fun x -> file ^ " " / ^ x ) in has_changed last_change ( inside_dirs @ rest ) | _ -> let stat = Unix . stat file in let modif_time = stat . st_mtime in if Float . ( modif_time > last_change ) then Some modif_time else has_changed last_change rest ) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.