--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/de/uapcore/sudoku/DocumentHandler.java Sat Jan 26 19:34:31 2013 +0100 @@ -0,0 +1,77 @@ +/* + * Copyright 2013 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +package de.uapcore.sudoku; + +import java.io.BufferedWriter; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; + +/** + * + * @author mike + */ +public class DocumentHandler { + + private String filename; + + public void load(Field field) throws IOException { + if (!isFilenameSet()) { + throw new IOException("no filename supplied"); + } + // TODO: implement + } + + public void save(Field field) throws IOException { + if (!isFilenameSet()) { + throw new IOException("no filename supplied"); + } + + try (BufferedWriter out = new BufferedWriter( + new OutputStreamWriter(new FileOutputStream(filename)))) { + for (int i = 0 ; i < 9 ; i++) { + int[] row = field.getRow(i); + for (int j = 0 ; j < 9 ; j++) { + out.append(row[j] > 0 ? Character.forDigit(row[j], 10):'_'); + out.append(j == 8 ? '\n': ' '); + } + } + } + } + + public void setFilename(String filename) { + this.filename = filename; + } + + public void clearFilename() { + filename = null; + } + + public boolean isFilenameSet() { + return filename != null; + } +}