Skip to content

Commit

Permalink
add NO_ZMAT flag, fix fread issue
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Oct 27, 2023
1 parent ce3c0a0 commit 4bf8232
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Contents.m
Expand Up @@ -34,7 +34,7 @@
% match_bracket - [endpos, maxlevel] = match_bracket(str,startpos,brackets)
% mergestruct - s=mergestruct(s1,s2)
% nestbracket2dim - [dims, isndarray, maxlevel, count] = nestbracket2dim(str,brackets)
% octavezz - output = octavezz(input, iscompress, zipmethod)
% octavezmat - output = octavezmat(input, iscompress, zipmethod)
% savebj - bjd=savebj(obj)
% savejd - savejd(rootname, obj, outputfile)
% savejson - json=savejson(obj)
Expand Down
2 changes: 1 addition & 1 deletion INDEX
Expand Up @@ -37,7 +37,7 @@ Compression and Decompression
lzmaencode
zlibdecode
zlibencode
octavezz
octavezmat
Helper Functions
decodevarname
encodevarname
Expand Down
3 changes: 1 addition & 2 deletions gzipdecode.m
Expand Up @@ -44,8 +44,7 @@
end
return;
elseif(isoctavemesh)
warning('You are recommended to install the ZMat toolbox (http://github.com/NeuroJSON/zmat) to use this function in Octave');
[varargout{1:nargout}]=octavezz(varargin{1}, 0, 'gzip');
[varargout{1:nargout}]=octavezmat(varargin{1}, 0, 'gzip');
return;
end
error(javachk('jvm'));
Expand Down
3 changes: 1 addition & 2 deletions gzipencode.m
Expand Up @@ -40,8 +40,7 @@
[varargout{1:nargout}]=zmat(varargin{1},1,'gzip');
return;
elseif(isoctavemesh)
warning('You are recommended to install the ZMat toolbox (http://github.com/NeuroJSON/zmat) to use this function in Octave');
[varargout{1:nargout}]=octavezz(varargin{1}, 1, 'gzip');
[varargout{1:nargout}]=octavezmat(varargin{1}, 1, 'gzip');
return;
end

Expand Down
47 changes: 36 additions & 11 deletions octavezz.m → octavezmat.m
@@ -1,14 +1,14 @@
function varargout=octavezz(data, iscompress, zipmethod)
function varargout=octavezmat(data, iscompress, zipmethod)
%
% output = octavezz(input, iscompress, zipmethod)
% output = octavezmat(input, iscompress, zipmethod)
% or
% [output, info] = octavezz(input, iscompress, zipmethod)
% unzipdata = octavezz(zipdata, info)
% [output, info] = octavezmat(input, iscompress, zipmethod)
% unzipdata = octavezmat(zipdata, info)
%
% Compress or decompress zlib and gzip memory buffers using zip/unzip/gzip/gunzip on Octave
% in case ZMat toolbox (http://github.com/NeuroJSON/zmat) was not installed (ZMat will be much faster)
% in case ZMat toolbox (http://github.com/NeuroJSON/zmat) was not installed (ZMat is much faster)
%
% Copyright (c) 2023, Qianqian Fang (q.fang <at> neu.edu)
% Author: Qianqian Fang (q.fang <at> neu.edu)
%
% input:
% input: the input data (can be either compressed or before compression),
Expand Down Expand Up @@ -46,17 +46,24 @@
% level, see above
%
% examples:
% [ss,info]=octavezz(ones(10))
% orig=octavezz(ss,info)
% NO_ZMAT=1 % by setting this flag to 1 in the caller or global workspace, octavezmat won't warn zmat is missing
% [ss,info]=octavezmat(ones(10))
% orig=octavezmat(ss,info)
%
% license:
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
%
% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
%

nowarning = getvarfrom({'caller', 'base'},'NO_ZMAT');

if(isempty(nowarning) || nowarning == 0)
warning('You are recommended to install ZMat (http://github.com/NeuroJSON/zmat) get much faster speed in Octave');
end

if(nargin < 1)
fprintf(1,'Format: output = octavezz(data, iscompress, zipmethod)\n');
fprintf(1,'Format: output = octavezmat(data, iscompress, zipmethod)\n');
return;
end

Expand All @@ -73,6 +80,14 @@
iscompress = 0;
end

if (~(ischar(data) || islogical(data) || (isnumeric(data) && isreal(data))))
error('input must be a char, non-complex numeric or logical vector or N-D array');
end

if (ischar(data))
data = uint8(data);
end

fname=tempname;
tmpfile=fname;
outputfile=fname;
Expand All @@ -91,7 +106,8 @@
if(~fd)
error('unable to create temporary file');
end
fwrite(fd, data, 'uint8');

fwrite(fd, typecast(data(:), 'uint8'), 'uint8');
fclose(fd);

if(iscompress)
Expand All @@ -113,13 +129,22 @@
end
end

if(exist(tmpfile, 'file'))
delete(tmpfile);
end

fd=fopen(outputfile, 'rb');
if(~fd)
error('failed to unzip buffer');
end
varargout{1}=fread(fd, [1 inf], 'uint8');
varargout{1}=fread(fd, [1 inf], 'uint8=>uint8');
fclose(fd);


if(exist(outputfile, 'file'))
delete(outputfile);
end

if(nargout>1)
varargout{2}=struct('type',class(data),'size',size(data),'method',zipmethod,'status',0, 'level', iscompress);
end
Expand Down
4 changes: 2 additions & 2 deletions zlibdecode.m
Expand Up @@ -45,10 +45,10 @@
end
return;
elseif(isoctavemesh)
warning('You are recommended to install the ZMat toolbox (http://github.com/NeuroJSON/zmat) to use this function in Octave');
[varargout{1:nargout}]=octavezz(varargin{1}, 0, 'zlib');
[varargout{1:nargout}]=octavezmat(varargin{1}, 0, 'zlib');
return;
end

error(javachk('jvm'));

if(ischar(varargin{1}))
Expand Down
3 changes: 1 addition & 2 deletions zlibencode.m
Expand Up @@ -39,8 +39,7 @@
[varargout{1:nargout}]=zmat(varargin{1},1,'zlib');
return;
elseif(isoctavemesh)
warning('You are recommended to install the ZMat toolbox (http://github.com/NeuroJSON/zmat) to use this function in Octave');
[varargout{1:nargout}]=octavezz(varargin{1}, 1, 'zlib');
[varargout{1:nargout}]=octavezmat(varargin{1}, 1, 'zlib');
return;
end

Expand Down

0 comments on commit 4bf8232

Please sign in to comment.