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: static int PetscViewerDestroy_String(PetscViewer viewer)
 16: {
 17:   PetscViewer_String *vstr = (PetscViewer_String *)viewer->data;
 18:   int                ierr;

 21:   PetscFree(vstr);
 22:   return(0);
 23: }

 25: /*@C
 26:     PetscViewerStringSPrintf - Prints information to a PetscViewer string.

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

 30:     Input Parameters:
 31: +   v - a string PetscViewer, formed by PetscViewerStringOpen()
 32: -   format - the format of the input

 34:     Level: developer

 36:     Fortran Note:
 37:     This routine is not supported in Fortran.

 39:    Concepts: printing^to string

 41: .seealso: PetscViewerStringOpen()
 42: @*/
 43: int PetscViewerStringSPrintf(PetscViewer viewer,char *format,...)
 44: {
 45:   va_list            Argp;
 46:   int                shift,ierr;
 47:   PetscTruth         isstring;
 48:   char               tmp[512];
 49:   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;

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

 58:   va_start(Argp,format);
 59: #if defined(PETSC_HAVE_VPRINTF_CHAR)
 60:   vsprintf(tmp,format,(char *)Argp);
 61: #else
 62:   vsprintf(tmp,format,Argp);
 63: #endif
 64:   va_end(Argp);

 66:   PetscStrlen(tmp,&shift);
 67:   if (shift > 512) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"String too long");
 68: 
 69:   if (shift >= vstr->maxlen - vstr->curlen - 1) shift = vstr->maxlen - vstr->curlen - 1;
 70:   PetscStrncpy(vstr->head,tmp,shift);

 72:   vstr->head   += shift;
 73:   vstr->curlen += shift;
 74:   return(0);
 75: }

 77: /*@C
 78:     PetscViewerStringOpen - Opens a string as a PetscViewer. This is a very 
 79:     simple PetscViewer; information on the object is simply stored into 
 80:     the string in a fairly nice way.

 82:     Collective on MPI_Comm

 84:     Input Parameters:
 85: +   comm - the communicator
 86: -   string - the string to use

 88:     Output Parameter:
 89: .   lab - the PetscViewer

 91:     Level: advanced

 93:     Fortran Note:
 94:     This routine is not supported in Fortran.

 96:   Concepts: PetscViewerString^creating

 98: .seealso: PetscViewerDestroy(), PetscViewerStringSPrintf()
 99: @*/
100: int PetscViewerStringOpen(MPI_Comm comm,char string[],int len,PetscViewer *lab)
101: {
103: 
105:   PetscViewerCreate(comm,lab);
106:   PetscViewerSetType(*lab,PETSC_VIEWER_STRING);
107:   PetscViewerStringSetString(*lab,string,len);
108:   return(0);
109: }

111: int PetscViewerGetSingleton_String(PetscViewer viewer,PetscViewer *sviewer)
112: {
113:   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
114:   int                ierr;

117:   PetscViewerStringOpen(PETSC_COMM_SELF,vstr->head,vstr->maxlen-vstr->curlen,sviewer);
118:   return(0);
119: }

121: int PetscViewerRestoreSingleton_String(PetscViewer viewer,PetscViewer *sviewer)
122: {
123:   int                ierr;
124:   PetscViewer_String *iviewer = (PetscViewer_String*)(*sviewer)->data;
125:   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;


129:   vstr->head    = iviewer->head;
130:   vstr->curlen += iviewer->curlen;
131:   PetscViewerDestroy(*sviewer);
132:   return(0);
133: }

135: EXTERN_C_BEGIN
136: int PetscViewerCreate_String(PetscViewer v)
137: {
138:   PetscViewer_String *vstr;
139:   int                ierr;

142:   v->ops->destroy          = PetscViewerDestroy_String;
143:   v->ops->view             = 0;
144:   v->ops->flush            = 0;
145:   v->ops->getsingleton     = PetscViewerGetSingleton_String;
146:   v->ops->restoresingleton = PetscViewerRestoreSingleton_String;
147:   ierr                     = PetscNew(PetscViewer_String,&vstr);
148:   v->data                  = (void*)vstr;
149:   vstr->string             = 0;
150:   return(0);
151: }
152: EXTERN_C_END

154: int PetscViewerStringSetString(PetscViewer viewer,char string[],int len)
155: {
156:   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
157:   int                ierr;
158:   PetscTruth         isstring;

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

167:   PetscMemzero(string,len*sizeof(char));
168:   vstr->string      = string;
169:   vstr->head        = string;

171:   vstr->curlen      = 0;
172:   vstr->maxlen      = len;
173:   return(0);
174: }