xref: /petsc/src/sys/classes/viewer/impls/string/stringv.c (revision 4dfa11a44d5adf2389f1d3acbc8f3c1116dc6c3a)
15c6c1daeSBarry Smith 
2af0996ceSBarry Smith #include <petsc/private/viewerimpl.h> /*I  "petscsys.h"  I*/
35c6c1daeSBarry Smith 
45c6c1daeSBarry Smith typedef struct {
55c6c1daeSBarry Smith   char     *string; /* string where info is stored */
6a5b23f4aSJose E. Roman   char     *head;   /* pointer to beginning of unused portion */
75c6c1daeSBarry Smith   size_t    curlen, maxlen;
836a9e3b9SBarry Smith   PetscBool ownstring; /* string viewer is responsable for freeing the string */
95c6c1daeSBarry Smith } PetscViewer_String;
105c6c1daeSBarry Smith 
119371c9d4SSatish Balay static PetscErrorCode PetscViewerDestroy_String(PetscViewer viewer) {
125c6c1daeSBarry Smith   PetscViewer_String *vstr = (PetscViewer_String *)viewer->data;
135c6c1daeSBarry Smith 
145c6c1daeSBarry Smith   PetscFunctionBegin;
151baa6e33SBarry Smith   if (vstr->ownstring) PetscCall(PetscFree(vstr->string));
169566063dSJacob Faibussowitsch   PetscCall(PetscFree(vstr));
175c6c1daeSBarry Smith   PetscFunctionReturn(0);
185c6c1daeSBarry Smith }
195c6c1daeSBarry Smith 
205c6c1daeSBarry Smith /*@C
21811af0c4SBarry Smith     PetscViewerStringSPrintf - Prints information to a `PETSCVIEWERSTRING` `PetscViewer` object
225c6c1daeSBarry Smith 
23811af0c4SBarry Smith     Logically Collective on viewer
245c6c1daeSBarry Smith 
255c6c1daeSBarry Smith     Input Parameters:
26811af0c4SBarry Smith +   v - a string `PetscViewer`, formed by `PetscViewerStringOpen()`
275c6c1daeSBarry Smith -   format - the format of the input
285c6c1daeSBarry Smith 
295c6c1daeSBarry Smith     Level: developer
305c6c1daeSBarry Smith 
31811af0c4SBarry Smith     Note:
32811af0c4SBarry Smith     Though this is collective each MPI rank maintains a separate string
33811af0c4SBarry Smith 
345c6c1daeSBarry Smith     Fortran Note:
355c6c1daeSBarry Smith     This routine is not supported in Fortran.
365c6c1daeSBarry Smith 
37811af0c4SBarry Smith .seealso: `PETSCVIEWERSTRING`, `PetscViewerStringOpen()`, `PetscViewerStringGetStringRead()`, `PetscViewerStringSetString()`
385c6c1daeSBarry Smith @*/
399371c9d4SSatish Balay PetscErrorCode PetscViewerStringSPrintf(PetscViewer viewer, const char format[], ...) {
405c6c1daeSBarry Smith   va_list             Argp;
415c6c1daeSBarry Smith   size_t              fullLength;
4289d949e2SBarry Smith   size_t              shift, cshift;
435c6c1daeSBarry Smith   PetscBool           isstring;
445c6c1daeSBarry Smith   char                tmp[4096];
455c6c1daeSBarry Smith   PetscViewer_String *vstr = (PetscViewer_String *)viewer->data;
465c6c1daeSBarry Smith 
475c6c1daeSBarry Smith   PetscFunctionBegin;
485c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
495c6c1daeSBarry Smith   PetscValidCharPointer(format, 2);
509566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSTRING, &isstring));
515c6c1daeSBarry Smith   if (!isstring) PetscFunctionReturn(0);
5228b400f6SJacob Faibussowitsch   PetscCheck(vstr->string, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Must call PetscViewerStringSetString() before using");
535c6c1daeSBarry Smith 
545c6c1daeSBarry Smith   va_start(Argp, format);
559566063dSJacob Faibussowitsch   PetscCall(PetscVSNPrintf(tmp, 4096, format, &fullLength, Argp));
565c6c1daeSBarry Smith   va_end(Argp);
579566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(tmp, &shift));
5889d949e2SBarry Smith   cshift = shift + 1;
5989d949e2SBarry Smith   if (cshift >= vstr->maxlen - vstr->curlen - 1) cshift = vstr->maxlen - vstr->curlen - 1;
609566063dSJacob Faibussowitsch   PetscCall(PetscStrncpy(vstr->head, tmp, cshift));
615c6c1daeSBarry Smith   vstr->head += shift;
625c6c1daeSBarry Smith   vstr->curlen += shift;
635c6c1daeSBarry Smith   PetscFunctionReturn(0);
645c6c1daeSBarry Smith }
655c6c1daeSBarry Smith 
665c6c1daeSBarry Smith /*@C
67811af0c4SBarry Smith     PetscViewerStringOpen - Opens a string as a `PETSCVIEWERSTRING` `PetscViewer`. This is a very
68811af0c4SBarry Smith     simple `PetscViewer`; information on the object is simply stored into
695c6c1daeSBarry Smith     the string in a fairly nice way.
705c6c1daeSBarry Smith 
71d083f849SBarry Smith     Collective
725c6c1daeSBarry Smith 
735c6c1daeSBarry Smith     Input Parameters:
745c6c1daeSBarry Smith +   comm - the communicator
755c6c1daeSBarry Smith .   string - the string to use
765c6c1daeSBarry Smith -   len    - the string length
775c6c1daeSBarry Smith 
785c6c1daeSBarry Smith     Output Parameter:
79811af0c4SBarry Smith .   lab - the `PetscViewer`
805c6c1daeSBarry Smith 
815c6c1daeSBarry Smith     Level: advanced
825c6c1daeSBarry Smith 
835c6c1daeSBarry Smith     Fortran Note:
845c6c1daeSBarry Smith     This routine is not supported in Fortran.
855c6c1daeSBarry Smith 
86811af0c4SBarry Smith .seealso: `PETSCVIEWERSTRING`, `PetscViewerDestroy()`, `PetscViewerStringSPrintf()`, `PetscViewerStringGetStringRead()`, `PetscViewerStringSetString()`
875c6c1daeSBarry Smith @*/
889371c9d4SSatish Balay PetscErrorCode PetscViewerStringOpen(MPI_Comm comm, char string[], size_t len, PetscViewer *lab) {
895c6c1daeSBarry Smith   PetscFunctionBegin;
909566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, lab));
919566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(*lab, PETSCVIEWERSTRING));
929566063dSJacob Faibussowitsch   PetscCall(PetscViewerStringSetString(*lab, string, len));
935c6c1daeSBarry Smith   PetscFunctionReturn(0);
945c6c1daeSBarry Smith }
955c6c1daeSBarry Smith 
969371c9d4SSatish Balay PetscErrorCode PetscViewerGetSubViewer_String(PetscViewer viewer, MPI_Comm comm, PetscViewer *sviewer) {
975c6c1daeSBarry Smith   PetscViewer_String *vstr = (PetscViewer_String *)viewer->data;
985c6c1daeSBarry Smith 
995c6c1daeSBarry Smith   PetscFunctionBegin;
1009566063dSJacob Faibussowitsch   PetscCall(PetscViewerStringOpen(PETSC_COMM_SELF, vstr->head, vstr->maxlen - vstr->curlen, sviewer));
1015c6c1daeSBarry Smith   PetscFunctionReturn(0);
1025c6c1daeSBarry Smith }
1035c6c1daeSBarry Smith 
1049371c9d4SSatish Balay PetscErrorCode PetscViewerRestoreSubViewer_String(PetscViewer viewer, MPI_Comm comm, PetscViewer *sviewer) {
1055c6c1daeSBarry Smith   PetscViewer_String *iviewer = (PetscViewer_String *)(*sviewer)->data;
1065c6c1daeSBarry Smith   PetscViewer_String *vstr    = (PetscViewer_String *)viewer->data;
1075c6c1daeSBarry Smith 
1085c6c1daeSBarry Smith   PetscFunctionBegin;
1095c6c1daeSBarry Smith   vstr->head = iviewer->head;
1105c6c1daeSBarry Smith   vstr->curlen += iviewer->curlen;
1119566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(sviewer));
1125c6c1daeSBarry Smith   PetscFunctionReturn(0);
1135c6c1daeSBarry Smith }
1145c6c1daeSBarry Smith 
1158556b5ebSBarry Smith /*MC
1168556b5ebSBarry Smith    PETSCVIEWERSTRING - A viewer that writes to a string
1178556b5ebSBarry Smith 
118811af0c4SBarry Smith   Level: beginner
119811af0c4SBarry Smith 
120db781477SPatrick Sanan .seealso: `PetscViewerStringOpen()`, `PetscViewerStringSPrintf()`, `PetscViewerSocketOpen()`, `PetscViewerDrawOpen()`, `PETSCVIEWERSOCKET`,
121db781477SPatrick Sanan           `PetscViewerCreate()`, `PetscViewerASCIIOpen()`, `PetscViewerBinaryOpen()`, `PETSCVIEWERBINARY`, `PETSCVIEWERDRAW`,
122db781477SPatrick Sanan           `PetscViewerMatlabOpen()`, `VecView()`, `DMView()`, `PetscViewerMatlabPutArray()`, `PETSCVIEWERASCII`, `PETSCVIEWERMATLAB`,
123db781477SPatrick Sanan           `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`, `PetscViewerFormat`, `PetscViewerType`, `PetscViewerSetType()`
1248556b5ebSBarry Smith M*/
1258556b5ebSBarry Smith 
1269371c9d4SSatish Balay PETSC_EXTERN PetscErrorCode PetscViewerCreate_String(PetscViewer v) {
1275c6c1daeSBarry Smith   PetscViewer_String *vstr;
1285c6c1daeSBarry Smith 
1295c6c1daeSBarry Smith   PetscFunctionBegin;
1305c6c1daeSBarry Smith   v->ops->destroy          = PetscViewerDestroy_String;
13102c9f0b5SLisandro Dalcin   v->ops->view             = NULL;
13202c9f0b5SLisandro Dalcin   v->ops->flush            = NULL;
133559f443fSBarry Smith   v->ops->getsubviewer     = PetscViewerGetSubViewer_String;
134559f443fSBarry Smith   v->ops->restoresubviewer = PetscViewerRestoreSubViewer_String;
135*4dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&vstr));
1365c6c1daeSBarry Smith   v->data      = (void *)vstr;
13702c9f0b5SLisandro Dalcin   vstr->string = NULL;
1385c6c1daeSBarry Smith   PetscFunctionReturn(0);
1395c6c1daeSBarry Smith }
1405c6c1daeSBarry Smith 
1415c6c1daeSBarry Smith /*@C
1425c6c1daeSBarry Smith 
143811af0c4SBarry Smith    PetscViewerStringGetStringRead - Returns the string that a `PETSCVIEWERSTRING` uses
14436a9e3b9SBarry Smith 
145811af0c4SBarry Smith    Logically Collective on viewer
14636a9e3b9SBarry Smith 
14736a9e3b9SBarry Smith   Input Parameter:
148811af0c4SBarry Smith .   viewer -  `PETSCVIEWERSTRING` viewer
14936a9e3b9SBarry Smith 
150fd292e60Sprj-   Output Parameters:
15136a9e3b9SBarry Smith +    string - the string, optional use NULL if you do not need
15236a9e3b9SBarry Smith -   len - the length of the string, optional use NULL if you do
15336a9e3b9SBarry Smith 
154811af0c4SBarry Smith   Note:
155811af0c4SBarry Smith   Do not write to the string nor free it
15636a9e3b9SBarry Smith 
15736a9e3b9SBarry Smith   Level: advanced
15836a9e3b9SBarry Smith 
159db781477SPatrick Sanan .seealso: `PetscViewerStringOpen()`, `PETSCVIEWERSTRING`, `PetscViewerStringSetString()`, `PetscViewerStringSPrintf()`,
160db781477SPatrick Sanan           `PetscViewerStringSetOwnString()`
16136a9e3b9SBarry Smith @*/
1629371c9d4SSatish Balay PetscErrorCode PetscViewerStringGetStringRead(PetscViewer viewer, const char *string[], size_t *len) {
16336a9e3b9SBarry Smith   PetscViewer_String *vstr = (PetscViewer_String *)viewer->data;
16436a9e3b9SBarry Smith   PetscBool           isstring;
16536a9e3b9SBarry Smith 
16636a9e3b9SBarry Smith   PetscFunctionBegin;
16736a9e3b9SBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
1689566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSTRING, &isstring));
16928b400f6SJacob Faibussowitsch   PetscCheck(isstring, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Only for PETSCVIEWERSTRING");
17036a9e3b9SBarry Smith   if (string) *string = vstr->string;
17136a9e3b9SBarry Smith   if (len) *len = vstr->maxlen;
17236a9e3b9SBarry Smith   PetscFunctionReturn(0);
17336a9e3b9SBarry Smith }
17436a9e3b9SBarry Smith 
17536a9e3b9SBarry Smith /*@C
17636a9e3b9SBarry Smith 
1775c6c1daeSBarry Smith    PetscViewerStringSetString - sets the string that a string viewer will print to
1785c6c1daeSBarry Smith 
179811af0c4SBarry Smith    Logically Collective on viewer
1805c6c1daeSBarry Smith 
1815c6c1daeSBarry Smith   Input Parameters:
1825c6c1daeSBarry Smith +   viewer - string viewer you wish to attach string to
1835c6c1daeSBarry Smith .   string - the string to print data into
1845c6c1daeSBarry Smith -   len - the length of the string
1855c6c1daeSBarry Smith 
186811af0c4SBarry Smith   Note: The function does not copy the string, it uses it directly therefore you cannot free
187811af0c4SBarry Smith    the string until the viewer is destroyed. If you call `PetscViewerStringSetOwnString()` the ownership
18836a9e3b9SBarry Smith    passes to the viewer and it will be responsable for freeing it. In this case the string must be
189811af0c4SBarry Smith    obtained with `PetscMalloc()`.
19036a9e3b9SBarry Smith 
1915c6c1daeSBarry Smith   Level: advanced
1925c6c1daeSBarry Smith 
193db781477SPatrick Sanan .seealso: `PetscViewerStringOpen()`, `PETSCVIEWERSTRING`, `PetscViewerStringGetStringRead()`, `PetscViewerStringSPrintf()`,
194db781477SPatrick Sanan           `PetscViewerStringSetOwnString()`
1955c6c1daeSBarry Smith @*/
1969371c9d4SSatish Balay PetscErrorCode PetscViewerStringSetString(PetscViewer viewer, char string[], size_t len) {
1975c6c1daeSBarry Smith   PetscViewer_String *vstr = (PetscViewer_String *)viewer->data;
1985c6c1daeSBarry Smith   PetscBool           isstring;
1995c6c1daeSBarry Smith 
2005c6c1daeSBarry Smith   PetscFunctionBegin;
2015c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
2025c6c1daeSBarry Smith   PetscValidCharPointer(string, 2);
2039566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSTRING, &isstring));
2045c6c1daeSBarry Smith   if (!isstring) PetscFunctionReturn(0);
20508401ef6SPierre Jolivet   PetscCheck(len > 2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "String must have length at least 2");
2065c6c1daeSBarry Smith 
2079566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(string, len));
2085c6c1daeSBarry Smith   vstr->string = string;
2095c6c1daeSBarry Smith   vstr->head   = string;
2105c6c1daeSBarry Smith   vstr->curlen = 0;
2115c6c1daeSBarry Smith   vstr->maxlen = len;
2125c6c1daeSBarry Smith   PetscFunctionReturn(0);
2135c6c1daeSBarry Smith }
2145c6c1daeSBarry Smith 
21536a9e3b9SBarry Smith /*@C
21636a9e3b9SBarry Smith 
21736a9e3b9SBarry Smith    PetscViewerStringSetOwnString - tells the viewer that it now owns the string and is responsible for freeing it
21836a9e3b9SBarry Smith 
219811af0c4SBarry Smith    Logically Collective on viewer
22036a9e3b9SBarry Smith 
22136a9e3b9SBarry Smith   Input Parameters:
22236a9e3b9SBarry Smith .   viewer - string viewer
22336a9e3b9SBarry Smith 
224811af0c4SBarry Smith   Note:
225811af0c4SBarry Smith   If you call this the string must have been obtained with `PetscMalloc()` and you cannot free the string
22636a9e3b9SBarry Smith 
22736a9e3b9SBarry Smith   Level: advanced
22836a9e3b9SBarry Smith 
229db781477SPatrick Sanan .seealso: `PetscViewerStringOpen()`, `PETSCVIEWERSTRING`, `PetscViewerStringGetStringRead()`, `PetscViewerStringSPrintf()`,
230db781477SPatrick Sanan           `PetscViewerStringSetString()`
23136a9e3b9SBarry Smith @*/
2329371c9d4SSatish Balay PetscErrorCode PetscViewerStringSetOwnString(PetscViewer viewer) {
23336a9e3b9SBarry Smith   PetscViewer_String *vstr = (PetscViewer_String *)viewer->data;
23436a9e3b9SBarry Smith   PetscBool           isstring;
23536a9e3b9SBarry Smith 
23636a9e3b9SBarry Smith   PetscFunctionBegin;
23736a9e3b9SBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
2389566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSTRING, &isstring));
23936a9e3b9SBarry Smith   if (!isstring) PetscFunctionReturn(0);
24036a9e3b9SBarry Smith 
24136a9e3b9SBarry Smith   vstr->ownstring = PETSC_TRUE;
24236a9e3b9SBarry Smith   PetscFunctionReturn(0);
24336a9e3b9SBarry Smith }
244