Re: Saving a sequence

new topic     » goto parent     » topic index » view thread      » older message » newer message

Hi Ryanj,

When storing sequences in logs for debug, some of us have written routines to display sequences in a human readable form. Here are mine:

------------------------------------------------------------------------------ 
 
public function showPrintable(sequence s) 
-- returns only printable characters of a sequence 
-- non printable characters are replace by a dot 
  sequence res 
 
  res = "" 
  for i = 1 to length(s) do 
    if integer(s[i]) then 
      if (s[i] > 31) and (s[i] < 127) then 
        res &= s[i] 
      elsif s[i] = 9 then 
        res &= "\\t" 
      elsif s[i] = 13 then 
        res &= "\\r" 
      elsif s[i] = 10 then 
        res &= "\\n" 
      else 
        res &= "." 
      end if 
    else 
      res &= "." 
    end if 
  end for 
  return res 
end function 
 
------------------------------------------------------------------------------ 
 
public function sequenceDump(sequence s) 
-- prints a sequence structure in a human readable way as one string 
  integer subSequence 
  sequence result = "" 
 
  if isString(s) then 
    result = sprintf("\"%s\"", {showPrintable(s)}) 
  else 
    subSequence = 0 
    for i=1 to length(s) do 
      if sequence(s[i]) then 
        subSequence = 1 
        exit 
      end if 
    end for 
    if subSequence then 
      result = sequenceDump(s[1]) 
      for i=2 to length(s) do 
        result &= ", " & sequenceDump(s[i]) 
      end for 
      result = "{" & result & "}" 
    else 
      result = sprint(s) & "\t'" & showPrintable(s) & "'" 
    end if 
  end if 
  return result 
end function 
 
------------------------------------------------------------------------------ 
 
public function objectDump(object x, integer detailed=0) 
-- returns an object in a human readable way 
  integer subSequence 
  sequence result = "" 
 
  if integer(x) then 
    result = sprintf("%d", {x}) 
  elsif atom(x) then 
    if (x >= 0) and (x = floor(x)) then 
      result = sprintf("%.0f", {x}) 
    elsif (x < 0) and (x = floor(x+1)) then 
      result = sprintf("%.0f", {x}) 
    else 
      result = sprintf("%f", {x}) 
    end if 
  elsif isString(x) then 
    if detailed then 
      result = sprintf("'%s'\t%s", {showPrintable(x), sprint(x)}) 
    else 
      result = sprintf("'%s'", {showPrintable(x)}) 
    end if 
  else 
    subSequence = 0 
    for i=1 to length(x) do 
      if sequence(x[i]) then 
        subSequence = 1 
        exit 
      end if 
    end for 
    if subSequence then 
      result = objectDump(x[1], 0) 
      for i=2 to length(x) do result &= ", " & objectDump(x[i], 0) end for 
      result = "{" & result & "}" 
    else 
      if detailed then 
        result = sprintf("%s\t'%s'", {sprint(x), showPrintable(x)}) 
      else 
        result = sprintf("%s", {sprint(x)}) 
      end if 
    end if 
  end if 
  return result 
end function 
 

Regards

Jean-Marc

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu