download Script.java
Language: Java
Copyright: Copyright 1995
LOC: 348
Project Info
JavaML
Server: SourceForge
Type: cvs
...ml\javaml\java‑ml\examples\
   .cvsignore
   Abacus.java
   AlphaBullet.java
   Animator.java
   AnInterface.java
   AnonymousClass.java
   AnotherApplet.java
   Assignments.java
   AudioItem.java
   Banner.java
   BigInit.java
   Blah.java
   BounceItem.java
   Bubbles.java
   cannon.java
   Chart.java
   ComplexApplet.java
   ComplexApplet2.java
   Continued.java
   crossword.java
   DiningPhilosophers.java
   escherpaint.java
   fillTriangle.java
   FirstApplet.java
   generic_paint.java
   Hangman.java
   ImageLoop.java
   ImageLoopItem.java
   ImageTape.java
   ImageTest.java
   JackhammerDuke.java
   LED.java
   LEDMessage.java
   Letters.java
   OuterClass.java
   Pythagoras.java
   RotateFilter.java
   Script.java
   SimpAnim.html
   SimpAnim.java
   SimpleAnimator.java
   stars.java
   SystemInfo.java
   TumbleItem.java
   voltage.java
   WordMatch.java

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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
///////////////////////////////////////////////////////////////////
//  Script.java   -- LED Sign V1.0f
//
//  Contains the following classes:
//      linkList   -- a linked list class to store the script
//      FuncInfo   -- a class (struct) to hold all the 
//                    information for any function.
//      Script     -- The class that manages the script
//                    including parsing, storage, and
//                    retrieval.
//
//  Revisions:
//     V1.0f: Written July 17 - August 6, 1995
//
//  by Darrick Brown
//     dbrown@cs.hope.edu
//     http://www.cs.hope.edu/~dbrown/
//
//   Copyright 1995
///////////////////////////////////////////////////////////////////

package LED;

import java.awt.*;
import java.io.*;
import java.net.*;

///////////////////////////////////////////////////////////////////
// Function            Code
// --------            ----
// Appear               0
// Sleep                1
// ScrollLeft           2
// ScrollRight          3
// ScrollUp             4
// ScrollDown           5
// Pixel                6
// Blink                7
// OverRight            8
// ScrollCenter         9
// OverCenter           10
// OverLeft             11
// OverUp               12
// OverDown             13
// Do                   97
// Repeat               98
// Reload               99
///////////////////////////////////////////////////////////////////

// A hacked linked list
class linkList
{
   FuncInfo fi;
   linkList next;
}

///////////////////////////////////////////////////////////////////
// The "struct" that contains all the information
// than any function/transition would need.
class FuncInfo
{
   int func;
   int delay;
   int startspace, endspace;
   int times, remaining;
   boolean centered;
   String color;
   String text;
   linkList ret;  // pointer to the return place in the script (for loops);
}

///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
// The class that parses the script and keeps it in memory 
class Script
{
   linkList list;               // the linked list for the script
   linkList ptr,start;          // the current line and start of the list
   String scrpt;
   URL baseURL;

   ///////////////////////////////////////////////////////////////////
   // The constructor
   public Script(URL url, String s) throws IOException
   {
      scrpt = s;
      baseURL = url;
      initScript();
   }

   ///////////////////////////////////////////////////////////////////
   // get the parameters from the functions in the script
   String getParam(String s, String sub)
   {
      int i,j;
      String tmp;

      i = s.indexOf(sub);
      j = s.indexOf("text");

      if(j == -1 || i <= j)  // if the first occurance of "sub" is before 
      {                    // the "text=" (ie not in the message)
         if(i == -1)
            return null;
         else
         {
            tmp = s.substring(i);  // forget everything before the sub
            i = tmp.indexOf("=");
            if(i == -1)
            {
               System.out.println("Error in '"+sub+"' parameter in "+s);
               return null;
            }
            else
            {
               i++;  // one spot after the "="
               if(sub.compareTo("text") == 0)
                  tmp = tmp.substring(i);
               else
               {
                  tmp = tmp.substring(i);
                  if(tmp.indexOf(" ") != -1)
                     tmp = tmp.substring(0,tmp.indexOf(" "));
               }
               tmp.trim();
               return tmp;
            }
         }
      }
      else
         return null;

   }  // End getParam()
   
