xref: /petsc/src/sys/classes/viewer/impls/string/stringv.c (revision 5f80ce2ab25dff0f4601e710601cbbcecf323266) !
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 
155c6c1daeSBarry Smith   PetscFunctionBegin;
1636a9e3b9SBarry Smith   if (vstr->ownstring) {
17*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(vstr->string));
1836a9e3b9SBarry Smith   }
19*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(vstr));
205c6c1daeSBarry Smith   PetscFunctionReturn(0);
215c6c1daeSBarry Smith }
225c6c1daeSBarry Smith 
235c6c1daeSBarry Smith /*@C
245c6c1daeSBarry Smith     PetscViewerStringSPrintf - Prints information to a PetscViewer string.
255c6c1daeSBarry Smith 
265c6c1daeSBarry Smith     Logically Collective on PetscViewer (Hmmm, each processor maintains a separate string)
275c6c1daeSBarry Smith 
285c6c1daeSBarry Smith     Input Parameters:
295c6c1daeSBarry Smith +   v - a string PetscViewer, formed by PetscViewerStringOpen()
305c6c1daeSBarry Smith -   format - the format of the input
315c6c1daeSBarry Smith 
325c6c1daeSBarry Smith     Level: developer
335c6c1daeSBarry Smith 
345c6c1daeSBarry Smith     Fortran Note:
355c6c1daeSBarry Smith     This routine is not supported in Fortran.
365c6c1daeSBarry Smith 
3736a9e3b9SBarry Smith .seealso: PetscViewerStringOpen(), PetscViewerStringGetStringRead(), PetscViewerStringSetString(), PETSCVIEWERSTRING
385c6c1daeSBarry Smith @*/
395c6c1daeSBarry Smith PetscErrorCode  PetscViewerStringSPrintf(PetscViewer viewer,const char format[],...)
405c6c1daeSBarry Smith {
415c6c1daeSBarry Smith   va_list            Argp;
425c6c1daeSBarry Smith   size_t             fullLength;
4389d949e2SBarry Smith   size_t             shift,cshift;
445c6c1daeSBarry Smith   PetscBool          isstring;
455c6c1daeSBarry Smith   char               tmp[4096];
465c6c1daeSBarry Smith   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
475c6c1daeSBarry Smith 
485c6c1daeSBarry Smith   PetscFunctionBegin;
495c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
505c6c1daeSBarry Smith   PetscValidCharPointer(format,2);
51*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring));
525c6c1daeSBarry Smith   if (!isstring) PetscFunctionReturn(0);
532c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!vstr->string,PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerStringSetString() before using");
545c6c1daeSBarry Smith 
555c6c1daeSBarry Smith   va_start(Argp,format);
56*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscVSNPrintf(tmp,4096,format,&fullLength,Argp));
575c6c1daeSBarry Smith   va_end(Argp);
58*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscStrlen(tmp,&shift));
5989d949e2SBarry Smith   cshift = shift+1;
6089d949e2SBarry Smith   if (cshift >= vstr->maxlen - vstr->curlen - 1) cshift = vstr->maxlen - vstr->curlen - 1;
61*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscStrncpy(vstr->head,tmp,cshift));
625c6c1daeSBarry Smith   vstr->head   += shift;
635c6c1daeSBarry Smith   vstr->curlen += shift;
645c6c1daeSBarry Smith   PetscFunctionReturn(0);
655c6c1daeSBarry Smith }
665c6c1daeSBarry Smith 
675c6c1daeSBarry Smith /*@C
685c6c1daeSBarry Smith     PetscViewerStringOpen - Opens a string as a PetscViewer. This is a very
695c6c1daeSBarry Smith     simple PetscViewer; information on the object is simply stored into
705c6c1daeSBarry Smith     the string in a fairly nice way.
715c6c1daeSBarry Smith 
72d083f849SBarry Smith     Collective
735c6c1daeSBarry Smith 
745c6c1daeSBarry Smith     Input Parameters:
755c6c1daeSBarry Smith +   comm - the communicator
765c6c1daeSBarry Smith .   string - the string to use
775c6c1daeSBarry Smith -   len    - the string length
785c6c1daeSBarry Smith 
795c6c1daeSBarry Smith     Output Parameter:
805c6c1daeSBarry Smith .   lab - the PetscViewer
815c6c1daeSBarry Smith 
825c6c1daeSBarry Smith     Level: advanced
835c6c1daeSBarry Smith 
845c6c1daeSBarry Smith     Fortran Note:
855c6c1daeSBarry Smith     This routine is not supported in Fortran.
865c6c1daeSBarry Smith 
8736a9e3b9SBarry Smith .seealso: PetscViewerDestroy(), PetscViewerStringSPrintf(), PetscViewerStringGetStringRead(), PetscViewerStringSetString(), PETSCVIEWERSTRING
885c6c1daeSBarry Smith @*/
8989d949e2SBarry Smith PetscErrorCode  PetscViewerStringOpen(MPI_Comm comm,char string[],size_t len,PetscViewer *lab)
905c6c1daeSBarry Smith {
915c6c1daeSBarry Smith   PetscFunctionBegin;
92*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerCreate(comm,lab));
93*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerSetType(*lab,PETSCVIEWERSTRING));
94*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerStringSetString(*lab,string,len));
955c6c1daeSBarry Smith   PetscFunctionReturn(0);
965c6c1daeSBarry Smith }
975c6c1daeSBarry Smith 
983f08860eSBarry Smith PetscErrorCode PetscViewerGetSubViewer_String(PetscViewer viewer,MPI_Comm comm,PetscViewer *sviewer)
995c6c1daeSBarry Smith {
1005c6c1daeSBarry Smith   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
1015c6c1daeSBarry Smith 
1025c6c1daeSBarry Smith   PetscFunctionBegin;
103*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerStringOpen(PETSC_COMM_SELF,vstr->head,vstr->maxlen-vstr->curlen,sviewer));
1045c6c1daeSBarry Smith   PetscFunctionReturn(0);
1055c6c1daeSBarry Smith }
1065c6c1daeSBarry Smith 
1073f08860eSBarry Smith PetscErrorCode PetscViewerRestoreSubViewer_String(PetscViewer viewer,MPI_Comm comm,PetscViewer *sviewer)
1085c6c1daeSBarry Smith {
1095c6c1daeSBarry Smith   PetscViewer_String *iviewer = (PetscViewer_String*)(*sviewer)->data;
1105c6c1daeSBarry Smith   PetscViewer_String *vstr    = (PetscViewer_String*)viewer->data;
1115c6c1daeSBarry Smith 
1125c6c1daeSBarry Smith   PetscFunctionBegin;
1135c6c1daeSBarry Smith   vstr->head    = iviewer->head;
1145c6c1daeSBarry Smith   vstr->curlen += iviewer->curlen;
115*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerDestroy(sviewer));
1165c6c1daeSBarry Smith   PetscFunctionReturn(0);
1175c6c1daeSBarry Smith }
1185c6c1daeSBarry Smith 
1198556b5ebSBarry Smith /*MC
1208556b5ebSBarry Smith    PETSCVIEWERSTRING - A viewer that writes to a string
1218556b5ebSBarry Smith 
1228556b5ebSBarry Smith .seealso:  PetscViewerStringOpen(), PetscViewerStringSPrintf(), PetscViewerSocketOpen(), PetscViewerDrawOpen(), PETSCVIEWERSOCKET,
1238556b5ebSBarry Smith            PetscViewerCreate(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PETSCVIEWERBINARY, PETSCVIEWERDRAW,
1248556b5ebSBarry Smith            PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERASCII, PETSCVIEWERMATLAB,
1258556b5ebSBarry Smith            PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType()
1268556b5ebSBarry Smith 
1271b266c99SBarry Smith   Level: beginner
1288556b5ebSBarry Smith M*/
1298556b5ebSBarry Smith 
1308cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_String(PetscViewer v)
1315c6c1daeSBarry Smith {
1325c6c1daeSBarry Smith   PetscViewer_String *vstr;
1335c6c1daeSBarry Smith 
1345c6c1daeSBarry Smith   PetscFunctionBegin;
1355c6c1daeSBarry Smith   v->ops->destroy          = PetscViewerDestroy_String;
13602c9f0b5SLisandro Dalcin   v->ops->view             = NULL;
13702c9f0b5SLisandro Dalcin   v->ops->flush            = NULL;
138559f443fSBarry Smith   v->ops->getsubviewer     = PetscViewerGetSubViewer_String;
139559f443fSBarry Smith   v->ops->restoresubviewer = PetscViewerRestoreSubViewer_String;
140*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscNewLog(v,&vstr));
1415c6c1daeSBarry Smith   v->data                  = (void*)vstr;
14202c9f0b5SLisandro Dalcin   vstr->string             = NULL;
1435c6c1daeSBarry Smith   PetscFunctionReturn(0);
1445c6c1daeSBarry Smith }
1455c6c1daeSBarry Smith 
1465c6c1daeSBarry Smith /*@C
1475c6c1daeSBarry Smith 
14836a9e3b9SBarry Smith    PetscViewerStringGetStringRead - Returns the string that a string viewer uses
14936a9e3b9SBarry Smith 
15036a9e3b9SBarry Smith    Logically Collective on PetscViewer
15136a9e3b9SBarry Smith 
15236a9e3b9SBarry Smith   Input Parameter:
15336a9e3b9SBarry Smith .   viewer - string viewer
15436a9e3b9SBarry Smith 
155fd292e60Sprj-   Output Parameters:
15636a9e3b9SBarry Smith +    string - the string, optional use NULL if you do not need
15736a9e3b9SBarry Smith -   len - the length of the string, optional use NULL if you do
15836a9e3b9SBarry Smith 
15936a9e3b9SBarry Smith   Notes: Do not write to the string nor free it
16036a9e3b9SBarry Smith 
16136a9e3b9SBarry Smith   Level: advanced
16236a9e3b9SBarry Smith 
16336a9e3b9SBarry Smith .seealso: PetscViewerStringOpen(), PETSCVIEWERSTRING, PetscViewerStringSetString(), PetscViewerStringSPrintf(),
16436a9e3b9SBarry Smith           PetscViewerStringSetOwnString()
16536a9e3b9SBarry Smith @*/
16636a9e3b9SBarry Smith PetscErrorCode  PetscViewerStringGetStringRead(PetscViewer viewer,const char *string[],size_t *len)
16736a9e3b9SBarry Smith {
16836a9e3b9SBarry Smith   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
16936a9e3b9SBarry Smith   PetscBool          isstring;
17036a9e3b9SBarry Smith 
17136a9e3b9SBarry Smith   PetscFunctionBegin;
17236a9e3b9SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
173*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring));
1742c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!isstring,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Only for PETSCVIEWERSTRING");
17536a9e3b9SBarry Smith   if (string) *string = vstr->string;
17636a9e3b9SBarry Smith   if (len)    *len    = vstr->maxlen;
17736a9e3b9SBarry Smith   PetscFunctionReturn(0);
17836a9e3b9SBarry Smith }
17936a9e3b9SBarry Smith 
18036a9e3b9SBarry Smith /*@C
18136a9e3b9SBarry Smith 
1825c6c1daeSBarry Smith    PetscViewerStringSetString - sets the string that a string viewer will print to
1835c6c1daeSBarry Smith 
1845c6c1daeSBarry Smith    Logically Collective on PetscViewer
1855c6c1daeSBarry Smith 
1865c6c1daeSBarry Smith   Input Parameters:
1875c6c1daeSBarry Smith +   viewer - string viewer you wish to attach string to
1885c6c1daeSBarry Smith .   string - the string to print data into
1895c6c1daeSBarry Smith -   len - the length of the string
1905c6c1daeSBarry Smith 
191effebcafSBarry Smith   Notes: The function does not copy the string, it uses it directly therefore you cannot free
19236a9e3b9SBarry Smith    the string until the viewer is destroyed. If you call PetscViewerStringSetOwnString() the ownership
19336a9e3b9SBarry Smith    passes to the viewer and it will be responsable for freeing it. In this case the string must be
19436a9e3b9SBarry Smith    obtained with PetscMalloc().
19536a9e3b9SBarry Smith 
1965c6c1daeSBarry Smith   Level: advanced
1975c6c1daeSBarry Smith 
19836a9e3b9SBarry Smith .seealso: PetscViewerStringOpen(), PETSCVIEWERSTRING, PetscViewerStringGetStringRead(), PetscViewerStringSPrintf(),
19936a9e3b9SBarry Smith           PetscViewerStringSetOwnString()
2005c6c1daeSBarry Smith @*/
20136a9e3b9SBarry Smith PetscErrorCode  PetscViewerStringSetString(PetscViewer viewer,char string[],size_t len)
2025c6c1daeSBarry Smith {
2035c6c1daeSBarry Smith   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
2045c6c1daeSBarry Smith   PetscBool          isstring;
2055c6c1daeSBarry Smith 
2065c6c1daeSBarry Smith   PetscFunctionBegin;
2075c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2085c6c1daeSBarry Smith   PetscValidCharPointer(string,2);
209*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring));
2105c6c1daeSBarry Smith   if (!isstring) PetscFunctionReturn(0);
2112c71b3e2SJacob Faibussowitsch   PetscCheckFalse(len <= 2,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"String must have length at least 2");
2125c6c1daeSBarry Smith 
213*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscArrayzero(string,len));
2145c6c1daeSBarry Smith   vstr->string = string;
2155c6c1daeSBarry Smith   vstr->head   = string;
2165c6c1daeSBarry Smith   vstr->curlen = 0;
2175c6c1daeSBarry Smith   vstr->maxlen = len;
2185c6c1daeSBarry Smith   PetscFunctionReturn(0);
2195c6c1daeSBarry Smith }
2205c6c1daeSBarry Smith 
22136a9e3b9SBarry Smith /*@C
22236a9e3b9SBarry Smith 
22336a9e3b9SBarry Smith    PetscViewerStringSetOwnString - tells the viewer that it now owns the string and is responsible for freeing it
22436a9e3b9SBarry Smith 
22536a9e3b9SBarry Smith    Logically Collective on PetscViewer
22636a9e3b9SBarry Smith 
22736a9e3b9SBarry Smith   Input Parameters:
22836a9e3b9SBarry Smith .   viewer - string viewer
22936a9e3b9SBarry Smith 
23036a9e3b9SBarry Smith   Notes: If you call this the string must have been obtained with PetscMalloc() and you cannot free the string
23136a9e3b9SBarry Smith 
23236a9e3b9SBarry Smith   Level: advanced
23336a9e3b9SBarry Smith 
23436a9e3b9SBarry Smith .seealso: PetscViewerStringOpen(), PETSCVIEWERSTRING, PetscViewerStringGetStringRead(), PetscViewerStringSPrintf(),
23536a9e3b9SBarry Smith           PetscViewerStringSetString()
23636a9e3b9SBarry Smith @*/
23736a9e3b9SBarry Smith PetscErrorCode  PetscViewerStringSetOwnString(PetscViewer viewer)
23836a9e3b9SBarry Smith {
23936a9e3b9SBarry Smith   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
24036a9e3b9SBarry Smith   PetscBool          isstring;
24136a9e3b9SBarry Smith 
24236a9e3b9SBarry Smith   PetscFunctionBegin;
24336a9e3b9SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
244*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring));
24536a9e3b9SBarry Smith   if (!isstring) PetscFunctionReturn(0);
24636a9e3b9SBarry Smith 
24736a9e3b9SBarry Smith   vstr->ownstring = PETSC_TRUE;
24836a9e3b9SBarry Smith   PetscFunctionReturn(0);
24936a9e3b9SBarry Smith }
250