Debian Codegraph @

arduino-core/src/processing/app/SketchFile.java

raw symbols SCIP
1 /*
2 SketchFile - data class for a single file inside a sketch
3 Part of the Processing project - http://processing.org
4
5 Copyright (c) 2004-08 Ben Fry and Casey Reas
6 Copyright (c) 2001-04 Massachusetts Institute of Technology
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software Foundation,
20 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23 package processing.app;
24
25 import processing.app.helpers.FileUtils;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.util.Arrays;
32 import java.util.List;
33 import java.util.stream.Collectors;
34 import java.util.stream.Stream;
35
36 import static processing.app.I18n.tr;
37
38 /**
39 * Represents a file within a sketch.
40 */
41 public class SketchFile {
42
43 /**
44 * File object for where this code is located
45 */
46 private File file;
47
48 /**
49 * The sketch this file belongs to.
50 */
51 private Sketch sketch;
52
53 /**
54 * Is this the primary file in the sketch?
55 */
56 private boolean primary;
57
58 /**
59 * Interface for an in-memory storage of text file contents. This is
60 * intended to allow a GUI to keep modified text in memory, and allow
61 * SketchFile to check for changes when needed.
62 */
63 public static interface TextStorage {
64 /** Get the current text */
65 public String getText();
66
67 /**
68 * Is the text modified externally, after the last call to
69 * clearModified() or setText()?
70 */
71 public boolean isModified();
72
73 /** Clear the isModified() result value */
74 public void clearModified();
75 }
76
77 /**
78 * A storage for this file's text. This can be set by a GUI, so we can
79 * have access to any modified version of the file. This can be null,
80 * in which case the file is never modified, and saving is a no-op.
81 */
82 private TextStorage storage;
83
84 /**
85 * Create a new SketchFile
86 *
87 * @param sketch
88 * The sketch this file belongs to
89 * @param file
90 * The file this SketchFile represents
91 * @param primary
92 * Whether this file is the primary file of the sketch
93 */
94 public SketchFile(Sketch sketch, File file) {
95 this.sketch = sketch;
96 this.file = file;
97 FileUtils.SplitFile split = FileUtils.splitFilename(file);
98 this.primary = split.basename.equals(sketch.getFolder().getName())
99 && Sketch.SKETCH_EXTENSIONS.contains(split.extension);
100 }
101
102 /**
103 * Set an in-memory storage for this file's text, that will be queried
104 * on compile, save, and whenever the text is needed. null can be
105 * passed to detach any attached storage.
106 */
107 public void setStorage(TextStorage text) {
108 this.storage = text;
109 }
110
111
112 public File getFile() {
113 return file;
114 }
115
116 /**
117 * Is this the primary file in the sketch?
118 */
119 public boolean isPrimary() {
120 return primary;
121 }
122
123 protected boolean fileExists() {
124 return file.exists();
125 }
126
127
128 protected boolean fileReadOnly() {
129 return !file.canWrite();
130 }
131
132
133 protected boolean delete(Path tempBuildFolder) throws IOException {
134 if (!file.delete()) {
135 return false;
136 }
137
138 List<Path> tempBuildFolders = Stream.of(tempBuildFolder, tempBuildFolder.resolve("sketch"))
139 .filter(path -> Files.exists(path)).collect(Collectors.toList());
140
141 for (Path folder : tempBuildFolders) {
142 if (!deleteCompiledFilesFrom(folder)) {
143 return false;
144 }
145 }
146
147 sketch.removeFile(this);
148
149 return true;
150 }
151
152 private boolean deleteCompiledFilesFrom(Path tempBuildFolder) throws IOException {
153 List<Path> compiledFiles = Files.list(tempBuildFolder)
154 .filter(pathname -> pathname.getFileName().toString().startsWith(getFileName()))
155 .collect(Collectors.toList());
156
157 for (Path compiledFile : compiledFiles) {
158 try {
159 Files.delete(compiledFile);
160 } catch (IOException e) {
161 return false;
162 }
163 }
164 return true;
165 }
166
167 /**
168 * Rename the given file to get the given name.
169 *
170 * @param newName
171 * The new name, including extension, excluding directory
172 * name.
173 * @throws IOException
174 * When a problem occurs, or is expected to occur. The error
175 * message should be already translated.
176 */
177 public void renameTo(String newName) throws IOException {
178 File newFile = new File(file.getParentFile(), newName);
179 sketch.checkNewFilename(newFile);
180 if (!file.exists() || file.renameTo(newFile)) {
181 renamedTo(newFile);
182 } else {
183 String msg = I18n.format(tr("Failed to rename \"{0}\" to \"{1}\""), file.getName(), newName);
184 throw new IOException(msg);
185 }
186 }
187
188 /**
189 * Should be called when this file was renamed and renameTo could not
190 * be used (e.g. when renaming the entire sketch directory).
191 */
192 protected void renamedTo(File what) {
193 file = what;
194 }
195
196 /*
197 * Returns the filename include extension.
198 */
199 public String getFileName() {
200 return file.getName();
201 }
202
203 /**
204 * Returns the filename without extension for normal sketch files
205 * (Sketch.SKETCH_EXTENSIONS) and the filename with extension for all
206 * others.
207 */
208 public String getPrettyName() {
209 if (!PreferencesData.getBoolean("editor.show_always_extensions") && isExtension(Sketch.SKETCH_EXTENSIONS))
210 return getBaseName();
211 else
212 return getFileName();
213 }
214
215 /**
216 * Returns the filename without extension
217 */
218 public String getBaseName() {
219 return FileUtils.splitFilename(file).basename;
220 }
221
222 public boolean isExtension(String... extensions) {
223 return isExtension(Arrays.asList(extensions));
224 }
225
226 public boolean isExtension(List<String> extensions) {
227 return FileUtils.hasExtension(file, extensions);
228 }
229
230
231 public String getProgram() {
232 if (storage != null)
233 return storage.getText();
234
235 return null;
236 }
237
238
239 public boolean isModified() {
240 if (storage != null)
241 return storage.isModified();
242 return false;
243 }
244
245 @Override
246 public boolean equals(Object o) {
247 return (o instanceof SketchFile) && file.equals(((SketchFile) o).file);
248 }
249
250 /**
251 * Load this piece of code from a file and return the contents. This
252 * completely ignores any changes in the linked storage, if any, and
253 * just directly reads the file.
254 */
255 public String load() throws IOException {
256 String text = BaseNoGui.loadFile(file);
257
258 if (text == null) {
259 throw new IOException();
260 }
261
262 if (text.indexOf('\uFFFD') != -1) {
263 System.err.println(
264 I18n.format(
265 tr("\"{0}\" contains unrecognized characters. " +
266 "If this code was created with an older version of Arduino, " +
267 "you may need to use Tools -> Fix Encoding & Reload to update " +
268 "the sketch to use UTF-8 encoding. If not, you may need to " +
269 "delete the bad characters to get rid of this warning."),
270 file.getName()
271 )
272 );
273 System.err.println();
274 }
275 return text;
276 }
277
278
279 /**
280 * Save this piece of code, regardless of whether the modified
281 * flag is set or not.
282 */
283 public void save() throws IOException {
284 if (storage == null)
285 return; /* Nothing to do */
286
287 BaseNoGui.saveFile(storage.getText(), file);
288 storage.clearModified();
289 }
290
291
292 /**
293 * Save this file to another location, used by Sketch.saveAs()
294 */
295 public void saveAs(File newFile) throws IOException {
296 if (storage == null)
297 return; /* Nothing to do */
298
299 BaseNoGui.saveFile(storage.getText(), newFile);
300 renamedTo(newFile);
301 storage.clearModified();
302 }
303 }