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 HeaderV2:STR:="#_Knot_Data_Ver.2.0_1996_KDM_#";
WriteKnotS(Knot:KNOT, outS:$OSTREAM, 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+HeaderV2+"\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;
WriteBraidS(Knot:KNOT, outS:$OSTREAM,writeHeader,writeDate:BOOL, memo:STR):BOOL is
return WriteBraidS(Knot, outS,writeHeader,writeDate, memo, false,false);
end;
WriteBraidS(Knot:KNOT, outS:$OSTREAM,writeHeader,writeDate:BOOL, memo:STR, artin,reduced:BOOL):BOOL is
wordStr:STR:=#;
if ~ BRAID_CNV::Knot2BraidWord(Knot,out wordStr) then
#OUT+"Cannot convert to braid word.\n";
return false;
end;
if artin then
b:BRAID:=#(wordStr);
wordStr:=ARTIN_FORM::GArtinNormalForm(b,reduced).str;
end;
if writeHeader then outS+HeaderV2+"\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;
WriteNDataS(Knot:KNOT, outS:$OSTREAM, 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+HeaderV2+"\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;
ReadKnotSKnot( out Knot:KNOT, inS:INSTREAM, setData:BOOL):BOOL is
line,KnotStr:STR;
length:CARD;
pos:CARD;
wlength:CARD;
-- #OUT+"ReadKNotSKnot: "+"\n";
Knot:=#; Knot.NoCompo0; 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.size;
wlength:=wlength+length+1;
KnotStr:=KnotStr+line+" ";
if line.search("endcode",pos)<length then
if ~ setData then return true; end;
return KNOT_STR::String2Knot(KnotStr,out Knot);
end;
end;
end;
ReadKnotSBraid( out Knot:KNOT, inS:INSTREAM, setData:BOOL):BOOL is
line,word:STR;
length,pos,wlength:CARD;
Knot:=#; Knot.NoCompo0; word:=""; wlength:=0;
loop -- read lines until 'endcode'
if inS.eof then return false; end;
line:=inS.get_str.remove('\n'); pos:=0; length:=line.size;
wlength:=wlength+length+1;
word:=word+line+" ";
if line.search("endcode",pos)<length then
if ~ setData then return true; end;
return BRAID_CNV::BraidWord2Knot(word,inout Knot,false,true);
end;
end;
end;
ReadKnotSNData(out Knot:KNOT, inS:INSTREAM, setData:BOOL):BOOL is
line,word:STR;
length,pos,wlength:CARD;
Knot:=#; Knot.NoCompo0; word:=""; wlength:=0;
loop -- read lines until 'endcode'
if inS.eof then return false; end;
line:=inS.get_str.remove('\n'); pos:=0; length:=line.size;
wlength:=wlength+length+1;
word:=word+line+" ";
if line.search("endcode",pos)<length then
if ~ setData then return true; end;
return NDATA_CNV::NDataStr2Knot(word,inout Knot);
end;
end;
end;
ReadKnotSTorus(out Knot:KNOT, inS:INSTREAM, setData:BOOL):BOOL is
line,word:STR;
length,pos,wlength:CARD;
Knot:=#;
Knot.NoCompo0; word:=""; wlength:=0;
loop -- read lines until 'endcode'
if inS.eof then return false; end;
line:=inS.get_str.remove('\n'); pos:=0; length:=line.size;
wlength:=wlength+length+1;
word:=word+line+" ";
if line.search("endcode",pos)<length then
if ~ setData then return true; end;
return TORUS_CNV::Torus2Knot(word,inout Knot);
end;
end;
end;
ReadKnotSTwoBridge(out Knot:KNOT, inS:INSTREAM, setData:BOOL):BOOL is
line,word:STR;
length,pos,wlength:CARD;
Knot:=#; Knot.NoCompo0; word:=""; wlength:=0;
loop -- read lines until 'endcode'
if inS.eof then return false; end;
line:=inS.get_str.remove('\n'); pos:=0; length:=line.size;
wlength:=wlength+length+1;
word:=word+line+" ";
if line.search("endcode",pos)<length then
if ~ setData then return true; end;
return TWOBRIDGE_CNV::TwoBridge2Knot(word,inout Knot);
end;
end;
end;
ReadKnotSPretzel(out Knot:KNOT, inS:INSTREAM, setData:BOOL):BOOL is
line,word:STR;
length,pos,wlength:CARD;
Knot:=#; Knot.NoCompo0; word:=""; wlength:=0;
loop -- read lines until 'endcode'
if inS.eof then return false; end;
line:=inS.get_str.remove('\n'); pos:=0; length:=line.size;
wlength:=wlength+length+1;
word:=word+line+" ";
if line.search("endcode",pos)<length then
if ~ setData then return true; end;
return PRETZEL_CNV::Pretzel2Knot(word,inout Knot);
end;
end;
end;
ReadKnotS(out Knot:KNOT, inS:INSTREAM, 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 /= HeaderV2 then
#OUT+"The file is not Knot_Data_Ver2.\n";
return false;
end;
end;
dataNum1:=0;
loop -- skip comment until [KNOT]/[BRAID]/[NDATA]/[TORUS]/[TWOBRIDGE]
line:=inS.get_str.remove('\n');
if (line.size>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;
elsif line="[TORUS]" then dataNum1:=dataNum1+1;
reply:=ReadKnotSTorus(out Knot,inS,(dataNum=dataNum1));
if ~ reply then #OUT+"Bad code in Torus.\n"; end;
if (dataNum=dataNum1) then return reply; end;
elsif line="[TWOBRIDGE]" then dataNum1:=dataNum1+1;
reply:=ReadKnotSTwoBridge(out Knot,inS,(dataNum=dataNum1));
if ~ reply then #OUT+"Bad code in Two Bridge.\n"; end;
if (dataNum=dataNum1) then return reply; end;
elsif line="[PRETZEL]" then dataNum1:=dataNum1+1;
reply:=ReadKnotSPretzel(out Knot,inS,(dataNum=dataNum1));
if ~ reply then #OUT+"Bad code in Pretzel knot.\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;
ReadKnotSVer1( out Knot:KNOT, inS:INSTREAM):BOOL is
-- Each value of KnotDataVer1 is short(2 byte) small endian.
ch1,ch2:OCTET;
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_octet; ch2:=inS.get_octet;
x:=ch1.int+(ch2.int-(ch2.bit_and(#OCTET(128)).int)*2)*256; -- As signed char.
if inS.eof then break!; end;
ch1:=inS.get_octet; ch2:=inS.get_octet;
y:=ch1.int+(ch2.int-(ch2.bit_and(#OCTET(128)).int)*2)*256; -- As signed char.
if inS.eof then break!; end;
ch1:=inS.get_octet; ch2:=inS.get_octet;
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:$OSTREAM):BOOL is
-- Each value of KnotDataVer1 is short(2 byte) small endian.
v:NUM_BITS;
ch1,ch2:CHAR;
loop
i::=0.up!;
v:=#(Knot[i].x);
ch1:=v.bit_and(#(255)).char; ch2:=v.right(8).bit_and(#(255)).char; outS+ch1.str+ch2.str;
v:=#(Knot[i].y);
ch1:=v.bit_and(#(255)).char; ch2:=v.right(8).bit_and(#(255)).char; outS+ch1.str+ch2.str;
v:=#(Knot[i].sep.card);
ch1:=v.bit_and(#(255)).char; ch2:=v.right(8).bit_and(#(255)).char; outS+ch1.str+ch2.str;
if VERTEXC::code_e = Knot[i].sep then return true; end;
end;
end;
WriteKnotN(Knot:KNOT, knotName:STR):BOOL is
if (Knot.length<=0) then
#OUT+"Knot diagram is bad. (WriteKnot)\n"; return false;
end;
-- if FILE::Exists(knotName) then ; end;
outStream::=#OUTSTREAM(knotName); -- TEXT_FILE::open_for_write(knotName);
--outStream::=TEXT_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
if (Knot.length<=0) then
#OUT+"Knot diagram is bad. (WriteKnot)\n"; return false;
end;
-- if KmsFIO.FExists(knotName) then ; end;
outStream::=#OUTSTREAM(knotName); -- TEXT_FILE::open_for_write(knotName);
--outStream::=TEXT_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
logout:LOGOUT:=#; return WriteKnotS(Knot,logout,true,true,"");
end;
WriteBraidN(Knot:KNOT, knotName:STR):BOOL is
if (Knot.length<=0) then
#OUT+"Knot diagram is bad. (WriteKnot)\n";
return false;
end;
-- if KmsFIO.FExists(knotName) then ; end;
--outStream::=TEXT_FILE::open_for_write(knotName);
outStream::=#OUTSTREAM(knotName); -- TEXT_FILE::open_for_write(knotName);
if outStream.error.not then
reply:BOOL:=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
logout:LOGOUT:=#; return WriteBraidS(Knot,logout,true,true,"");
end;
WriteNDataN(Knot:KNOT, knotName:STR):BOOL is
if (Knot.length<=0) then
#OUT+"Knot diagram is bad.(WriteKnot)\n";
return false;
end;
-- if KmsFIO.FExists(knotName) then ; end;
outStream::=#OUTSTREAM(knotName); -- TEXT_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
logout:LOGOUT:=#; return WriteNDataS(Knot,logout,true,true,"");
end;
ReadKnotNS(out Knot:KNOT, inStream:INSTREAM):BOOL is
-- inStream: STD_CHANS::stdin, TEXT_FILE
Knot:=#;
if inStream.error.not then
HISTORY::put(Knot);
pos::=inStream.position;
reply::=ReadKnotS(out Knot,inStream,true);
if ~ reply then
inStream.seek(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
-- #OUT+"knot name to read :["+knotName+"]\n";
Knot:=#;
inStream::=#INSTREAM(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;