Actual source code: stringv.c
1: #define PETSC_DLL
2: #include src/sys/viewer/viewerimpl.h
3: #include <stdarg.h>
4: #if defined(PETSC_HAVE_STDLIB_H)
5: #include <stdlib.h>
6: #endif
7: #include "petscfix.h"
9: typedef struct {
10: char *string; /* string where info is stored */
11: char *head; /* pointer to begining of unused portion */
12: size_t curlen,maxlen;
13: } PetscViewer_String;
17: static PetscErrorCode PetscViewerDestroy_String(PetscViewer viewer)
18: {
19: PetscViewer_String *vstr = (PetscViewer_String *)viewer->data;
20: PetscErrorCode ierr;
23: PetscFree(vstr);
24: return(0);
25: }
29: /*@C
30: PetscViewerStringSPrintf - Prints information to a PetscViewer string.
32: Collective on PetscViewer (Hmmm, each processor maintains a separate string)
34: Input Parameters:
35: + v - a string PetscViewer, formed by PetscViewerStringOpen()
36: - format - the format of the input
38: Level: developer
40: Fortran Note:
41: This routine is not supported in Fortran.
43: Concepts: printing^to string
45: .seealso: PetscViewerStringOpen()
46: @*/
47: PetscErrorCode PetscViewerStringSPrintf(PetscViewer viewer,const char format[],...)
48: {
49: va_list Argp;
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,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: PetscNew(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: }