home *** CD-ROM | disk | FTP | other *** search
/ Hacks & Cracks / Hacks_and_Cracks.iso / cracks / cracks2.zip / LEDSIGN.ZIP / LEDSign / LED / Script.java < prev   
Text File  |  1996-03-21  |  21KB  |  704 lines

  1. ///////////////////////////////////////////////////////////////////
  2. //  Script.java   -- LED Sign V2.5
  3. //
  4. //  Contains the following classes:
  5. //      Script     -- The class that manages the script
  6. //                    including parsing, storage, and
  7. //                    retrieval.
  8. //
  9. //  Revisions:
  10. //     V2.7: See "Revisions" doc for more info
  11. //
  12. //     V2.5: Fixed all known bugs in previous versions!  Added
  13. //           the new feature of ledsize, which allows the user
  14. //           to specify in pixels how big the LED's (1-4).
  15. //           Thanks to Robert B. Denny (rdenny@dc3.com) for
  16. //           code and input!
  17. //           Modified Dec 20-26, 1995
  18. //
  19. //     V2.0beta: Modified V1.0 to comply with Pre-Beta java.
  20. //               A problem with delay causes a jerky display.
  21. //               Modified Oct 20 - 29, 1995
  22. //
  23. //     V1.0: Written July 17 - August 6, 1995
  24. //
  25. //  by Darrick Brown
  26. //     dbrown@cs.hope.edu
  27. //     http://www.cs.hope.edu/~dbrown/
  28. //
  29. //  ⌐ Copyright 1995
  30. ///////////////////////////////////////////////////////////////////
  31.  
  32. import java.awt.*;
  33. import java.io.*;
  34. import java.util.*;
  35. import java.net.*;
  36. import FuncInfo;
  37. import linkList;
  38.  
  39. ///////////////////////////////////////////////////////////////////
  40. // Function            Code
  41. // --------            ----
  42. // Appear               0
  43. // Sleep                1
  44. // ScrollLeft           2
  45. // ScrollRight          3
  46. // ScrollUp             4
  47. // ScrollDown           5
  48. // Pixel                6
  49. // Blink                7
  50. // OverRight            8
  51. // ScrollCenter         9
  52. // OverCenter           10
  53. // OverLeft             11
  54. // OverUp               12
  55. // OverDown             13
  56. // Do                   97
  57. // Repeat               98
  58. // Reload               99
  59. ///////////////////////////////////////////////////////////////////
  60.  
  61. ///////////////////////////////////////////////////////////////////
  62. ///////////////////////////////////////////////////////////////////
  63. // The class that parses the script and keeps it in memory 
  64. public class Script
  65. {
  66.    linkList list;               // the linked list for the script
  67.    linkList ptr,start;          // the current line and start of the list
  68.    int ok;
  69.    String scrpt;
  70.    URL documentURL;
  71.  
  72.    ///////////////////////////////////////////////////////////////////
  73.    // The constructor
  74.    public Script(URL url, String s)
  75.    {
  76.       scrpt = s;
  77.       documentURL = url;
  78.       if(initScript() == -1)
  79.       {
  80.          ok = -1;
  81.       }
  82.       else
  83.       {
  84.          ok = 1;
  85.       }
  86.    }
  87.  
  88.    ///////////////////////////////////////////////////////////////////
  89.    // get the parameters from the functions in the script
  90.    String getParam(String s, String sub)
  91.    {
  92.       int i,j;
  93.       String tmp;
  94.  
  95.       i = s.indexOf(sub);
  96.       j = s.indexOf("text");
  97.  
  98.       if(j == -1 || i <= j)  // if the first occurance of "sub" is before 
  99.       {                      // the "text=" (ie not in the message)
  100.          if(i == -1)
  101.             return null;
  102.          else
  103.          {
  104.             tmp = s.substring(i);  // forget everything before the sub
  105.             i = tmp.indexOf("=");
  106.             if(i == -1)
  107.             {
  108.                System.out.println("Error in '"+sub+"' parameter in "+s);
  109.                return null;
  110.             }
  111.             else
  112.             {
  113.                i++;  // one spot after the "="
  114.                if(sub.compareTo("text") == 0)
  115.                   tmp = tmp.substring(i);
  116.                else
  117.                {
  118.                   tmp = tmp.substring(i);
  119.                   if(tmp.indexOf(" ") != -1)
  120.                      tmp = tmp.substring(0,tmp.indexOf(" "));
  121.                }
  122.                tmp.trim();
  123.                return tmp;
  124.             }
  125.          }
  126.       }
  127.       else
  128.          return null;
  129.  
  130.    }  // End getParam()
  131.    
  132.    ///////////////////////////////////////////////////////////////////
  133.    // get the function info
  134.    FuncInfo getFunc(String s)
  135.    {
  136.       int i;
  137.       String tmp;
  138.       FuncInfo fi = new FuncInfo();
  139.       
  140.       // Assign the defaults
  141.       fi.func = -1;
  142.       fi.delay = 40;
  143.       fi.startspace = 10;
  144.       fi.endspace = 20;
  145.       fi.times = -1;
  146.       fi.remaining = 0;
  147.       fi.centered = false;
  148.       fi.color = new String("");
  149.       fi.text = new String("No text specified");
  150.       fi.url = null;
  151.       fi.ret = null;
  152.       
  153.       //get rid of any starting (and ending) white space, just to be sure.
  154.       s = s.trim(); 
  155.  
  156.       ////////////////////////////////////////////////////
  157.       // Any parameters that might exist.  This will
  158.       // read in any command line parameters for each
  159.       // function.  For example: Sleep text=blah blah
  160.       // is accepted, but the text will never be used
  161.  
  162.       tmp = getParam(s,"delay");
  163.       if(tmp != null)
  164.          fi.delay = (new Integer(tmp)).intValue();
  165.  
  166.       tmp = getParam(s,"clear");
  167.       if(tmp != null && tmp.compareTo("true") == 0)
  168.       {
  169.          fi.centered = true;
  170.          fi.text = new String("");
  171.       }
  172.       else
  173.       {
  174.          tmp = getParam(s,"center");
  175.          if(tmp != null && tmp.compareTo("true") == 0)
  176.             fi.centered = true;
  177.          else
  178.          {
  179.             fi.centered = false;
  180.             tmp = getParam(s,"startspace");
  181.             if(tmp != null)
  182.                fi.startspace = (new Integer(tmp)).intValue();
  183.  
  184.             tmp = getParam(s,"endspace");
  185.             if(tmp != null)
  186.                fi.endspace = (new Integer(tmp)).intValue();
  187.          }
  188.  
  189.          tmp = getParam(s,"text");
  190.          if(tmp != null)
  191.             fi.text = tmp;
  192.       }
  193.  
  194.       tmp = getParam(s,"times");
  195.       if(tmp != null)
  196.       {
  197.          fi.times = (new Integer(tmp)).intValue();
  198.          fi.remaining = fi.times;
  199.       }
  200.  
  201.       tmp = getParam(s,"pixels");
  202.       if(tmp != null)
  203.       {
  204.          fi.times = (new Integer(tmp)).intValue();
  205.          fi.remaining = fi.times;
  206.       }
  207.  
  208.       tmp = getParam(s,"URL");
  209.       if(tmp != null)
  210.       {
  211.          if(tmp.indexOf(',') != -1)
  212.          {
  213.             // They specified a frame target.
  214.             // Separate out the target and URL.
  215.             fi.target = tmp.substring(tmp.indexOf(',') + 1);
  216.             tmp = tmp.substring(0,tmp.indexOf(','));
  217.          }
  218.          else
  219.             fi.target = new String("");
  220.  
  221.          try
  222.          {
  223.             fi.url = new URL(tmp);
  224.          }
  225.          catch(MalformedURLException e)
  226.          {
  227.             System.out.println("Bad URL: "+tmp);
  228.             fi.url = null;
  229.          }
  230.       }
  231.       else
  232.       {
  233.          fi.url = null;
  234.       }
  235.  
  236.       ////////////////////////////////////////////////////
  237.       // set the function number (and some minor
  238.       // tweeks/precautions)
  239.       i = s.indexOf(" ");
  240.       if(i != -1)
  241.          tmp = s.substring(0,i);
  242.       else
  243.          tmp = s;
  244.          
  245.       if(tmp.compareTo("Appear") == 0)
  246.       {
  247.          fi.func = 0;
  248.       }
  249.       else if(tmp.compareTo("Sleep") == 0)
  250.       {
  251.          fi.func = 1;
  252.       }
  253.       else if(tmp.compareTo("ScrollLeft") == 0)
  254.       {
  255.          fi.func = 2;
  256.       }
  257.       else if(tmp.compareTo("ScrollRight") == 0)
  258.       {
  259.          fi.func = 3;
  260.       }
  261.       else if(tmp.compareTo("ScrollUp") == 0)
  262.       {
  263.          fi.func = 4;
  264.       }
  265.       else if(tmp.compareTo("ScrollDown") == 0)
  266.       {
  267.          fi.func = 5;
  268.       }
  269.       else if(tmp.compareTo("Pixel") == 0)
  270.       {
  271.          fi.func = 6;
  272.          
  273.          // Just for precautions dealing with a delay problem.
  274.          // This shouldn't be noticable.
  275.          if(fi.delay < 1)
  276.             fi.delay = 1;
  277.  
  278.          // Can't allow "times" to be 0 or less, it will cause
  279.          // the sign to freeze (not procede).
  280.          if(fi.times < 1)
  281.             fi.times = 15;
  282.       }
  283.       else if(tmp.compareTo("Blink") == 0)
  284.       {
  285.          fi.func = 7;
  286.          
  287.          if(fi.times < 1)
  288.             fi.times = 2;
  289.       }
  290.       else if(tmp.compareTo("OverRight") == 0)
  291.       {
  292.          fi.func = 8;
  293.       }
  294.       else if(tmp.compareTo("ScrollCenter") == 0)
  295.       {
  296.          fi.func = 9;
  297.       }
  298.       else if(tmp.compareTo("OverCenter") == 0)
  299.       {
  300.          fi.func = 10;
  301.       }
  302.       else if(tmp.compareTo("OverLeft") == 0)
  303.       {
  304.          fi.func = 11;
  305.       }
  306.       else if(tmp.compareTo("OverUp") == 0)
  307.       {
  308.          fi.func = 12;
  309.       }
  310.       else if(tmp.compareTo("OverDown") == 0)
  311.       {
  312.          fi.func = 13;
  313.       }
  314.       else if(tmp.compareTo("Do") == 0)
  315.       {
  316.          fi.func = 97;  // This marks a place for the "repeats" to go back to.
  317.       }
  318.       else if(tmp.compareTo("Repeat") == 0)
  319.       {
  320.          fi.func = 98;
  321.       }
  322.       else if(tmp.compareTo("Reload") == 0)
  323.       {
  324.          fi.func = 99;
  325.       }
  326.  
  327.       fi.store = fi.text;
  328.  
  329.       return fi;
  330.    }  // End getFunc()
  331.  
  332.    //////////////////////////////////////////////////////////////////
  333.    // get the next function
  334.    FuncInfo nextFunc()
  335.    {
  336.       FuncInfo fi;
  337.  
  338.       fi = ptr.fi;
  339.       ptr = ptr.next;
  340.  
  341.       switch(fi.func)
  342.       {
  343.          case 97:  // Do
  344.             fi = nextFunc();   // skip the "Do function; its just a marker
  345.            break;
  346.  
  347.          case 98:  // a Repeat
  348.  
  349.             // If it doesn't repeat infinitely...
  350.             if(fi.times >= 0)
  351.             {
  352.                // One less time
  353.                fi.remaining--;
  354.                if(fi.remaining <= 0)
  355.                {
  356.                   fi.remaining = fi.times;  // reset the loop
  357.                   fi = nextFunc();
  358.                }
  359.                else
  360.                {
  361.                   ptr = fi.ret;  // Jump back to the last "Do"
  362.                   fi = nextFunc();
  363.                }
  364.             }
  365.             else
  366.             {
  367.                ptr = fi.ret;  // Jump back to the last "Do"
  368.                fi = nextFunc();
  369.             }
  370.            break;
  371.  
  372.          case 99:  // Reload
  373.             // Reload the script from the URL
  374.             if(initScript() == -1)  // If the script path is bad...
  375.             {
  376.                fi = null;
  377.             }
  378.             else
  379.             {
  380.                fi = nextFunc();   // and get the first function.
  381.             }
  382.            break;
  383.       }
  384.  
  385.       return fi;
  386.    }  // End nextFunc()
  387.  
  388.    //////////////////////////////////////////////////////////////////
  389.    // just a simple function to see if it is a color code
  390.    boolean isColor(char t)
  391.    {
  392.       if(t == 'r' || t == 'g' || t == 'b' || t == 'y' || t == 'o' || t == 'p' || t == 'w' || t == 'c')
  393.          return true;
  394.       else
  395.          return false;
  396.    }
  397.       
  398.  
  399.    //////////////////////////////////////////////////////////////////
  400.    // Get the varible defined
  401.    String getVar(String s, int i)
  402.    {
  403.       String t;
  404.       
  405.       if(s.charAt(i) == '{')
  406.       {
  407.          t = s.substring(i+1);
  408.          t = t.substring(0,t.indexOf('}'));
  409.       }
  410.       else
  411.          t = String.valueOf(s.charAt(i));
  412.  
  413.       return t;
  414.    }
  415.  
  416.    //////////////////////////////////////////////////////////////////
  417.    // create the final text line from parsing the store line
  418.    //   Add any codes (ie \t, \r, \g, \b, etc.) here to parse
  419.    //   out of the text line.
  420.    FuncInfo parseLine(FuncInfo fi)
  421.    {
  422.       String tmp;
  423.       String time;
  424.       String month[] = {"Jan","Feb","Mar","Apr","May","Jun",
  425.                         "Jul","Aug","Sept","Oct","Nov","Dec"};
  426.       String Month[] = {"January","February","March","April","May","June",
  427.                         "July","August","September","October","November","December"};
  428.       String day[] = {"Sun","Mon","Tues","Wed","Thur","Fri","Sat"};
  429.       String Day[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
  430.       String ddmmyy;
  431.       int min;
  432.       int pm;
  433.       Date date = new Date();
  434.       int a,b;
  435.       int i;
  436.       char c;
  437.       String t;         // The tag  (eg. text=Hello \ythere,  t=y)
  438.  
  439.       tmp = fi.store;
  440.       fi.color = "";
  441.  
  442.       if(fi.func == 0 || (fi.func >= 2 && fi.func <= 97))
  443.       {
  444.          c = 'r';  // the default color
  445.          b = 0;
  446.          while(b < tmp.length())
  447.          {
  448.             if(tmp.charAt(b) == '\\')  // if there is a '\' does the following
  449.             {                                          // letter indicate a color.
  450.                b++;
  451.                // Get the tag!
  452.                if(tmp.charAt(b) == '{')
  453.                {
  454.                   t = tmp.substring(b+1);
  455.  
  456.                   // cut out the \{XX}
  457.                   tmp = tmp.substring(0,b-1).concat(t.substring(t.indexOf('}')+1));
  458.                   t = t.substring(0,t.indexOf('}'));
  459.                   b -= 1;
  460.                }
  461.                else
  462.                {
  463.                   t = tmp.substring(b,b+1);
  464.                   tmp = (tmp.substring(0,b-1)).concat(tmp.substring(b+1));  // take the "\r" out
  465.                   b -= 1;
  466.                }
  467.  
  468.                // set the 
  469.                if(t.length() == 1 && isColor(t.charAt(0)))
  470.                {
  471.                   c = t.charAt(0);
  472.                }
  473.                else if(t.compareTo("tt") == 0)
  474.                {
  475.                   // it is the "time" variable!!
  476.                   if(date.getHours() >= 12)
  477.                      pm = 1;
  478.                   else 
  479.                      pm = 0;
  480.  
  481.                   if(pm == 1)
  482.                   {
  483.                      a = date.getHours();
  484.                      if(a == 12)
  485.                         time  = String.valueOf(12);
  486.                      else
  487.                         time = String.valueOf(date.getHours()-12);
  488.                   }
  489.                   else
  490.                   {
  491.                      a = date.getHours();
  492.                      if(a == 0)
  493.                         time = String.valueOf(12);
  494.                      else
  495.                         time = String.valueOf(a);
  496.                   }
  497.  
  498.                   time = time.concat(":");
  499.                   
  500.                   min = date.getMinutes();
  501.                   if(min >= 10)
  502.                      time = time.concat(String.valueOf(min));
  503.                   else
  504.                   {
  505.                      time = time.concat("0");
  506.                      time = time.concat(String.valueOf(min));
  507.                   }
  508.  
  509.                   if(pm == 1)
  510.                      time = time.concat(" pm");
  511.                      else
  512.                      time = time.concat(" am");
  513.  
  514.                   tmp = ((tmp.substring(0,b)).concat(time)).concat(tmp.substring(b));
  515.  
  516.                   b += time.length();
  517.  
  518.                   for(i = 0; i < time.length(); i++)
  519.                      fi.color = (fi.color).concat((new Character(c)).toString());
  520.  
  521.                } // End time
  522.                else if(t.compareTo("dd") == 0 || t.compareTo("DD") == 0)   // Set the current date
  523.                {
  524.                   if(t.compareTo("dd") == 0)
  525.                      ddmmyy = day[date.getDay()];
  526.                   else
  527.                      ddmmyy = Day[date.getDay()];
  528.                   
  529.                   // Set up the color
  530.                   for(i = 0; i < ddmmyy.length(); i++)
  531.                      fi.color = (fi.color).concat((new Character(c)).toString());
  532.  
  533.                   tmp = ((tmp.substring(0,b)).concat(ddmmyy)).concat(tmp.substring(b));
  534.                   b += ddmmyy.length();
  535.                }
  536.                else if(t.compareTo("dn") == 0)
  537.                {
  538.                   ddmmyy = String.valueOf(date.getDate());
  539.  
  540.                   // Set up the color
  541.                   for(i = 0; i < ddmmyy.length(); i++)
  542.                      fi.color = (fi.color).concat((new Character(c)).toString());
  543.  
  544.                   tmp = ((tmp.substring(0,b)).concat(ddmmyy)).concat(tmp.substring(b));
  545.                   b += ddmmyy.length();
  546.                }
  547.                else if(t.compareTo("mm") == 0 || t.compareTo("MM") == 0)  
  548.                {
  549.                   if(t.compareTo("mm") == 0)
  550.                      ddmmyy = month[date.getMonth()];
  551.                   else
  552.                      ddmmyy = Month[date.getMonth()];
  553.  
  554.                   // Set up the color
  555.                   for(i = 0; i < ddmmyy.length(); i++)
  556.                      fi.color = (fi.color).concat((new Character(c)).toString());
  557.  
  558.                   tmp = ((tmp.substring(0,b)).concat(ddmmyy)).concat(tmp.substring(b));
  559.                   b += ddmmyy.length();
  560.                }
  561.                else if(t.compareTo("mn") == 0)
  562.                {
  563.                   ddmmyy = String.valueOf(date.getMonth()+1);
  564.  
  565.                   // Set up the color
  566.                   for(i = 0; i < ddmmyy.length(); i++)
  567.                      fi.color = (fi.color).concat((new Character(c)).toString());
  568.  
  569.                   tmp = ((tmp.substring(0,b)).concat(ddmmyy)).concat(tmp.substring(b));
  570.                   b += ddmmyy.length();
  571.                }
  572.                else if(t.compareTo("yy") == 0 || t.compareTo("YY") == 0)
  573.                {
  574.                   if(t.compareTo("YY") == 0)
  575.                      ddmmyy = String.valueOf(date.getYear()+1900);
  576.                   else
  577.                      ddmmyy = String.valueOf(date.getYear()%100);
  578.  
  579.                   // Set up the color 
  580.                   for(i = 0; i < ddmmyy.length(); i++)
  581.                      fi.color = (fi.color).concat((new Character(c)).toString());
  582.  
  583.                   tmp = ((tmp.substring(0,b)).concat(ddmmyy)).concat(tmp.substring(b));
  584.                   b += ddmmyy.length();
  585.  
  586.                }  // End short date
  587.                else if(t.compareTo("\\") == 0)  // Are they trying to delimit the backslash?
  588.                {
  589.                   tmp = (tmp.substring(0,b)).concat(tmp.substring(b+1));  // delimit the '\'
  590.                   b--;
  591.                }
  592.                else
  593.                {
  594.                   // A little error output
  595.                   System.out.println("Backslash (\\) error in text line: "+ fi.store);
  596.                }
  597.                
  598.             }  // END - if(tmp.charAt(b) == '\\') 
  599.             else
  600.             {
  601.                b++;
  602.                fi.color = fi.color.concat((new Character(c)).toString());
  603.             }
  604.             
  605.          }  // END - for(...) 
  606.  
  607.       } // END - if(fi.func == ...)
  608.  
  609.       fi.text = tmp;
  610.       
  611.       return fi;
  612.       
  613.    }
  614.  
  615.    //////////////////////////////////////////////////////////////////
  616.    // Read in the script into a linked list of FuncInfo's 
  617.    int initScript()
  618.    {
  619.       InputStream file;
  620.       DataInputStream dis;
  621.       URL url;
  622.       String line;
  623.       int listlen;
  624.       int dos;
  625.       int a;
  626.  
  627.       try
  628.       {
  629.          url = new URL(documentURL,scrpt);
  630.             
  631.          file = url.openStream();
  632.          dis = new DataInputStream(file);
  633.       }
  634.       catch(IOException e)
  635.       {
  636.          e.printStackTrace();
  637.          return -1;
  638.       }
  639.  
  640.       try
  641.       {
  642.          list = new linkList();                                    // The linked list
  643.          start = list;                                             // The head of the list
  644.          ptr = list;                                               // The current element
  645.          listlen = 0;
  646.          dos = 0;                                                  // Used to know how many Do's there are
  647.          while((line = dis.readLine()) != null)
  648.          {
  649.             line = line.trim();                                    // cut off white space at the beginning and end
  650.             if(!(line.startsWith("!!")) && (line.length() != 0))   // Not a comment or blank line
  651.             {
  652.                listlen++;
  653.                ptr.fi = getFunc(line);                             // Get the function number
  654.                if(ptr.fi.func == 97)
  655.                   dos++;                                           // Chalk up another "Do"
  656.                ptr.next = new linkList();
  657.                ptr = ptr.next;  // advance to the next command
  658.             }
  659.          }
  660.  
  661.          // Ok now lets set the return pointers for the loops
  662.          ptr = start;
  663.          linkList stack[] = new linkList[dos];  // Allocate the array
  664.          dos = 0;
  665.          for(a=0;a<listlen;a++)
  666.          {
  667.             if(ptr.fi.func == 97) // A "Do"
  668.             {
  669.                stack[dos] = new linkList();
  670.                stack[dos] = ptr;
  671.                dos++;
  672.             }
  673.             else if(ptr.fi.func == 98)  // A Repeat
  674.             {
  675.                if(dos > 0)
  676.                {
  677.                   dos--;
  678.                   ptr.fi.ret = stack[dos];
  679.                }
  680.                else
  681.                {
  682.                   // OMYGOSH!! Script error output!!!!
  683.                   System.out.println("Repeat error in line : Repeat times="+ptr.fi.times);
  684.                   System.out.println("     Mismatched Do/Repeats?");
  685.                }
  686.             }
  687.             ptr = ptr.next;
  688.          }
  689.  
  690.          ptr = start;
  691.  
  692.          file.close();
  693.          dis.close();
  694.       }
  695.       catch (IOException e)
  696.       {
  697.          // Error!
  698.          return -1;  // We could not read from the script.  This is a bad script path.
  699.       }
  700.       
  701.       return 1;
  702.    }  // End initScript()
  703. }  // End Class Script
  704.