xref: /petsc/src/sys/classes/viewer/impls/string/stringv.c (revision 1b266c996c0ee2177e49587321b28ae9c622ce9a)
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;
85c6c1daeSBarry Smith } PetscViewer_String;
95c6c1daeSBarry Smith 
105c6c1daeSBarry Smith #undef __FUNCT__
115c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerDestroy_String"
125c6c1daeSBarry Smith static PetscErrorCode PetscViewerDestroy_String(PetscViewer viewer)
135c6c1daeSBarry Smith {
145c6c1daeSBarry Smith   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
155c6c1daeSBarry Smith   PetscErrorCode     ierr;
165c6c1daeSBarry Smith 
175c6c1daeSBarry Smith   PetscFunctionBegin;
185c6c1daeSBarry Smith   ierr = PetscFree(vstr);CHKERRQ(ierr);
195c6c1daeSBarry Smith   PetscFunctionReturn(0);
205c6c1daeSBarry Smith }
215c6c1daeSBarry Smith 
225c6c1daeSBarry Smith #undef __FUNCT__
235c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerStringSPrintf"
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 
385c6c1daeSBarry Smith    Concepts: printing^to string
395c6c1daeSBarry Smith 
405c6c1daeSBarry Smith .seealso: PetscViewerStringOpen()
415c6c1daeSBarry Smith @*/
425c6c1daeSBarry Smith PetscErrorCode  PetscViewerStringSPrintf(PetscViewer viewer,const char format[],...)
435c6c1daeSBarry Smith {
445c6c1daeSBarry Smith   va_list            Argp;
455c6c1daeSBarry Smith   size_t             fullLength;
4689d949e2SBarry Smith   size_t             shift,cshift;
475c6c1daeSBarry Smith   PetscErrorCode     ierr;
485c6c1daeSBarry Smith   PetscBool          isstring;
495c6c1daeSBarry Smith   char               tmp[4096];
505c6c1daeSBarry Smith   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
515c6c1daeSBarry Smith 
525c6c1daeSBarry Smith   PetscFunctionBegin;
535c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
545c6c1daeSBarry Smith   PetscValidCharPointer(format,2);
555c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
565c6c1daeSBarry Smith   if (!isstring) PetscFunctionReturn(0);
575c6c1daeSBarry Smith   if (!vstr->string) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerStringSetString() before using");
585c6c1daeSBarry Smith 
595c6c1daeSBarry Smith   va_start(Argp,format);
605c6c1daeSBarry Smith   ierr = PetscVSNPrintf(tmp,4096,format,&fullLength,Argp);CHKERRQ(ierr);
615c6c1daeSBarry Smith   va_end(Argp);
625c6c1daeSBarry Smith   ierr = PetscStrlen(tmp,&shift);CHKERRQ(ierr);
6389d949e2SBarry Smith   cshift = shift+1;
6489d949e2SBarry Smith   if (cshift >= vstr->maxlen - vstr->curlen - 1) cshift = vstr->maxlen - vstr->curlen - 1;
6589d949e2SBarry Smith   ierr = PetscStrncpy(vstr->head,tmp,cshift);CHKERRQ(ierr);
665c6c1daeSBarry Smith   vstr->head   += shift;
675c6c1daeSBarry Smith   vstr->curlen += shift;
685c6c1daeSBarry Smith   PetscFunctionReturn(0);
695c6c1daeSBarry Smith }
705c6c1daeSBarry Smith 
715c6c1daeSBarry Smith #undef __FUNCT__
725c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerStringOpen"
735c6c1daeSBarry Smith /*@C
745c6c1daeSBarry Smith     PetscViewerStringOpen - Opens a string as a PetscViewer. This is a very
755c6c1daeSBarry Smith     simple PetscViewer; information on the object is simply stored into
765c6c1daeSBarry Smith     the string in a fairly nice way.
775c6c1daeSBarry Smith 
785c6c1daeSBarry Smith     Collective on MPI_Comm
795c6c1daeSBarry Smith 
805c6c1daeSBarry Smith     Input Parameters:
815c6c1daeSBarry Smith +   comm - the communicator
825c6c1daeSBarry Smith .   string - the string to use
835c6c1daeSBarry Smith -   len    - the string length
845c6c1daeSBarry Smith 
855c6c1daeSBarry Smith     Output Parameter:
865c6c1daeSBarry Smith .   lab - the PetscViewer
875c6c1daeSBarry Smith 
885c6c1daeSBarry Smith     Level: advanced
895c6c1daeSBarry Smith 
905c6c1daeSBarry Smith     Fortran Note:
915c6c1daeSBarry Smith     This routine is not supported in Fortran.
925c6c1daeSBarry Smith 
935c6c1daeSBarry Smith   Concepts: PetscViewerString^creating
945c6c1daeSBarry Smith 
955c6c1daeSBarry Smith .seealso: PetscViewerDestroy(), PetscViewerStringSPrintf()
965c6c1daeSBarry Smith @*/
9789d949e2SBarry Smith PetscErrorCode  PetscViewerStringOpen(MPI_Comm comm,char string[],size_t len,PetscViewer *lab)
985c6c1daeSBarry Smith {
995c6c1daeSBarry Smith   PetscErrorCode ierr;
1005c6c1daeSBarry Smith 
1015c6c1daeSBarry Smith   PetscFunctionBegin;
1025c6c1daeSBarry Smith   ierr = PetscViewerCreate(comm,lab);CHKERRQ(ierr);
1035c6c1daeSBarry Smith   ierr = PetscViewerSetType(*lab,PETSCVIEWERSTRING);CHKERRQ(ierr);
1045c6c1daeSBarry Smith   ierr = PetscViewerStringSetString(*lab,string,len);CHKERRQ(ierr);
1055c6c1daeSBarry Smith   PetscFunctionReturn(0);
1065c6c1daeSBarry Smith }
1075c6c1daeSBarry Smith 
1085c6c1daeSBarry Smith #undef __FUNCT__
1093f08860eSBarry Smith #define __FUNCT__ "PetscViewerGetSubViewer_String"
1103f08860eSBarry Smith PetscErrorCode PetscViewerGetSubViewer_String(PetscViewer viewer,MPI_Comm comm,PetscViewer *sviewer)
1115c6c1daeSBarry Smith {
1125c6c1daeSBarry Smith   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
1135c6c1daeSBarry Smith   PetscErrorCode     ierr;
1145c6c1daeSBarry Smith 
1155c6c1daeSBarry Smith   PetscFunctionBegin;
1165c6c1daeSBarry Smith   ierr = PetscViewerStringOpen(PETSC_COMM_SELF,vstr->head,vstr->maxlen-vstr->curlen,sviewer);CHKERRQ(ierr);
1175c6c1daeSBarry Smith   PetscFunctionReturn(0);
1185c6c1daeSBarry Smith }
1195c6c1daeSBarry Smith 
1205c6c1daeSBarry Smith #undef __FUNCT__
1213f08860eSBarry Smith #define __FUNCT__ "PetscViewerRestoreSubViewer_String"
1223f08860eSBarry Smith PetscErrorCode PetscViewerRestoreSubViewer_String(PetscViewer viewer,MPI_Comm comm,PetscViewer *sviewer)
1235c6c1daeSBarry Smith {
1245c6c1daeSBarry Smith   PetscErrorCode     ierr;
1255c6c1daeSBarry Smith   PetscViewer_String *iviewer = (PetscViewer_String*)(*sviewer)->data;
1265c6c1daeSBarry Smith   PetscViewer_String *vstr    = (PetscViewer_String*)viewer->data;
1275c6c1daeSBarry Smith 
1285c6c1daeSBarry Smith   PetscFunctionBegin;
1295c6c1daeSBarry Smith   vstr->head    = iviewer->head;
1305c6c1daeSBarry Smith   vstr->curlen += iviewer->curlen;
1315c6c1daeSBarry Smith   ierr          = PetscViewerDestroy(sviewer);CHKERRQ(ierr);
1325c6c1daeSBarry Smith   PetscFunctionReturn(0);
1335c6c1daeSBarry Smith }
1345c6c1daeSBarry Smith 
1358556b5ebSBarry Smith /*MC
1368556b5ebSBarry Smith    PETSCVIEWERSTRING - A viewer that writes to a string
1378556b5ebSBarry Smith 
1388556b5ebSBarry Smith 
1398556b5ebSBarry Smith .seealso:  PetscViewerStringOpen(), PetscViewerStringSPrintf(), PetscViewerSocketOpen(), PetscViewerDrawOpen(), PETSCVIEWERSOCKET,
1408556b5ebSBarry Smith            PetscViewerCreate(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PETSCVIEWERBINARY, PETSCVIEWERDRAW,
1418556b5ebSBarry Smith            PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERASCII, PETSCVIEWERMATLAB,
1428556b5ebSBarry Smith            PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType()
1438556b5ebSBarry Smith 
144*1b266c99SBarry Smith   Level: beginner
1458556b5ebSBarry Smith M*/
1468556b5ebSBarry Smith 
1475c6c1daeSBarry Smith #undef __FUNCT__
1485c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerCreate_String"
1498cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_String(PetscViewer v)
1505c6c1daeSBarry Smith {
1515c6c1daeSBarry Smith   PetscViewer_String *vstr;
1525c6c1daeSBarry Smith   PetscErrorCode     ierr;
1535c6c1daeSBarry Smith 
1545c6c1daeSBarry Smith   PetscFunctionBegin;
1555c6c1daeSBarry Smith   v->ops->destroy          = PetscViewerDestroy_String;
1565c6c1daeSBarry Smith   v->ops->view             = 0;
1575c6c1daeSBarry Smith   v->ops->flush            = 0;
158559f443fSBarry Smith   v->ops->getsubviewer     = PetscViewerGetSubViewer_String;
159559f443fSBarry Smith   v->ops->restoresubviewer = PetscViewerRestoreSubViewer_String;
160b00a9115SJed Brown   ierr                     = PetscNewLog(v,&vstr);CHKERRQ(ierr);
1615c6c1daeSBarry Smith   v->data                  = (void*)vstr;
1625c6c1daeSBarry Smith   vstr->string             = 0;
1635c6c1daeSBarry Smith   PetscFunctionReturn(0);
1645c6c1daeSBarry Smith }
1655c6c1daeSBarry Smith 
1665c6c1daeSBarry Smith #undef __FUNCT__
1675c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerStringSetString"
1685c6c1daeSBarry Smith /*@C
1695c6c1daeSBarry Smith 
1705c6c1daeSBarry Smith    PetscViewerStringSetString - sets the string that a string viewer will print to
1715c6c1daeSBarry Smith 
1725c6c1daeSBarry Smith    Logically Collective on PetscViewer
1735c6c1daeSBarry Smith 
1745c6c1daeSBarry Smith   Input Parameters:
1755c6c1daeSBarry Smith +   viewer - string viewer you wish to attach string to
1765c6c1daeSBarry Smith .   string - the string to print data into
1775c6c1daeSBarry Smith -   len - the length of the string
1785c6c1daeSBarry Smith 
1795c6c1daeSBarry Smith   Level: advanced
1805c6c1daeSBarry Smith 
1815c6c1daeSBarry Smith .seealso: PetscViewerStringOpen()
1825c6c1daeSBarry Smith @*/
1835c6c1daeSBarry Smith PetscErrorCode  PetscViewerStringSetString(PetscViewer viewer,char string[],PetscInt len)
1845c6c1daeSBarry Smith {
1855c6c1daeSBarry Smith   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
1865c6c1daeSBarry Smith   PetscErrorCode     ierr;
1875c6c1daeSBarry Smith   PetscBool          isstring;
1885c6c1daeSBarry Smith 
1895c6c1daeSBarry Smith   PetscFunctionBegin;
1905c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
1915c6c1daeSBarry Smith   PetscValidCharPointer(string,2);
1925c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
1935c6c1daeSBarry Smith   if (!isstring) PetscFunctionReturn(0);
1945c6c1daeSBarry Smith   if (len <= 2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"String must have length at least 2");
1955c6c1daeSBarry Smith 
1965c6c1daeSBarry Smith   ierr         = PetscMemzero(string,len*sizeof(char));CHKERRQ(ierr);
1975c6c1daeSBarry Smith   vstr->string = string;
1985c6c1daeSBarry Smith   vstr->head   = string;
1995c6c1daeSBarry Smith   vstr->curlen = 0;
2005c6c1daeSBarry Smith   vstr->maxlen = len;
2015c6c1daeSBarry Smith   PetscFunctionReturn(0);
2025c6c1daeSBarry Smith }
2035c6c1daeSBarry Smith 
2045c6c1daeSBarry Smith 
2055c6c1daeSBarry Smith 
2065c6c1daeSBarry Smith 
2075c6c1daeSBarry Smith 
2085c6c1daeSBarry Smith 
209