[armedbear-cvs] r11406 - branches/open-external-format/src/org/armedbear/lisp

Erik Huelsmann ehuelsmann at common-lisp.net
Sun Nov 30 20:47:00 UTC 2008


Author: ehuelsmann
Date: Sun Nov 30 20:46:59 2008
New Revision: 11406

Log:
Handle external format in Stream.java, in preparation of it being generally applicable to streams.

Modified:
   branches/open-external-format/src/org/armedbear/lisp/FileStream.java
   branches/open-external-format/src/org/armedbear/lisp/Stream.java

Modified: branches/open-external-format/src/org/armedbear/lisp/FileStream.java
==============================================================================
--- branches/open-external-format/src/org/armedbear/lisp/FileStream.java	(original)
+++ branches/open-external-format/src/org/armedbear/lisp/FileStream.java	Sun Nov 30 20:46:59 2008
@@ -54,22 +54,9 @@
     private Reader reader;
     private Writer writer;
 
-    public enum EolStyle {
-        CR,
-        CRLF,
-        LF
-    }
-    
-    static final private Symbol keywordCodePage = Packages.internKeyword("CODE-PAGE");
-    
-    private final static EolStyle platformEolStyle = Utilities.isPlatformWindows ? EolStyle.CRLF : EolStyle.LF;
-    
-    private EolStyle eolStyle = platformEolStyle;
-    private char eolChar = 0;
-    
     public FileStream(Pathname pathname, String namestring,
                       LispObject elementType, LispObject direction,
-                      LispObject ifExists, String encoding, EolStyle eol)
+                      LispObject ifExists, LispObject format)
         throws IOException
     {
         /* externalFormat is a LispObject of which the first char is a
@@ -114,6 +101,8 @@
                     raf.setLength(0);
             }
         }
+        setExternalFormat(format);
+        
 	// don't touch raf directly after passing it to racf.
 	// the state will become inconsistent if you do that.
         racf = new RandomAccessCharacterFile(raf, encoding);
@@ -146,7 +135,6 @@
 		outst = racf.getOutputStream();
 	    }
         }
-        eolChar = (eol == EolStyle.CR) ? '\r' : '\n';
     }
 
     @Override
@@ -295,7 +283,7 @@
         }
     }
 
-
+    @Override
     public void _writeChars(char[] chars, int start, int end)
         throws ConditionThrowable {
 	_writeChars(chars, start, end, true);
@@ -497,31 +485,13 @@
             LispObject ifExists = fifth;
             LispObject externalFormat = sixth;
             
-            String encoding = "ISO-8859-1";
-            if (externalFormat != NIL) {
-		if (externalFormat instanceof Symbol) {
-		    Symbol enc = (Symbol)externalFormat; //FIXME: class cast exception to be caught
-		    if (enc != NIL) {
-			if (enc != keywordCodePage) {
-			    encoding = enc.getName();
-			}
-			//FIXME: the else for the keywordCodePage to be filled in
-		    }
-		    //FIXME: the else for the == NIL to be filled in: raise an error...
-		} else if (externalFormat instanceof AbstractString) {
-		    AbstractString encName = (AbstractString) externalFormat;
-		    encoding = encName.getStringValue();
-		}
-            }
-            
-            
             if (direction != Keyword.INPUT && direction != Keyword.OUTPUT &&
                 direction != Keyword.IO)
                 error(new LispError("Direction must be :INPUT, :OUTPUT, or :IO."));
             try {
                 return new FileStream(pathname, namestring.getStringValue(),
                                       elementType, direction, ifExists,
-                                      encoding, platformEolStyle);
+                                      externalFormat);
             }
             catch (FileNotFoundException e) {
                 return NIL;

Modified: branches/open-external-format/src/org/armedbear/lisp/Stream.java
==============================================================================
--- branches/open-external-format/src/org/armedbear/lisp/Stream.java	(original)
+++ branches/open-external-format/src/org/armedbear/lisp/Stream.java	Sun Nov 30 20:46:59 2008
@@ -80,6 +80,31 @@
    */
   protected int charPos;
   
+  public enum EolStyle {
+    RAW,
+    CR,
+    CRLF,
+    LF
+  }
+
+  static final private Symbol keywordDefault = Packages.internKeyword("DEFAULT");
+  
+  static final private Symbol keywordCodePage = Packages.internKeyword("CODE-PAGE");
+  static final private Symbol keywordID = Packages.internKeyword("ID");
+
+  static final private Symbol keywordEolStyle = Packages.internKeyword("EOL-STYLE");
+  static final private Symbol keywordCR = Packages.internKeyword("CR");
+  static final private Symbol keywordLF = Packages.internKeyword("LF");
+  static final private Symbol keywordCRLF = Packages.internKeyword("CRLF");
+  static final private Symbol keywordRAW = Packages.internKeyword("RAW");
+    
+  public final static EolStyle platformEolStyle = Utilities.isPlatformWindows ? EolStyle.CRLF : EolStyle.LF;
+    
+  protected EolStyle eolStyle = platformEolStyle;
+  protected char eolChar = 0;
+  protected LispObject externalFormat = LispObject.NIL;
+  protected String encoding = null;
+  
   // Binary input.
   private BufferedInputStream in;
 
@@ -215,6 +240,71 @@
     interactive = b;
   }
 
+  public LispObject getExternalFormat() {
+      return externalFormat;
+  }
+  
+  public String getEncoding() {
+      return encoding;
+  }
+  
+  public void setExternalFormat(LispObject format) {
+    if (format == keywordDefault) {
+      encoding = null;
+      eolStyle = platformEolStyle;
+      eolChar = (eolStyle == EolStyle.CR) ? '\r' : '\n';
+      externalFormat = format;
+      
+      return;
+    }
+      
+    try {
+      LispObject enc;
+      boolean encIsCp = false;
+      
+      if (format instanceof Cons) {
+          // meaning a non-empty list
+          enc = format.car();
+
+          if (enc == keywordCodePage) {
+              encIsCp = true;
+
+              enc = LispObject.getf(format.cdr(), keywordID, null);
+          }
+          
+          LispObject eol = LispObject.getf(format.cdr(), keywordEolStyle, keywordRAW);
+          if (eol == keywordCR)
+              eolStyle = EolStyle.CR;
+          else if (eol == keywordLF)
+              eolStyle = EolStyle.LF;
+          else if (eol == keywordCRLF)
+              eolStyle = EolStyle.CRLF;
+          else if (eol != keywordRAW)
+              //###FIXME: raise an error
+              ;
+          
+      } else
+        enc = format;
+      
+      if (enc.numberp())
+          encoding = enc.toString();
+      else if (enc instanceof AbstractString)
+          encoding = enc.getStringValue();
+      else if (enc instanceof Symbol)
+          encoding = ((Symbol)enc).getName();
+      else
+          //###FIXME: raise an error!
+          ;
+      
+      if (encIsCp)
+          encoding = "Cp" + encoding;
+    }
+    catch (ConditionThrowable ct) { }
+    
+    eolChar = (eolStyle == EolStyle.CR) ? '\r' : '\n';
+    externalFormat = format;
+  }
+  
   public boolean isOpen()
   {
     return open;




More information about the armedbear-cvs mailing list