Actual source code: stringv.c

petsc-3.7.5 2017-01-01
Report Typos and Errors
  2: #include <petsc/private/viewerimpl.h>   /*I  "petscsys.h"  I*/

  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;

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

 18:   PetscFree(vstr);
 19:   return(0);
 20: }

 24: /*@C
 25:     PetscViewerStringSPrintf - Prints information to a PetscViewer string.

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

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

 33:     Level: developer

 35:     Fortran Note:
 36:     This routine is not supported in Fortran.

 38:    Concepts: printing^to string

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

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

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

 73: /*@C
 74:     PetscViewerStringOpen - Opens a string as a PetscViewer. This is a very
 75:     simple PetscViewer; information on the object is simply stored into
 76:     the string in a fairly nice way.

 78:     Collective on MPI_Comm

 80:     Input Parameters:
 81: +   comm - the communicator
 82: .   string - the string to use
 83: -   len    - the string length

 85:     Output Parameter:
 86: .   lab - the PetscViewer

 88:     Level: advanced

 90:     Fortran Note:
 91:     This routine is not supported in Fortran.

 93:   Concepts: PetscViewerString^creating

 95: .seealso: PetscViewerDestroy(), PetscViewerStringSPrintf()
 96: @*/
 97: PetscErrorCode  PetscViewerStringOpen(MPI_Comm comm,char string[],size_t len,PetscViewer *lab)
 98: {

102:   PetscViewerCreate(comm,lab);
103:   PetscViewerSetType(*lab,PETSCVIEWERSTRING);
104:   PetscViewerStringSetString(*lab,string,len);
105:   return(0);
106: }

110: PetscErrorCode PetscViewerGetSubViewer_String(PetscViewer viewer,MPI_Comm comm,PetscViewer *sviewer)
111: {
112:   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
113:   PetscErrorCode     ierr;

116:   PetscViewerStringOpen(PETSC_COMM_SELF,vstr->head,vstr->maxlen-vstr->curlen,sviewer);
117:   return(0);
118: }

122: PetscErrorCode PetscViewerRestoreSubViewer_String(PetscViewer viewer,MPI_Comm comm,PetscViewer *sviewer)
123: {
124:   PetscErrorCode     ierr;
125:   PetscViewer_String *iviewer = (PetscViewer_String*)(*sviewer)->data;
126:   PetscViewer_String *vstr    = (PetscViewer_String*)viewer->data;

129:   vstr->head    = iviewer->head;
130:   vstr->curlen += iviewer->curlen;
131:   PetscViewerDestroy(sviewer);
132:   return(0);
133: }

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: }

156: /*@C

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

160:    Logically Collective on PetscViewer

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

167:   Level: advanced

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

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

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