Determining the mime file type

How to determine mime file type (in OCaml)?

I am trying to set the language for the GtkSourceView control, but for this I need to define the language first. The only way I can do this is to use mime-type - there is a function that will return the correct language as follows:

GSourceView.source_languages_manager#get_language_from_mime_type : string -> source_language option

I really don't want to hardcode the language into my source code. If it is not possible to determine the mime type in OCaml (and I have not yet found a way by searching the documentation), maybe there is another way to determine the source language?

+3
source share
4 answers

, , OCaml. Apache mime.types - . - , . OCaml:

let mimetype_of_extension = function
    | "txt" | "log" -> "text/plain"
    | "html" | "htm" -> "text/html"
    | "zip" | "application/zip"
...

- , .

, , text/plain. ; , get_language_from_mime_type.

, , , . , .

let extension_of_filename filename =
    let pos = (String.rindex filename '.') + 1 in
    let len = String.length filename in
    let ext = String.create (len - pos) in
    String.blit filename pos ext 0 (len - pos);
    ext;;

, , , Brainfuck OCaml, . - "c" - C, "h"; "" OCaml; .

+3

gedit, , glib, . g_file_info_get_content_type(). g_content_type_get_mime_type(), glib.

, , , .

+3

GTK , .

/etc/mime.types - , . , Ocsigen Ocamlnet , , , (, , Ocamlnet netstring).

+2

, , ( /etc/mime.types IMO), OCaml libmagic, .

+1

Source: https://habr.com/ru/post/1748457/


All Articles