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:   int                fullLength;
 51:   size_t             shift;
 52:   PetscErrorCode     ierr;
 53:   PetscTruth         isstring;
 54:   char               tmp[4096];
 55:   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;

 60:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);
 61:   if (!isstring) return(0);
 62:   if (!vstr->string) SETERRQ(PETSC_ERR_ORDER,"Must call PetscViewerStringSetString() before using");

 64:   va_start(Argp,format);
 65:   PetscVSNPrintf(tmp,4096,format,&fullLength,Argp);
 66:   va_end(Argp);

 68:   PetscStrlen(tmp,&shift);
 69:   if (shift >= vstr->maxlen - vstr->curlen - 1) shift = vstr->maxlen - vstr->curlen - 1;
 70:   PetscStrncpy(vstr->head,tmp,shift);

 72:   vstr->head   += shift;
 73:   vstr->curlen += shift;
 74:   return(0);
 75: }

 79: /*@C
 80:     PetscViewerStringOpen - Opens a string as a PetscViewer. This is a very 
 81:     simple PetscViewer; information on the object is simply stored into 
 82:     the string in a fairly nice way.

 84:     Collective on MPI_Comm

 86:     Input Parameters:
 87: +   comm - the communicator
 88: -   string - the string to use

 90:     Output Parameter:
 91: .   lab - the PetscViewer

 93:     Level: advanced

 95:     Fortran Note:
 96:     This routine is not supported in Fortran.

 98:   Concepts: PetscViewerString^creating

100: .seealso: PetscViewerDestroy(), PetscViewerStringSPrintf()
101: @*/
102: PetscErrorCode  PetscViewerStringOpen(MPI_Comm comm,char string[],PetscInt len,PetscViewer *lab)
103: {
105: 
107:   PetscViewerCreate(comm,lab);
108:   PetscViewerSetType(*lab,PETSC_VIEWER_STRING);
109:   PetscViewerStringSetString(*lab,string,len);
110:   return(0);
111: }

115: PetscErrorCode PetscViewerGetSingleton_String(PetscViewer viewer,PetscViewer *sviewer)
116: {
117:   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
118:   PetscErrorCode     ierr;

121:   PetscViewerStringOpen(PETSC_COMM_SELF,vstr->head,vstr->maxlen-vstr->curlen,sviewer);
122:   return(0);
123: }

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;

134:   vstr->head    = iviewer->head;
135:   vstr->curlen += iviewer->curlen;
136:   PetscViewerDestroy(*sviewer);
137:   return(0);
138: }

143: PetscErrorCode  PetscViewerCreate_String(PetscViewer v)
144: {
145:   PetscViewer_String *vstr;
146:   PetscErrorCode     ierr;

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:   PetscNewLog(v,PetscViewer_String,&vstr);
155:   v->data                  = (void*)vstr;
156:   vstr->string             = 0;
157:   return(0);
158: }

163: /*@C

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

167:    Collective on PetscViewer

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

174:   Level: advanced

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:   PetscTruth         isstring;

187:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);
188:   if (!isstring)  return(0);
189:   if (len <= 2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"String must have length at least 2");

191:   PetscMemzero(string,len*sizeof(char));
192:   vstr->string      = string;
193:   vstr->head        = string;
194:   vstr->curlen      = 0;
195:   vstr->maxlen      = len;
196:   return(0);
197: }