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