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__ "PetscViewerGetSingleton_String" 110 PetscErrorCode PetscViewerGetSingleton_String(PetscViewer viewer,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__ "PetscViewerRestoreSingleton_String" 122 PetscErrorCode PetscViewerRestoreSingleton_String(PetscViewer viewer,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 #undef __FUNCT__ 136 #define __FUNCT__ "PetscViewerCreate_String" 137 PETSC_EXTERN PetscErrorCode PetscViewerCreate_String(PetscViewer v) 138 { 139 PetscViewer_String *vstr; 140 PetscErrorCode ierr; 141 142 PetscFunctionBegin; 143 v->ops->destroy = PetscViewerDestroy_String; 144 v->ops->view = 0; 145 v->ops->flush = 0; 146 v->ops->getsingleton = PetscViewerGetSingleton_String; 147 v->ops->restoresingleton = PetscViewerRestoreSingleton_String; 148 ierr = PetscNewLog(v,&vstr);CHKERRQ(ierr); 149 v->data = (void*)vstr; 150 vstr->string = 0; 151 PetscFunctionReturn(0); 152 } 153 154 #undef __FUNCT__ 155 #define __FUNCT__ "PetscViewerStringSetString" 156 /*@C 157 158 PetscViewerStringSetString - sets the string that a string viewer will print to 159 160 Logically Collective on PetscViewer 161 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 166 167 Level: advanced 168 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; 176 177 PetscFunctionBegin; 178 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 179 PetscValidCharPointer(string,2); 180 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 181 if (!isstring) PetscFunctionReturn(0); 182 if (len <= 2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"String must have length at least 2"); 183 184 ierr = PetscMemzero(string,len*sizeof(char));CHKERRQ(ierr); 185 vstr->string = string; 186 vstr->head = string; 187 vstr->curlen = 0; 188 vstr->maxlen = len; 189 PetscFunctionReturn(0); 190 } 191 192 193 194 195 196 197