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