home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / sos3-2.lha / src / dir / dir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-23  |  5.9 KB  |  205 lines

  1. /* --------------------------------------------------------------------------
  2.  * Copyright 1992 by Forschungszentrum Informatik (FZI)
  3.  *
  4.  * You can use and distribute this software under the terms of the licence
  5.  * you should have received along with this program.
  6.  * If not or if you want additional information, write to
  7.  * Forschungszentrum Informatik, "STONE", Haid-und-Neu-Strasse 10-14,
  8.  * D-7500 Karlsruhe 1, Germany.
  9.  * --------------------------------------------------------------------------
  10.  */
  11. #include "sys.h"
  12. #include "smg.h"
  13. #include "trc_dir.h"
  14. #include "dir_err.h"
  15. #include "dir_sos.h"
  16.  
  17. // *************************************************************************
  18. void sos_Object_Directory::local_initialize(sos_Object_Directory dir)
  19. // *************************************************************************
  20. {
  21.    T_PROC ("sos_Object_Directory::local_initialize");
  22.    TT (dir_H, T_ENTER);
  23.  
  24.    dir.set_name (sos_String::copy (dir.get_name(), dir.container()));
  25.  
  26.    TT (dir_H, T_LEAVE);
  27. }
  28.  
  29. // *************************************************************************
  30. void sos_Object_Directory::local_finalize(sos_Object_Directory dir)
  31. // *************************************************************************
  32. {
  33.    T_PROC ("sos_Object_Directory::local_finalize");
  34.    TT (dir_H, T_ENTER);
  35.  
  36.    dir.get_name().destroy();
  37.    agg_iterate_association (dir, sos_String name, sos_Object elem)
  38.       name.destroy();
  39.    agg_iterate_association_end (dir, name, elem);
  40.  
  41.    TT (dir_H, T_LEAVE);
  42. }
  43.  
  44.  
  45. // *************************************************************************
  46. sos_Object_Directory sos_Object_Directory::root()
  47. // *************************************************************************
  48. {
  49.    T_PROC ("sos_Object_Directory::root");
  50.    TT (dir_H, T_ENTER);
  51.  
  52.    sos_Bool open = (sos_Bool) ((ROOT_CONTAINER.status() == READABLE) OR
  53.                        (ROOT_CONTAINER.status() == WRITEABLE));
  54.  
  55.    if (NOT open) ROOT_CONTAINER.open (READING, WAITING);
  56.  
  57.    sos_Object_Directory d =
  58.       sos_Object_Directory::make (ROOT_CONTAINER.root_object());
  59.  
  60.    if (NOT open) ROOT_CONTAINER.close ();
  61.  
  62.    TT (dir_H, T_LEAVE);
  63.  
  64.    return d;
  65. }
  66.  
  67. // *************************************************************************
  68. sos_Object sos_Object_Directory::lookup(sos_String path)
  69. // *************************************************************************
  70. {
  71.    T_PROC ("sos_Object_Directory::lookup");
  72.    TT (dir_H, T_ENTER);
  73.  
  74.    sos_Object result = sos_Object_Directory::root();
  75.    sos_Object_Directory dir;
  76.    smg_String s0 = path;
  77.    sos_Cstring s = s0.make_Cstring (SMG_BORROW);
  78.    sos_Cstring name = new char[strlen(s)];
  79.  
  80.    for (;;)
  81.    {  if (s[0] == '/'  AND  result.isa (sos_Object_Directory_type))
  82.      dir = sos_Object_Directory::make (result);
  83.       else
  84.       {  result = sos_Object_Directory::make (NO_OBJECT);
  85.      break;
  86.       }
  87.       if (*++s == EOS)
  88.      break;
  89.       sscanf (s, "%[^/]", name);
  90.       if (name[0] != EOS)
  91.       {  sos_String str = smg_String (name).make_String (TEMP_CONTAINER);
  92.      result = dir [str];
  93.      str.destroy();
  94.      s += strlen (name);
  95.      if (result == NO_OBJECT  OR  s[0] == EOS)
  96.         break;
  97.       }
  98.    }
  99.    delete name;
  100.  
  101.    TT (dir_H, T_LEAVE);
  102.  
  103.    return result;
  104. }
  105.  
  106. // *************************************************************************
  107. void sos_Object_Directory::insert(sos_Object so, sos_Object o)
  108. // *************************************************************************
  109. {
  110.    T_PROC ("sos_Object_Directory::insert");
  111.    TT (dir_H, T_ENTER);
  112.  
  113.    sos_String s = sos_String::make (so);
  114.    sos_String name;
  115.    if (self.is_key (s))
  116.       name = s;
  117.    else
  118.       name = sos_String::copy (s, self.container());
  119.    sos_Object_sos_Object_Mapping::insert (name, o);
  120.    TT (dir_H, T_LEAVE);
  121. }
  122.  
  123.  
  124. // *************************************************************************
  125. void sos_Object_Directory::remove(sos_Object so)
  126. // *************************************************************************
  127. {
  128.    T_PROC ("sos_Object_Directory::remove");
  129.    TT (dir_H, T_ENTER);
  130.  
  131.    sos_String s = sos_String::make (so);
  132.    sos_Cursor c = self.open_cursor ();
  133.    self.move_cursor (c, s);
  134.    sos_Object name = self.get_key (c);
  135.    sos_Object_sos_Object_Mapping::remove (s);
  136.    name.destroy();
  137.    self.close_cursor (c);
  138.  
  139.    TT (dir_H, T_LEAVE);
  140. }
  141.  
  142. static sos_Object_Directory *wd;
  143.  
  144. // *************************************************************************
  145. void sos_Object_Directory::set_wd (sos_String path)
  146. // *************************************************************************
  147. {
  148.    T_PROC ("sos_Object_Directory::set_wd");
  149.    TT (dir_H, T_ENTER);
  150.  
  151.    sos_Object d = sos_Object_Directory::lookup (path);
  152.    
  153.    if (d == NO_OBJECT OR NOT d.isa (sos_Object_Directory_type))
  154.    {  sos_Cstring pname = path.make_Cstring();
  155.       err_raise (err_SYS, err_DIR_NO_DIR, pname);
  156.       delete pname;
  157.    }
  158.    else
  159.    {  wd = new sos_Object_Directory;
  160.      *wd = sos_Object_Directory::make (d);
  161.    }
  162.  
  163.    TT (dir_H, T_LEAVE);
  164. }
  165.  
  166. // *************************************************************************
  167. void sos_Object_Directory::set_wd_from_env ()
  168. // *************************************************************************
  169. {
  170.    T_PROC ("sos_Object_Directory::set_wd_from_env");
  171.    TT (dir_H, T_ENTER);
  172.  
  173.    sos_Cstring e=getenv("SOSDIR");
  174.    if (e==0)
  175.       err_raise (err_SYS, err_DIR_NO_DIR, NULL, FALSE);
  176.    else
  177.    {
  178.       sos_String s = smg_String (e).make_String (TEMP_CONTAINER);
  179.       sos_Object_Directory::set_wd(s);
  180.       s.destroy();
  181.    }
  182.  
  183.    TT (dir_H, T_LEAVE);
  184. }
  185.  
  186. // *************************************************************************
  187. sos_Object_Directory sos_Object_Directory::get_wd ()
  188. // *************************************************************************
  189. {
  190.    T_PROC ("sos_Object_Directory::get_wd");
  191.    TT (dir_H, T_ENTER);
  192.  
  193.    sos_Object_Directory result;
  194.  
  195.    if (wd)
  196.       result = *wd;
  197.    else
  198.    {  err_raise (err_SYS, err_DIR_NO_WD, NULL, FALSE);;
  199.       result = sos_Object_Directory::make (NO_OBJECT);
  200.    }
  201.  
  202.    TT (dir_H, T_LEAVE);
  203.    return result;
  204. }
  205.