Actual source code: stringv.c
1: #define PETSC_DLL
2: #include private/viewerimpl.h
3: #include <stdarg.h>
4: #if defined(PETSC_HAVE_STDLIB_H)
5: #include <stdlib.h>
6: #endif
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;
16: static PetscErrorCode PetscViewerDestroy_String(PetscViewer viewer)
17: {
18: PetscViewer_String *vstr = (PetscViewer_String *)viewer->data;
19: PetscErrorCode ierr;
22: PetscFree(vstr);
23: return(0);
24: }
28: /*@C
29: PetscViewerStringSPrintf - Prints information to a PetscViewer string.
31: Collective on PetscViewer (Hmmm, each processor maintains a separate string)
33: Input Parameters:
34: + v - a string PetscViewer, formed by PetscViewerStringOpen()
35: - format - the format of the input
37: Level: developer
39: Fortran Note:
40: This routine is not supported in Fortran.
42: Concepts: printing^to string
44: .seealso: PetscViewerStringOpen()
45: @*/
46: PetscErrorCode PetscViewerStringSPrintf(PetscViewer viewer,const char format[],...)
47: {
48: va_list Argp;
49: int fullLength;
50: size_t shift;
51: PetscErrorCode ierr;
52: PetscTruth isstring;
53: char tmp[4096];
54: PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
59: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);
60: if (!isstring) return(0);
61: if (!vstr->string) SETERRQ(PETSC_ERR_ORDER,"Must call PetscViewerStringSetString() before using");
63: va_start(Argp,format);
64: PetscVSNPrintf(tmp,4096,format,&fullLength,Argp);
65: va_end(Argp);
67: PetscStrlen(tmp,&shift);
68: if (shift >= vstr->maxlen - vstr->curlen - 1) shift = vstr->maxlen - vstr->curlen - 1;
69: PetscStrncpy(vstr->head,tmp,shift);
71: vstr->head += shift;
72: vstr->curlen += shift;
73: return(0);
74: }
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.
83: Collective on MPI_Comm
85: Input Parameters:
86: + comm - the communicator
87: - string - the string to use
89: Output Parameter:
90: . lab - the PetscViewer
92: Level: advanced
94: Fortran Note:
95: This routine is not supported in Fortran.
97: Concepts: PetscViewerString^creating
99: .seealso: PetscViewerDestroy(), PetscViewerStringSPrintf()
100: @*/
101: PetscErrorCode PetscViewerStringOpen(MPI_Comm comm,char string[],PetscInt len,PetscViewer *lab)
102: {
104:
106: PetscViewerCreate(comm,lab);
107: PetscViewerSetType(*lab,PETSC_VIEWER_STRING);
108: PetscViewerStringSetString(*lab,string,len);
109: return(0);
110: }
114: PetscErrorCode PetscViewerGetSingleton_String(PetscViewer viewer,PetscViewer *sviewer)
115: {
116: PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
117: PetscErrorCode ierr;
120: PetscViewerStringOpen(PETSC_COMM_SELF,vstr->head,vstr->maxlen-vstr->curlen,sviewer);
121: return(0);
122: }
126: PetscErrorCode PetscViewerRestoreSingleton_String(PetscViewer viewer,PetscViewer *sviewer)
127: {
128: PetscErrorCode ierr;
129: PetscViewer_String *iviewer = (PetscViewer_String*)(*sviewer)->data;
130: PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
133: vstr->head = iviewer->head;
134: vstr->curlen += iviewer->curlen;
135: PetscViewerDestroy(*sviewer);
136: return(0);
137: }
142: PetscErrorCode PetscViewerCreate_String(PetscViewer v)
143: {
144: PetscViewer_String *vstr;
145: PetscErrorCode ierr;
148: v->ops->destroy = PetscViewerDestroy_String;
149: v->ops->view = 0;
150: v->ops->flush = 0;
151: v->ops->getsingleton = PetscViewerGetSingleton_String;
152: v->ops->restoresingleton = PetscViewerRestoreSingleton_String;
153: PetscNewLog(v,PetscViewer_String,&vstr);
154: v->data = (void*)vstr;
155: vstr->string = 0;
156: return(0);
157: }
162: /*@C
164: PetscViewerStringSetString - sets the string that a string viewer will print to
166: Collective on PetscViewer
168: Input Parameters:
169: + viewer - string viewer you wish to attach string to
170: . string - the string to print data into
171: - len - the length of the string
173: Level: advanced
175: .seealso: PetscViewerStringOpen()
176: @*/
177: PetscErrorCode PetscViewerStringSetString(PetscViewer viewer,char string[],PetscInt len)
178: {
179: PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
180: PetscErrorCode ierr;
181: PetscTruth isstring;
186: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);
187: if (!isstring) return(0);
188: if (len <= 2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"String must have length at least 2");
190: PetscMemzero(string,len*sizeof(char));
191: vstr->string = string;
192: vstr->head = string;
193: vstr->curlen = 0;
194: vstr->maxlen = len;
195: return(0);
196: }