xref: /petsc/src/sys/classes/viewer/impls/string/stringv.c (revision 36a9e3b9f6565ce1252c167e0dc4a4cf71b0f2ec)
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;
8*36a9e3b9SBarry 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;
17*36a9e3b9SBarry Smith   if (vstr->ownstring) {
18*36a9e3b9SBarry Smith     ierr = PetscFree(vstr->string);CHKERRQ(ierr);
19*36a9e3b9SBarry 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 
385c6c1daeSBarry Smith    Concepts: printing^to string
395c6c1daeSBarry Smith 
40*36a9e3b9SBarry Smith .seealso: PetscViewerStringOpen(), PetscViewerStringGetStringRead(), PetscViewerStringSetString(), PETSCVIEWERSTRING
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 /*@C
725c6c1daeSBarry Smith     PetscViewerStringOpen - Opens a string as a PetscViewer. This is a very
735c6c1daeSBarry Smith     simple PetscViewer; information on the object is simply stored into
745c6c1daeSBarry Smith     the string in a fairly nice way.
755c6c1daeSBarry Smith 
765c6c1daeSBarry Smith     Collective on MPI_Comm
775c6c1daeSBarry Smith 
785c6c1daeSBarry Smith     Input Parameters:
795c6c1daeSBarry Smith +   comm - the communicator
805c6c1daeSBarry Smith .   string - the string to use
815c6c1daeSBarry Smith -   len    - the string length
825c6c1daeSBarry Smith 
835c6c1daeSBarry Smith     Output Parameter:
845c6c1daeSBarry Smith .   lab - the PetscViewer
855c6c1daeSBarry Smith 
865c6c1daeSBarry Smith     Level: advanced
875c6c1daeSBarry Smith 
885c6c1daeSBarry Smith     Fortran Note:
895c6c1daeSBarry Smith     This routine is not supported in Fortran.
905c6c1daeSBarry Smith 
915c6c1daeSBarry Smith   Concepts: PetscViewerString^creating
925c6c1daeSBarry Smith 
93*36a9e3b9SBarry Smith .seealso: PetscViewerDestroy(), PetscViewerStringSPrintf(), PetscViewerStringGetStringRead(), PetscViewerStringSetString(), PETSCVIEWERSTRING
945c6c1daeSBarry Smith @*/
9589d949e2SBarry Smith PetscErrorCode  PetscViewerStringOpen(MPI_Comm comm,char string[],size_t len,PetscViewer *lab)
965c6c1daeSBarry Smith {
975c6c1daeSBarry Smith   PetscErrorCode ierr;
985c6c1daeSBarry Smith 
995c6c1daeSBarry Smith   PetscFunctionBegin;
1005c6c1daeSBarry Smith   ierr = PetscViewerCreate(comm,lab);CHKERRQ(ierr);
1015c6c1daeSBarry Smith   ierr = PetscViewerSetType(*lab,PETSCVIEWERSTRING);CHKERRQ(ierr);
1025c6c1daeSBarry Smith   ierr = PetscViewerStringSetString(*lab,string,len);CHKERRQ(ierr);
1035c6c1daeSBarry Smith   PetscFunctionReturn(0);
1045c6c1daeSBarry Smith }
1055c6c1daeSBarry Smith 
1063f08860eSBarry Smith PetscErrorCode PetscViewerGetSubViewer_String(PetscViewer viewer,MPI_Comm comm,PetscViewer *sviewer)
1075c6c1daeSBarry Smith {
1085c6c1daeSBarry Smith   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
1095c6c1daeSBarry Smith   PetscErrorCode     ierr;
1105c6c1daeSBarry Smith 
1115c6c1daeSBarry Smith   PetscFunctionBegin;
1125c6c1daeSBarry Smith   ierr = PetscViewerStringOpen(PETSC_COMM_SELF,vstr->head,vstr->maxlen-vstr->curlen,sviewer);CHKERRQ(ierr);
1135c6c1daeSBarry Smith   PetscFunctionReturn(0);
1145c6c1daeSBarry Smith }
1155c6c1daeSBarry Smith 
1163f08860eSBarry Smith PetscErrorCode PetscViewerRestoreSubViewer_String(PetscViewer viewer,MPI_Comm comm,PetscViewer *sviewer)
1175c6c1daeSBarry Smith {
1185c6c1daeSBarry Smith   PetscErrorCode     ierr;
1195c6c1daeSBarry Smith   PetscViewer_String *iviewer = (PetscViewer_String*)(*sviewer)->data;
1205c6c1daeSBarry Smith   PetscViewer_String *vstr    = (PetscViewer_String*)viewer->data;
1215c6c1daeSBarry Smith 
1225c6c1daeSBarry Smith   PetscFunctionBegin;
1235c6c1daeSBarry Smith   vstr->head    = iviewer->head;
1245c6c1daeSBarry Smith   vstr->curlen += iviewer->curlen;
1255c6c1daeSBarry Smith   ierr          = PetscViewerDestroy(sviewer);CHKERRQ(ierr);
1265c6c1daeSBarry Smith   PetscFunctionReturn(0);
1275c6c1daeSBarry Smith }
1285c6c1daeSBarry Smith 
1298556b5ebSBarry Smith /*MC
1308556b5ebSBarry Smith    PETSCVIEWERSTRING - A viewer that writes to a string
1318556b5ebSBarry Smith 
1328556b5ebSBarry Smith 
1338556b5ebSBarry Smith .seealso:  PetscViewerStringOpen(), PetscViewerStringSPrintf(), PetscViewerSocketOpen(), PetscViewerDrawOpen(), PETSCVIEWERSOCKET,
1348556b5ebSBarry Smith            PetscViewerCreate(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PETSCVIEWERBINARY, PETSCVIEWERDRAW,
1358556b5ebSBarry Smith            PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERASCII, PETSCVIEWERMATLAB,
1368556b5ebSBarry Smith            PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType()
1378556b5ebSBarry Smith 
1381b266c99SBarry Smith   Level: beginner
1398556b5ebSBarry Smith M*/
1408556b5ebSBarry Smith 
1418cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_String(PetscViewer v)
1425c6c1daeSBarry Smith {
1435c6c1daeSBarry Smith   PetscViewer_String *vstr;
1445c6c1daeSBarry Smith   PetscErrorCode     ierr;
1455c6c1daeSBarry Smith 
1465c6c1daeSBarry Smith   PetscFunctionBegin;
1475c6c1daeSBarry Smith   v->ops->destroy          = PetscViewerDestroy_String;
1485c6c1daeSBarry Smith   v->ops->view             = 0;
1495c6c1daeSBarry Smith   v->ops->flush            = 0;
150559f443fSBarry Smith   v->ops->getsubviewer     = PetscViewerGetSubViewer_String;
151559f443fSBarry Smith   v->ops->restoresubviewer = PetscViewerRestoreSubViewer_String;
152b00a9115SJed Brown   ierr                     = PetscNewLog(v,&vstr);CHKERRQ(ierr);
1535c6c1daeSBarry Smith   v->data                  = (void*)vstr;
1545c6c1daeSBarry Smith   vstr->string             = 0;
1555c6c1daeSBarry Smith   PetscFunctionReturn(0);
1565c6c1daeSBarry Smith }
1575c6c1daeSBarry Smith 
1585c6c1daeSBarry Smith /*@C
1595c6c1daeSBarry Smith 
160*36a9e3b9SBarry Smith    PetscViewerStringGetStringRead - Returns the string that a string viewer uses
161*36a9e3b9SBarry Smith 
162*36a9e3b9SBarry Smith    Logically Collective on PetscViewer
163*36a9e3b9SBarry Smith 
164*36a9e3b9SBarry Smith   Input Parameter:
165*36a9e3b9SBarry Smith .   viewer - string viewer
166*36a9e3b9SBarry Smith 
167*36a9e3b9SBarry Smith   Output Paramters:
168*36a9e3b9SBarry Smith +    string - the string, optional use NULL if you do not need
169*36a9e3b9SBarry Smith -   len - the length of the string, optional use NULL if you do
170*36a9e3b9SBarry Smith 
171*36a9e3b9SBarry Smith   Notes: Do not write to the string nor free it
172*36a9e3b9SBarry Smith 
173*36a9e3b9SBarry Smith   Level: advanced
174*36a9e3b9SBarry Smith 
175*36a9e3b9SBarry Smith .seealso: PetscViewerStringOpen(), PETSCVIEWERSTRING, PetscViewerStringSetString(), PetscViewerStringSPrintf(),
176*36a9e3b9SBarry Smith           PetscViewerStringSetOwnString()
177*36a9e3b9SBarry Smith @*/
178*36a9e3b9SBarry Smith PetscErrorCode  PetscViewerStringGetStringRead(PetscViewer viewer,const char *string[],size_t *len)
179*36a9e3b9SBarry Smith {
180*36a9e3b9SBarry Smith   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
181*36a9e3b9SBarry Smith   PetscErrorCode     ierr;
182*36a9e3b9SBarry Smith   PetscBool          isstring;
183*36a9e3b9SBarry Smith 
184*36a9e3b9SBarry Smith   PetscFunctionBegin;
185*36a9e3b9SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
186*36a9e3b9SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
187*36a9e3b9SBarry Smith   if (!isstring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Only for PETSCVIEWERSTRING");
188*36a9e3b9SBarry Smith   if (string) *string = vstr->string;
189*36a9e3b9SBarry Smith   if (len)    *len    = vstr->maxlen;
190*36a9e3b9SBarry Smith   PetscFunctionReturn(0);
191*36a9e3b9SBarry Smith }
192*36a9e3b9SBarry Smith 
193*36a9e3b9SBarry Smith /*@C
194*36a9e3b9SBarry Smith 
1955c6c1daeSBarry Smith    PetscViewerStringSetString - sets the string that a string viewer will print to
1965c6c1daeSBarry Smith 
1975c6c1daeSBarry Smith    Logically Collective on PetscViewer
1985c6c1daeSBarry Smith 
1995c6c1daeSBarry Smith   Input Parameters:
2005c6c1daeSBarry Smith +   viewer - string viewer you wish to attach string to
2015c6c1daeSBarry Smith .   string - the string to print data into
2025c6c1daeSBarry Smith -   len - the length of the string
2035c6c1daeSBarry Smith 
204*36a9e3b9SBarry Smith   Notes: The function does not copy the string, it uses it directly therefor you cannot free
205*36a9e3b9SBarry Smith    the string until the viewer is destroyed. If you call PetscViewerStringSetOwnString() the ownership
206*36a9e3b9SBarry Smith    passes to the viewer and it will be responsable for freeing it. In this case the string must be
207*36a9e3b9SBarry Smith    obtained with PetscMalloc().
208*36a9e3b9SBarry Smith 
2095c6c1daeSBarry Smith   Level: advanced
2105c6c1daeSBarry Smith 
211*36a9e3b9SBarry Smith .seealso: PetscViewerStringOpen(), PETSCVIEWERSTRING, PetscViewerStringGetStringRead(), PetscViewerStringSPrintf(),
212*36a9e3b9SBarry Smith           PetscViewerStringSetOwnString()
2135c6c1daeSBarry Smith @*/
214*36a9e3b9SBarry Smith PetscErrorCode  PetscViewerStringSetString(PetscViewer viewer,char string[],size_t len)
2155c6c1daeSBarry Smith {
2165c6c1daeSBarry Smith   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
2175c6c1daeSBarry Smith   PetscErrorCode     ierr;
2185c6c1daeSBarry Smith   PetscBool          isstring;
2195c6c1daeSBarry Smith 
2205c6c1daeSBarry Smith   PetscFunctionBegin;
2215c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2225c6c1daeSBarry Smith   PetscValidCharPointer(string,2);
2235c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
2245c6c1daeSBarry Smith   if (!isstring) PetscFunctionReturn(0);
2255c6c1daeSBarry Smith   if (len <= 2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"String must have length at least 2");
2265c6c1daeSBarry Smith 
2275c6c1daeSBarry Smith   ierr         = PetscMemzero(string,len*sizeof(char));CHKERRQ(ierr);
2285c6c1daeSBarry Smith   vstr->string = string;
2295c6c1daeSBarry Smith   vstr->head   = string;
2305c6c1daeSBarry Smith   vstr->curlen = 0;
2315c6c1daeSBarry Smith   vstr->maxlen = len;
2325c6c1daeSBarry Smith   PetscFunctionReturn(0);
2335c6c1daeSBarry Smith }
2345c6c1daeSBarry Smith 
235*36a9e3b9SBarry Smith /*@C
236*36a9e3b9SBarry Smith 
237*36a9e3b9SBarry Smith    PetscViewerStringSetOwnString - tells the viewer that it now owns the string and is responsible for freeing it
238*36a9e3b9SBarry Smith 
239*36a9e3b9SBarry Smith    Logically Collective on PetscViewer
240*36a9e3b9SBarry Smith 
241*36a9e3b9SBarry Smith   Input Parameters:
242*36a9e3b9SBarry Smith .   viewer - string viewer
243*36a9e3b9SBarry Smith 
244*36a9e3b9SBarry Smith   Notes: If you call this the string must have been obtained with PetscMalloc() and you cannot free the string
245*36a9e3b9SBarry Smith 
246*36a9e3b9SBarry Smith   Level: advanced
247*36a9e3b9SBarry Smith 
248*36a9e3b9SBarry Smith .seealso: PetscViewerStringOpen(), PETSCVIEWERSTRING, PetscViewerStringGetStringRead(), PetscViewerStringSPrintf(),
249*36a9e3b9SBarry Smith           PetscViewerStringSetString()
250*36a9e3b9SBarry Smith @*/
251*36a9e3b9SBarry Smith PetscErrorCode  PetscViewerStringSetOwnString(PetscViewer viewer)
252*36a9e3b9SBarry Smith {
253*36a9e3b9SBarry Smith   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
254*36a9e3b9SBarry Smith   PetscErrorCode     ierr;
255*36a9e3b9SBarry Smith   PetscBool          isstring;
256*36a9e3b9SBarry Smith 
257*36a9e3b9SBarry Smith   PetscFunctionBegin;
258*36a9e3b9SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
259*36a9e3b9SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
260*36a9e3b9SBarry Smith   if (!isstring) PetscFunctionReturn(0);
261*36a9e3b9SBarry Smith 
262*36a9e3b9SBarry Smith   vstr->ownstring = PETSC_TRUE;
263*36a9e3b9SBarry Smith   PetscFunctionReturn(0);
264*36a9e3b9SBarry Smith }
2655c6c1daeSBarry Smith 
2665c6c1daeSBarry Smith 
2675c6c1daeSBarry Smith 
2685c6c1daeSBarry Smith 
2695c6c1daeSBarry Smith 
270