Date: 2021-03-03T22:28:22+00:00

Actual source code: PetscBagRead.m
petsc-3.14.5 2021-03-03
Report Typos and Errors

      1: function bag = PetscBagRead(fd)
      2: %
      3: %  Reads in PETSc binary file bag object
      4: %  emits as Matlab struct.  Called from
      5: %  PetscBinaryRead.m.
      6: %

      8: [name_len help_len] = ParsePetscBagDotH;

     10: bagsize = read(fd,1,'int32');  %  no longer used after petsc-3.2 just here for backward compatibility of the binary files
     11: count = read(fd,1,'int32');

     13: bag.bag_name      = deblank(char(read(fd,name_len,'uchar')'));
     14: bag.help.bag_help = deblank(char(read(fd,help_len,'uchar')'));

     16: for lcv = 1:count
     17:   offsetdtype = read(fd,2,'int32');
     18:   dtype = offsetdtype(2);
     19:   name  = strclean(deblank(char(read(fd,name_len,'uchar')')));
     20:   help  = deblank(char(read(fd,help_len,'uchar')'));
     21:   msize = read(fd,1,'int32');

     23:   if dtype == 16     % integer
     24:     val = read(fd,msize,'int32');
     25:   elseif dtype == 1 % double
     26:     val = read(fd,msize,'double');
     27:   elseif dtype == 6 % char
     28:     val = deblank(char(read(fd,msize,'uchar')'));
     29:   elseif dtype == 9 % truth
     30:     val = read(fd,1,'int32');
     31: % PETSC_LOGICAL is a bit boolean and not currently handled
     32: %  elseif dtype == 7 % boolean
     33: %    val = read(fd,1,'bit1');
     34:   elseif dtype == 8 % Enum
     35:     val   = read(fd,1,'int32');
     36:     n     = read(fd,1,'int32');
     37:     sizes = read(fd,n,'int32');
     38:     enumnames = {'  '};
     39:     for i=1:n-2,
     40:       enumnames{i} = deblank(char(read(fd,sizes(i),'uchar')));
     41:     end
     42:     val  = char(enumnames{val+1})';
     43:     enumname   = deblank(char(read(fd,sizes(n-1),'uchar')));
     44:     enumprefix = deblank(char(read(fd,sizes(n),'uchar')));
     45:   else
     46:     val = [];
     47:     warning('Bag entry %s could not be read',name);
     48:   end
     49:   bag      = setfield(bag     ,name,val);
     50:   bag.help = setfield(bag.help,name,help);
     51: end
     52: return

     54: % ---------------------------------------------------- %
     55:
     56: function [n, h] = ParsePetscBagDotH
     57:
     58:    petscbagh = [GetPetscDir,'/include/petsc/private/bagimpl.h'];
     59:    fid = fopen(petscbagh,'rt');
     60:    if (fid<0)
     61:       errstr = sprintf('Could not open %s.',petscbagh);
     62:       error(errstr);
     63:    end
     64:
     65:    nametag = '#define PETSC_BAG_NAME_LENGTH';
     66:    helptag = '#define PETSC_BAG_HELP_LENGTH';
     67:    n = 0; h = 0;
     68:    while ~feof(fid)
     69:       lin = fgetl(fid);
     70:       ni = strfind(lin,nametag);
     71:       nh = strfind(lin,helptag);
     72:       if ni
     73:          n = str2num(lin(ni+length(nametag):end));
     74:       elseif nh
     75:          h = str2num(lin(nh+length(helptag):end));
     76:       end
     77:       if (n>0 & h>0) break; end;
     78:    end
     79:    if (n==0 | h==0)
     80:       errstr = sprintf('Could not parse %s.',petscbagh);
     81:       error(errstr);
     82:    end
     83:    fclose(fid);
     84:    return
     85:
     86: % ---------------------------------------------------- %
     87:
     88: function str = strclean(str)
     89:
     90:    badchars = ' ()[]<>{}.-';
     91:    for i=1:length(badchars);
     92:       str(strfind(str,badchars(i))) = '_';
     93:    end
     94:    return
     95:
     96: % ---------------------------------------------------- %
     97:
     98: function dir = GetPetscDir
     99:
    100:    dir = getenv('PETSC_DIR');
    101:    if length(dir)==0
    102:       error(['Please set environment variable PETSC_DIR' ...
    103:              ' and try again.'])
    104:    end
    105:    return