Actual source code: stringv.c
2: #include <petsc/private/viewerimpl.h>
4: typedef struct {
5: char *string; /* string where info is stored */
6: char *head; /* pointer to beginning of unused portion */
7: size_t curlen,maxlen;
8: PetscBool ownstring; /* string viewer is responsable for freeing the string */
9: } PetscViewer_String;
11: static PetscErrorCode PetscViewerDestroy_String(PetscViewer viewer)
12: {
13: PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
14: PetscErrorCode ierr;
17: if (vstr->ownstring) {
18: PetscFree(vstr->string);
19: }
20: PetscFree(vstr);
21: return(0);
22: }
24: /*@C
25: PetscViewerStringSPrintf - Prints information to a PetscViewer string.
27: Logically Collective on PetscViewer (Hmmm, each processor maintains a separate string)
29: Input Parameters:
30: + v - a string PetscViewer, formed by PetscViewerStringOpen()
31: - format - the format of the input
33: Level: developer
35: Fortran Note:
36: This routine is not supported in Fortran.
38: .seealso: PetscViewerStringOpen(), PetscViewerStringGetStringRead(), PetscViewerStringSetString(), PETSCVIEWERSTRING
39: @*/
40: PetscErrorCode PetscViewerStringSPrintf(PetscViewer viewer,const char format[],...)
41: {
42: va_list Argp;
43: size_t fullLength;
44: size_t shift,cshift;
45: PetscErrorCode ierr;
46: PetscBool isstring;
47: char tmp[4096];
48: PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
53: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);
54: if (!isstring) return(0);
55: if (!vstr->string) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerStringSetString() before using");
57: va_start(Argp,format);
58: PetscVSNPrintf(tmp,4096,format,&fullLength,Argp);
59: va_end(Argp);
60: PetscStrlen(tmp,&shift);
61: cshift = shift+1;
62: if (cshift >= vstr->maxlen - vstr->curlen - 1) cshift = vstr->maxlen - vstr->curlen - 1;
63: PetscStrncpy(vstr->head,tmp,cshift);
64: vstr->head += shift;
65: vstr->curlen += shift;
66: return(0);
67: }
69: /*@C
70: PetscViewerStringOpen - Opens a string as a PetscViewer. This is a very
71: simple PetscViewer; information on the object is simply stored into
72: the string in a fairly nice way.
74: Collective
76: Input Parameters:
77: + comm - the communicator
78: . string - the string to use
79: - len - the string length
81: Output Parameter:
82: . lab - the PetscViewer
84: Level: advanced
86: Fortran Note:
87: This routine is not supported in Fortran.
89: .seealso: PetscViewerDestroy(), PetscViewerStringSPrintf(), PetscViewerStringGetStringRead(), PetscViewerStringSetString(), PETSCVIEWERSTRING
90: @*/
91: PetscErrorCode PetscViewerStringOpen(MPI_Comm comm,char string[],size_t len,PetscViewer *lab)
92: {
96: PetscViewerCreate(comm,lab);
97: PetscViewerSetType(*lab,PETSCVIEWERSTRING);
98: PetscViewerStringSetString(*lab,string,len);
99: return(0);
100: }
102: PetscErrorCode PetscViewerGetSubViewer_String(PetscViewer viewer,MPI_Comm comm,PetscViewer *sviewer)
103: {
104: PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
105: PetscErrorCode ierr;
108: PetscViewerStringOpen(PETSC_COMM_SELF,vstr->head,vstr->maxlen-vstr->curlen,sviewer);
109: return(0);
110: }
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;
119: vstr->head = iviewer->head;
120: vstr->curlen += iviewer->curlen;
121: PetscViewerDestroy(sviewer);
122: return(0);
123: }
125: /*MC
126: PETSCVIEWERSTRING - A viewer that writes to a string
128: .seealso: PetscViewerStringOpen(), PetscViewerStringSPrintf(), PetscViewerSocketOpen(), PetscViewerDrawOpen(), PETSCVIEWERSOCKET,
129: PetscViewerCreate(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PETSCVIEWERBINARY, PETSCVIEWERDRAW,
130: PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERASCII, PETSCVIEWERMATLAB,
131: PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType()
133: Level: beginner
134: M*/
136: PETSC_EXTERN PetscErrorCode PetscViewerCreate_String(PetscViewer v)
137: {
138: PetscViewer_String *vstr;
139: PetscErrorCode ierr;
142: v->ops->destroy = PetscViewerDestroy_String;
143: v->ops->view = NULL;
144: v->ops->flush = NULL;
145: v->ops->getsubviewer = PetscViewerGetSubViewer_String;
146: v->ops->restoresubviewer = PetscViewerRestoreSubViewer_String;
147: PetscNewLog(v,&vstr);
148: v->data = (void*)vstr;
149: vstr->string = NULL;
150: return(0);
151: }
153: /*@C
155: PetscViewerStringGetStringRead - Returns the string that a string viewer uses
157: Logically Collective on PetscViewer
159: Input Parameter:
160: . viewer - string viewer
162: Output Parameters:
163: + string - the string, optional use NULL if you do not need
164: - len - the length of the string, optional use NULL if you do
166: Notes: Do not write to the string nor free it
168: Level: advanced
170: .seealso: PetscViewerStringOpen(), PETSCVIEWERSTRING, PetscViewerStringSetString(), PetscViewerStringSPrintf(),
171: PetscViewerStringSetOwnString()
172: @*/
173: PetscErrorCode PetscViewerStringGetStringRead(PetscViewer viewer,const char *string[],size_t *len)
174: {
175: PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
176: PetscErrorCode ierr;
177: PetscBool isstring;
181: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);
182: if (!isstring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Only for PETSCVIEWERSTRING");
183: if (string) *string = vstr->string;
184: if (len) *len = vstr->maxlen;
185: return(0);
186: }
188: /*@C
190: PetscViewerStringSetString - sets the string that a string viewer will print to
192: Logically Collective on PetscViewer
194: Input Parameters:
195: + viewer - string viewer you wish to attach string to
196: . string - the string to print data into
197: - len - the length of the string
199: Notes: The function does not copy the string, it uses it directly therefore you cannot free
200: the string until the viewer is destroyed. If you call PetscViewerStringSetOwnString() the ownership
201: passes to the viewer and it will be responsable for freeing it. In this case the string must be
202: obtained with PetscMalloc().
204: Level: advanced
206: .seealso: PetscViewerStringOpen(), PETSCVIEWERSTRING, PetscViewerStringGetStringRead(), PetscViewerStringSPrintf(),
207: PetscViewerStringSetOwnString()
208: @*/
209: PetscErrorCode PetscViewerStringSetString(PetscViewer viewer,char string[],size_t len)
210: {
211: PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
212: PetscErrorCode ierr;
213: PetscBool isstring;
218: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);
219: if (!isstring) return(0);
220: if (len <= 2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"String must have length at least 2");
222: PetscArrayzero(string,len);
223: vstr->string = string;
224: vstr->head = string;
225: vstr->curlen = 0;
226: vstr->maxlen = len;
227: return(0);
228: }
230: /*@C
232: PetscViewerStringSetOwnString - tells the viewer that it now owns the string and is responsible for freeing it
234: Logically Collective on PetscViewer
236: Input Parameters:
237: . viewer - string viewer
239: Notes: If you call this the string must have been obtained with PetscMalloc() and you cannot free the string
241: Level: advanced
243: .seealso: PetscViewerStringOpen(), PETSCVIEWERSTRING, PetscViewerStringGetStringRead(), PetscViewerStringSPrintf(),
244: PetscViewerStringSetString()
245: @*/
246: PetscErrorCode PetscViewerStringSetOwnString(PetscViewer viewer)
247: {
248: PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
249: PetscErrorCode ierr;
250: PetscBool isstring;
254: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);
255: if (!isstring) return(0);
257: vstr->ownstring = PETSC_TRUE;
258: return(0);
259: }