xref: /petsc/share/petsc/matlab/PetscBagRead.m (revision 7c6f9176db5c4da645a58ce748da585ef5af9aaf)
1function bag = PetscBagRead(fd)
2%
3%  Reads in PETSc binary file bag object
4%  emits as MATLAB struct.  Called from
5%  PetscBinaryRead.m.
6%
7
8[name_len help_len] = ParsePetscBagDotH;
9
10bagsize = read(fd,1,'int32');  %  no longer used after petsc-3.2 just here for backward compatibility of the binary files
11count = read(fd,1,'int32');
12
13bag.bag_name      = deblank(char(read(fd,name_len,'uchar')'));
14bag.help.bag_help = deblank(char(read(fd,help_len,'uchar')'));
15
16for 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');
22
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);
51end
52return
53
54% ---------------------------------------------------- %
55
56function [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
88function 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
98function 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
106