xref: /petsc/src/sys/classes/viewer/impls/string/stringv.c (revision d083f849a86f1f43e18d534ee43954e2786cb29a)
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 */
65c6c1daeSBarry Smith   char      *head;           /* pointer to begining 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);
555c6c1daeSBarry Smith   if (!vstr->string) SETERRQ(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 
74*d083f849SBarry 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 
1298556b5ebSBarry Smith .seealso:  PetscViewerStringOpen(), PetscViewerStringSPrintf(), PetscViewerSocketOpen(), PetscViewerDrawOpen(), PETSCVIEWERSOCKET,
1308556b5ebSBarry Smith            PetscViewerCreate(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PETSCVIEWERBINARY, PETSCVIEWERDRAW,
1318556b5ebSBarry Smith            PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERASCII, PETSCVIEWERMATLAB,
1328556b5ebSBarry Smith            PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType()
1338556b5ebSBarry Smith 
1341b266c99SBarry Smith   Level: beginner
1358556b5ebSBarry Smith M*/
1368556b5ebSBarry Smith 
1378cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_String(PetscViewer v)
1385c6c1daeSBarry Smith {
1395c6c1daeSBarry Smith   PetscViewer_String *vstr;
1405c6c1daeSBarry Smith   PetscErrorCode     ierr;
1415c6c1daeSBarry Smith 
1425c6c1daeSBarry Smith   PetscFunctionBegin;
1435c6c1daeSBarry Smith   v->ops->destroy          = PetscViewerDestroy_String;
1445c6c1daeSBarry Smith   v->ops->view             = 0;
1455c6c1daeSBarry Smith   v->ops->flush            = 0;
146559f443fSBarry Smith   v->ops->getsubviewer     = PetscViewerGetSubViewer_String;
147559f443fSBarry Smith   v->ops->restoresubviewer = PetscViewerRestoreSubViewer_String;
148b00a9115SJed Brown   ierr                     = PetscNewLog(v,&vstr);CHKERRQ(ierr);
1495c6c1daeSBarry Smith   v->data                  = (void*)vstr;
1505c6c1daeSBarry Smith   vstr->string             = 0;
1515c6c1daeSBarry Smith   PetscFunctionReturn(0);
1525c6c1daeSBarry Smith }
1535c6c1daeSBarry Smith 
1545c6c1daeSBarry Smith /*@C
1555c6c1daeSBarry Smith 
15636a9e3b9SBarry Smith    PetscViewerStringGetStringRead - Returns the string that a string viewer uses
15736a9e3b9SBarry Smith 
15836a9e3b9SBarry Smith    Logically Collective on PetscViewer
15936a9e3b9SBarry Smith 
16036a9e3b9SBarry Smith   Input Parameter:
16136a9e3b9SBarry Smith .   viewer - string viewer
16236a9e3b9SBarry Smith 
16336a9e3b9SBarry Smith   Output Paramters:
16436a9e3b9SBarry Smith +    string - the string, optional use NULL if you do not need
16536a9e3b9SBarry Smith -   len - the length of the string, optional use NULL if you do
16636a9e3b9SBarry Smith 
16736a9e3b9SBarry Smith   Notes: Do not write to the string nor free it
16836a9e3b9SBarry Smith 
16936a9e3b9SBarry Smith   Level: advanced
17036a9e3b9SBarry Smith 
17136a9e3b9SBarry Smith .seealso: PetscViewerStringOpen(), PETSCVIEWERSTRING, PetscViewerStringSetString(), PetscViewerStringSPrintf(),
17236a9e3b9SBarry Smith           PetscViewerStringSetOwnString()
17336a9e3b9SBarry Smith @*/
17436a9e3b9SBarry Smith PetscErrorCode  PetscViewerStringGetStringRead(PetscViewer viewer,const char *string[],size_t *len)
17536a9e3b9SBarry Smith {
17636a9e3b9SBarry Smith   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
17736a9e3b9SBarry Smith   PetscErrorCode     ierr;
17836a9e3b9SBarry Smith   PetscBool          isstring;
17936a9e3b9SBarry Smith 
18036a9e3b9SBarry Smith   PetscFunctionBegin;
18136a9e3b9SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
18236a9e3b9SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
18336a9e3b9SBarry Smith   if (!isstring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Only for PETSCVIEWERSTRING");
18436a9e3b9SBarry Smith   if (string) *string = vstr->string;
18536a9e3b9SBarry Smith   if (len)    *len    = vstr->maxlen;
18636a9e3b9SBarry Smith   PetscFunctionReturn(0);
18736a9e3b9SBarry Smith }
18836a9e3b9SBarry Smith 
18936a9e3b9SBarry Smith /*@C
19036a9e3b9SBarry Smith 
1915c6c1daeSBarry Smith    PetscViewerStringSetString - sets the string that a string viewer will print to
1925c6c1daeSBarry Smith 
1935c6c1daeSBarry Smith    Logically Collective on PetscViewer
1945c6c1daeSBarry Smith 
1955c6c1daeSBarry Smith   Input Parameters:
1965c6c1daeSBarry Smith +   viewer - string viewer you wish to attach string to
1975c6c1daeSBarry Smith .   string - the string to print data into
1985c6c1daeSBarry Smith -   len - the length of the string
1995c6c1daeSBarry Smith 
20036a9e3b9SBarry Smith   Notes: The function does not copy the string, it uses it directly therefor you cannot free
20136a9e3b9SBarry Smith    the string until the viewer is destroyed. If you call PetscViewerStringSetOwnString() the ownership
20236a9e3b9SBarry Smith    passes to the viewer and it will be responsable for freeing it. In this case the string must be
20336a9e3b9SBarry Smith    obtained with PetscMalloc().
20436a9e3b9SBarry Smith 
2055c6c1daeSBarry Smith   Level: advanced
2065c6c1daeSBarry Smith 
20736a9e3b9SBarry Smith .seealso: PetscViewerStringOpen(), PETSCVIEWERSTRING, PetscViewerStringGetStringRead(), PetscViewerStringSPrintf(),
20836a9e3b9SBarry Smith           PetscViewerStringSetOwnString()
2095c6c1daeSBarry Smith @*/
21036a9e3b9SBarry Smith PetscErrorCode  PetscViewerStringSetString(PetscViewer viewer,char string[],size_t len)
2115c6c1daeSBarry Smith {
2125c6c1daeSBarry Smith   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
2135c6c1daeSBarry Smith   PetscErrorCode     ierr;
2145c6c1daeSBarry Smith   PetscBool          isstring;
2155c6c1daeSBarry Smith 
2165c6c1daeSBarry Smith   PetscFunctionBegin;
2175c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2185c6c1daeSBarry Smith   PetscValidCharPointer(string,2);
2195c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
2205c6c1daeSBarry Smith   if (!isstring) PetscFunctionReturn(0);
2215c6c1daeSBarry Smith   if (len <= 2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"String must have length at least 2");
2225c6c1daeSBarry Smith 
2235c6c1daeSBarry Smith   ierr         = PetscMemzero(string,len*sizeof(char));CHKERRQ(ierr);
2245c6c1daeSBarry Smith   vstr->string = string;
2255c6c1daeSBarry Smith   vstr->head   = string;
2265c6c1daeSBarry Smith   vstr->curlen = 0;
2275c6c1daeSBarry Smith   vstr->maxlen = len;
2285c6c1daeSBarry Smith   PetscFunctionReturn(0);
2295c6c1daeSBarry Smith }
2305c6c1daeSBarry Smith 
23136a9e3b9SBarry Smith /*@C
23236a9e3b9SBarry Smith 
23336a9e3b9SBarry Smith    PetscViewerStringSetOwnString - tells the viewer that it now owns the string and is responsible for freeing it
23436a9e3b9SBarry Smith 
23536a9e3b9SBarry Smith    Logically Collective on PetscViewer
23636a9e3b9SBarry Smith 
23736a9e3b9SBarry Smith   Input Parameters:
23836a9e3b9SBarry Smith .   viewer - string viewer
23936a9e3b9SBarry Smith 
24036a9e3b9SBarry Smith   Notes: If you call this the string must have been obtained with PetscMalloc() and you cannot free the string
24136a9e3b9SBarry Smith 
24236a9e3b9SBarry Smith   Level: advanced
24336a9e3b9SBarry Smith 
24436a9e3b9SBarry Smith .seealso: PetscViewerStringOpen(), PETSCVIEWERSTRING, PetscViewerStringGetStringRead(), PetscViewerStringSPrintf(),
24536a9e3b9SBarry Smith           PetscViewerStringSetString()
24636a9e3b9SBarry Smith @*/
24736a9e3b9SBarry Smith PetscErrorCode  PetscViewerStringSetOwnString(PetscViewer viewer)
24836a9e3b9SBarry Smith {
24936a9e3b9SBarry Smith   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
25036a9e3b9SBarry Smith   PetscErrorCode     ierr;
25136a9e3b9SBarry Smith   PetscBool          isstring;
25236a9e3b9SBarry Smith 
25336a9e3b9SBarry Smith   PetscFunctionBegin;
25436a9e3b9SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
25536a9e3b9SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
25636a9e3b9SBarry Smith   if (!isstring) PetscFunctionReturn(0);
25736a9e3b9SBarry Smith 
25836a9e3b9SBarry Smith   vstr->ownstring = PETSC_TRUE;
25936a9e3b9SBarry Smith   PetscFunctionReturn(0);
26036a9e3b9SBarry Smith }
2615c6c1daeSBarry Smith 
2625c6c1daeSBarry Smith 
2635c6c1daeSBarry Smith 
2645c6c1daeSBarry Smith 
2655c6c1daeSBarry Smith 
266