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 EXTERN_C_BEGIN 137 #undef __FUNCT__ 138 #define __FUNCT__ "PetscViewerCreate_String" 139 PetscErrorCode PetscViewerCreate_String(PetscViewer v) 140 { 141 PetscViewer_String *vstr; 142 PetscErrorCode ierr; 143 144 PetscFunctionBegin; 145 v->ops->destroy = PetscViewerDestroy_String; 146 v->ops->view = 0; 147 v->ops->flush = 0; 148 v->ops->getsingleton = PetscViewerGetSingleton_String; 149 v->ops->restoresingleton = PetscViewerRestoreSingleton_String; 150 ierr = PetscNewLog(v,PetscViewer_String,&vstr);CHKERRQ(ierr); 151 v->data = (void*)vstr; 152 vstr->string = 0; 153 PetscFunctionReturn(0); 154 } 155 EXTERN_C_END 156 157 #undef __FUNCT__ 158 #define __FUNCT__ "PetscViewerStringSetString" 159 /*@C 160 161 PetscViewerStringSetString - sets the string that a string viewer will print to 162 163 Logically Collective on PetscViewer 164 165 Input Parameters: 166 + viewer - string viewer you wish to attach string to 167 . string - the string to print data into 168 - len - the length of the string 169 170 Level: advanced 171 172 .seealso: PetscViewerStringOpen() 173 @*/ 174 PetscErrorCode PetscViewerStringSetString(PetscViewer viewer,char string[],PetscInt len) 175 { 176 PetscViewer_String *vstr = (PetscViewer_String*)viewer->data; 177 PetscErrorCode ierr; 178 PetscBool isstring; 179 180 PetscFunctionBegin; 181 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 182 PetscValidCharPointer(string,2); 183 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 184 if (!isstring) PetscFunctionReturn(0); 185 if (len <= 2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"String must have length at least 2"); 186 187 ierr = PetscMemzero(string,len*sizeof(char));CHKERRQ(ierr); 188 vstr->string = string; 189 vstr->head = string; 190 vstr->curlen = 0; 191 vstr->maxlen = len; 192 PetscFunctionReturn(0); 193 } 194 195 196 197 198 199 200