% [BLOCK,DATA]=BLOCK_MAP(BUFFER,DELIN) 
%
% BLOCK_MAP() determines the data blocks inside of an
% arbitrary ASCII data buffer named BUFFER and maps each
% entry separated by space into a text type or a number type.
% It is assumed that numerical data is separated by a char
% deliniator DELIN (e.g. DELIN=' ').
%
% All the numerical data is saved into a DATA array. The
% type map of a file is saved in BLOCK array. 

function [block,data]=block_map(buffer,delin);

% ---------------------------------------------------------
%         Mapping the type structure of the file
% ---------------------------------------------------------
xbs=size(buffer,2);
ybs=size(buffer,1);
carray=sparse(0);wc=0;block=sparse(0);
for linec=1:ybs
    i=1;flag=0;wwc=0;fdelin=0;
	while(i~=(xbs+1))
		if (buffer(linec,i)==0)
			break;
		end
	
%--------------------------------------------------------------------------
		
        flagnum=0;	
		
		if ((i>2)&(i<(xbs-1)))
			if (((buffer(linec,i)<=57)&(buffer(linec,i)>=48))|...
				(((buffer(linec,i)==43)|(buffer(linec,i)==45))&((buffer(linec,i+1)<=57)&(buffer(linec,i+1)>=48)))|...
				(((buffer(linec,i)==43)|(buffer(linec,i)==45))&((buffer(linec,i+1)==46)&((buffer(linec,i+2)<=57)&(buffer(linec,i+2)>=48)))|...
				((buffer(linec,i)==46)&((buffer(linec,i+1)<=57)&(buffer(linec,i+1)>=48)))|...
				((buffer(linec,i)==46)&((buffer(linec,i-1)<=57)&(buffer(linec,i-1)>=48)))|...
				(((buffer(linec,i)==101)|(buffer(linec,i)==69))&((buffer(linec,i-1)<=57)&(buffer(linec,i-1)>=48))&((buffer(linec,i+1)<=57)&(buffer(linec,i+1)>=48)))|...
				(((buffer(linec,i)==101)|(buffer(linec,i)==69))&((buffer(linec,i-2)<=57)&(buffer(linec,i-2)>=48))&(buffer(linec,i-1)==46)&((buffer(linec,i+1)<=57)&(buffer(linec,i+1)>=48)))|...
				(((buffer(linec,i)==101)|(buffer(linec,i)==69))&((buffer(linec,i-2)<=57)&(buffer(linec,i-2)>=48))&(buffer(linec,i-1)==46)&((buffer(linec,i+1)==43)|(buffer(linec,i+1)==45))&((buffer(linec,i+2)<=57)&(buffer(linec,i+2)>=48)))|...
				(((buffer(linec,i)==101)|(buffer(linec,i)==69))&((buffer(linec,i-1)<=57)&(buffer(linec,i-1)>=48))&((buffer(linec,i+1)==43)|(buffer(linec,i+1)==45))&((buffer(linec,i+2)<=57)&(buffer(linec,i+2)>=48)))))
				flagnum=1;
			end
		end
	
		if ((i==2)&(xbs>3))
			if (((buffer(linec,i)<=57)&(buffer(linec,i)>=48))|...
				(((buffer(linec,i)==43)|(buffer(linec,i)==45))&((buffer(linec,i+1)<=57)&(buffer(linec,i+1)>=48)))|...
				(((buffer(linec,i)==43)|(buffer(linec,i)==45))&((buffer(linec,i+1)==46)&((buffer(linec,i+2)<=57)&(buffer(linec,i+2)>=48)))|...
				((buffer(linec,i)==46)&((buffer(linec,i+1)<=57)&(buffer(linec,i+1)>=48)))|...
				((buffer(linec,i)==46)&((buffer(linec,i-1)<=57)&(buffer(linec,i-1)>=48)))|...
				(((buffer(linec,i)==101)|(buffer(linec,i)==69))&((buffer(linec,i-1)<=57)&(buffer(linec,i-1)>=48))&((buffer(linec,i+1)<=57)&(buffer(linec,i+1)>=48)))|...
				(((buffer(linec,i)==101)|(buffer(linec,i)==69))&((buffer(linec,i-1)<=57)&(buffer(linec,i-1)>=48))&((buffer(linec,i+1)==43)|(buffer(linec,i+1)==45))&((buffer(linec,i+2)<=57)&(buffer(linec,i+2)>=48)))))
				flagnum=1;
			end
		end
	
		if ((i==2)&(xbs==3))
			if (((buffer(linec,i)<=57)&(buffer(linec,i)>=48))|...
				(((buffer(linec,i)==43)|(buffer(linec,i)==45))&((buffer(linec,i+1)<=57)&(buffer(linec,i+1)>=48)))|...
				((buffer(linec,i)==46)&((buffer(linec,i+1)<=57)&(buffer(linec,i+1)>=48)))|...
				((buffer(linec,i)==46)&((buffer(linec,i-1)<=57)&(buffer(linec,i-1)>=48)))|...
				(((buffer(linec,i)==101)|(buffer(linec,i)==69))&((buffer(linec,i-1)<=57)&(buffer(linec,i-1)>=48))&((buffer(linec,i+1)<=57)&(buffer(linec,i+1)>=48))))
				flagnum=1;
			end
		end		
	
		if ((i==2)&(xbs==2))
			if (((buffer(linec,i)<=57)&(buffer(linec,i)>=48))|...
				((buffer(linec,i)==46)&((buffer(linec,i-1)<=57)&(buffer(linec,i-1)>=48))))
				flagnum=1;
			end
		end	
		
		if ((i==(xbs-1))&(xbs>3))
			if (((buffer(linec,i)<=57)&(buffer(linec,i)>=48))|...
				(((buffer(linec,i)==43)|(buffer(linec,i)==45))&((buffer(linec,i+1)<=57)&(buffer(linec,i+1)>=48)))|...
				((buffer(linec,i)==46)&((buffer(linec,i+1)<=57)&(buffer(linec,i+1)>=48)))|...
				((buffer(linec,i)==46)&((buffer(linec,i-1)<=57)&(buffer(linec,i-1)>=48)))|...
				(((buffer(linec,i)==101)|(buffer(linec,i)==69))&((buffer(linec,i-1)<=57)&(buffer(linec,i-1)>=48))&((buffer(linec,i+1)<=57)&(buffer(linec,i+1)>=48)))|...
				(((buffer(linec,i)==101)|(buffer(linec,i)==69))&((buffer(linec,i-2)<=57)&(buffer(linec,i-2)>=48))&(buffer(linec,i-1)==46)&((buffer(linec,i+1)<=57)&(buffer(linec,i+1)>=48))))
				flagnum=1;
			end
		end
		
		if ((i==1)&(xbs>2))
			if (((buffer(linec,i)<=57)&(buffer(linec,i)>=48))|...
				(((buffer(linec,i)==43)|(buffer(linec,i)==45))&((buffer(linec,i+1)<=57)&(buffer(linec,i+1)>=48)))|...
				(((buffer(linec,i)==43)|(buffer(linec,i)==45))&((buffer(linec,i+1)==46)&((buffer(linec,i+2)<=57)&(buffer(linec,i+2)>=48)))|...
				((buffer(linec,i)==46)&((buffer(linec,i+1)<=57)&(buffer(linec,i+1)>=48)))))
				flagnum=1;
			end
		end
	
		if ((i==1)&(xbs==2))
			if (((buffer(linec,i)<=57)&(buffer(linec,i)>=48))|...
				(((buffer(linec,i)==43)|(buffer(linec,i)==45))&((buffer(linec,i+1)<=57)&(buffer(linec,i+1)>=48)))|...
				((buffer(linec,i)==46)&((buffer(linec,i+1)<=57)&(buffer(linec,i+1)>=48))))
				flagnum=1;
			end
		end
	
		if ((i==1)&(xbs==1))
			if (((buffer(linec,i)<=57)&(buffer(linec,i)>=48)))
				flagnum=1;
			end
		end		
		
		if ((i==xbs)&(xbs>2))
			if (((buffer(linec,i)<=57)&(buffer(linec,i)>=48))|...
				((buffer(linec,i)==46)&((buffer(linec,i-1)<=57)&(buffer(linec,i-1)>=48))))
				flagnum=1;
			end
		end
		
