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