2009-06-25 07:16:22 -07:00
|
|
|
|
/*
|
|
|
|
|
* TextMemoryStream.cs
|
|
|
|
|
* Copyright (c) 2008-2009 kbinani
|
|
|
|
|
*
|
|
|
|
|
* This file is part of Boare.Lib.Vsq.
|
|
|
|
|
*
|
|
|
|
|
* Boare.Lib.Vsq is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the BSD License.
|
|
|
|
|
*
|
|
|
|
|
* Boare.Lib.Vsq is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
*/
|
2010-03-16 20:14:08 -07:00
|
|
|
|
#if JAVA
|
|
|
|
|
package org.kbinani.vsq;
|
2009-06-25 07:16:22 -07:00
|
|
|
|
|
2010-03-16 20:14:08 -07:00
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import org.kbinani.*;
|
|
|
|
|
#else
|
|
|
|
|
using System;
|
2009-09-07 03:44:18 -07:00
|
|
|
|
using bocoree;
|
2010-03-16 20:14:08 -07:00
|
|
|
|
using bocoree.java.util;
|
|
|
|
|
using bocoree.java.io;
|
2009-09-07 03:44:18 -07:00
|
|
|
|
|
2009-06-25 07:16:22 -07:00
|
|
|
|
namespace Boare.Lib.Vsq {
|
2010-03-16 20:14:08 -07:00
|
|
|
|
#endif
|
2009-06-25 07:16:22 -07:00
|
|
|
|
|
2010-03-16 20:14:08 -07:00
|
|
|
|
#if JAVA
|
|
|
|
|
public class TextMemoryStream implements ITextWriter {
|
|
|
|
|
#else
|
|
|
|
|
public class TextMemoryStream : IDisposable, ITextWriter {
|
|
|
|
|
#endif
|
2009-09-07 03:44:18 -07:00
|
|
|
|
private static readonly String NL = (char)0x0d + "" + (char)0x0a;
|
2009-06-25 07:16:22 -07:00
|
|
|
|
|
2009-09-07 03:44:18 -07:00
|
|
|
|
private Vector<String> m_lines;
|
2009-07-30 08:02:59 -07:00
|
|
|
|
private int m_index;
|
2009-06-25 07:16:22 -07:00
|
|
|
|
|
2009-07-30 08:02:59 -07:00
|
|
|
|
public TextMemoryStream() {
|
2009-09-07 03:44:18 -07:00
|
|
|
|
m_lines = new Vector<String>();
|
|
|
|
|
m_lines.add( "" );
|
2009-07-30 08:02:59 -07:00
|
|
|
|
m_index = 0;
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-03-16 20:14:08 -07:00
|
|
|
|
public TextMemoryStream( String path, String encoding ) {
|
2009-09-07 03:44:18 -07:00
|
|
|
|
m_lines = new Vector<String>();
|
2009-07-30 08:02:59 -07:00
|
|
|
|
m_index = 0;
|
2010-03-16 20:14:08 -07:00
|
|
|
|
if ( PortUtil.isFileExists( path ) ) {
|
|
|
|
|
BufferedReader sr = null;
|
|
|
|
|
try {
|
|
|
|
|
sr = new BufferedReader( new InputStreamReader( new FileInputStream( path ), encoding ) );
|
|
|
|
|
while ( sr.ready() ) {
|
|
|
|
|
String line = sr.readLine();
|
2009-09-07 03:44:18 -07:00
|
|
|
|
m_lines.add( line );
|
2009-07-30 08:02:59 -07:00
|
|
|
|
m_index++;
|
|
|
|
|
}
|
2010-03-16 20:14:08 -07:00
|
|
|
|
} catch ( Exception ex ) {
|
|
|
|
|
} finally {
|
|
|
|
|
if ( sr != null ) {
|
|
|
|
|
try {
|
|
|
|
|
sr.close();
|
|
|
|
|
} catch ( Exception ex2 ) {
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-07-30 08:02:59 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
2009-09-07 03:44:18 -07:00
|
|
|
|
public void write( String value ) {
|
2009-07-30 08:02:59 -07:00
|
|
|
|
appendString( value );
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
2009-09-07 03:44:18 -07:00
|
|
|
|
public void writeLine( String value ) {
|
2009-07-30 08:02:59 -07:00
|
|
|
|
appendString( value + NL );
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-03-16 20:14:08 -07:00
|
|
|
|
private static Vector<String> splitLine( String line ) {
|
|
|
|
|
Vector<String> ret = new Vector<String>();
|
|
|
|
|
String[] spl_0x0d_0x0a = PortUtil.splitString( line, new String[] { NL }, false );
|
|
|
|
|
for ( int i = 0; i < spl_0x0d_0x0a.Length; i++ ) {
|
|
|
|
|
String[] spl_0x0d = PortUtil.splitString( spl_0x0d_0x0a[i], (char)0x0d );
|
|
|
|
|
for ( int j = 0; j < spl_0x0d.Length; j++ ) {
|
|
|
|
|
String[] spl_0x0a = PortUtil.splitString( spl_0x0d[j], (char)0x0a );
|
|
|
|
|
for ( int k = 0; k < spl_0x0a.Length; k++ ) {
|
|
|
|
|
ret.add( spl_0x0a[k] );
|
|
|
|
|
}
|
2009-07-30 08:02:59 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2010-03-16 20:14:08 -07:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void appendString( String value ) {
|
|
|
|
|
Vector<String> lines2 = splitLine( value );
|
2009-09-07 03:44:18 -07:00
|
|
|
|
int count = lines2.size();
|
2009-07-30 08:02:59 -07:00
|
|
|
|
if ( count > 0 ) {
|
2009-09-07 03:44:18 -07:00
|
|
|
|
m_lines.set( m_index, m_lines.get( m_index ) + lines2.get( 0 ) );
|
2009-07-30 08:02:59 -07:00
|
|
|
|
for ( int i = 1; i < count; i++ ) {
|
2009-09-07 03:44:18 -07:00
|
|
|
|
m_lines.add( lines2.get( i ) );
|
2009-07-30 08:02:59 -07:00
|
|
|
|
m_index++;
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-30 08:02:59 -07:00
|
|
|
|
public void rewind() {
|
|
|
|
|
m_index = 0;
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
2009-09-07 03:44:18 -07:00
|
|
|
|
public String readLine() {
|
2009-07-30 08:02:59 -07:00
|
|
|
|
m_index++;
|
2009-09-07 03:44:18 -07:00
|
|
|
|
return m_lines.get( m_index - 1 );
|
2009-07-30 08:02:59 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int peek() {
|
2009-09-07 03:44:18 -07:00
|
|
|
|
if ( m_index < m_lines.size() ) {
|
|
|
|
|
if ( m_lines.get( m_index ).Equals( "" ) ) {
|
2009-07-30 08:02:59 -07:00
|
|
|
|
return -1;
|
|
|
|
|
} else {
|
2010-03-16 20:14:08 -07:00
|
|
|
|
#if JAVA
|
|
|
|
|
return (int)m_lines.get( m_index ).charAt( 0 );
|
|
|
|
|
#else
|
2009-09-07 03:44:18 -07:00
|
|
|
|
return (int)m_lines.get( m_index )[0];
|
2010-03-16 20:14:08 -07:00
|
|
|
|
#endif
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
2009-07-30 08:02:59 -07:00
|
|
|
|
} else {
|
|
|
|
|
return -1;
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-30 08:02:59 -07:00
|
|
|
|
public void close() {
|
2009-09-07 03:44:18 -07:00
|
|
|
|
m_lines.clear();
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
2009-07-30 08:02:59 -07:00
|
|
|
|
public void Dispose() {
|
|
|
|
|
close();
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-16 20:14:08 -07:00
|
|
|
|
#if !JAVA
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
2010-03-16 20:14:08 -07:00
|
|
|
|
#endif
|