   ///////////////////////////////////////////////////////////////////
   // get the function info
   FuncInfo getFunc(String s)
   {
      int i;
      String tmp;
      FuncInfo fi = new FuncInfo();
      
      // Assign the defaults
      fi.func = -1;
      fi.delay = 40;
      fi.startspace = 10;
      fi.endspace = 20;
      fi.times = -1;
      fi.remaining = 0;
      fi.centered = false;
      fi.color = new String("");
      fi.text = new String("No text specified");
      fi.ret = null;
      
      //get rid of any starting (and ending) white space, just to be sure.
      s = s.trim(); 

      ////////////////////////////////////////////////////
      // Any parameters that might exist.  This will
      // read in any command line parameters for each
      // function.  For example: Sleep text=blah blah
      // is accepted, but the text will never be used

      tmp = getParam(s,"delay");
      if(tmp != null)
         fi.delay = (new Integer(tmp)).intValue();

      tmp = getParam(s,"clear");
      if(tmp != null && tmp.compareTo("true") == 0)
      {
         fi.centered = true;
         fi.text = new String("");
      }
      else
      {
         tmp = getParam(s,"center");
         if(tmp != null && tmp.compareTo("true") == 0)
            fi.centered = true;
         else
         {
            fi.centered = false;
            tmp = getParam(s,"startspace");
            if(tmp != null)
               fi.startspace = (new Integer(tmp)).intValue();

            tmp = getParam(s,"endspace");
            if(tmp != null)
               fi.endspace = (new Integer(tmp)).intValue();
         }

         tmp = getParam(s,"text");
         if(tmp != null)
            fi.text = tmp;
      }

      tmp = getParam(s,"times");
      if(tmp != null)
      {
         fi.times = (new Integer(tmp)).intValue();
         fi.remaining = fi.times;
      }

      tmp = getParam(s,"pixels");
      if(tmp != null)
      {
         fi.times = (new Integer(tmp)).intValue();
         fi.remaining = fi.times;
      }

      ////////////////////////////////////////////////////
      // set the function number (and some minor
      // tweeks/precautions)
      i = s.indexOf(" ");
      if(i != -1)
         tmp = s.substring(0,i);
      else
         tmp = s;
         
      if(tmp.compareTo("Appear") == 0)
      {
         fi.func = 0;
      }
      else if(tmp.compareTo("Sleep") == 0)
      {
         fi.func = 1;
      }
      else if(tmp.compareTo("ScrollLeft") == 0)
      {
         fi.func = 2;
      }
      else if(tmp.compareTo("ScrollRight") == 0)
      {
         fi.func = 3;
      }
      else if(tmp.compareTo("ScrollUp") == 0)
      {
         fi.func = 4;
      }
      else if(tmp.compareTo("ScrollDown") == 0)
      {
         fi.func = 5;
      }
      else if(tmp.compareTo("Pixel") == 0)
      {
         fi.func = 6;
         
         // Just for precautions dealing with a delay problem.
         // This shouldn't be noticable.
         if(fi.delay < 1)
            fi.delay = 1;

         // Can't allow "times" to be 0 or less, it will cause
         // the sign to freeze (not procede).
         if(fi.times < 1)
            fi.times = 15;
      }
      else if(tmp.compareTo("Blink") == 0)
      {
         fi.func = 7;
         
         if(fi.times < 1)
            fi.times = 2;
      }
      else if(tmp.compareTo("OverRight") == 0)
      {
         fi.func = 8;
      }
      else if(tmp.compareTo("ScrollCenter") == 0)
      {
         fi.func = 9;
      }
      else if(tmp.compareTo("OverCenter") == 0)
      {
         fi.func = 10;
      }
      else if(tmp.compareTo("OverLeft") == 0)
      {
         fi.func = 11;
      }
      else if(tmp.compareTo("OverUp") == 0)
      {
         fi.func = 12;
      }
      else if(tmp.compareTo("OverDown") == 0)
      {
         fi.func = 13;
      }
      else if(tmp.compareTo("Do") == 0)
      {
         fi.func = 97;  // This marks a place for the "repeats" to go back to.
      }
      else if(tmp.compareTo("Repeat") == 0)
      {
         fi.func = 98;
      }
      else if(tmp.compareTo("Reload") == 0)
      {
         fi.func = 99;
      }

      return fi;
   }  // End getFunc()

