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.
|
|
|
|
|
*/
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
2009-09-07 03:44:18 -07:00
|
|
|
|
using bocoree;
|
|
|
|
|
|
2009-06-25 07:16:22 -07:00
|
|
|
|
namespace Boare.Lib.Vsq {
|
|
|
|
|
|
|
|
|
|
public class TextMemoryStream : IDisposable {
|
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
|
|
|
|
}
|
|
|
|
|
|
2009-09-07 03:44:18 -07:00
|
|
|
|
public TextMemoryStream( String path, Encoding encoding ) {
|
|
|
|
|
m_lines = new Vector<String>();
|
2009-07-30 08:02:59 -07:00
|
|
|
|
m_index = 0;
|
|
|
|
|
if ( File.Exists( path ) ) {
|
|
|
|
|
using ( StreamReader sr = new StreamReader( path, encoding ) ) {
|
|
|
|
|
while ( sr.Peek() >= 0 ) {
|
2009-09-07 03:44:18 -07:00
|
|
|
|
String line = sr.ReadLine();
|
|
|
|
|
m_lines.add( line );
|
2009-07-30 08:02:59 -07:00
|
|
|
|
m_index++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
2009-09-07 03:44:18 -07:00
|
|
|
|
private void appendString( String value ) {
|
|
|
|
|
String[] lines = value.Split( new String[] { NL }, StringSplitOptions.None );
|
|
|
|
|
Vector<String> lines2 = new Vector<String>();
|
2009-07-30 08:02:59 -07:00
|
|
|
|
for ( int i = 0; i < lines.Length; i++ ) {
|
2009-09-07 03:44:18 -07:00
|
|
|
|
String[] spl = lines[i].Split( (char)0x0d, (char)0x0a );
|
2009-07-30 08:02:59 -07:00
|
|
|
|
for ( int j = 0; j < spl.Length; j++ ) {
|
2009-09-07 03:44:18 -07:00
|
|
|
|
lines2.add( spl[j] );
|
2009-07-30 08:02:59 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
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 {
|
2009-09-07 03:44:18 -07:00
|
|
|
|
return (int)m_lines.get( m_index )[0];
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|