logo

class

compiler::FUtil

sys::Obj
  compiler::FUtil : compiler::FConst
  1  //
  2  // Copyright (c) 2006, Brian Frank and Andy Frank
  3  // Licensed under the Academic Free License version 3.0
  4  //
  5  // History:
  6  //   19 Aug 06  Brian Frank  Creation
  7  //
  8  
  9  **
 10  ** FUtil provides fcode encoding and decoding utilities.
 11  **
 12  class FUtil : FConst
 13  {
 14  
 15  //////////////////////////////////////////////////////////////////////////
 16  // Buf
 17  //////////////////////////////////////////////////////////////////////////
 18  
 19    static Void writeBuf(OutStream out, Buf buf)
 20    {
 21      if (buf == null)
 22      {
 23        out.writeI2(0)
 24      }
 25      else
 26      {
 27        out.writeI2(buf.size)
 28        out.writeBuf(buf.seek(0))
 29      }
 30    }
 31  
 32    static Buf readBuf(InStream in)
 33    {
 34      size := in.readU2
 35      if (size === 0) return null
 36      return in.readBufFully(null, size)
 37    }
 38  
 39  //////////////////////////////////////////////////////////////////////////
 40  // Doc
 41  //////////////////////////////////////////////////////////////////////////
 42  
 43    **
 44    ** Write a fandoc item to the specified output stream.  The fandoc file
 45    ** format is an extremely simple plan text format with left justified
 46    ** type/slot qnames, followed by the fandoc content indented two spaces.
 47    **
 48    static Void writeDoc(OutStream out, Str key, Str doc)
 49    {
 50      if (doc == null) return
 51      out.print(key).print("\n").print("  ")
 52      doc.each |Int ch|
 53      {
 54        if (ch === '\r') throw ArgErr.make
 55        out.writeChar(ch)
 56        if (ch === '\n') out.print("  ")
 57      }
 58      out.print("\n\n")
 59    }
 60  
 61  }