///////////////////////////////////////////////////////////////////////////////
//                            Lisaac OS Library                              //
//                                                                           //
//                   LSIIT - ULP - CNRS - INRIA - FRANCE                     //
//                                                                           //
//   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/>.   //
//                                                                           //
//                     http://isaacproject.u-strasbg.fr/                     //
///////////////////////////////////////////////////////////////////////////////
Section Header
  
  + name        :=DIRECTORY;

  - copyright   := "2003-2005 Jérome Boutet, 2003-2007 Benoit Sonntag";
  
  - bibliography:="http://IsaacOS.com";
  
  - author      :="Benoit Sonntag (bsonntag@loria.fr)";
  
  - comment     :="Directory management";
  
  - external := 
`
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
`;
  
Section Inherit
  
  + parent_abstract_directory:Expanded ABSTRACT_DIRECTORY;
  
Section Private  
  
  //
  // Physical implementation.
  //
  
  //
  // Scanning
  //
      
  - physical_refresh:BOOLEAN <-
  ( + p,n:NATIVE_ARRAY[CHARACTER];
    + dir,dirent:POINTER;
    + new_entry:ENTRY;
    + result:BOOLEAN;
    + i:INTEGER;
    
    list.clear;
    p := path.to_external;
    dir := `opendir(@p)`:POINTER; 
    (dir != NULL).if {
      result := TRUE;
      {

	dirent := `readdir(@dir)`:POINTER;
	(dirent != NULL).if {
	  n := `((struct dirent *)@dirent)->d_name`:NATIVE_ARRAY[CHARACTER];	
	  string_tmp.clear;
	  i := 0;
	  {n.item i = '\0'}.until_do { 
	    string_tmp.add_last (n.item i);
	    i := i + 1;
	  };
	  (string_tmp !== ".".to_string).if {
	    string_tmp.add_first '/';
	    string_tmp.prepend path;
	    reduce_path string_tmp;
	    new_entry := get_entry string_tmp;
	    (new_entry = NULL).if {	      	      
	      result := FALSE;
	    } else {
	      (new_entry.path.count < path.count).if {
		parent := new_entry;
	      } else {
		list.add_last new_entry;
	      };
	    };
	  };
	};
      }.do_while {(dirent != NULL) && {result}};
      `closedir(@dir)`;
    };
    result
  );
    
  - physical_make_directory new_path:STRING :BOOLEAN <-
  ( + p:NATIVE_ARRAY[CHARACTER];
    p := new_path.to_external;
    `mkdir(@p,S_IRWXU)`:(INTEGER) = 0
  );

  - physical_make_file new_path:STRING :BOOLEAN <-
  ( + p:NATIVE_ARRAY[CHARACTER];
    + stream:POINTER;
    + result:BOOLEAN;
    
    p := new_path.to_external;
    stream := `fopen((char*)@p,"w+b")`:POINTER;
    (stream != NULL).if {
      result := `fclose((FILE*)@stream)`:INTEGER = 0;
    };
    result
  );

  