%--------------------------------------------------------------------------
		
		if (flagnum==1)
			if (flag==1)
				ac=ac+1;
				carray(wc,ac)=buffer(linec,i);
			else
				flag=1;
				ac=1;
				wc=wc+1;
				carray(wc,ac)=buffer(linec,i);
				wwc=wwc+1;
				block(linec,wwc)=1;
			end
		else
			if ((flag==-1)&(fdelin==0))
				ac=ac+1;
				carray(wc,ac)=buffer(linec,i);
			else
				if (char(buffer(linec,i))~=delin)
					fdelin=0;
					flag=-1;
					ac=1;
					wc=wc+1;
					carray(wc,ac)=buffer(linec,i);	
					wwc=wwc+1;
					block(linec,wwc)=-1;		
				else
					flag=-1;fdelin=1;
				end
			end
		end
	i=i+1;
	end
end

xs=size(block,2);
ys=size(block,1);
count=0;dcount=0;data=[];
for iy=1:ys
	for ix=1:xs
		if (block(iy,ix)~=0)
			count=count+1;
			numb=str2num(char(full(carray(count,:))));
			if (isempty(numb))
				block(iy,ix)=-1;
			else
				dcount=dcount+1;
				data(dcount)=numb;
			end	
		else
			break;
		end
	end
end
% ---------------------------------------------------------

