Actual source code: stringv.c

petsc-3.8.3 2017-12-09
Report Typos and Errors

  2:  #include <petsc/private/viewerimpl.h>

  4: typedef struct  {
  5:   char   *string;         /* string where info is stored */
  6:   char   *head;           /* pointer to begining of unused portion */
  7:   size_t curlen,maxlen;
  8: } PetscViewer_String;

 10: static PetscErrorCode PetscViewerDestroy_String(PetscViewer viewer)
 11: {
 12:   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
 13:   PetscErrorCode     ierr;

 16:   PetscFree(vstr);
 17:   return(0);
 18: }

 20: /*@C
 21:     PetscViewerStringSPrintf - Prints information to a PetscViewer string.

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

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

 29:     Level: developer

 31:     Fortran Note:
 32:     This routine is not supported in Fortran.

 34:    Concepts: printing^to string

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

 51:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);
 52:   if (!isstring) return(0);
 53:   if (!vstr->string) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerStringSetString() before using");

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

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

 72:     Collective on MPI_Comm

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

 79:     Output Parameter:
 80: .   lab - the PetscViewer

 82:     Level: advanced

 84:     Fortran Note:
 85:     This routine is not supported in Fortran.

 87:   Concepts: PetscViewerString^creating

 89: .seealso: PetscViewerDestroy(), PetscViewerStringSPrintf()
 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


129: .seealso:  PetscViewerStringOpen(), PetscViewerStringSPrintf(), PetscViewerSocketOpen(), PetscViewerDrawOpen(), PETSCVIEWERSOCKET,
130:            PetscViewerCreate(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PETSCVIEWERBINARY, PETSCVIEWERDRAW,
131:            PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERASCII, PETSCVIEWERMATLAB,
132:            PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType()

134:   Level: beginner
135: M*/

137: PETSC_EXTERN PetscErrorCode PetscViewerCreate_String(PetscViewer v)
138: {
139:   PetscViewer_String *vstr;
140:   PetscErrorCode     ierr;

143:   v->ops->destroy          = PetscViewerDestroy_String;
144:   v->ops->view             = 0;
145:   v->ops->flush            = 0;
146:   v->ops->getsubviewer     = PetscViewerGetSubViewer_String;
147:   v->ops->restoresubviewer = PetscViewerRestoreSubViewer_String;
148:   PetscNewLog(v,&vstr);
149:   v->data                  = (void*)vstr;
150:   vstr->string             = 0;
151:   return(0);
152: }

154: /*@C

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

158:    Logically Collective on PetscViewer

160:   Input Parameters:
161: +   viewer - string viewer you wish to attach string to
162: .   string - the string to print data into
163: -   len - the length of the string

165:   Level: advanced

167: .seealso: PetscViewerStringOpen()
168: @*/
169: PetscErrorCode  PetscViewerStringSetString(PetscViewer viewer,char string[],PetscInt len)
170: {
171:   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
172:   PetscErrorCode     ierr;
173:   PetscBool          isstring;

178:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);
179:   if (!isstring) return(0);
180:   if (len <= 2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"String must have length at least 2");

182:   PetscMemzero(string,len*sizeof(char));
183:   vstr->string = string;
184:   vstr->head   = string;
185:   vstr->curlen = 0;
186:   vstr->maxlen = len;
187:   return(0);
188: }