include "c:/mesdoc~1/infosp~1/td/analys~1/excell/porky";;


(* D'abord, un compteur d'octets *)



let taille file_name = (* nom du fichier avec extension *)
  let c=ref 0 and toto=ref ` ` and file=open_in file_name in
   try
     while true do toto:=input_char file; c:=!c+1 done;
     2
   with End_of_file -> !c;;

taille "truc.slx";;


let get_number file=
  let q=ref 0 in
    try
      while(!q <> 12) do
        match (!q,input_char file) with
          | (0,`N`) -> q:=1;
          | (1,`b`) -> q:=2;
          | (2,` `) -> q:=3;
          | (3,`e`) -> q:=4;          
          | (4,`l`) -> q:=5;
          | (5,`e`) -> q:=6;          
          | (6,`v`) -> q:=7;
          | (7,`e`) -> q:=8;
          | (8,`s`) -> q:=9;
          | (9,` `) -> q:=10;          
          | (10,`:`) -> q:=11;
          | (11,` `) -> q:=12;
          | (_,_) -> q:=0;
      done;
      while (input_char file)=` ` do done;
      seek_in file ((pos_in file)-1);
      input_binary_int file
    with
      |End_of_file -> failwith "error : bad format input file";;

let next_student f=
  let q=ref 0 and c=ref ` ` and name=ref "" in
    try
      while(!q <> 5) do
        match (!q,input_char f) with
          | (0,`N`) -> q:=1;
          | (1,`o`) -> q:=2;
          | (2,`m`) -> q:=3;
          | (3,` `) -> q:=4;          
          | (4,`:`) -> q:=5;
          | (_,_) -> q:=0;
      done;
      while !c=` ` do c:=input_char f done;
      while !c<>` ` do name:= !name^(string_of_char !c); c:=input_char f done;
      while !c=` ` do c:=input_char f done;
      q:=0;
      while(!q <> 5) do
        match (!q,input_char f) with
          | (0,`o`) -> q:=1;
          | (1,`t`) -> q:=2;
          | (2,`e`) -> q:=3;
          | (3,` `) -> q:=4;          
          | (4,`:`) -> q:=5;
          | (_,_) -> q:=0;
      done;
      while (input_char f)=` ` do done;
      seek_in f ((pos_in f)-1);
    !name,input_binary_int f
    with
      |End_of_file -> failwith "error : bad format input file";;

#open "sort";;

let lexico s t = (s=t) ||  (s < t);;

let lexico_fst x y=lexico (fst x) (fst y);;

let slx2txt name=
  let in_file=open_in (name^".slx") and out_file=open_out (name^".txt")
  in let n= (get_number in_file) in let v=make_vect n ("",0) in
    for i=0 to (n-1) do v.(i)<-next_student in_file done;
    output_string out_file "Nombre d'eleves : ";
    output_string out_file (string_of_int n);
    output_char out_file `\n`;
    let w=vect_of_list (sort lexico_fst (list_of_vect v)) in
    for i=0 to n-1 do
      output_string out_file ((fst w.(i))^(" : ")^(string_of_int (snd w.(i)))^"\n")
    done;
    close_in in_file; close_out out_file;;


slx2txt "azertyu";;