/* Copyright (C) 2014 A.Vontobel  
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * The license is in LICENCE-GPL.txt.
 * If not, see http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
 */

package net.sf.drawj2d.yacas;

import java.awt.Graphics2D;
import java.awt.Dimension;
import java.io.InputStreamReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author av
 */
public class PlotDataParser {
    
    Graphics2D g;
    Dimension dim;
    private int xleft = 0;
    private int ytop = 0;
    
    private boolean verbose = false;
    private final boolean debug = false;
    
    /**
     * Creates a new instance of PlotDataParser
     * @param g The graphic context. java.awt.Graphics2D
     */
    public PlotDataParser (Graphics2D g, int pxwidth, int pxheight) {
        this.g = g;
        dim = new Dimension(pxwidth, pxheight);
    }
    
    /** TODO Zweck? Auswirkung in Grapher klären. */
    public void setOrigin(int xleft, int ytop) {
        this.xleft = xleft;
        this.ytop = ytop;
    }
    
    public void setVerbose(boolean verbose) {
        this.verbose = verbose;
    }
    
    /** Let the interpreter source the input file
     * @param sourcefile source file or "_opt_stdin"
     * @return true if no exception has been caught. */
    public boolean sourceFile(String sourcefile) {
        boolean success = false;
        StringBuilder sb = new StringBuilder();
        
        InputStreamReader eingabestrom;
        try {
            if (!sourcefile.equals("_opt_stdin")) {
                eingabestrom = new InputStreamReader(new FileInputStream(new File(sourcefile)));
            } else {
                eingabestrom = new InputStreamReader(System.in); // Read Stdin
            }
            BufferedReader eingabe = new BufferedReader(eingabestrom);

            String zeile;
            boolean isEOF = false;

            do {
                zeile = eingabe.readLine();
                if (zeile != null) {
                    sb.append(AddOutputLine(zeile));
                    sb.append("\n");
                } else {
                    isEOF = true;
                }

            } while (isEOF == false);
            eingabe.close();
            eingabestrom.close();
        } catch (FileNotFoundException ex) {
            if (debug) Logger.getLogger(PlotDataParser.class.getName()).log(Level.SEVERE, null, ex);
            else System.err.println(ex.getLocalizedMessage());
        } catch (IOException ex) {
            Logger.getLogger(PlotDataParser.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        /*
        
	    } else {
                LinkedList sourcelist = new LinkedList();
                InputStreamReader eingabestrom;
                eingabestrom = new InputStreamReader(System.in); // Read Stdin
                BufferedReader eingabe = new BufferedReader(eingabestrom);
                String zeile;
                boolean isEOF = false;
                do {
                    zeile = eingabe.readLine();
                    if (zeile != null) sourcelist.add(zeile);
                    else isEOF = true;
                } while (isEOF == false);
                eingabe.close();
                eingabestrom.close();
                // Evaluate sourcelist
                if (verbose) System.out.println();
                int zeilennr = 0;
                for (String s : sourcelist) {
                    zeilennr++;
                    if (verbose) System.out.println(Fkt.nf(zeilennr, 3) + ":  " + s);
                    Thing input = new Thing(s);
                    Thing ans = interp.eval(input);
                    if (verbose) System.out.println("   @  " + ans.toString());
                }
                if (verbose) System.out.println();
                success = true;
	    }
        */
        
        if (verbose) System.out.println(sb.toString());
        Grapher yg_parser = new Grapher(sb.toString());
        success = true;
        yg_parser.paint(g, 0, 0, dim);
        
        return success;
    }
    
    
  /** strips $dplot2d and trailing $ 
   * Function is derived from class net.sf.yacas.ConsoleApplet, 2014*/
  private String AddOutputLine(String outp)
  {
      String dollared = "";
    if (outp.length() > 0)
    {
      int dollarPos = outp.indexOf("$");
      while (dollarPos >= 0)
      {
//        // Print plain text before the dollared content
//        if (dollarPos > 0)
//        {
//          AddLinesStatic(48,"",outp.substring(0,dollarPos));
//        }
        // Strip off the left dollar sign
        outp = outp.substring(dollarPos+1,outp.length());

        // Find the right dollar sign, and split there too
        dollarPos = outp.indexOf("$");
        dollared = outp.substring(0,dollarPos);
        outp = outp.substring(dollarPos+1,outp.length());

//System.out.println("Dollared: "+dollared);
        int plotPos = dollared.indexOf("plot2d:");
        if (plotPos >= 0)
        {
          dollared = dollared.substring(plotPos+7);
//System.out.println("Plotting: ["+dollared+"]");
//          addLine(new PromptedGraph2DLine(48, "Out>", iPromptFont, iPromptColor, dollared));
        }
        else
        {
//          addLine(new PromptedFormulaLine(48, "Out>", iPromptFont, iPromptColor, dollared));
        }
        dollarPos = outp.indexOf("$");
      }
//      // If there is some plain text left at the end, print
//      if (outp.length() > 0)
//      {
//        AddLinesStatic(48,"",outp.toString());
//      }
    }
//    outputDirty = true;
    return dollared;
  }
}

 

Ads help maintain this website.