src/main/java/de/uapcore/sudoku/DocumentHandler.java

Sat, 25 Jul 2020 14:01:28 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 25 Jul 2020 14:01:28 +0200
changeset 9
576e7a2861ae
parent 7
src/de/uapcore/sudoku/DocumentHandler.java@2c0a2766461c
child 10
369903afbb29
permissions
-rw-r--r--

converts to maven project

universe@5 1 /*
universe@5 2 * Copyright 2013 Mike Becker. All rights reserved.
universe@5 3 *
universe@5 4 * Redistribution and use in source and binary forms, with or without
universe@5 5 * modification, are permitted provided that the following conditions are met:
universe@5 6 *
universe@5 7 * 1. Redistributions of source code must retain the above copyright
universe@5 8 * notice, this list of conditions and the following disclaimer.
universe@5 9 *
universe@5 10 * 2. Redistributions in binary form must reproduce the above copyright
universe@5 11 * notice, this list of conditions and the following disclaimer in the
universe@5 12 * documentation and/or other materials provided with the distribution.
universe@5 13 *
universe@5 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@5 15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@5 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@5 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@5 18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@5 19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@5 20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@5 21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@5 22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@5 23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@5 24 * POSSIBILITY OF SUCH DAMAGE.
universe@5 25 */
universe@5 26
universe@5 27 package de.uapcore.sudoku;
universe@5 28
universe@9 29 import java.io.*;
universe@6 30 import java.util.regex.Matcher;
universe@6 31 import java.util.regex.Pattern;
universe@5 32
universe@5 33 /**
universe@5 34 *
universe@5 35 * @author mike
universe@5 36 */
universe@5 37 public class DocumentHandler {
universe@5 38
universe@5 39 private String filename;
universe@5 40
universe@5 41 public void load(Field field) throws IOException {
universe@5 42 if (!isFilenameSet()) {
universe@5 43 throw new IOException("no filename supplied");
universe@5 44 }
universe@6 45 int row = 0;
universe@6 46 try (BufferedReader in = new BufferedReader(
universe@6 47 new InputStreamReader(new FileInputStream(filename)))) {
universe@6 48 Pattern pat = Pattern.compile("^\\s*(?:[1-9_] ){8}[1-9_]\\s*$");
universe@6 49 String line;
universe@6 50 while ((line = in.readLine()) != null) {
universe@6 51 if (line.matches("^\\s*$")) {
universe@6 52 continue;
universe@6 53 }
universe@6 54 Matcher m = pat.matcher(line);
universe@6 55 if (m.matches()) {
universe@6 56 String c[] = line.trim().split(" ");
universe@6 57 if (c.length != 9) {
universe@6 58 break;
universe@6 59 }
universe@6 60 for (int i = 0 ; i < 9 ; i++) {
universe@6 61 field.setCellValue(i, row,
universe@6 62 c[i].equals("_") ? 0 : Integer.valueOf(c[i]));
universe@6 63 }
universe@6 64 row++;
universe@6 65 } else {
universe@6 66 break;
universe@6 67 }
universe@6 68 }
universe@6 69 if (row != 9) {
universe@6 70 throw new IOException("Kein Sudoku-Feld enthalten!");
universe@6 71 }
universe@6 72 }
universe@7 73 field.setAllCellsModified(false);
universe@5 74 }
universe@5 75
universe@5 76 public void save(Field field) throws IOException {
universe@5 77 if (!isFilenameSet()) {
universe@5 78 throw new IOException("no filename supplied");
universe@5 79 }
universe@5 80
universe@5 81 try (BufferedWriter out = new BufferedWriter(
universe@5 82 new OutputStreamWriter(new FileOutputStream(filename)))) {
universe@5 83 for (int i = 0 ; i < 9 ; i++) {
universe@5 84 int[] row = field.getRow(i);
universe@5 85 for (int j = 0 ; j < 9 ; j++) {
universe@5 86 out.append(row[j] > 0 ? Character.forDigit(row[j], 10):'_');
universe@5 87 out.append(j == 8 ? '\n': ' ');
universe@5 88 }
universe@5 89 }
universe@5 90 }
universe@5 91 }
universe@5 92
universe@5 93 public void setFilename(String filename) {
universe@5 94 this.filename = filename;
universe@5 95 }
universe@5 96
universe@5 97 public void clearFilename() {
universe@5 98 filename = null;
universe@5 99 }
universe@5 100
universe@5 101 public boolean isFilenameSet() {
universe@5 102 return filename != null;
universe@5 103 }
universe@5 104 }

mercurial