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