xref: /petsc/src/sys/classes/viewer/impls/string/stringv.c (revision 2c71b3e237ead271e4f3aa1505f92bf476e3413d)
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 
115c6c1daeSBarry Smith static PetscErrorCode PetscViewerDestroy_String(PetscViewer viewer)
125c6c1daeSBarry Smith {
135c6c1daeSBarry Smith   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
145c6c1daeSBarry Smith   PetscErrorCode     ierr;
155c6c1daeSBarry Smith 
165c6c1daeSBarry Smith   PetscFunctionBegin;
1736a9e3b9SBarry Smith   if (vstr->ownstring) {
1836a9e3b9SBarry Smith     ierr = PetscFree(vstr->string);CHKERRQ(ierr);
1936a9e3b9SBarry Smith   }
205c6c1daeSBarry Smith   ierr = PetscFree(vstr);CHKERRQ(ierr);
215c6c1daeSBarry Smith   PetscFunctionReturn(0);
225c6c1daeSBarry Smith }
235c6c1daeSBarry Smith 
245c6c1daeSBarry Smith /*@C
255c6c1daeSBarry Smith     PetscViewerStringSPrintf - Prints information to a PetscViewer string.
265c6c1daeSBarry Smith 
275c6c1daeSBarry Smith     Logically Collective on PetscViewer (Hmmm, each processor maintains a separate string)
285c6c1daeSBarry Smith 
295c6c1daeSBarry Smith     Input Parameters:
305c6c1daeSBarry Smith +   v - a string PetscViewer, formed by PetscViewerStringOpen()
315c6c1daeSBarry Smith -   format - the format of the input
325c6c1daeSBarry Smith 
335c6c1daeSBarry Smith     Level: developer
345c6c1daeSBarry Smith 
355c6c1daeSBarry Smith     Fortran Note:
365c6c1daeSBarry Smith     This routine is not supported in Fortran.
375c6c1daeSBarry Smith 
3836a9e3b9SBarry Smith .seealso: PetscViewerStringOpen(), PetscViewerStringGetStringRead(), PetscViewerStringSetString(), PETSCVIEWERSTRING
395c6c1daeSBarry Smith @*/
405c6c1daeSBarry Smith PetscErrorCode  PetscViewerStringSPrintf(PetscViewer viewer,const char format[],...)
415c6c1daeSBarry Smith {
425c6c1daeSBarry Smith   va_list            Argp;
435c6c1daeSBarry Smith   size_t             fullLength;
4489d949e2SBarry Smith   size_t             shift,cshift;
455c6c1daeSBarry Smith   PetscErrorCode     ierr;
465c6c1daeSBarry Smith   PetscBool          isstring;
475c6c1daeSBarry Smith   char               tmp[4096];
485c6c1daeSBarry Smith   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
495c6c1daeSBarry Smith 
505c6c1daeSBarry Smith   PetscFunctionBegin;
515c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
525c6c1daeSBarry Smith   PetscValidCharPointer(format,2);
535c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
545c6c1daeSBarry Smith   if (!isstring) PetscFunctionReturn(0);
55*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!vstr->string,PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerStringSetString() before using");
565c6c1daeSBarry Smith 
575c6c1daeSBarry Smith   va_start(Argp,format);
585c6c1daeSBarry Smith   ierr = PetscVSNPrintf(tmp,4096,format,&fullLength,Argp);CHKERRQ(ierr);
595c6c1daeSBarry Smith   va_end(Argp);
605c6c1daeSBarry Smith   ierr = PetscStrlen(tmp,&shift);CHKERRQ(ierr);
6189d949e2SBarry Smith   cshift = shift+1;
6289d949e2SBarry Smith   if (cshift >= vstr->maxlen - vstr->curlen - 1) cshift = vstr->maxlen - vstr->curlen - 1;
6389d949e2SBarry Smith   ierr = PetscStrncpy(vstr->head,tmp,cshift);CHKERRQ(ierr);
645c6c1daeSBarry Smith   vstr->head   += shift;
655c6c1daeSBarry Smith   vstr->curlen += shift;
665c6c1daeSBarry Smith   PetscFunctionReturn(0);
675c6c1daeSBarry Smith }
685c6c1daeSBarry Smith 
695c6c1daeSBarry Smith /*@C
705c6c1daeSBarry Smith     PetscViewerStringOpen - Opens a string as a PetscViewer. This is a very
715c6c1daeSBarry Smith     simple PetscViewer; information on the object is simply stored into
725c6c1daeSBarry Smith     the string in a fairly nice way.
735c6c1daeSBarry Smith 
74d083f849SBarry Smith     Collective
755c6c1daeSBarry Smith 
765c6c1daeSBarry Smith     Input Parameters:
775c6c1daeSBarry Smith +   comm - the communicator
785c6c1daeSBarry Smith .   string - the string to use
795c6c1daeSBarry Smith -   len    - the string length
805c6c1daeSBarry Smith 
815c6c1daeSBarry Smith     Output Parameter:
825c6c1daeSBarry Smith .   lab - the PetscViewer
835c6c1daeSBarry Smith 
845c6c1daeSBarry Smith     Level: advanced
855c6c1daeSBarry Smith 
865c6c1daeSBarry Smith     Fortran Note:
875c6c1daeSBarry Smith     This routine is not supported in Fortran.
885c6c1daeSBarry Smith 
8936a9e3b9SBarry Smith .seealso: PetscViewerDestroy(), PetscViewerStringSPrintf(), PetscViewerStringGetStringRead(), PetscViewerStringSetString(), PETSCVIEWERSTRING
905c6c1daeSBarry Smith @*/
9189d949e2SBarry Smith PetscErrorCode  PetscViewerStringOpen(MPI_Comm comm,char string[],size_t len,PetscViewer *lab)
925c6c1daeSBarry Smith {
935c6c1daeSBarry Smith   PetscErrorCode ierr;
945c6c1daeSBarry Smith 
955c6c1daeSBarry Smith   PetscFunctionBegin;
965c6c1daeSBarry Smith   ierr = PetscViewerCreate(comm,lab);CHKERRQ(ierr);
975c6c1daeSBarry Smith   ierr = PetscViewerSetType(*lab,PETSCVIEWERSTRING);CHKERRQ(ierr);
985c6c1daeSBarry Smith   ierr = PetscViewerStringSetString(*lab,string,len);CHKERRQ(ierr);
995c6c1daeSBarry Smith   PetscFunctionReturn(0);
1005c6c1daeSBarry Smith }
1015c6c1daeSBarry Smith 
1023f08860eSBarry Smith PetscErrorCode PetscViewerGetSubViewer_String(PetscViewer viewer,MPI_Comm comm,PetscViewer *sviewer)
1035c6c1daeSBarry Smith {
1045c6c1daeSBarry Smith   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
1055c6c1daeSBarry Smith   PetscErrorCode     ierr;
1065c6c1daeSBarry Smith 
1075c6c1daeSBarry Smith   PetscFunctionBegin;
1085c6c1daeSBarry Smith   ierr = PetscViewerStringOpen(PETSC_COMM_SELF,vstr->head,vstr->maxlen-vstr->curlen,sviewer);CHKERRQ(ierr);
1095c6c1daeSBarry Smith   PetscFunctionReturn(0);
1105c6c1daeSBarry Smith }
1115c6c1daeSBarry Smith 
1123f08860eSBarry Smith PetscErrorCode PetscViewerRestoreSubViewer_String(PetscViewer viewer,MPI_Comm comm,PetscViewer *sviewer)
1135c6c1daeSBarry Smith {
1145c6c1daeSBarry Smith   PetscErrorCode     ierr;
1155c6c1daeSBarry Smith   PetscViewer_String *iviewer = (PetscViewer_String*)(*sviewer)->data;
1165c6c1daeSBarry Smith   PetscViewer_String *vstr    = (PetscViewer_String*)viewer->data;
1175c6c1daeSBarry Smith 
1185c6c1daeSBarry Smith   PetscFunctionBegin;
1195c6c1daeSBarry Smith   vstr->head    = iviewer->head;
1205c6c1daeSBarry Smith   vstr->curlen += iviewer->curlen;
1215c6c1daeSBarry Smith   ierr          = PetscViewerDestroy(sviewer);CHKERRQ(ierr);
1225c6c1daeSBarry Smith   PetscFunctionReturn(0);
1235c6c1daeSBarry Smith }
1245c6c1daeSBarry Smith 
1258556b5ebSBarry Smith /*MC
1268556b5ebSBarry Smith    PETSCVIEWERSTRING - A viewer that writes to a string
1278556b5ebSBarry Smith 
1288556b5ebSBarry Smith .seealso:  PetscViewerStringOpen(), PetscViewerStringSPrintf(), PetscViewerSocketOpen(), PetscViewerDrawOpen(), PETSCVIEWERSOCKET,
1298556b5ebSBarry Smith            PetscViewerCreate(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PETSCVIEWERBINARY, PETSCVIEWERDRAW,
1308556b5ebSBarry Smith            PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERASCII, PETSCVIEWERMATLAB,
1318556b5ebSBarry Smith            PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType()
1328556b5ebSBarry Smith 
1331b266c99SBarry Smith   Level: beginner
1348556b5ebSBarry Smith M*/
1358556b5ebSBarry Smith 
1368cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_String(PetscViewer v)
1375c6c1daeSBarry Smith {
1385c6c1daeSBarry Smith   PetscViewer_String *vstr;
1395c6c1daeSBarry Smith   PetscErrorCode     ierr;
1405c6c1daeSBarry Smith 
1415c6c1daeSBarry Smith   PetscFunctionBegin;
1425c6c1daeSBarry Smith   v->ops->destroy          = PetscViewerDestroy_String;
14302c9f0b5SLisandro Dalcin   v->ops->view             = NULL;
14402c9f0b5SLisandro Dalcin   v->ops->flush            = NULL;
145559f443fSBarry Smith   v->ops->getsubviewer     = PetscViewerGetSubViewer_String;
146559f443fSBarry Smith   v->ops->restoresubviewer = PetscViewerRestoreSubViewer_String;
147b00a9115SJed Brown   ierr                     = PetscNewLog(v,&vstr);CHKERRQ(ierr);
1485c6c1daeSBarry Smith   v->data                  = (void*)vstr;
14902c9f0b5SLisandro Dalcin   vstr->string             = NULL;
1505c6c1daeSBarry Smith   PetscFunctionReturn(0);
1515c6c1daeSBarry Smith }
1525c6c1daeSBarry Smith 
1535c6c1daeSBarry Smith /*@C
1545c6c1daeSBarry Smith 
15536a9e3b9SBarry Smith    PetscViewerStringGetStringRead - Returns the string that a string viewer uses
15636a9e3b9SBarry Smith 
15736a9e3b9SBarry Smith    Logically Collective on PetscViewer
15836a9e3b9SBarry Smith 
15936a9e3b9SBarry Smith   Input Parameter:
16036a9e3b9SBarry Smith .   viewer - string viewer
16136a9e3b9SBarry Smith 
162fd292e60Sprj-   Output Parameters:
16336a9e3b9SBarry Smith +    string - the string, optional use NULL if you do not need
16436a9e3b9SBarry Smith -   len - the length of the string, optional use NULL if you do
16536a9e3b9SBarry Smith 
16636a9e3b9SBarry Smith   Notes: Do not write to the string nor free it
16736a9e3b9SBarry Smith 
16836a9e3b9SBarry Smith   Level: advanced
16936a9e3b9SBarry Smith 
17036a9e3b9SBarry Smith .seealso: PetscViewerStringOpen(), PETSCVIEWERSTRING, PetscViewerStringSetString(), PetscViewerStringSPrintf(),
17136a9e3b9SBarry Smith           PetscViewerStringSetOwnString()
17236a9e3b9SBarry Smith @*/
17336a9e3b9SBarry Smith PetscErrorCode  PetscViewerStringGetStringRead(PetscViewer viewer,const char *string[],size_t *len)
17436a9e3b9SBarry Smith {
17536a9e3b9SBarry Smith   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
17636a9e3b9SBarry Smith   PetscErrorCode     ierr;
17736a9e3b9SBarry Smith   PetscBool          isstring;
17836a9e3b9SBarry Smith 
17936a9e3b9SBarry Smith   PetscFunctionBegin;
18036a9e3b9SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
18136a9e3b9SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
182*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!isstring,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Only for PETSCVIEWERSTRING");
18336a9e3b9SBarry Smith   if (string) *string = vstr->string;
18436a9e3b9SBarry Smith   if (len)    *len    = vstr->maxlen;
18536a9e3b9SBarry Smith   PetscFunctionReturn(0);
18636a9e3b9SBarry Smith }
18736a9e3b9SBarry Smith 
18836a9e3b9SBarry Smith /*@C
18936a9e3b9SBarry Smith 
1905c6c1daeSBarry Smith    PetscViewerStringSetString - sets the string that a string viewer will print to
1915c6c1daeSBarry Smith 
1925c6c1daeSBarry Smith    Logically Collective on PetscViewer
1935c6c1daeSBarry Smith 
1945c6c1daeSBarry Smith   Input Parameters:
1955c6c1daeSBarry Smith +   viewer - string viewer you wish to attach string to
1965c6c1daeSBarry Smith .   string - the string to print data into
1975c6c1daeSBarry Smith -   len - the length of the string
1985c6c1daeSBarry Smith 
199effebcafSBarry Smith   Notes: The function does not copy the string, it uses it directly therefore you cannot free
20036a9e3b9SBarry Smith    the string until the viewer is destroyed. If you call PetscViewerStringSetOwnString() the ownership
20136a9e3b9SBarry Smith    passes to the viewer and it will be responsable for freeing it. In this case the string must be
20236a9e3b9SBarry Smith    obtained with PetscMalloc().
20336a9e3b9SBarry Smith 
2045c6c1daeSBarry Smith   Level: advanced
2055c6c1daeSBarry Smith 
20636a9e3b9SBarry Smith .seealso: PetscViewerStringOpen(), PETSCVIEWERSTRING, PetscViewerStringGetStringRead(), PetscViewerStringSPrintf(),
20736a9e3b9SBarry Smith           PetscViewerStringSetOwnString()
2085c6c1daeSBarry Smith @*/
20936a9e3b9SBarry Smith PetscErrorCode  PetscViewerStringSetString(PetscViewer viewer,char string[],size_t len)
2105c6c1daeSBarry Smith {
2115c6c1daeSBarry Smith   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
2125c6c1daeSBarry Smith   PetscErrorCode     ierr;
2135c6c1daeSBarry Smith   PetscBool          isstring;
2145c6c1daeSBarry Smith 
2155c6c1daeSBarry Smith   PetscFunctionBegin;
2165c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2175c6c1daeSBarry Smith   PetscValidCharPointer(string,2);
2185c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
2195c6c1daeSBarry Smith   if (!isstring) PetscFunctionReturn(0);
220*2c71b3e2SJacob Faibussowitsch   PetscCheckFalse(len <= 2,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"String must have length at least 2");
2215c6c1daeSBarry Smith 
222580bdb30SBarry Smith   ierr         = PetscArrayzero(string,len);CHKERRQ(ierr);
2235c6c1daeSBarry Smith   vstr->string = string;
2245c6c1daeSBarry Smith   vstr->head   = string;
2255c6c1daeSBarry Smith   vstr->curlen = 0;
2265c6c1daeSBarry Smith   vstr->maxlen = len;
2275c6c1daeSBarry Smith   PetscFunctionReturn(0);
2285c6c1daeSBarry Smith }
2295c6c1daeSBarry Smith 
23036a9e3b9SBarry Smith /*@C
23136a9e3b9SBarry Smith 
23236a9e3b9SBarry Smith    PetscViewerStringSetOwnString - tells the viewer that it now owns the string and is responsible for freeing it
23336a9e3b9SBarry Smith 
23436a9e3b9SBarry Smith    Logically Collective on PetscViewer
23536a9e3b9SBarry Smith 
23636a9e3b9SBarry Smith   Input Parameters:
23736a9e3b9SBarry Smith .   viewer - string viewer
23836a9e3b9SBarry Smith 
23936a9e3b9SBarry Smith   Notes: If you call this the string must have been obtained with PetscMalloc() and you cannot free the string
24036a9e3b9SBarry Smith 
24136a9e3b9SBarry Smith   Level: advanced
24236a9e3b9SBarry Smith 
24336a9e3b9SBarry Smith .seealso: PetscViewerStringOpen(), PETSCVIEWERSTRING, PetscViewerStringGetStringRead(), PetscViewerStringSPrintf(),
24436a9e3b9SBarry Smith           PetscViewerStringSetString()
24536a9e3b9SBarry Smith @*/
24636a9e3b9SBarry Smith PetscErrorCode  PetscViewerStringSetOwnString(PetscViewer viewer)
24736a9e3b9SBarry Smith {
24836a9e3b9SBarry Smith   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
24936a9e3b9SBarry Smith   PetscErrorCode     ierr;
25036a9e3b9SBarry Smith   PetscBool          isstring;
25136a9e3b9SBarry Smith 
25236a9e3b9SBarry Smith   PetscFunctionBegin;
25336a9e3b9SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
25436a9e3b9SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
25536a9e3b9SBarry Smith   if (!isstring) PetscFunctionReturn(0);
25636a9e3b9SBarry Smith 
25736a9e3b9SBarry Smith   vstr->ownstring = PETSC_TRUE;
25836a9e3b9SBarry Smith   PetscFunctionReturn(0);
25936a9e3b9SBarry Smith }
2605c6c1daeSBarry Smith 
261