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;

 15:   if (vstr->ownstring) {
 16:     PetscFree(vstr->string);
 17:   }
 18:   PetscFree(vstr);
 19:   return 0;
 20: }

 22: /*@C
 23:     PetscViewerStringSPrintf - Prints information to a PetscViewer string.

 25:     Logically Collective on PetscViewer (Hmmm, each processor maintains a separate string)

 27:     Input Parameters:
 28: +   v - a string PetscViewer, formed by PetscViewerStringOpen()
 29: -   format - the format of the input

 31:     Level: developer

 33:     Fortran Note:
 34:     This routine is not supported in Fortran.

 36: .seealso: PetscViewerStringOpen(), PetscViewerStringGetStringRead(), PetscViewerStringSetString(), PETSCVIEWERSTRING
 37: @*/
 38: PetscErrorCode  PetscViewerStringSPrintf(PetscViewer viewer,const char format[],...)
 39: {
 40:   va_list            Argp;
 41:   size_t             fullLength;
 42:   size_t             shift,cshift;
 43:   PetscBool          isstring;
 44:   char               tmp[4096];
 45:   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;

 49:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);
 50:   if (!isstring) return 0;

 53:   va_start(Argp,format);
 54:   PetscVSNPrintf(tmp,4096,format,&fullLength,Argp);
 55:   va_end(Argp);
 56:   PetscStrlen(tmp,&shift);
 57:   cshift = shift+1;
 58:   if (cshift >= vstr->maxlen - vstr->curlen - 1) cshift = vstr->maxlen - vstr->curlen - 1;
 59:   PetscStrncpy(vstr->head,tmp,cshift);
 60:   vstr->head   += shift;
 61:   vstr->curlen += shift;
 62:   return 0;
 63: }

 65: /*@C
 66:     PetscViewerStringOpen - Opens a string as a PetscViewer. This is a very
 67:     simple PetscViewer; information on the object is simply stored into
 68:     the string in a fairly nice way.

 70:     Collective

 72:     Input Parameters:
 73: +   comm - the communicator
 74: .   string - the string to use
 75: -   len    - the string length

 77:     Output Parameter:
 78: .   lab - the PetscViewer

 80:     Level: advanced

 82:     Fortran Note:
 83:     This routine is not supported in Fortran.

 85: .seealso: PetscViewerDestroy(), PetscViewerStringSPrintf(), PetscViewerStringGetStringRead(), PetscViewerStringSetString(), PETSCVIEWERSTRING
 86: @*/
 87: PetscErrorCode  PetscViewerStringOpen(MPI_Comm comm,char string[],size_t len,PetscViewer *lab)
 88: {
 89:   PetscViewerCreate(comm,lab);
 90:   PetscViewerSetType(*lab,PETSCVIEWERSTRING);
 91:   PetscViewerStringSetString(*lab,string,len);
 92:   return 0;
 93: }

 95: PetscErrorCode PetscViewerGetSubViewer_String(PetscViewer viewer,MPI_Comm comm,PetscViewer *sviewer)
 96: {
 97:   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;

 99:   PetscViewerStringOpen(PETSC_COMM_SELF,vstr->head,vstr->maxlen-vstr->curlen,sviewer);
100:   return 0;
101: }

103: PetscErrorCode PetscViewerRestoreSubViewer_String(PetscViewer viewer,MPI_Comm comm,PetscViewer *sviewer)
104: {
105:   PetscViewer_String *iviewer = (PetscViewer_String*)(*sviewer)->data;
106:   PetscViewer_String *vstr    = (PetscViewer_String*)viewer->data;

108:   vstr->head    = iviewer->head;
109:   vstr->curlen += iviewer->curlen;
110:   PetscViewerDestroy(sviewer);
111:   return 0;
112: }

