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