home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / SmallEiffel.lha / SmallEiffel / lib_std / basic_directory.e < prev    next >
Text File  |  1999-06-05  |  9KB  |  315 lines

  1. -- This file is  free  software, which  comes  along  with  SmallEiffel. This
  2. -- software  is  distributed  in the hope that it will be useful, but WITHOUT 
  3. -- ANY  WARRANTY;  without  even  the  implied warranty of MERCHANTABILITY or
  4. -- FITNESS  FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
  5. -- this header is kept unaltered, and a notification of the changes is added.
  6. -- You  are  allowed  to  redistribute  it and sell it, alone or as a part of 
  7. -- another product.
  8. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  9. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr 
  10. --                       http://www.loria.fr/SmallEiffel
  11. --
  12. class BASIC_DIRECTORY
  13.    --
  14.    -- Low-level basic tools for file-system directory handling.
  15.    --
  16.    -- Also consider hight level facade class DIRECTORY for a safer usage.
  17.    --
  18.  
  19. creation make
  20.  
  21. feature
  22.  
  23.    path: STRING;
  24.          -- Void or some `path' which may be absolute or not (see `set_path').
  25.  
  26.    name_list: FIXED_ARRAY[STRING];
  27.          -- Actual list of entries (files or subdirectories)..
  28.  
  29. feature {NONE} -- Creation :
  30.  
  31.    make is
  32.          -- Make a new object which is _not_ connected to any 
  33.          -- directory on disk.
  34.          -- Use `set_path' to connect Current to a directory on disk.
  35.       do
  36.          !!name_list.with_capacity(64);
  37.       end;
  38.  
  39. feature -- Access :
  40.  
  41.    lower: INTEGER is 0;
  42.          -- Index of the first item.
  43.  
  44.    upper: INTEGER is
  45.          -- Index of the last item.
  46.       do
  47.          Result := name_list.upper;
  48.       end;
  49.  
  50.    count: INTEGER is
  51.          -- Number of items (files or directories) in Current.
  52.       do
  53.          Result := name_list.count;
  54.       ensure
  55.          Result >= 0
  56.       end;
  57.  
  58.    valid_index(index: INTEGER): BOOLEAN is
  59.       do
  60.          Result := index >= 0 and then index <= name_list.upper;
  61.       ensure
  62.          Result = index.in_range(0,upper)
  63.       end;
  64.  
  65.    is_subdirectory(index: INTEGER): BOOLEAN is
  66.          -- Is the item at `index' a subdirectory ?
  67.       require
  68.          valid_index(index)
  69.       do
  70.          not_yet_implemented;
  71.       end;
  72.  
  73.    is_file(index: INTEGER): BOOLEAN is
  74.          -- Is the item at `index' a file ?
  75.       require
  76.          valid_index(index)
  77.       do
  78.          Result := not is_subdirectory(index);
  79.       ensure
  80.          Result = not is_subdirectory(index)
  81.       end;
  82.  
  83.    absolute_path: STRING is
  84.          -- Gives acces to the absolute path of Current.
  85.       do
  86.          not_yet_implemented;
  87.       ensure
  88.          Result /= Void
  89.       end;
  90.  
  91.    absolute_path_of(index: INTEGER): STRING is
  92.          -- Gives access to absolute path of item at `index'.
  93.       require
  94.          valid_index(index)
  95.       do
  96.          not_yet_implemented;
  97.       ensure
  98.          Result /= Void
  99.       end;
  100.  
  101.    get_parent_directory: like Current is
  102.          -- Get new object corresponding to parent directory.
  103.          -- Return Void in case of failure.
  104.          -- Also consider `go_parent_directory'.
  105.       do
  106.          not_yet_implemented;
  107.       end;
  108.  
  109.    get_subdirectory(index: INTEGER): like Current is
  110.          -- Get new directory object corresponding to item at `index'.
  111.          -- Return Void in case of failure.
  112.          -- Also consider `go_subdirectory'.
  113.       require
  114.          is_subdirectory(index)
  115.       do
  116.          not_yet_implemented;
  117.       end;
  118.  
  119.    has(entry_name: STRING): BOOLEAN is
  120.          -- Does Current contain the `entry_name' (file or subdirectory) ?
  121.       require
  122.          not entry_name.empty
  123.       do
  124.          Result := name_list.has(entry_name);
  125.       end;
  126.  
  127.    index_of(entry_name: STRING): INTEGER is
  128.          -- Index of `entry_name' (file or subdirectory).
  129.          -- Return `count + 1' if not found.
  130.       do
  131.          Result := name_list.index_of(entry_name);
  132.       ensure
  133.          (Result = count + 1) or valid_index(Result)
  134.       end;
  135.  
  136.    has_file(filename : STRING): BOOLEAN is
  137.          -- Does Current contain `filename' as an entry for a file ?
  138.       require
  139.          not filename.empty
  140.       local
  141.          i: INTEGER
  142.       do
  143.          i := index_of(filename);
  144.          if valid_index(i) then
  145.             Result := is_file(i);
  146.          end;
  147.       ensure
  148.          Result implies is_file(index_of(filename))
  149.       end;
  150.  
  151.    has_subdirectory(dirname : STRING): BOOLEAN is
  152.          -- Does Current contain `dirname' as an entry for a subdirectory ?
  153.       require
  154.          not dirname.empty
  155.       local
  156.          i: INTEGER
  157.       do
  158.          i := index_of(dirname);
  159.          if valid_index(i) then
  160.             Result := is_subdirectory(i);
  161.          end;
  162.       ensure
  163.          Result implies is_subdirectory(index_of(dirname))
  164.       end;
  165.  
  166.    name(index: INTEGER): STRING is
  167.          -- Return the name of entry (file or subdirectory) at `index'.
  168.       require
  169.          valid_index(index)
  170.       do
  171.          Result := name_list.item(index);
  172.       ensure
  173.          has(Result)
  174.       end;
  175.  
  176. feature -- Modification :
  177.  
  178.    go_parent_directory: BOOLEAN is
  179.          -- Change Current to its parent directory; do not change 
  180.          -- anything in case of failure.
  181.          -- Return true if succeeded, false otherwise.
  182.          -- Also consider `get_parent_directory'.
  183.       do
  184.          not_yet_implemented;
  185.       end;
  186.  
  187.    go_subdirectory(index: INTEGER): BOOLEAN is
  188.          -- Change Current to subdirectory at `index'; do not change 
  189.          -- anything in case of failure.
  190.          -- Return true if succeeded, false otherwise.
  191.          -- Also consider `get_subdirectory'.
  192.       require
  193.          is_subdirectory(index)
  194.       do
  195.          not_yet_implemented;
  196.       end;
  197.  
  198. feature -- Disk access :
  199.  
  200.    update: BOOLEAN is
  201.          -- Update Current status by reloading information from the 
  202.          -- disk.
  203.          -- Return true if succeeded, false otherwise.
  204.       local
  205.          path_pointer, dirstream_pointer, entry_pointer: POINTER;
  206.          entry: STRING;
  207.       do
  208.          name_list.clear;
  209.          path_pointer := path.to_external;
  210.          dirstream_pointer := se_dir_open(path_pointer);
  211.          if dirstream_pointer.is_not_null then
  212.             from
  213.                Result := true;
  214.             until
  215.                dirstream_pointer.is_null
  216.             loop
  217.                entry_pointer := se_dir_name(dirstream_pointer);
  218.                if entry_pointer.is_not_null then
  219.                   !!entry.from_external_copy(entry_pointer);
  220.                   name_list.add_last(entry);
  221.                end;
  222.                dirstream_pointer := se_dir_next(dirstream_pointer);
  223.             end;
  224.          end;
  225.       end;
  226.  
  227. feature -- Disk access :
  228.  
  229.    set_path(p: like path): BOOLEAN is
  230.          -- Connects Current to the existing directory `path' on disk.
  231.          -- Support of absolute and/or relative paths is not guaranteed for 
  232.          -- portability reasons. :-)
  233.          -- Return true if succeeded, false otherwise.
  234.          -- In case of success, `update' has been successfully done.
  235.       require
  236.          not p.empty
  237.       do
  238.          path := p;
  239.          if update then
  240.             Result := true;
  241.          else
  242.             path := Void;
  243.          end;
  244.       ensure
  245.          Result implies path = p
  246.       end;
  247.  
  248. feature -- current directory handling :
  249.  
  250.    current_working_directory is
  251.          -- Update Current with the current working directory.
  252.       local
  253.          path_pointer: POINTER;
  254.       do
  255.          path_pointer := se_dir_gcwd(0);
  256.          !!path.from_external_copy(path_pointer);
  257.          if update then 
  258.          else
  259.             path := Void;
  260.          end;
  261.       end;
  262.  
  263. feature -- Disk modification :
  264.  
  265.    create_subdirectory(dirname: STRING): BOOLEAN is
  266.          -- Create a new (one-level) subdirectory `dirname' on disk.
  267.          -- Nested creations support and semantics are not guaranteed for 
  268.          -- portability reasons.
  269.          -- Return true if succeeded, false otherwise.
  270.          -- In case of success, `update' has been successfully done.
  271.       require
  272.          not has(dirname)
  273.       do
  274.          not_yet_implemented;
  275.       end;
  276.  
  277.    delete_subdirectory(dirname: STRING): BOOLEAN is
  278.          -- Delete (one-level) subdirectory `dirname' on disk.
  279.          -- Nested deletions support and semantics are not guaranteed for 
  280.          -- portability reasons.
  281.          -- Return true if succeeded, false otherwise.
  282.          -- In case of success, `update' has been successfully done.
  283.       require
  284.          is_subdirectory(index_of(dirname))
  285.       do
  286.          not_yet_implemented;
  287.       end;
  288.  
  289. feature {NONE}
  290.  
  291.    se_dir_open(path_pointer: POINTER): POINTER is
  292.       require
  293.          path_pointer.is_not_null
  294.       external "SmallEiffel"
  295.       end;
  296.  
  297.    se_dir_name(dirstream_pointer: POINTER): POINTER is
  298.       require
  299.          dirstream_pointer.is_not_null
  300.       external "SmallEiffel"
  301.       end;
  302.  
  303.    se_dir_next(dirstream_pointer: POINTER): POINTER is
  304.       require
  305.          dirstream_pointer.is_not_null
  306.       external "SmallEiffel"
  307.       end;
  308.  
  309.    se_dir_gcwd(dummy: INTEGER): POINTER is
  310.       external "SmallEiffel"
  311.       end;
  312.  
  313. end -- BASIC_DIRECTORY
  314.  
  315.