Skip to content

Commit f82f0a9

Browse files
author
Markus Pfeiffer
committed
Refactor OMGetObjectWithAttributes (step 1/n)
1 parent a5c52b2 commit f82f0a9

File tree

2 files changed

+46
-91
lines changed

2 files changed

+46
-91
lines changed

lib/openmath.g

Lines changed: 40 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -237,97 +237,52 @@ OMsymRecord.meta := rec(
237237
## [ ["call_id", "user007" ], ["option_runtime", 300000] ]
238238
## This is a counterpart of the function OMGetObject from OpenMath package .
239239
##
240+
## TODO: This should be somehow replaced by OMGetObject from the OpenMath package
240241
InstallGlobalFunction( OMGetObjectWithAttributes,
241242
function( stream )
242-
local return_tree,
243-
fromgap, # string
244-
firstbyte,
245-
gap_obj,
246-
success, # whether PipeOpenMathObject worked
247-
readline;
248-
249-
if IsClosedStream( stream ) then
250-
Error( "closed stream" );
251-
elif IsEndOfStream( stream ) then
252-
Error( "end of stream" );
253-
fi;
254-
255-
if ValueOption("return_tree") <> fail then
256-
return_tree := true;
257-
else
258-
return_tree := false;
243+
local ReadUntil, tree;
244+
245+
ReadUntil := function(stream, string)
246+
local readline;
247+
repeat
248+
readline := ReadLine(stream);
249+
if readline = fail then
250+
return fail;
251+
fi;
252+
NormalizeWhitespace( readline );
253+
if Length(readline) > 0 then
254+
Info( InfoSCSCP, 2, readline );
255+
fi;
256+
until readline = string;
257+
return true;
258+
end;
259+
260+
if IsClosedStream(stream) then
261+
Error("<stream> is closed");
262+
elif IsEndOfStream(stream) then
263+
Error("<stream> is in state end of stream" );
259264
fi;
260265

261266
# read new line until <?scscp start ?>
262-
repeat
263-
readline:=ReadLine(stream);
264-
if readline=fail then
265-
return fail;
266-
fi;
267-
NormalizeWhitespace( readline );
268-
if Length( readline ) > 0 then
269-
Info( InfoSCSCP, 2, readline );
270-
fi;
271-
until readline= "<?scscp start ?>";
272-
273-
firstbyte := ReadByte(stream);
274-
275-
if firstbyte = 24 then
276-
# Reading binary encoding => set reply mode to binary
277-
IN_SCSCP_BINARY_MODE:=true;
278-
gap_obj := GetNextObject( stream, firstbyte );
279-
gap_obj := OMParseXmlObj( gap_obj.content[1] );
280-
return rec( object := gap_obj, attributes := OMParseXmlObj( OMTempVars.OMATTR ) );
281-
else
282-
283-
if firstbyte = fail then
284-
Info( InfoSCSCP, 2, "OpenMath object not retrieved by PipeOpenMathObject" );
285-
return fail;
286-
fi;
287-
288-
# Reading XML encoding => set reply mode to XML
289-
IN_SCSCP_BINARY_MODE:=false;
290-
fromgap := "";
291-
# Get one OpenMath object from 'stream' and put into 'fromgap',
292-
# using PipeOpenMathObject
293-
294-
success := PipeOpenMathObject( stream, fromgap, firstbyte );
295-
296-
if success <> true then
297-
Info( InfoSCSCP, 2, "OpenMath object not retrieved by PipeOpenMathObject" );
298-
return fail;
299-
fi;
300-
301-
# Now 'fromgap' is the string with OpenMath encoding
302-
303-
if InfoLevel( InfoSCSCP ) > 2 then
304-
Print("#I Received message: \n");
305-
Print( fromgap );
306-
Print( "\n" );
307-
fi;
308-
309-
# read new line until <?scscp end ?>
310-
repeat
311-
readline:=ReadLine(stream);
312-
if readline=fail then
313-
return fail;
314-
fi;
315-
NormalizeWhitespace( readline );
316-
if Length( readline ) > 0 then
317-
Info( InfoSCSCP, 2, readline );
318-
fi;
319-
until readline= "<?scscp end ?>";
320-
321-
# convert the OpenMath string into a Gap object using an appropriate
322-
# function
323-
324-
if return_tree then
325-
return OMgetObjectXMLTreeWithAttributes( fromgap : return_tree );
326-
else
327-
return OMgetObjectXMLTreeWithAttributes( fromgap );
328-
fi;
267+
# TODO: How to switch binary/nonbinary?
268+
# previously this code had this switch:
269+
# IN_SCSCP_BINARY_MODE:=true;
270+
# IN_SCSCP_BINARY_MODE:=false;
271+
# (again state in global variables...)
272+
# TODO: Handle attributes, they seem to end up
273+
# in temp vars for some reason, are then
274+
# extracted to attributes in the returned record?
275+
# TODO: Need to return fail if ReadUntil fails
276+
277+
ReadUntil("<?scscp start ?>");
278+
tree := OMGetTree(stream);
279+
ReadUntil("<?scscp end ?>");
280+
281+
if ValueOption("return_tree") = fail then
282+
return OMParseXmlObj( tree.content[1] );
329283
fi;
330-
end );
284+
return tree;
285+
end);
331286

332287

333288
#############################################################################

lib/process.gi

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,11 @@ if IN_SCSCP_TRACING_MODE then SCSCPTraceSuspendThread(); fi;
227227
IO_Select( [ tcpstream![1] ], [ ], [ ], [ ], 60*60, 0 );
228228
if IN_SCSCP_TRACING_MODE then SCSCPTraceRunThread(); fi;
229229
if IN_SCSCP_TRACING_MODE then SCSCPTraceReceiveMessage( tcpstream![3][1] ); fi;
230-
if output_option="tree" then
231-
result := OMGetObjectWithAttributes( tcpstream : return_tree );
232-
else
233-
result := OMGetObjectWithAttributes( tcpstream );
234-
fi;
230+
result := OMGetTree(tcpstream);
231+
232+
if output_option <> "tree" then
233+
result := OMParseXmlObj( result.content[1] );
234+
fi;
235235

236236
if result = fail then
237237
Info( InfoSCSCP, 2, "CompleteProcess failed to get result from ",
@@ -470,4 +470,4 @@ end);
470470
###########################################################################
471471
##
472472
#E
473-
##
473+
##

0 commit comments

Comments
 (0)