   //////////////////////////////////////////////////////////////////
   // get the next function
   FuncInfo nextFunc()
   {
      FuncInfo fi;

      fi = ptr.fi;
      ptr = ptr.next;
      
      switch(fi.func)
      {
         case 97:  // Do
            fi = nextFunc();   // skip the "Do function; its just a marker
           break;

         case 98:  // a Repeat

            // If it doesn't repeat infinitely...
            if(fi.times != -1)
            {
               // One less time
               fi.remaining--;
               if(fi.remaining == 0)
               {
                  fi.remaining = fi.times;  // reset the loop
                  fi = nextFunc();
               }
               else
               {
                  ptr = fi.ret;  // Jump back to the last "Do"
                  fi = nextFunc();
               }
            }
            else
            {
               ptr = fi.ret;  // Jump back to the last "Do"
               fi = nextFunc();
            }
           break;

         case 99:  // Reload
            try {
		initScript();      // Reload the script from the URL
	    } catch (IOException e) {
	    }
            fi = nextFunc();   // and get the first function.
           break;
      }

      return fi;
   }  // End nextFunc()

   //////////////////////////////////////////////////////////////////
   // just a simple function to see if it is a color code
   boolean isColor(char t)
   {
      if(t == 'r' || t == 'g' || t == 'b' || t == 'y' || t == 'o' || t == 'p')
         return true;
      else
         return false;
   }
      
   //////////////////////////////////////////////////////////////////
   // Read in the script into a linked list of FuncInfo's 
   void initScript() throws IOException
   {
      InputStream file;
      DataInputStream dis;
      String line;
      String tmp;
      int listlen;
      int a,b;
      int dos;
      char c;
      char t;

      file = (new URL(baseURL,scrpt)).openStream();
      dis = new DataInputStream(file);

      list = new linkList();                                    // The linked list
      start = list;                                             // The head of the list
      ptr = list;                                               // The current element
      listlen = 0;
      dos = 0;                                                  // Used to know how many Do's there are
      while((line = dis.readLine()) != null)
      {
         line = line.trim();                                    // cut off white space at the beginning and end
         if(!(line.startsWith("!!")) && (line.length() != 0))   // Not a comment or blank line
         {
            listlen++;
            ptr.fi = getFunc(line);                             // Get the function number
            if(ptr.fi.func == 97)
               dos++;                                           // Chalk up another "Do"
            ptr.next = new linkList();
            ptr = ptr.next;  // advance to the next command
         }
      }

      // Parse out the color codes!!!!  (if there are any)
      ptr = start;
      for(a=0;a<listlen;a++)
      {
         if(ptr.fi.func >= 2 && ptr.fi.func <= 97)
         {
            tmp = ptr.fi.text;
            c = 'r';  // the default color
            for(b=0;b<tmp.length();b++)
            {
               if((char)(tmp.charAt(b)) == (char)('\\'))  // if there is a '\' does the following
               {                                          // letter indicate a color.
                  b++;
                  t = tmp.charAt(b);
                  if(isColor(t))
                  {
                     c = t;
                     tmp = (tmp.substring(0,b-1)).concat(tmp.substring(b+1));  // take the "\r" out
                     b-=1;
                  }
                  else if(t == '\\')  // Are they trying to delimit the backslash?
                  {
                     tmp = (tmp.substring(0,b)).concat(tmp.substring(b+1));  // delimit the '\'
                     b--;
                  }
               }

               ptr.fi.color = ptr.fi.color.concat((new Character(c)).toString());
            }
            ptr.fi.text = tmp;
         }
         ptr = ptr.next;
      }

      // Ok now lets set the return pointers for the loops
      ptr = start;
      linkList stack[] = new linkList[dos];  // Allocate the array
      dos = 0;
      for(a=0;a<listlen;a++)
      {
         if(ptr.fi.func == 97)
         {
            stack[a] = new linkList();
            stack[a] = ptr;
            dos++;
         }
         else if(ptr.fi.func == 98)
         {
            if(dos > 0)
            {
               ptr.fi.ret = stack[dos-1];
               dos--;
            }
            else
            {
               // OMYGOSH!! Script error output!!!!
               System.out.println("Repeat error in line : Repeat times="+ptr.fi.times);
               System.out.println("     Mismatched Do/Repeats?");
            }
         }
         ptr = ptr.next;
      }

      ptr = start;

      file.close();
      dis.close();

   }  // End initScript()
}  // End Class Script

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us