1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 /****************************************************************************/
4 /*                                                                              */
5 /* File:      ugenv.h                                                       */
6 /*                                                                          */
7 /* Purpose:   header file for ug environment manager                        */
8 /*                                                                          */
9 /* Author:      Peter Bastian                                               */
10 /*              Interdisziplinaeres Zentrum fuer Wissenschaftliches Rechnen */
11 /*              Universitaet Heidelberg                                     */
12 /*              Im Neuenheimer Feld 368                                     */
13 /*              6900 Heidelberg                                             */
14 /*                                                                          */
15 /* History:   06.02.92 begin, ug version 2.0                                */
16 /*                                                                          */
17 /* Revision:  08.09.95                                                      */
18 /*                                                                          */
19 /****************************************************************************/
20 
21 /** \file
22     \brief General data management concept in a tree structure
23 
24     The environment management of ug provides the possibility to store data in
25     a tree structure.
26     The data structures of the environment allow to create directories and items
27     of specified size. Both data structures start with a general head (among
28     others a name by which one can refer to it). The remaining memory up to the
29     specified size can be used in arbitrary way.
30 
31     The head is identical with the struct ENVVAR.
32 
33     All items are members of doubly linked lists.
34 
35     The data structure for the directory ENVDIR has just an extra component to the
36     start of a list which is the directory contents (and can consist of
37     directories itself, of course).
38 
39     The tree starts with a root directory "/" and there is always a current
40     or working directory. Paths are specified in UNIX-style. The current
41     directory can be changed using 'ChangeEnvDir' while 'GetCurrentDir'
42     returns a pointer to the current directory. The routine
43     'MakeEnvItem' creates the specified item in the current directory and
44     it is possible to 'RemoveEnvItem's created previously.
45 
46     Finally 'SearchEnv' offers the possibility to hierarchically search
47     the environment tree for an item specified by its name.
48 
49  */
50 
51 
52 
53 /****************************************************************************/
54 /*                                                                          */
55 /* auto include mechanism and other include files                           */
56 /*                                                                          */
57 /****************************************************************************/
58 
59 #ifndef __UGENV__
60 #define __UGENV__
61 
62 
63 #include "ugtypes.h"
64 
65 #include "namespace.h"
66 
67 START_UG_NAMESPACE
68 
69 /*****************************************************************************/
70 /*                                                                           */
71 /* defines in the following order                                            */
72 /*                                                                           */
73 /*          compile time constants defining static data size (i.e. arrays)   */
74 /*          other constants                                                  */
75 /*          macros                                                           */
76 /*                                                                           */
77 /*****************************************************************************/
78 
79 /* CAUTION: when changing NAMESIZE also change scanf sizes!!! */
80 enum {NAMESIZE = 128};                 /* max length of name string            */
81 enum {NAMELEN  = 127};                 /* NAMESIZE-1                            */
82 #define NAMELENSTR             "127"    /* NAMESIZE-1 as string                    */
83 
84 enum {SEARCHALL = -1};                 /*!< Scan through all directories         */
85 #define DIRSEP                "/"     /* character separating directories     */
86 
87 /* directories with odd numbers */
88 #define ROOT_DIR            1        /* indicates root directory             */
89 
90 /** \brief Return pointer to the first 'ENVITEM' contained in the directory. */
91 #define ENVITEM_DOWN(p)         (((ENVITEM *)(p))->d.down)
92 
93 /** \brief Return pointer to the first 'ENVDIR' contained in the directory. */
94 #define ENVDIR_DOWN(p)            ((p)->down)
95 
96 /** \brief Return pointer to the next 'ENVITEM' in the doubly linked list */
97 #define NEXT_ENVITEM(p)         (((ENVITEM *)(p))->v.next)
98 
99 /** \brief Return pointer to the previous 'ENVITEM' in the doubly linked list. */
100 #define PREV_ENVITEM(p)         (((ENVITEM *)(p))->v.previous)
101 
102 /** \brief Return the type of the 'ENVITEM' (odd: 'ENVDIR', even: 'ENVVAR'). */
103 #define ENVITEM_TYPE(p)         (((ENVITEM *)(p))->v.type)
104 
105 #define IS_ENVDIR(p)            (ENVITEM_TYPE(p)%2==1)
106 
107 /** \brief This macro returns a pointer to the name string of the 'ENVITEM'. */
108 #define ENVITEM_NAME(p)         (((ENVITEM *)(p))->v.name)
109 
110 /** \brief 'RemoveEnvItem' checks this and returns an error if true. */
111 #define ENVITEM_LOCKED(p)         (((ENVITEM *)(p))->v.locked)
112 
113 /****************************************************************************/
114 /*                                                                          */
115 /* data structures exported by the corresponding source file                */
116 /*                                                                          */
117 /****************************************************************************/
118 
119 /** \brief User-defined variable */
120 typedef struct {
121 
122   /** \brief even number by GetNewEnvVarID           */
123   INT type;
124 
125   /** \brief May not be changed or deleted            */
126   INT locked;
127 
128   /** \brief Doubly linked list of environment items    */
129   union envitem *next;
130   union envitem *previous;
131 
132   /** \brief Name of that item. May be longer, but of no interest for env*/
133   char name[NAMESIZE];
134 
135 } ENVVAR;
136 
137 /** \brief Directory */
138 typedef struct {
139 
140   /** \brief odd number by GetNewEnvDirID        */
141   INT type;
142 
143   /** \brief May not be changed or deleted            */
144   INT locked;
145 
146   /** \brief Doubly linked list of environment items    */
147   union envitem *next;
148   union envitem *previous;
149 
150   /** \brief Name of that item                        */
151   char name[NAMESIZE];
152 
153   /** \brief One level down in the tree                */
154   union envitem *down;
155 } ENVDIR;
156 
157 union envitem {
158   ENVVAR v;
159   ENVDIR d;
160 };
161 
162 typedef union envitem ENVITEM;
163 
164 /****************************************************************************/
165 /*                                                                          */
166 /* function declarations                                                    */
167 /*                                                                          */
168 /****************************************************************************/
169 
170 /* initialize environment  */
171 INT      InitUgEnv        ();
172 
173 /* Free all memory allocated for the environment */
174 INT      ExitUgEnv();
175 
176 /* change directory allows /, .., etc */
177 ENVDIR    *ChangeEnvDir    (const char *s);
178 
179 /* get the current working directory */
180 ENVDIR    *GetCurrentDir    (void);
181 
182 /* get path name of the current directory */
183 void     GetPathName    (char *s);
184 
185 /* create a new environment item with user defined size */
186 ENVITEM *MakeEnvItem    (const char *name, const INT type, const INT size);
187 
188 /* remove an item */
189 INT      RemoveEnvItem    (ENVITEM *theItem);
190 
191 /* remove an complete directory */
192 INT      RemoveEnvDir     (ENVITEM *theItem);
193 
194 /* move an envitem to a new directory */
195 INT              MoveEnvItem      (ENVITEM *item, ENVDIR *oldDir, ENVDIR *newDir);
196 
197 /* search the environment for an item */
198 ENVITEM *SearchEnv        (const char *name, const char *where, INT type, INT dirtype);
199 
200 /* allocate memory from the environment heap */
201 void    *AllocEnvMemory (INT size);
202 
203 /* deallocate memory from the environment heap */
204 void     FreeEnvMemory    (void *buffer);
205 
206 /* print used and size of environment heap */
207 void     EnvHeapInfo     (char *s);
208 
209 /* return a unique ID for a new ENVDIR/ENVVAR type */
210 INT      GetNewEnvDirID (void);
211 INT      GetNewEnvVarID (void);
212 
213 
214 END_UG_NAMESPACE
215 
216 #endif
217