add NO_ZMAT flag, fix fread issue · NeuroJSON/jsonlab@4bf8232 · GitHub
Skip to content

Commit 4bf8232

Browse files
committed
add NO_ZMAT flag, fix fread issue
1 parent ce3c0a0 commit 4bf8232

7 files changed

Lines changed: 43 additions & 21 deletions

File tree

‎Contents.m‎

Lines changed: 1 addition & 1 deletion

‎INDEX‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Compression and Decompression
3737
lzmaencode
3838
zlibdecode
3939
zlibencode
40-
octavezz
40+
octavezmat
4141
Helper Functions
4242
decodevarname
4343
encodevarname

‎gzipdecode.m‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@
4444
end
4545
return;
4646
elseif(isoctavemesh)
47-
warning('You are recommended to install the ZMat toolbox (http://github.com/NeuroJSON/zmat) to use this function in Octave');
48-
[varargout{1:nargout}]=octavezz(varargin{1}, 0, 'gzip');
47+
[varargout{1:nargout}]=octavezmat(varargin{1}, 0, 'gzip');
4948
return;
5049
end
5150
error(javachk('jvm'));

‎gzipencode.m‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
[varargout{1:nargout}]=zmat(varargin{1},1,'gzip');
4141
return;
4242
elseif(isoctavemesh)
43-
warning('You are recommended to install the ZMat toolbox (http://github.com/NeuroJSON/zmat) to use this function in Octave');
44-
[varargout{1:nargout}]=octavezz(varargin{1}, 1, 'gzip');
43+
[varargout{1:nargout}]=octavezmat(varargin{1}, 1, 'gzip');
4544
return;
4645
end
4746

octavezz.m renamed to octavezmat.m

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
function varargout=octavezz(data, iscompress, zipmethod)
1+
function varargout=octavezmat(data, iscompress, zipmethod)
22
%
3-
% output = octavezz(input, iscompress, zipmethod)
3+
% output = octavezmat(input, iscompress, zipmethod)
44
% or
5-
% [output, info] = octavezz(input, iscompress, zipmethod)
6-
% unzipdata = octavezz(zipdata, info)
5+
% [output, info] = octavezmat(input, iscompress, zipmethod)
6+
% unzipdata = octavezmat(zipdata, info)
77
%
88
% Compress or decompress zlib and gzip memory buffers using zip/unzip/gzip/gunzip on Octave
9-
% in case ZMat toolbox (http://github.com/NeuroJSON/zmat) was not installed (ZMat will be much faster)
9+
% in case ZMat toolbox (http://github.com/NeuroJSON/zmat) was not installed (ZMat is much faster)
1010
%
11-
% Copyright (c) 2023, Qianqian Fang (q.fang <at> neu.edu)
11+
% Author: Qianqian Fang (q.fang <at> neu.edu)
1212
%
1313
% input:
1414
% input: the input data (can be either compressed or before compression),
@@ -46,17 +46,24 @@
4646
% level, see above
4747
%
4848
% examples:
49-
% [ss,info]=octavezz(ones(10))
50-
% orig=octavezz(ss,info)
49+
% NO_ZMAT=1 % by setting this flag to 1 in the caller or global workspace, octavezmat won't warn zmat is missing
50+
% [ss,info]=octavezmat(ones(10))
51+
% orig=octavezmat(ss,info)
5152
%
5253
% license:
5354
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
5455
%
5556
% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
5657
%
5758

59+
nowarning = getvarfrom({'caller', 'base'},'NO_ZMAT');
60+
61+
if(isempty(nowarning) || nowarning == 0)
62+
warning('You are recommended to install ZMat (http://github.com/NeuroJSON/zmat) get much faster speed in Octave');
63+
end
64+
5865
if(nargin < 1)
59-
fprintf(1,'Format: output = octavezz(data, iscompress, zipmethod)\n');
66+
fprintf(1,'Format: output = octavezmat(data, iscompress, zipmethod)\n');
6067
return;
6168
end
6269

@@ -73,6 +80,14 @@
7380
iscompress = 0;
7481
end
7582

83+
if (~(ischar(data) || islogical(data) || (isnumeric(data) && isreal(data))))
84+
error('input must be a char, non-complex numeric or logical vector or N-D array');
85+
end
86+
87+
if (ischar(data))
88+
data = uint8(data);
89+
end
90+
7691
fname=tempname;
7792
tmpfile=fname;
7893
outputfile=fname;
@@ -91,7 +106,8 @@
91106
if(~fd)
92107
error('unable to create temporary file');
93108
end
94-
fwrite(fd, data, 'uint8');
109+
110+
fwrite(fd, typecast(data(:), 'uint8'), 'uint8');
95111
fclose(fd);
96112

97113
if(iscompress)
@@ -113,13 +129,22 @@
113129
end
114130
end
115131

132+
if(exist(tmpfile, 'file'))
133+
delete(tmpfile);
134+
end
135+
116136
fd=fopen(outputfile, 'rb');
117137
if(~fd)
118138
error('failed to unzip buffer');
119139
end
120-
varargout{1}=fread(fd, [1 inf], 'uint8');
140+
varargout{1}=fread(fd, [1 inf], 'uint8=>uint8');
121141
fclose(fd);
122142

143+
144+
if(exist(outputfile, 'file'))
145+
delete(outputfile);
146+
end
147+
123148
if(nargout>1)
124149
varargout{2}=struct('type',class(data),'size',size(data),'method',zipmethod,'status',0, 'level', iscompress);
125150
end

‎zlibdecode.m‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@
4545
end
4646
return;
4747
elseif(isoctavemesh)
48-
warning('You are recommended to install the ZMat toolbox (http://github.com/NeuroJSON/zmat) to use this function in Octave');
49-
[varargout{1:nargout}]=octavezz(varargin{1}, 0, 'zlib');
48+
[varargout{1:nargout}]=octavezmat(varargin{1}, 0, 'zlib');
5049
return;
5150
end
51+
5252
error(javachk('jvm'));
5353

5454
if(ischar(varargin{1}))

‎zlibencode.m‎

Lines changed: 1 addition & 2 deletions

0 commit comments

Comments
 (0)