Date: 2013-07-02T14:38:07+00:00

Actual source code: stringv.c
petsc-3.4.2 2013-07-02

      2: #include <petsc-private/viewerimpl.h>   /*I  "petscsys.h"  I*/

      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;

     12: static PetscErrorCode PetscViewerDestroy_String(PetscViewer viewer)
     13: {
     14:   PetscViewer_String *vstr = (PetscViewer_String*)viewer->data;
     15:   PetscErrorCode     ierr;

     18:   PetscFree(vstr);
     19:   return(0);
     20: }

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

     27:     Logically Collective on PetscViewer (Hmmm, each processor maintains a separate string)

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

     33:     Level: developer

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

     38:    Concepts: printing^to string

     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;

     55:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);
     56:   if (!isstring) return(0);
     57:   if (!vstr->string) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerStringSetString() before using");

     59:   va_start(Argp,format);
     60:   PetscVSNPrintf(tmp,4096,format,&fullLength,Argp);
     61:   va_end(Argp);

     63:   PetscStrlen(tmp,&shift);
     64:   if (shift >= vstr->maxlen - vstr->curlen - 1) shift = vstr->maxlen - vstr->curlen - 1;
     65:   PetscStrncpy(vstr->head,tmp,shift);

     67:   vstr->head   += shift;
     68:   vstr->curlen += shift;
     69:   return(0);
     70: }

     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.

     79:     Collective on MPI_Comm

     81:     Input Parameters:
     82: +   comm - the communicator
     83: .   string - the string to use
     84: -   len    - the string length

     86:     Output Parameter:
     87: .   lab - the PetscViewer

     89:     Level: advanced

     91:     Fortran Note:
     92:     This routine is not supported in Fortran.

     94:   Concepts: PetscViewerString^creating

     96: .seealso: PetscViewerDestroy(), PetscViewerStringSPrintf()
     97: @*/
     98: PetscErrorCode  PetscViewerStringOpen(MPI_Comm comm,char string[],PetscInt len,PetscViewer *lab)
     99: {

    103:   PetscViewerCreate(comm,lab);
    104:   PetscViewerSetType(*lab,PETSCVIEWERSTRING);
    105:   PetscViewerStringSetString(*lab,string,len);
    106:   return(0);
    107: }

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

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

    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;

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

    138: PETSC_EXTERN PetscErrorCode PetscViewerCreate_String(PetscViewer v)
    139: {
    140:   PetscViewer_String *vstr;
    141:   PetscErrorCode     ierr;

    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:   PetscNewLog(v,PetscViewer_String,&vstr);
    150:   v->data                  = (void*)vstr;
    151:   vstr->string             = 0;
    152:   return(0);
    153: }

    157: /*@C

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

    161:    Logically Collective on PetscViewer

    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

    168:   Level: advanced

    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;

    181:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);
    182:   if (!isstring) return(0);
    183:   if (len <= 2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"String must have length at least 2");

    185:   PetscMemzero(string,len*sizeof(char));
    186:   vstr->string = string;
    187:   vstr->head   = string;
    188:   vstr->curlen = 0;
    189:   vstr->maxlen = len;
    190:   return(0);
    191: }