114: /*MC
115:    PETSCVIEWERSTRING - A viewer that writes to a string

117: .seealso:  PetscViewerStringOpen(), PetscViewerStringSPrintf(), PetscViewerSocketOpen(), PetscViewerDrawOpen(), PETSCVIEWERSOCKET,
118:            PetscViewerCreate(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PETSCVIEWERBINARY, PETSCVIEWERDRAW,
119:            PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERASCII, PETSCVIEWERMATLAB,
120:            PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType()

122:   Level: beginner
123: M*/

125: PETSC_EXTERN PetscErrorCode PetscViewerCreate_String(PetscViewer v)
126: {
127:   PetscViewer_String *vstr;

129:   v->ops->destroy          = PetscViewerDestroy_String;
130:   v->ops->view             = NULL;
131:   v->ops->flush            = NULL;
132:   v->ops->getsubviewer     = PetscViewerGetSubViewer_String;
133:   v->ops->restoresubviewer = PetscViewerRestoreSubViewer_String;
134:   PetscNewLog(v,&vstr);
135:   v->data                  = (void*)vstr;
136:   vstr->string             = NULL;
137:   return 0;
138: }

140: /*@C

142:    PetscViewerStringGetStringRead - Returns the string that a string viewer uses

144:    Logically Collective on PetscViewer

146:   Input Parameter:
147: .   viewer - string viewer

149:   Output Parameters:
150: +    string - the string, optional use NULL if you do not need
151: -   len - the length of the string, optional use NULL if you do

153:   Notes: Do not write to the string nor free it

155:   Level: advanced

157: .seealso: PetscViewerStringOpen(), PETSCVIEWERSTRING, PetscViewerStringSetString(), PetscViewerStringSPrintf(),
158:           PetscViewerStringSetOwnString()
159: @*/
160: PetscErrorCode  PetscViewerStringGetStringRead(PetscViewer viewer,const char *string[],size_t *len)
161: {
162:   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
163:   PetscBool          isstring;

166:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);
168:   if (string) *string = vstr->string;
169:   if (len)    *len    = vstr->maxlen;
170:   return 0;
171: }

173: /*@C

175:    PetscViewerStringSetString - sets the string that a string viewer will print to

177:    Logically Collective on PetscViewer

179:   Input Parameters:
180: +   viewer - string viewer you wish to attach string to
181: .   string - the string to print data into
182: -   len - the length of the string

184:   Notes: The function does not copy the string, it uses it directly therefore you cannot free
185:    the string until the viewer is destroyed. If you call PetscViewerStringSetOwnString() the ownership
186:    passes to the viewer and it will be responsable for freeing it. In this case the string must be
187:    obtained with PetscMalloc().

189:   Level: advanced

191: .seealso: PetscViewerStringOpen(), PETSCVIEWERSTRING, PetscViewerStringGetStringRead(), PetscViewerStringSPrintf(),
192:           PetscViewerStringSetOwnString()
193: @*/
194: PetscErrorCode  PetscViewerStringSetString(PetscViewer viewer,char string[],size_t len)
195: {
196:   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
197:   PetscBool          isstring;

201:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);
202:   if (!isstring) return 0;

205:   PetscArrayzero(string,len);
206:   vstr->string = string;
207:   vstr->head   = string;
208:   vstr->curlen = 0;
209:   vstr->maxlen = len;
210:   return 0;
211: }

213: /*@C

215:    PetscViewerStringSetOwnString - tells the viewer that it now owns the string and is responsible for freeing it

217:    Logically Collective on PetscViewer

219:   Input Parameters:
220: .   viewer - string viewer

222:   Notes: If you call this the string must have been obtained with PetscMalloc() and you cannot free the string

224:   Level: advanced

226: .seealso: PetscViewerStringOpen(), PETSCVIEWERSTRING, PetscViewerStringGetStringRead(), PetscViewerStringSPrintf(),
227:           PetscViewerStringSetString()
228: @*/
229: PetscErrorCode  PetscViewerStringSetOwnString(PetscViewer viewer)
230: {
231:   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
232:   PetscBool          isstring;

235:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);
236:   if (!isstring) return 0;

238:   vstr->ownstring = PETSC_TRUE;
239:   return 0;
240: }