knotFIO.sa
Generated by gen_html_sa_files from ICSI. Contact gomes@icsi.berkeley.edu for details
-- 2000/08 K.Kodama GNU/Sather version
-- 1998/04 Knot Ver1 byte order free
-- 1996/10 Linux version Knot data ver.2
-- Copyright (C) 1996 Kouji KODAMA
class KNOTFIO
class KNOTFIO is
const Header:STR:="#_Knot_Data_Ver.2.0_1996_KDM_#";
WriteKnotS(Knot:KNOT, outS:FILE,
writeHeader,writeDate:BOOL, memo:STR):BOOL is
KnotStr:STR;
if ~ KNOT_STR::Knot2String(Knot,out KnotStr) then
#OUT+"\nCannot convert to text.\n";
return false;
end;
if writeHeader then outS+Header+"\n"; end;
if writeDate then outS+STRINGSK::TimeStr+"\n"; end;
outS+"#"+memo+"\n";
outS+"[KNOT]"+"\n";
outS+KnotStr+"\n";
if writeDate then outS+"#"+"\n"; end;
return true;
end ;
WriteKnotSLog( Knot:KNOT,
writeHeader,writeDate:BOOL, memo:STR):BOOL is
KnotStr:STR:=#;
LogOut:LOGOUT:=#;
if ~ KNOT_STR::Knot2String(Knot,out KnotStr) then
#OUT+"Cannot convert to text.\n";
return false;
end;
if writeHeader then LogOut+Header+"\n"; end;
if writeDate then LogOut+(STRINGSK::TimeStr)+"\n"; end;
LogOut+"#"+memo+"\n";
LogOut+"[KNOT]"+"\n";
LogOut+KnotStr+"\n";
if writeDate then LogOut+"#"+"\n"; end;
return true;
end;
WriteBraidS(Knot:KNOT, outS:FILE,
writeHeader,writeDate:BOOL, memo:STR):BOOL is
wordStr:STR:=#;
if ~ BRAID_CNV::Knot2BraidWord(Knot,out wordStr) then
#OUT+"Cannot convert to braid word.\n";
return false;
end;
if writeHeader then outS+Header+"\n"; end;
if writeDate then outS+STRINGSK::TimeStr+"\n"; end;
outS+"#"+memo+"\n";
outS+"[BRAID]""\n"+wordStr+"\n";
if writeDate then outS+"#"+"\n"; end;
return true;
end;
WriteBraidSLog(Knot:KNOT,
writeHeader,writeDate:BOOL, memo:STR):BOOL is
wordStr:STR:=#;
LogOut:LOGOUT:=#;
if ~ BRAID_CNV::Knot2BraidWord(Knot,out wordStr) then
#OUT+"Cannot convert to braid word.\n";
return false;
end;
if writeHeader then LogOut+Header+"\n"; end;
if writeDate then LogOut+(STRINGSK::TimeStr)+"\n"; end;
LogOut+"#"+memo+"\n";
LogOut+"[BRAID]"+"\n";
LogOut+wordStr+"\n";
if writeDate then LogOut+"#"+"\n"; end;
return true;
end;
WriteNDataS(Knot:KNOT, outS:FILE,
writeHeader,writeDate:BOOL, memo:STR):BOOL is
word:STR:="";
if ~ NDATA_CNV::Knot2NDataStr(Knot,out word) then
#OUT+"Cannot convert to braid word.\n";
return false;
end;
if writeHeader then outS+Header+"\n"; end;
if writeDate then outS+(STRINGSK::TimeStr)+"\n"; end;
outS+"#"+memo+"\n";
outS+"[NDATA]"+"\n";
outS+word+"\n";
if writeDate then outS+"#"+"\n"; end;
return true;
end;
WriteNDataSLog( Knot:KNOT,
writeHeader,writeDate:BOOL, memo:STR):BOOL is
word:STR:="";
LogOut:LOGOUT:=#;
if ~ NDATA_CNV::Knot2NDataStr(Knot, out word) then
#OUT+"Cannot convert to braid word.";
return false;
end;
if writeHeader then LogOut+"Header"+"\n"; end;
if writeDate then LogOut+(STRINGSK::TimeStr)+"\n"; end;
LogOut+"#"+memo+"\n";
LogOut+"[NDATA]"+"\n";
LogOut+word+"\n";
if writeDate then LogOut+"#"+"\n"; end;
return true;
end;
ReadKnotSKnot( out Knot:KNOT, inS:FILE, setData:BOOL):BOOL is
line,KnotStr:STR;
length,pos,wlength:INT;
-- #OUT+"ReadKNotSKnot: "+"\n";
Knot:=#; KnotStr:=""; wlength:=0;
loop -- read lines until 'endcode'
if inS.eof then return false; end;
line:=inS.get_str.remove('\n');
pos:=0; length:=line.length;
wlength:=wlength+length+1;
KnotStr:=KnotStr+line+" ";
if 0<=line.search("endcode",pos) then
if ~ setData then return true; end;
return KNOT_STR::String2Knot(KnotStr,out Knot);
end;
end;
end;
ReadKnotSBraid( out Knot:KNOT, inS:FILE, setData:BOOL):BOOL is
line,word:STR;
length,pos,wlength:INT;
Knot:=#; word:=""; wlength:=0;
loop -- read lines until 'endcode'
if inS.eof then return false; end;
line:=inS.get_str; pos:=0; length:=line.length;
wlength:=wlength+length+1;
word:=word+line+" ";
if 0<=line.search("endcode",pos) then
if ~ setData then return true; end;
return BRAID_CNV::BraidWord2Knot(word,inout Knot);
end;
end;
end;
ReadKnotSNData(out Knot:KNOT, inS:FILE, setData:BOOL):BOOL is
line,word:STR;
length,pos,wlength:INT;
Knot:=#;
Knot.NoCompo0; word:=""; wlength:=0;
loop -- read lines until 'endcode'
if inS.eof then return false; end;
line:=inS.get_str; pos:=0; length:=line.length;
wlength:=wlength+length+1;
word:=word+line+" ";
if 0<=line.search("endcode",pos) then
if ~ setData then return NDATA_CNV::NDataStr2Knot(word,inout Knot);
end;
end;
end;
end;
ReadKnotS(out Knot:KNOT, inS:FILE, checkHead:BOOL):BOOL is
line:STR;
length:INT;
reply:BOOL;
dataNum1:INT;
dataNum:INT:=1;
Knot:=#;
if checkHead then
line:=inS.get_str.remove('\n');
if line /= Header then
#OUT+"The file is not Knot_Data_Ver2.\n";
return false;
end;
end;
dataNum1:=0;
loop -- skip comment until [KNOT]/[BRAID]/[NDATA]
line:=inS.get_str.remove('\n');
if (line.length>0)and('#'=line[0]) then -- comment
elsif line="[KNOT]" then
dataNum1:=dataNum1+1;
reply:=ReadKnotSKnot(out Knot,inS,(dataNum=dataNum1));
if ~ reply then #OUT+"Bad code in Knot Data.\n"; end;
if (dataNum=dataNum1) then return reply; end;
elsif line="[BRAID]" then dataNum1:=dataNum1+1;
reply:=ReadKnotSBraid(out Knot,inS,(dataNum=dataNum1));
if ~ reply then #OUT+"Bad code in Braid word.\n"; end;
if (dataNum=dataNum1) then return reply; end;
elsif line="[NDATA]" then dataNum1:=dataNum1+1;
reply:=ReadKnotSNData(out Knot,inS,(dataNum=dataNum1));
if ~ reply then #OUT+"Bad code in NData.\n"; end;
if (dataNum=dataNum1) then return reply; end;
else -- bad file
#OUT+"File is not a Knot-Data. (unknown format)\n";
return false;
end;
end;
end;
-- The byte order of KnotDataVer1 is from low byte to high byte.
-- Note that "VERTEXC" is a type of BITSET.
-- And, order of bit set in Modula2 is not conpatible in C.
-- See definition module "KnotVar" .
ReadKnotSVer1( out Knot:KNOT, inS:FILE):BOOL is
ch1,ch2:CHAR;
x,y:INT;
code:VERTEXC;
#OUT+"Try loading Knot_Data_Ver1.\n";
Knot:=#; Knot.k:=#;
loop
if inS.eof then; break!; end;
ch1:=inS.get_char; ch2:=inS.get_char;
x:=ch1.int+(ch2.int-2*(ch2.int.band(128)))*256; -- As signed char.
if inS.eof then break!; end;
ch1:=inS.get_char; ch2:=inS.get_char;
y:=ch1.int+(ch2.int-2*(ch2.int.band(128)))*256; -- As signed char.
if inS.eof then break!; end;
ch1:=inS.get_char; ch2:=inS.get_char;
code:=#(ch1.int+(ch2.int)*256);
Knot.CodeIn(x, y, Knot.k.size);
Knot[Knot.length].sep:=code;
if code=VERTEXC::code_e then return true; end;
end;
Knot.NoCompo0; return false;
end;
WriteKnotSVer1(Knot:KNOT, outS:FILE):BOOL is
i:INT;
x,y,c:INT;
ch1,ch2:CHAR;
i:=0;
loop
x:=Knot[i].x;
y:=Knot[i].y;
c:=Knot[i].sep.int;
ch1:=x.band(255).char; ch2:=x.rshift(8).band(255).char;
outS+ch1; outS+ch2;
ch1:=y.band(255).char; ch2:=y.rshift(8).band(255).char;
outS+ch1; outS+ch2;
ch1:=c.band(255).char; ch2:=c.rshift(8).band(255).char;
outS+ch1; outS+ch2;
if VERTEXC::code_e = Knot[i].sep then return true; end;
i:=i+1;
end;
end;
WriteKnotN(Knot:KNOT, knotName:STR):BOOL is
outStream:FILE;
reply:BOOL;
if (Knot.length<=0) then
#OUT+"Knot diagram is bad. (WriteKnot)\n";
return false;
end;
-- if FILE::Exists(knotName) then ; end;
outStream:=FILE::open_for_write(knotName);
if outStream.error.not then
reply:=WriteKnotS(Knot,outStream,true,true,knotName);
outStream.flush; outStream.close;
if ~ reply then #OUT+"Failed in WriteKnot.\n"; end;
return reply;
else
outStream.clear; outStream.close;
#OUT+"File cannot open.\n";
return false;
end;
end;
WriteKnotNVer1(Knot:KNOT, knotName:STR):BOOL is
outStream:FILE;
reply:BOOL;
if (Knot.length<=0) then
#OUT+"Knot diagram is bad. (WriteKnot)\n";
return false;
end;
-- if KmsFIO.FExists(knotName) then ; end;
outStream:=FILE::open_for_write(knotName);
if outStream.error.not then
reply:=WriteKnotSVer1(Knot,outStream);
outStream.flush; outStream.close;
if ~ reply then #OUT+"Failed in WriteKnot.\n"; end;
return reply;
else
outStream.clear; outStream.close;
#OUT+"File cannot open.\n";
return false;
end;
end;
WriteKnotLog(Knot:KNOT):BOOL is
return WriteKnotSLog(Knot,true,true,"");
end;
WriteBraidN(Knot:KNOT, knotName:STR):BOOL is
outStream:FILE;
reply:BOOL;
if (Knot.length<=0) then
#OUT+"Knot diagram is bad. (WriteKnot)\n";
return false;
end;
-- if KmsFIO.FExists(knotName) then ; end;
outStream:=FILE::open_for_write(knotName);
if outStream.error.not then
reply:=WriteBraidS(Knot,outStream,true,true,knotName);
outStream.flush; outStream.close;
if ~ reply then #OUT+"Failed in WriteBraid.\n"; end;
return reply;
else
outStream.clear; outStream.close;
#OUT+"File cannot open.\n";
return false;
end;
end;
WriteBraidLog(Knot:KNOT):BOOL is
return WriteBraidSLog(Knot,true,true,"");
end;
WriteNDataN(Knot:KNOT, knotName:STR):BOOL is
outStream:FILE;
reply:BOOL;
if (Knot.length<=0) then
#OUT+"Knot diagram is bad.(WriteKnot)\n";
return false;
end;
-- if KmsFIO.FExists(knotName) then ; end;
outStream:=FILE::open_for_write(knotName);
if outStream.error.not then
reply:=WriteNDataS(Knot,outStream,true,true,knotName);
outStream.flush; outStream.close;
if ~ reply then #OUT+"Failed in WriteBraid.\n"; end;
return reply;
else
outStream.clear; outStream.close;
#OUT+"File cannot open.\n";
return false;
end;
end;
WriteNDataLog(Knot:KNOT):BOOL is
return WriteNDataSLog(Knot,true,true,"");
end;
-- ReadKnotNVer1(knotName:ARRAY OF CHAR):BOOL;
-- inout inStream:File;
-- reply:BOOL;
-- if ~ KmsFIO.FExists(knotName) then
-- ErrorMessage(knotName,' is not exist.');
-- return false;
-- end;
--inStream:=msFIO.OpenRead(knotName);
--if inStream/=NIL then
-- History.put;
-- reply:=ReadKnotSVer1(inStream);
-- msFIO.Close(inStream);
-- if ~ reply then History.back;
-- else UpdateCross:=true;
-- end;
-- SLength;
-- return reply;
--else
-- msFIO.ClearError(inStream);
-- msFIO.Close(inStream);
-- ErrorMessage('The file cannot open.','');
-- return false;
--end;
--return true;
--end ReadKnotNVer1;
ReadKnotNS(out Knot:KNOT, inStream:FILE):BOOL is
reply:BOOL;
pos:INT;
Knot:=#;
if inStream.error.not then
HISTORY::put(Knot);
pos:=inStream.current_loc;
reply:=ReadKnotS(out Knot,inStream,true);
if ~ reply then
inStream.seek_from_front(pos);
reply:=ReadKnotSVer1(out Knot,inStream);
end;
if ~ reply then HISTORY::back(inout Knot);
else Knot.updateCross:=true;
end;
if reply then Knot.shiftToInside; end;
return reply;
else return false;
end;
end;
ReadKnotN(out Knot:KNOT, knotName:STR):BOOL is
inStream:FILE;
reply:BOOL;
Knot:=#;
inStream:=FILE::open_for_read(knotName);
if inStream.error.not then
reply:=ReadKnotNS(out Knot,inStream);
inStream.close;
return reply;
else
inStream.clear; inStream.close;
#OUT+knotName+" file cannot open.\n";
return false;
end;
return true;
end;
end;