diff -r c2ce9d916103 -r 72ea1c6d836c java/rozsirene-atributy/src/cz/frantovo/rozsireneatributy/CSV.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/rozsirene-atributy/src/cz/frantovo/rozsireneatributy/CSV.java Sat Dec 16 14:09:00 2023 +0100 @@ -0,0 +1,65 @@ +/** + * Rozšířené atributy – program na správu rozšířených atributů souborů + * Copyright © 2023 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License. + * + * This program 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. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package cz.frantovo.rozsireneatributy; + +import java.io.IOException; +import java.io.Writer; + +/** + * Generátor standardních CSV. + * + * @author Ing. František Kučera + */ +public class CSV { + + private final Writer výstup; + private int sloupec = 0; + private int početSloupců = -1; + + public CSV(Writer výstup) { + this.výstup = výstup; + } + + public void hodnota(String hodnota) throws IOException { + if (sloupec > 0) výstup.write(','); + if (hodnota != null && !hodnota.isEmpty()) { + výstup.write('"'); + for (char ch : hodnota.toCharArray()) { + if (ch == '"') výstup.write('"'); + výstup.write(ch); + } + výstup.write('"'); + } + sloupec++; + výstup.flush(); + } + + public void konecŘádku() throws IOException { + if (početSloupců < 0) početSloupců = sloupec; + if (sloupec == početSloupců) { + výstup.write("\r\n"); + sloupec = 0; + } else { + throw new IOException("Neodpovídá počet sloupců:" + + " aktuální=" + sloupec + + " celkový=" + početSloupců + ); + } + výstup.flush(); + } + +}