xref: /petsc/src/sys/objects/prefix.c (revision 7453f77509fc006cbcc7de2dd772f60dc49feac5)
1 
2 /*
3      Provides utility routines for manulating any type of PETSc object.
4 */
5 #include <petsc/private/petscimpl.h>  /*I   "petscsys.h"    I*/
6 
7 #undef __FUNCT__
8 #define __FUNCT__ "PetscObjectSetOptions"
9 /*@C
10    PetscObjectSetOptions - Sets the options database used by the object
11 
12    Collective on PetscObject
13 
14    Input Parameters:
15 +  obj - any PETSc object, for example a Vec, Mat or KSP.
16 -  options - the options database, use NULL for default
17 
18    Notes: if this is not called the object will use the default options database
19 
20   Level: advanced
21 
22 .seealso: PetscOptionsCreate(), PetscOptionsDestroy()
23 
24 @*/
25 PetscErrorCode  PetscObjectSetOptions(PetscObject obj,PetscOptions options)
26 {
27   PetscFunctionBegin;
28   PetscValidHeader(obj,1);
29   obj->options = options;
30   PetscFunctionReturn(0);
31 }
32 
33 #undef __FUNCT__
34 #define __FUNCT__ "PetscObjectSetOptionsPrefix"
35 /*
36    PetscObjectSetOptionsPrefix - Sets the prefix used for searching for all
37    options of PetscObjectType in the database.
38 
39    Collective on Object
40 
41    Input Parameters:
42 .  obj - any PETSc object, for example a Vec, Mat or KSP.
43 .  prefix - the prefix string to prepend to option requests of the object.
44 
45    Notes:
46    A hyphen (-) must NOT be given at the beginning of the prefix name.
47    The first character of all runtime options is AUTOMATICALLY the
48    hyphen.
49 
50    Concepts: prefix^setting
51 
52 */
53 PetscErrorCode  PetscObjectSetOptionsPrefix(PetscObject obj,const char prefix[])
54 {
55   PetscErrorCode ierr;
56 
57   PetscFunctionBegin;
58   PetscValidHeader(obj,1);
59   if (!prefix) {
60     ierr = PetscFree(obj->prefix);CHKERRQ(ierr);
61   } else {
62     if (prefix[0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Options prefix should not begin with a hypen");
63     if (prefix != obj->prefix) {
64       ierr = PetscFree(obj->prefix);CHKERRQ(ierr);
65       ierr = PetscStrallocpy(prefix,&obj->prefix);CHKERRQ(ierr);
66     }
67   }
68   PetscFunctionReturn(0);
69 }
70 
71 #undef __FUNCT__
72 #define __FUNCT__ "PetscObjectAppendOptionsPrefix"
73 /*
74    PetscObjectAppendOptionsPrefix - Sets the prefix used for searching for all
75    options of PetscObjectType in the database.
76 
77    Input Parameters:
78 .  obj - any PETSc object, for example a Vec, Mat or KSP.
79 .  prefix - the prefix string to prepend to option requests of the object.
80 
81    Notes:
82    A hyphen (-) must NOT be given at the beginning of the prefix name.
83    The first character of all runtime options is AUTOMATICALLY the
84    hyphen.
85 
86    Concepts: prefix^setting
87 
88 */
89 PetscErrorCode  PetscObjectAppendOptionsPrefix(PetscObject obj,const char prefix[])
90 {
91   char           *buf = obj->prefix;
92   PetscErrorCode ierr;
93   size_t         len1,len2;
94 
95   PetscFunctionBegin;
96   PetscValidHeader(obj,1);
97   if (!prefix) PetscFunctionReturn(0);
98   if (!buf) {
99     ierr = PetscObjectSetOptionsPrefix(obj,prefix);CHKERRQ(ierr);
100     PetscFunctionReturn(0);
101   }
102   if (prefix[0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Options prefix should not begin with a hypen");
103 
104   ierr = PetscStrlen(prefix,&len1);CHKERRQ(ierr);
105   ierr = PetscStrlen(buf,&len2);CHKERRQ(ierr);
106   ierr = PetscMalloc1(1+len1+len2,&obj->prefix);CHKERRQ(ierr);
107   ierr = PetscStrcpy(obj->prefix,buf);CHKERRQ(ierr);
108   ierr = PetscStrcat(obj->prefix,prefix);CHKERRQ(ierr);
109   ierr = PetscFree(buf);CHKERRQ(ierr);
110   PetscFunctionReturn(0);
111 }
112 
113 #undef __FUNCT__
114 #define __FUNCT__ "PetscObjectGetOptionsPrefix"
115 /*
116    PetscObjectGetOptionsPrefix - Gets the prefix of the PetscObject.
117 
118    Input Parameters:
119 .  obj - any PETSc object, for example a Vec, Mat or KSP.
120 
121    Output Parameters:
122 .  prefix - pointer to the prefix string used is returned
123 
124    Concepts: prefix^getting
125 
126 */
127 PetscErrorCode  PetscObjectGetOptionsPrefix(PetscObject obj,const char *prefix[])
128 {
129   PetscFunctionBegin;
130   PetscValidHeader(obj,1);
131   PetscValidPointer(prefix,2);
132   *prefix = obj->prefix;
133   PetscFunctionReturn(0);
134 }
135 
136 #undef __FUNCT__
137 #define __FUNCT__ "PetscObjectPrependOptionsPrefix"
138 /*
139    PetscObjectPrependOptionsPrefix - Sets the prefix used for searching for all
140    options of PetscObjectType in the database.
141 
142    Input Parameters:
143 .  obj - any PETSc object, for example a Vec, Mat or KSP.
144 .  prefix - the prefix string to prepend to option requests of the object.
145 
146    Notes:
147    A hyphen (-) must NOT be given at the beginning of the prefix name.
148    The first character of all runtime options is AUTOMATICALLY the
149    hyphen.
150 
151    Concepts: prefix^setting
152 
153 */
154 PetscErrorCode  PetscObjectPrependOptionsPrefix(PetscObject obj,const char prefix[])
155 {
156   char           *buf;
157   size_t         len1,len2;
158   PetscErrorCode ierr;
159 
160   PetscFunctionBegin;
161   PetscValidHeader(obj,1);
162   buf = obj->prefix;
163   if (!prefix) PetscFunctionReturn(0);
164   if (!buf) {
165     ierr = PetscObjectSetOptionsPrefix(obj,prefix);CHKERRQ(ierr);
166     PetscFunctionReturn(0);
167   }
168   if (prefix[0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Options prefix should not begin with a hypen");
169 
170   ierr = PetscStrlen(prefix,&len1);CHKERRQ(ierr);
171   ierr = PetscStrlen(buf,&len2);CHKERRQ(ierr);
172   ierr = PetscMalloc1(1+len1+len2,&obj->prefix);CHKERRQ(ierr);
173   ierr = PetscStrcpy(obj->prefix,prefix);CHKERRQ(ierr);
174   ierr = PetscStrcat(obj->prefix,buf);CHKERRQ(ierr);
175   ierr = PetscFree(buf);CHKERRQ(ierr);
176   PetscFunctionReturn(0);
177 }
178 
179