Actual source code: ex1.c

      2: static char help[] = "Reads an AODatabase and displays the key and segment names. Runtime options include:\n\
      3:     -f input_file : Specifies input file\n\
      4:     -d : Dumps the entire database\n\
      5:     -e : Allows addition of character string values to the database\n\
      6:     -r : Allows removal of items from the database\n\n";

      8: /*T
      9:    Concepts: AOData^using an AOData database for grid information;
     10:    Processors: n
     11: T*/

     13: /*
     14:   Include "petscao.h" so that we can use the various AO and AOData routines for
     15:   manipulating simple parallel databases of grid (and related) information.
     16:   Note that this file automatically includes:
     17:      petsc.h       - base PETSc routines
     18:      petscsys.h    - system routines
     19:      petscis.h     - index sets
     20: */

     22:  #include petscao.h

     26: int main(int argc,char **argv)
     27: {
     28:   int            ierr,zero = 0,edited = 0;
     29:   size_t         bs;
     30:   char           filename[PETSC_MAX_PATH_LEN],string[256],*segname,*value,keyname[256],*ikeyname;
     31:   PetscViewer    binary;
     32:   AOData         aodata;
     33:   PetscTruth     keyexists,flag;

     35:   /* ---------------------------------------------------------------------
     36:        Initialize PETSc
     37:      --------------------------------------------------------------------- */

     39:   PetscInitialize(&argc,&argv,(char *)0,help);

     41:   /*
     42:       Load the grid database and initialize graphics
     43:   */
     44:   /*
     45:      Load in the grid database
     46:   */
     47:   PetscOptionsGetString(PETSC_NULL,"-f",filename,256,&flag);
     48:   if (!flag) SETERRQ(1,"Unable to open database, must run with: ex1 -f filename");
     49:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,PETSC_FILE_RDONLY,&binary);
     50:   AODataLoadBasic(binary,&aodata);
     51:   PetscViewerDestroy(binary);

     53:   PetscOptionsHasName(PETSC_NULL,"-d",&flag);
     54:   if (!flag) {
     55:     PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO);CHKERRQ(ierr)
     56:   }
     57:   AODataView(aodata,PETSC_VIEWER_STDOUT_WORLD);

     60:   /*
     61:        Allow user to add text keys to database
     62:   */
     63:   PetscOptionsHasName(PETSC_NULL,"-e",&flag);
     64:   if (flag) {
     65:     edited = 1;
     66:     printf("Enter keyname: (or return to end) ");
     67:     fgets(string, 256, stdin);
     68:     while (string[0] != 0) {
     69:       AODataKeyExists(aodata,string,&keyexists);
     70:       if (!keyexists) {
     71:         AODataKeyAdd(aodata,string,1,1);
     72:       }
     73:       PetscStrcpy(keyname,string);
     74:       printf("Enter segmentname: value (or return to end) ");
     75:       fgets(string, 256, stdin);
     76:       while (string[0] != 0) {
     77:         PetscToken *token;
     78:         PetscTokenCreate(string,' ',&token);
     79:         PetscTokenFind(token,&segname);
     80:         PetscTokenFind(token,&value);
     81:         PetscTokenDestroy(token);
     82:         PetscStrlen(value,&bs);
     83:         AODataSegmentAdd(aodata,keyname,segname,bs,1,&zero,value,PETSC_CHAR);
     84:         printf("Enter segmentname: value (or return to end) ");
     85:         fgets(string, 256, stdin);
     86:       }
     87:       printf("Enter keyname: (or return to end) ");
     88:       fgets(string, 256, stdin);
     89:     }
     90:     PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO);CHKERRQ(ierr)
     91:     AODataView(aodata,PETSC_VIEWER_STDOUT_WORLD);
     92:   }

     94:   /*
     95:       Allow user to remove keys and segements from database
     96:   */
     97:   PetscOptionsHasName(PETSC_NULL,"-r",&flag);
     98:   if (flag) {
     99:     edited = 1;
    100:     printf("Enter keyname to remove: (or return to end) ");
    101:     fgets(string, 256, stdin);
    102:     while (string[0] != 0) {
    103:       AODataKeyRemove(aodata,string);
    104:       printf("Enter keyname to remove: (or return to end) ");
    105:       fgets(string, 256, stdin);
    106:     }
    107:     printf("Enter keyname segment name to remove: (or return to end) ");
    108:     fgets(string, 256, stdin);
    109:     while (string[0] != 0) {
    110:       PetscToken *token;
    111:       PetscTokenCreate(string,' ',&token);
    112:       PetscTokenFind(token,&ikeyname);
    113:       PetscTokenFind(token,&segname);
    114:       PetscTokenDestroy(token);
    115:       AODataSegmentRemove(aodata,ikeyname,segname);
    116:       printf("Enter keyname segment name to remove: (or return to end) ");
    117:       fgets(string, 256, stdin);
    118:     }
    119:     PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO);CHKERRQ(ierr)
    120:     AODataView(aodata,PETSC_VIEWER_STDOUT_WORLD);
    121:   }

    123:   if (edited) {
    124:     PetscStrcat(filename,".new");
    125:     PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,PETSC_FILE_CREATE,&binary);
    126:     AODataView(aodata,binary);
    127:     PetscViewerDestroy(binary);
    128:   }

    130:   AODataDestroy(aodata);

    133:   PetscFinalize();

    135:   return(0);
    136: }