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; 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 63 ierr = PetscStrlen(tmp,&shift);CHKERRQ(ierr); 64 if (shift >= vstr->maxlen - vstr->curlen - 1) shift = vstr->maxlen - vstr->curlen - 1; 65 ierr = PetscStrncpy(vstr->head,tmp,shift);CHKERRQ(ierr); 66 67 vstr->head += shift; 68 vstr->curlen += shift; 69 PetscFunctionReturn(0); 70 } 71 72 #undef __FUNCT__ 73 #define __FUNCT__ "PetscViewerStringOpen" 74 /*@C 75 PetscViewerStringOpen - Opens a string as a PetscViewer. This is a very 76 simple PetscViewer; information on the object is simply stored into 77 the string in a fairly nice way. 78 79 Collective on MPI_Comm 80 81 Input Parameters: 82 + comm - the communicator 83 . string - the string to use 84 - len - the string length 85 86 Output Parameter: 87 . lab - the PetscViewer 88 89 Level: advanced 90 91 Fortran Note: 92 This routine is not supported in Fortran. 93 94 Concepts: PetscViewerString^creating 95 96 .seealso: PetscViewerDestroy(), PetscViewerStringSPrintf() 97 @*/ 98 PetscErrorCode PetscViewerStringOpen(MPI_Comm comm,char string[],PetscInt len,PetscViewer *lab) 99 { 100 PetscErrorCode ierr; 101 102 PetscFunctionBegin; 103 ierr = PetscViewerCreate(comm,lab);CHKERRQ(ierr); 104 ierr = PetscViewerSetType(*lab,PETSCVIEWERSTRING);CHKERRQ(ierr); 105 ierr = PetscViewerStringSetString(*lab,string,len);CHKERRQ(ierr); 106 PetscFunctionReturn(0); 107 } 108 109 #undef __FUNCT__ 110 #define __FUNCT__ "PetscViewerGetSingleton_String" 111 PetscErrorCode PetscViewerGetSingleton_String(PetscViewer viewer,PetscViewer *sviewer) 112 { 113 PetscViewer_String *vstr = (PetscViewer_String*)viewer->data; 114 PetscErrorCode ierr; 115 116 PetscFunctionBegin; 117 ierr = PetscViewerStringOpen(PETSC_COMM_SELF,vstr->head,vstr->maxlen-vstr->curlen,sviewer);CHKERRQ(ierr); 118 PetscFunctionReturn(0); 119 } 120 121 #undef __FUNCT__ 122 #define __FUNCT__ "PetscViewerRestoreSingleton_String" 123 PetscErrorCode PetscViewerRestoreSingleton_String(PetscViewer viewer,PetscViewer *sviewer) 124 { 125 PetscErrorCode ierr; 126 PetscViewer_String *iviewer = (PetscViewer_String*)(*sviewer)->data; 127 PetscViewer_String *vstr = (PetscViewer_String*)viewer->data; 128 129 PetscFunctionBegin; 130 vstr->head = iviewer->head; 131 vstr->curlen += iviewer->curlen; 132 ierr = PetscViewerDestroy(sviewer);CHKERRQ(ierr); 133 PetscFunctionReturn(0); 134 } 135 136 #undef __FUNCT__ 137 #define __FUNCT__ "PetscViewerCreate_String" 138 PETSC_EXTERN_C PetscErrorCode PetscViewerCreate_String(PetscViewer v) 139 { 140 PetscViewer_String *vstr; 141 PetscErrorCode ierr; 142 143 PetscFunctionBegin; 144 v->ops->destroy = PetscViewerDestroy_String; 145 v->ops->view = 0; 146 v->ops->flush = 0; 147 v->ops->getsingleton = PetscViewerGetSingleton_String; 148 v->ops->restoresingleton = PetscViewerRestoreSingleton_String; 149 ierr = PetscNewLog(v,PetscViewer_String,&vstr);CHKERRQ(ierr); 150 v->data = (void*)vstr; 151 vstr->string = 0; 152 PetscFunctionReturn(0); 153 } 154 155 #undef __FUNCT__ 156 #define __FUNCT__ "PetscViewerStringSetString" 157 /*@C 158 159 PetscViewerStringSetString - sets the string that a string viewer will print to 160 161 Logically Collective on PetscViewer 162 163 Input Parameters: 164 + viewer - string viewer you wish to attach string to 165 . string - the string to print data into 166 - len - the length of the string 167 168 Level: advanced 169 170 .seealso: PetscViewerStringOpen() 171 @*/ 172 PetscErrorCode PetscViewerStringSetString(PetscViewer viewer,char string[],PetscInt len) 173 { 174 PetscViewer_String *vstr = (PetscViewer_String*)viewer->data; 175 PetscErrorCode ierr; 176 PetscBool isstring; 177 178 PetscFunctionBegin; 179 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 180 PetscValidCharPointer(string,2); 181 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 182 if (!isstring) PetscFunctionReturn(0); 183 if (len <= 2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"String must have length at least 2"); 184 185 ierr = PetscMemzero(string,len*sizeof(char));CHKERRQ(ierr); 186 vstr->string = string; 187 vstr->head = string; 188 vstr->curlen = 0; 189 vstr->maxlen = len; 190 PetscFunctionReturn(0); 191 } 192 193 194 195 196 197 198