xref: /petsc/src/sys/objects/prefix.c (revision a06fd7f2936c6d6cb7e186e1f2a14fea2a327697)
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     ierr = PetscFree(obj->prefix);CHKERRQ(ierr);
64     ierr = PetscStrallocpy(prefix,&obj->prefix);CHKERRQ(ierr);
65   }
66   PetscFunctionReturn(0);
67 }
68 
69 #undef __FUNCT__
70 #define __FUNCT__ "PetscObjectAppendOptionsPrefix"
71 /*
72    PetscObjectAppendOptionsPrefix - Sets the prefix used for searching for all
73    options of PetscObjectType in the database.
74 
75    Input Parameters:
76 .  obj - any PETSc object, for example a Vec, Mat or KSP.
77 .  prefix - the prefix string to prepend to option requests of the object.
78 
79    Notes:
80    A hyphen (-) must NOT be given at the beginning of the prefix name.
81    The first character of all runtime options is AUTOMATICALLY the
82    hyphen.
83 
84    Concepts: prefix^setting
85 
86 */
87 PetscErrorCode  PetscObjectAppendOptionsPrefix(PetscObject obj,const char prefix[])
88 {
89   char           *buf = obj->prefix;
90   PetscErrorCode ierr;
91   size_t         len1,len2;
92 
93   PetscFunctionBegin;
94   PetscValidHeader(obj,1);
95   if (!prefix) PetscFunctionReturn(0);
96   if (!buf) {
97     ierr = PetscObjectSetOptionsPrefix(obj,prefix);CHKERRQ(ierr);
98     PetscFunctionReturn(0);
99   }
100   if (prefix[0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Options prefix should not begin with a hypen");
101 
102   ierr = PetscStrlen(prefix,&len1);CHKERRQ(ierr);
103   ierr = PetscStrlen(buf,&len2);CHKERRQ(ierr);
104   ierr = PetscMalloc1(1+len1+len2,&obj->prefix);CHKERRQ(ierr);
105   ierr = PetscStrcpy(obj->prefix,buf);CHKERRQ(ierr);
106   ierr = PetscStrcat(obj->prefix,prefix);CHKERRQ(ierr);
107   ierr = PetscFree(buf);CHKERRQ(ierr);
108   PetscFunctionReturn(0);
109 }
110 
111 #undef __FUNCT__
112 #define __FUNCT__ "PetscObjectGetOptionsPrefix"
113 /*
114    PetscObjectGetOptionsPrefix - Gets the prefix of the PetscObject.
115 
116    Input Parameters:
117 .  obj - any PETSc object, for example a Vec, Mat or KSP.
118 
119    Output Parameters:
120 .  prefix - pointer to the prefix string used is returned
121 
122    Concepts: prefix^getting
123 
124 */
125 PetscErrorCode  PetscObjectGetOptionsPrefix(PetscObject obj,const char *prefix[])
126 {
127   PetscFunctionBegin;
128   PetscValidHeader(obj,1);
129   PetscValidPointer(prefix,2);
130   *prefix = obj->prefix;
131   PetscFunctionReturn(0);
132 }
133 
134 #undef __FUNCT__
135 #define __FUNCT__ "PetscObjectPrependOptionsPrefix"
136 /*
137    PetscObjectPrependOptionsPrefix - Sets the prefix used for searching for all
138    options of PetscObjectType in the database.
139 
140    Input Parameters:
141 .  obj - any PETSc object, for example a Vec, Mat or KSP.
142 .  prefix - the prefix string to prepend to option requests of the object.
143 
144    Notes:
145    A hyphen (-) must NOT be given at the beginning of the prefix name.
146    The first character of all runtime options is AUTOMATICALLY the
147    hyphen.
148 
149    Concepts: prefix^setting
150 
151 */
152 PetscErrorCode  PetscObjectPrependOptionsPrefix(PetscObject obj,const char prefix[])
153 {
154   char           *buf;
155   size_t         len1,len2;
156   PetscErrorCode ierr;
157 
158   PetscFunctionBegin;
159   PetscValidHeader(obj,1);
160   buf = obj->prefix;
161   if (!prefix) PetscFunctionReturn(0);
162   if (!buf) {
163     ierr = PetscObjectSetOptionsPrefix(obj,prefix);CHKERRQ(ierr);
164     PetscFunctionReturn(0);
165   }
166   if (prefix[0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Options prefix should not begin with a hypen");
167 
168   ierr = PetscStrlen(prefix,&len1);CHKERRQ(ierr);
169   ierr = PetscStrlen(buf,&len2);CHKERRQ(ierr);
170   ierr = PetscMalloc1(1+len1+len2,&obj->prefix);CHKERRQ(ierr);
171   ierr = PetscStrcpy(obj->prefix,prefix);CHKERRQ(ierr);
172   ierr = PetscStrcat(obj->prefix,buf);CHKERRQ(ierr);
173   ierr = PetscFree(buf);CHKERRQ(ierr);
174   PetscFunctionReturn(0);
175 }
176 
177