[Home]Ugluk/Persistence

Robo Home | Ugluk | Changes | Preferences | AllPages

Showing revision 2

package pedersen.core;

The following class deals with the file io mechanism, which I imagine if you have dealt with it you've found it to be as royal a pain in the ass as I have. Fortunately the CannonCaches? switch helps with the errors being thrown, which was pretty sporadic and fux0rd the bot.

My implementation receives a list of entries to print (which are not necessarily one line each) and retrives entries as a list of lines in the file. It does not deal with the cache size. Modify to suit.


/**
 * Stores and retrieves data from the local cache.
 * Data may be targeting or movement statistics, debug information, etc.
 * 
 * @author Martin Alan Pedersen
 */
public class Persistence
{
	//:://////////////////////////////////////
	//::  Static Methods
	//:://////////////////////////////////////
	
	/**
	 * Writes a list of strings to a file.
	 * 
	 * @param list the list of strings
	 * @param filename the name of the file to write to
	 */
	public static void writeListToFile( List list, String filename )
	{
		try
		{
			RobocodeFileWriter outputStream = null;
			try
			{
				outputStream = new RobocodeFileWriter( Persistence.getFullPathToFilename( filename ), false );
				String content;
				Iterator iterator = list.iterator();
				while( iterator.hasNext() )
				{
					content = Persistence.encrypt( (String) iterator.next() );
					outputStream.write( content, 0, content.length() );
				}
			}
			finally
			{
				if( outputStream != null ) outputStream.close();
			}
		}
		catch( Exception ex )
		{
			Debug.exception( "Persistence.writeBufferToFile", ex );
		}
	}

	
	/**
	 * Reads a file and converts each line into a list entry.
	 * 
	 * @param filename the name of the file to read.
	 * 
	 * @return the lines of the file in the form of a list
	 */
	public static List readListFromFile( String filename )
	{
		List list = new ArrayList();
		try
		{
			FileReader inputStream = null;
			try
			{
				char[] cbuf = new char[100];
				StringBuffer buffer = new StringBuffer();
				inputStream = new FileReader( Persistence.getFullPathToFilename( filename ) );
				
				for( int readResult = inputStream.read( cbuf, 0, 100 ); readResult != -1; readResult = inputStream.read( cbuf, 0, 100 ) )
				{
					buffer.append( cbuf );
				}
				
				int indexStart = 0;
				int indexEnd = buffer.indexOf( "\n", indexStart );
				while( indexEnd != -1 )
				{
					list.add( Persistence.decrypt( buffer.substring( indexStart, indexEnd ) ) );
					indexStart = indexEnd + 1;
					indexEnd = buffer.indexOf( "\n", indexStart );
				}
			}
			finally
			{
				if( inputStream != null ) inputStream.close();
			}
		}
		catch( Exception ex )
		{
			Debug.exception( "Persistence.writeBufferToFile", ex );
		}
		return list;
	}
	
	
	/**
	 * Gets the full path name to the file you are accessing.
	 * 
	 * @param filename the immediate file you are accessing
	 * 
	 * @return the full path to the file
	 */
	private static String getFullPathToFilename( String filename )
	{
		File directory = Host.io.getDataDirectory();
		return directory.getAbsolutePath() + File.separator + Persistence.mangle( filename );
	}
	
	
	/**
	 * Converts readable data into encrypted data.
	 * 
	 * @param readable the readable data
	 * 
	 * @return encrypted data
	 */
	private static String encrypt( String readable )
	{
		String unreadable = null;
		unreadable = readable; // placeholder
		return unreadable;
	}
	
	
	/**
	 * Converts encrypted data into readable data.
	 * 
	 * @param unreadable encrypted data
	 * 
	 * @return readable data
	 */
	private static String decrypt( String unreadable )
	{
		String readable = null;
		readable = unreadable; // placeholder
		return readable;
	}
	
	
	/**
	 * Converts a string into a file-friendly one.  Intended for converting tank names.
	 * 
	 * @param original the original name
	 * 
	 * @return the converted filename
	 */
	private static String mangle( String original )
	{
		StringBuffer buffer = new StringBuffer();
		char character;
		for( int i = 0; i < original.length(); i++ )
		{
			character = original.charAt( i );
			if( ( character > 47 ) && ( character < 58 ) ) buffer.append( character );
			else if( ( character > 64 ) && ( character < 91 ) ) buffer.append( character );
			else if( ( character > 96 ) && ( character < 123 ) ) buffer.append( character );
		}
		buffer.append( ".txt" );
		return buffer.toString();
	}
	
}

Robo Home | Ugluk | Changes | Preferences | AllPages
Edit revision 2 of this page | View other revisions | View current revision
Edited November 15, 2005 22:56 EST by Martin Alan Pedersen (diff)
Search: