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