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