-
Notifications
You must be signed in to change notification settings - Fork 15
/
Data.pde
121 lines (93 loc) · 2.59 KB
/
Data.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
///////////////////////////
// DATA CLASS
// Marius Watz - http://workshop.evolutionzone.com
class Data {
ArrayList datalist;
String filename,data[];
int datalineId;
// begin data saving
void beginSave() {
datalist=new ArrayList();
}
void add(String s) {
datalist.add(s);
}
void add(float val) {
datalist.add(""+val);
}
void add(int val) {
datalist.add(""+val);
}
void add(boolean val) {
datalist.add(""+val);
}
void endSave(String _filename) {
filename=_filename;
data=new String[datalist.size()];
data=(String [])datalist.toArray(data);
saveStrings(filename, data);
println("Saved data to '"+filename+
"', "+data.length+" lines.");
}
void load(String _filename) {
filename=_filename;
datalineId=0;
data=loadStrings(filename);
println("Loaded data from '"+filename+
"', "+data.length+" lines.");
}
float readFloat() {
return float(data[datalineId++]);
}
int readInt() {
return int(data[datalineId++]);
}
boolean readBoolean() {
return boolean(data[datalineId++]);
}
String readString() {
return data[datalineId++];
}
// Utility function to auto-increment filenames
// based on filename templates like "name-###.txt"
public String getIncrementalFilename(String templ) {
String s="",prefix,suffix,padstr,numstr;
int index=0,first,last,count;
File f;
boolean ok;
first=templ.indexOf('#');
last=templ.lastIndexOf('#');
count=last-first+1;
if( (first!=-1)&& (last-first>0)) {
prefix=templ.substring(0, first);
suffix=templ.substring(last+1);
// Comment out if you want to use absolute paths
// or if you're not using this inside PApplet
//if(sketchPath!=null) prefix=savePath(prefix);
index=0;
ok=false;
do {
padstr="";
numstr=""+index;
for(int i=0; i< count-numstr.length(); i++) padstr+="0";
s=prefix+padstr+numstr+suffix;
f=new File(s);
ok=!f.exists();
index++;
// Provide a panic button. If index > 10000 chances are it's an
// invalid filename.
if(index>10000) ok=true;
}
while(!ok);
// Panic button - comment out if you know what you're doing
if(index> 10000) {
println("getIncrementalFilename thinks there is a problem - "+
"Is there more than 10000 files already in the sequence "+
" or is the filename invalid?");
println("Returning "+prefix+"ERR"+suffix);
return prefix+"ERR"+suffix;
}
}
return s;
}
}