1 2 #include <petsc/private/viewerimpl.h> /*I "petscsys.h" I*/ 3 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; 9 10 #undef __FUNCT__ 11 #define __FUNCT__ "PetscViewerDestroy_String" 12 static PetscErrorCode PetscViewerDestroy_String(PetscViewer viewer) 13 { 14 PetscViewer_String *vstr = (PetscViewer_String*)viewer->data; 15 PetscErrorCode ierr; 16 17 PetscFunctionBegin; 18 ierr = PetscFree(vstr);CHKERRQ(ierr); 19 PetscFunctionReturn(0); 20 } 21 22 #undef __FUNCT__ 23 #define __FUNCT__ "PetscViewerStringSPrintf" 24 /*@C 25 PetscViewerStringSPrintf - Prints information to a PetscViewer string. 26 27 Logically Collective on PetscViewer (Hmmm, each processor maintains a separate string) 28 29 Input Parameters: 30 + v - a string PetscViewer, formed by PetscViewerStringOpen() 31 - format - the format of the input 32 33 Level: developer 34 35 Fortran Note: 36 This routine is not supported in Fortran. 37 38 Concepts: printing^to string 39 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; 51 52 PetscFunctionBegin; 53 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 54 PetscValidCharPointer(format,2); 55 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 56 if (!isstring) PetscFunctionReturn(0); 57 if (!vstr->string) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerStringSetString() before using"); 58 59 va_start(Argp,format); 60 ierr = PetscVSNPrintf(tmp,4096,format,&fullLength,Argp);CHKERRQ(ierr); 61 va_end(Argp); 62 ierr = PetscStrlen(tmp,&shift);CHKERRQ(ierr); 63 cshift = shift+1; 64 if (cshift >= vstr->maxlen - vstr->curlen - 1) cshift = vstr->maxlen - vstr->curlen - 1; 65 ierr = PetscStrncpy(vstr->head,tmp,cshift);CHKERRQ(ierr); 66 vstr->head += shift; 67 vstr->curlen += shift; 68 PetscFunctionReturn(0); 69 } 70 71 #undef __FUNCT__ 72 #define __FUNCT__ "PetscViewerStringOpen" 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. 77 78 Collective on MPI_Comm 79 80 Input Parameters: 81 + comm - the communicator 82 . string - the string to use 83 - len - the string length 84 85 Output Parameter: 86 . lab - the PetscViewer 87 88 Level: advanced 89 90 Fortran Note: 91 This routine is not supported in Fortran. 92 93 Concepts: PetscViewerString^creating 94 95 .seealso: PetscViewerDestroy(), PetscViewerStringSPrintf() 96 @*/ 97 PetscErrorCode PetscViewerStringOpen(MPI_Comm comm,char string[],size_t len,PetscViewer *lab) 98 { 99 PetscErrorCode ierr; 100 101 PetscFunctionBegin; 102 ierr = PetscViewerCreate(comm,lab);CHKERRQ(ierr); 103 ierr = PetscViewerSetType(*lab,PETSCVIEWERSTRING);CHKERRQ(ierr); 104 ierr = PetscViewerStringSetString(*lab,string,len);CHKERRQ(ierr); 105 PetscFunctionReturn(0); 106 } 107 108 #undef __FUNCT__ 109 #define __FUNCT__ "PetscViewerGetSubViewer_String" 110 PetscErrorCode PetscViewerGetSubViewer_String(PetscViewer viewer,MPI_Comm comm,PetscViewer *sviewer) 111 { 112 PetscViewer_String *vstr = (PetscViewer_String*)viewer->data; 113 PetscErrorCode ierr; 114 115 PetscFunctionBegin; 116 ierr = PetscViewerStringOpen(PETSC_COMM_SELF,vstr->head,vstr->maxlen-vstr->curlen,sviewer);CHKERRQ(ierr); 117 PetscFunctionReturn(0); 118 } 119 120 #undef __FUNCT__ 121 #define __FUNCT__ "PetscViewerRestoreSubViewer_String" 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; 127 128 PetscFunctionBegin; 129 vstr->head = iviewer->head; 130 vstr->curlen += iviewer->curlen; 131 ierr = PetscViewerDestroy(sviewer);CHKERRQ(ierr); 132 PetscFunctionReturn(0); 133 } 134 135 /*MC 136 PETSCVIEWERSTRING - A viewer that writes to a string 137 138 139 .seealso: PetscViewerStringOpen(), PetscViewerStringSPrintf(), PetscViewerSocketOpen(), PetscViewerDrawOpen(), PETSCVIEWERSOCKET, 140 PetscViewerCreate(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PETSCVIEWERBINARY, PETSCVIEWERDRAW, 141 PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERASCII, PETSCVIEWERMATLAB, 142 PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType() 143 144 M*/ 145 146 #undef __FUNCT__ 147 #define __FUNCT__ "PetscViewerCreate_String" 148 PETSC_EXTERN PetscErrorCode PetscViewerCreate_String(PetscViewer v) 149 { 150 PetscViewer_String *vstr; 151 PetscErrorCode ierr; 152 153 PetscFunctionBegin; 154 v->ops->destroy = PetscViewerDestroy_String; 155 v->ops->view = 0; 156 v->ops->flush = 0; 157 v->ops->getsubviewer = PetscViewerGetSubViewer_String; 158 v->ops->restoresubviewer = PetscViewerRestoreSubViewer_String; 159 ierr = PetscNewLog(v,&vstr);CHKERRQ(ierr); 160 v->data = (void*)vstr; 161 vstr->string = 0; 162 PetscFunctionReturn(0); 163 } 164 165 #undef __FUNCT__ 166 #define __FUNCT__ "PetscViewerStringSetString" 167 /*@C 168 169 PetscViewerStringSetString - sets the string that a string viewer will print to 170 171 Logically Collective on PetscViewer 172 173 Input Parameters: 174 + viewer - string viewer you wish to attach string to 175 . string - the string to print data into 176 - len - the length of the string 177 178 Level: advanced 179 180 .seealso: PetscViewerStringOpen() 181 @*/ 182 PetscErrorCode PetscViewerStringSetString(PetscViewer viewer,char string[],PetscInt len) 183 { 184 PetscViewer_String *vstr = (PetscViewer_String*)viewer->data; 185 PetscErrorCode ierr; 186 PetscBool isstring; 187 188 PetscFunctionBegin; 189 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 190 PetscValidCharPointer(string,2); 191 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 192 if (!isstring) PetscFunctionReturn(0); 193 if (len <= 2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"String must have length at least 2"); 194 195 ierr = PetscMemzero(string,len*sizeof(char));CHKERRQ(ierr); 196 vstr->string = string; 197 vstr->head = string; 198 vstr->curlen = 0; 199 vstr->maxlen = len; 200 PetscFunctionReturn(0); 201 } 202 203 204 205 206 207 208