xref: /petsc/src/sys/classes/viewer/impls/string/stringv.c (revision feff33ee0b5b037fa8f9f294dede656a2f85cc47)
1 
2 #include <petsc/private/viewerimpl.h>   /*I  "petscsys.h"  I*/
3 
4 typedef struct  {
5   char   *string;         /* string where info is stored */
6   char   *head;           /* pointer to begining of unused portion */
7   size_t curlen,maxlen;
8 } PetscViewer_String;
9 
10 static PetscErrorCode PetscViewerDestroy_String(PetscViewer viewer)
11 {
12   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
13   PetscErrorCode     ierr;
14 
15   PetscFunctionBegin;
16   ierr = PetscFree(vstr);CHKERRQ(ierr);
17   PetscFunctionReturn(0);
18 }
19 
20 /*@C
21     PetscViewerStringSPrintf - Prints information to a PetscViewer string.
22 
23     Logically Collective on PetscViewer (Hmmm, each processor maintains a separate string)
24 
25     Input Parameters:
26 +   v - a string PetscViewer, formed by PetscViewerStringOpen()
27 -   format - the format of the input
28 
29     Level: developer
30 
31     Fortran Note:
32     This routine is not supported in Fortran.
33 
34    Concepts: printing^to string
35 
36 .seealso: PetscViewerStringOpen()
37 @*/
38 PetscErrorCode  PetscViewerStringSPrintf(PetscViewer viewer,const char format[],...)
39 {
40   va_list            Argp;
41   size_t             fullLength;
42   size_t             shift,cshift;
43   PetscErrorCode     ierr;
44   PetscBool          isstring;
45   char               tmp[4096];
46   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
47 
48   PetscFunctionBegin;
49   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
50   PetscValidCharPointer(format,2);
51   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
52   if (!isstring) PetscFunctionReturn(0);
53   if (!vstr->string) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerStringSetString() before using");
54 
55   va_start(Argp,format);
56   ierr = PetscVSNPrintf(tmp,4096,format,&fullLength,Argp);CHKERRQ(ierr);
57   va_end(Argp);
58   ierr = PetscStrlen(tmp,&shift);CHKERRQ(ierr);
59   cshift = shift+1;
60   if (cshift >= vstr->maxlen - vstr->curlen - 1) cshift = vstr->maxlen - vstr->curlen - 1;
61   ierr = PetscStrncpy(vstr->head,tmp,cshift);CHKERRQ(ierr);
62   vstr->head   += shift;
63   vstr->curlen += shift;
64   PetscFunctionReturn(0);
65 }
66 
67 /*@C
68     PetscViewerStringOpen - Opens a string as a PetscViewer. This is a very
69     simple PetscViewer; information on the object is simply stored into
70     the string in a fairly nice way.
71 
72     Collective on MPI_Comm
73 
74     Input Parameters:
75 +   comm - the communicator
76 .   string - the string to use
77 -   len    - the string length
78 
79     Output Parameter:
80 .   lab - the PetscViewer
81 
82     Level: advanced
83 
84     Fortran Note:
85     This routine is not supported in Fortran.
86 
87   Concepts: PetscViewerString^creating
88 
89 .seealso: PetscViewerDestroy(), PetscViewerStringSPrintf()
90 @*/
91 PetscErrorCode  PetscViewerStringOpen(MPI_Comm comm,char string[],size_t len,PetscViewer *lab)
92 {
93   PetscErrorCode ierr;
94 
95   PetscFunctionBegin;
96   ierr = PetscViewerCreate(comm,lab);CHKERRQ(ierr);
97   ierr = PetscViewerSetType(*lab,PETSCVIEWERSTRING);CHKERRQ(ierr);
98   ierr = PetscViewerStringSetString(*lab,string,len);CHKERRQ(ierr);
99   PetscFunctionReturn(0);
100 }
101 
102 PetscErrorCode PetscViewerGetSubViewer_String(PetscViewer viewer,MPI_Comm comm,PetscViewer *sviewer)
103 {
104   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
105   PetscErrorCode     ierr;
106 
107   PetscFunctionBegin;
108   ierr = PetscViewerStringOpen(PETSC_COMM_SELF,vstr->head,vstr->maxlen-vstr->curlen,sviewer);CHKERRQ(ierr);
109   PetscFunctionReturn(0);
110 }
111 
112 PetscErrorCode PetscViewerRestoreSubViewer_String(PetscViewer viewer,MPI_Comm comm,PetscViewer *sviewer)
113 {
114   PetscErrorCode     ierr;
115   PetscViewer_String *iviewer = (PetscViewer_String*)(*sviewer)->data;
116   PetscViewer_String *vstr    = (PetscViewer_String*)viewer->data;
117 
118   PetscFunctionBegin;
119   vstr->head    = iviewer->head;
120   vstr->curlen += iviewer->curlen;
121   ierr          = PetscViewerDestroy(sviewer);CHKERRQ(ierr);
122   PetscFunctionReturn(0);
123 }
124 
125 /*MC
126    PETSCVIEWERSTRING - A viewer that writes to a string
127 
128 
129 .seealso:  PetscViewerStringOpen(), PetscViewerStringSPrintf(), PetscViewerSocketOpen(), PetscViewerDrawOpen(), PETSCVIEWERSOCKET,
130            PetscViewerCreate(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PETSCVIEWERBINARY, PETSCVIEWERDRAW,
131            PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERASCII, PETSCVIEWERMATLAB,
132            PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType()
133 
134   Level: beginner
135 M*/
136 
137 PETSC_EXTERN PetscErrorCode PetscViewerCreate_String(PetscViewer v)
138 {
139   PetscViewer_String *vstr;
140   PetscErrorCode     ierr;
141 
142   PetscFunctionBegin;
143   v->ops->destroy          = PetscViewerDestroy_String;
144   v->ops->view             = 0;
145   v->ops->flush            = 0;
146   v->ops->getsubviewer     = PetscViewerGetSubViewer_String;
147   v->ops->restoresubviewer = PetscViewerRestoreSubViewer_String;
148   ierr                     = PetscNewLog(v,&vstr);CHKERRQ(ierr);
149   v->data                  = (void*)vstr;
150   vstr->string             = 0;
151   PetscFunctionReturn(0);
152 }
153 
154 /*@C
155 
156    PetscViewerStringSetString - sets the string that a string viewer will print to
157 
158    Logically Collective on PetscViewer
159 
160   Input Parameters:
161 +   viewer - string viewer you wish to attach string to
162 .   string - the string to print data into
163 -   len - the length of the string
164 
165   Level: advanced
166 
167 .seealso: PetscViewerStringOpen()
168 @*/
169 PetscErrorCode  PetscViewerStringSetString(PetscViewer viewer,char string[],PetscInt len)
170 {
171   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
172   PetscErrorCode     ierr;
173   PetscBool          isstring;
174 
175   PetscFunctionBegin;
176   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
177   PetscValidCharPointer(string,2);
178   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
179   if (!isstring) PetscFunctionReturn(0);
180   if (len <= 2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"String must have length at least 2");
181 
182   ierr         = PetscMemzero(string,len*sizeof(char));CHKERRQ(ierr);
183   vstr->string = string;
184   vstr->head   = string;
185   vstr->curlen = 0;
186   vstr->maxlen = len;
187   PetscFunctionReturn(0);
188 }
189 
190 
191 
192 
193 
194 
195