From 0919b0e4fe15d0c5c0a7e765fe6b1cc79576eb81 Mon Sep 17 00:00:00 2001 From: Claudio Indellicati Date: Fri, 22 Aug 2014 18:35:15 +0200 Subject: [PATCH] Moved countLines() and loadFile() from Base to BaseNoGui. --- app/src/processing/app/Base.java | 12 +++--------- app/src/processing/app/BaseNoGui.java | 21 +++++++++++++++++++++ app/src/processing/app/SketchCode.java | 10 +++++----- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 965d42faa..687a21f6d 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -2478,11 +2478,7 @@ public class Base { * characters inside a String (and adding 1). */ static public int countLines(String what) { - int count = 1; - for (char c : what.toCharArray()) { - if (c == '\n') count++; - } - return count; + return BaseNoGui.countLines(what); } @@ -2560,10 +2556,8 @@ public class Base { * Grab the contents of a file as a string. */ static public String loadFile(File file) throws IOException { - String[] contents = PApplet.loadStrings(file); - if (contents == null) return null; - return PApplet.join(contents, "\n"); - } + return BaseNoGui.loadFile(file); + } /** diff --git a/app/src/processing/app/BaseNoGui.java b/app/src/processing/app/BaseNoGui.java index ddf1b1d89..988e3255e 100644 --- a/app/src/processing/app/BaseNoGui.java +++ b/app/src/processing/app/BaseNoGui.java @@ -75,6 +75,18 @@ public class BaseNoGui { return file; } + /** + * Get the number of lines in a file by counting the number of newline + * characters inside a String (and adding 1). + */ + static public int countLines(String what) { + int count = 1; + for (char c : what.toCharArray()) { + if (c == '\n') count++; + } + return count; + } + static public String getAvrBasePath() { String path = getHardwarePath() + File.separator + "tools" + File.separator + "avr" + File.separator + "bin" + File.separator; @@ -349,6 +361,15 @@ public class BaseNoGui { } } + /** + * Grab the contents of a file as a string. + */ + static public String loadFile(File file) throws IOException { + String[] contents = PApplet.loadStrings(file); + if (contents == null) return null; + return PApplet.join(contents, "\n"); + } + static public void populateImportToLibraryTable() { // Populate importToLibraryTable importToLibraryTable = new HashMap(); diff --git a/app/src/processing/app/SketchCode.java b/app/src/processing/app/SketchCode.java index a2e0bb772..e752cef3e 100644 --- a/app/src/processing/app/SketchCode.java +++ b/app/src/processing/app/SketchCode.java @@ -114,7 +114,7 @@ public class SketchCode { protected void copyTo(File dest) throws IOException { - Base.saveFile(program, dest); + BaseNoGui.saveFile(program, dest); } @@ -148,7 +148,7 @@ public class SketchCode { public int getLineCount() { - return Base.countLines(program); + return BaseNoGui.countLines(program); } @@ -184,7 +184,7 @@ public class SketchCode { * Load this piece of code from a file. */ public void load() throws IOException { - program = Base.loadFile(file); + program = BaseNoGui.loadFile(file); if (program.indexOf('\uFFFD') != -1) { System.err.println( @@ -212,7 +212,7 @@ public class SketchCode { // TODO re-enable history //history.record(s, SketchHistory.SAVE); - Base.saveFile(program, file); + BaseNoGui.saveFile(program, file); setModified(false); } @@ -221,6 +221,6 @@ public class SketchCode { * Save this file to another location, used by Sketch.saveAs() */ public void saveAs(File newFile) throws IOException { - Base.saveFile(program, newFile); + BaseNoGui.saveFile(program, newFile); } }