Actual source code: stringv.c

  1: /*$Id: stringv.c,v 1.44 2001/04/10 19:34:06 bsmith Exp $*/
 2:  #include src/sys/src/viewer/viewerimpl.h
  3: #include <stdarg.h>
  4: #if defined(PETSC_HAVE_STDLIB_H)
  5: #include <stdlib.h>
  6: #endif
  7: #include "petscfix.h"

  9: typedef struct  {
 10:   char         *string;   /* string where info is stored */
 11:   char         *head;     /* pointer to begining of unused portion */
 12:   int          curlen,maxlen;
 13: } PetscViewer_String;

 15: #undef __FUNCT__  
 17: static int PetscViewerDestroy_String(PetscViewer viewer)
 18: {
 19:   PetscViewer_String *vstr = (PetscViewer_String *)viewer->data;
 20:   int                ierr;

 23:   PetscFree(vstr);
 24:   return(0);
 25: }

 27: #undef __FUNCT__  
 29: /*@C
 30:     PetscViewerStringSPrintf - Prints information to a PetscViewer string.

 32:     Collective on PetscViewer (Hmmm, each processor maintains a seperate string)

 34:     Input Parameters:
 35: +   v - a string PetscViewer, formed by PetscViewerStringOpen()
 36: -   format - the format of the input

 38:     Level: developer

 40:     Fortran Note:
 41:     This routine is not supported in Fortran.

 43:    Concepts: printing^to string

 45: .seealso: PetscViewerStringOpen()
 46: @*/
 47: int PetscViewerStringSPrintf(PetscViewer viewer,char *format,...)
 48: {
 49:   va_list            Argp;
 50:   int                shift,ierr;
 51:   PetscTruth         isstring;
 52:   char               tmp[4096];
 53:   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;

 58:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);
 59:   if (!isstring) return(0);
 60:   if (!vstr->string) SETERRQ(1,"Must call PetscViewerStringSetString() before using");

 62:   va_start(Argp,format);
 63: #if defined(PETSC_HAVE_VPRINTF_CHAR)
 64:   vsprintf(tmp,format,(char *)Argp);
 65: #else
 66:   vsprintf(tmp,format,Argp);
 67: #endif
 68:   va_end(Argp);

 70:   PetscStrlen(tmp,&shift);
 71:   if (shift > 4096) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"String too long");
 72: 
 73:   if (shift >= vstr->maxlen - vstr->curlen - 1) shift = vstr->maxlen - vstr->curlen - 1;
 74:   PetscStrncpy(vstr->head,tmp,shift);

 76:   vstr->head   += shift;
 77:   vstr->curlen += shift;
 78:   return(0);
 79: }

 81: #undef __FUNCT__  
 83: /*@C
 84:     PetscViewerStringOpen - Opens a string as a PetscViewer. This is a very 
 85:     simple PetscViewer; information on the object is simply stored into 
 86:     the string in a fairly nice way.

 88:     Collective on MPI_Comm

 90:     Input Parameters:
 91: +   comm - the communicator
 92: -   string - the string to use

 94:     Output Parameter:
 95: .   lab - the PetscViewer

 97:     Level: advanced

 99:     Fortran Note:
100:     This routine is not supported in Fortran.

102:   Concepts: PetscViewerString^creating

104: .seealso: PetscViewerDestroy(), PetscViewerStringSPrintf()
105: @*/
106: int PetscViewerStringOpen(MPI_Comm comm,char string[],int len,PetscViewer *lab)
107: {
109: 
111:   PetscViewerCreate(comm,lab);
112:   PetscViewerSetType(*lab,PETSC_VIEWER_STRING);
113:   PetscViewerStringSetString(*lab,string,len);
114:   return(0);
115: }

117: #undef __FUNCT__  
119: int PetscViewerGetSingleton_String(PetscViewer viewer,PetscViewer *sviewer)
120: {
121:   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
122:   int                ierr;

125:   PetscViewerStringOpen(PETSC_COMM_SELF,vstr->head,vstr->maxlen-vstr->curlen,sviewer);
126:   return(0);
127: }

129: #undef __FUNCT__  
131: int PetscViewerRestoreSingleton_String(PetscViewer viewer,PetscViewer *sviewer)
132: {
133:   int                ierr;
134:   PetscViewer_String *iviewer = (PetscViewer_String*)(*sviewer)->data;
135:   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;


139:   vstr->head    = iviewer->head;
140:   vstr->curlen += iviewer->curlen;
141:   PetscViewerDestroy(*sviewer);
142:   return(0);
143: }

145: EXTERN_C_BEGIN
146: #undef __FUNCT__  
148: int PetscViewerCreate_String(PetscViewer v)
149: {
150:   PetscViewer_String *vstr;
151:   int                ierr;

154:   v->ops->destroy          = PetscViewerDestroy_String;
155:   v->ops->view             = 0;
156:   v->ops->flush            = 0;
157:   v->ops->getsingleton     = PetscViewerGetSingleton_String;
158:   v->ops->restoresingleton = PetscViewerRestoreSingleton_String;
159:   ierr                     = PetscNew(PetscViewer_String,&vstr);
160:   v->data                  = (void*)vstr;
161:   vstr->string             = 0;
162:   return(0);
163: }
164: EXTERN_C_END

166: #undef __FUNCT__  
168: /*@C

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

172:    Collective on PetscViewer

174:   Input Parameters:
175: +   viewer - string viewer you wish to attach string to
176: .   string - the string to print data into
177: -   len - the length of the string

179:   Level: advanced

181: .seealso: PetscViewerStringOpen()
182: @*/
183: int PetscViewerStringSetString(PetscViewer viewer,char string[],int len)
184: {
185:   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
186:   int                ierr;
187:   PetscTruth         isstring;

192:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);
193:   if (!isstring)  return(0);
194:   if (len <= 2) SETERRQ(1,"String must have length at least 2");

196:   PetscMemzero(string,len*sizeof(char));
197:   vstr->string      = string;
198:   vstr->head        = string;

200:   vstr->curlen      = 0;
201:   vstr->maxlen      = len;
202:   return(0);
203: }