Gl Graph

Gl Graph Source of main.cpp


#include <main.h>
#include <iostream>
#include <fstream>
#include <string>
#include <unordered_map>
#include <glxwin.h>
#include <glutwin.h>
#include <glimagerenderer.h>
#include <drawableset.h>
#include "mathfunctiondrawable.h"
#include "lines2d.h"
#include "lines3d.h"
#include "map3d.h"
#include "coloredmap.h"
#include "mapquad.h"
#include <GL/glut.h>
 
 
/**
 * parse paramater whose has the format : --key=value and store in the map
 **/
 
 
static void read_param(int argc, char *argv[], std::unordered_map<std::string, std::string> * map)
{
    for (int i = 0; i < argc; i++)
    {
        std::string arg(argv[i]);
        std::string param;
        std::string value;
 
        int pos = arg.find_first_of('=', 0);
        if (pos == -1)
        {
            param = arg;
            value = "";
        }
        else
        {
            param = std::string(arg, 0, pos);
            value = std::string(arg, pos + 1, arg.length() - 1);
        }
 
        (*map)[param] = value;
    }
}
 
static void print_usage()
{
    std::cout << "glgraph Usage :" << std::endl;
    std::cout << "--file[id]=/somepath/somefile  or --stdin to specifie input data" << std::endl;
    std::cout << "--format=someformat where someformat is the formating string as used in scanf" << std::endl;
    std::cout << "       default format is '%f;%f'" << std::endl;
    std::cout << "       example of other format :  '%f %*s %f'" << std::endl;
    std::cout << "       example of other format :  '%f %*20s %f'" << std::endl;
    std::cout << "       example of other format :  '%f %*20s %f'" << std::endl;
    std::cout << "--maxline=somenumber is the maximum proceded lines" << std::endl;
    std::cout << "--title=somewindowtitle  is wished title of the window" << std::endl;
    std::cout << "--width=somewidth is wished width of the window" << std::endl;
    std::cout << "--heigh=someheigh is wished height of the window" << std::endl;
    std::cout << "--debug to print additionnal debug info" << std::endl;
    std::cout << "--glut  to use Glut lib" << std::endl;
    std::cout << "--help display this help" << std::endl;
}
 
 
static void load(DrawableSet * set,std::string  filename,std::string  datatype,int maxline,std::string format,std::unordered_map<std::string, std::string> & args)
{
    DrawableDataReader * data=NULL;
    if (!datatype.empty())
    {
        if (datatype.compare("Line3D") == 0)
            data = new Lines3D(&filename, &format, maxline,&args);
        else if (datatype.compare("Map3D") == 0)
            data = new Map3D(&filename, &format, maxline,&args);
        else if  (datatype.compare("ColoredMap") == 0)
            data = new ColoredMap(&filename, &format, maxline,&args);
        else if (datatype.compare("MapQuad") == 0)
            data = new MapQuad(&filename, &format, maxline,&args);
        else if (datatype.compare("Line2D") == 0)
            data = new Lines2D(&filename, &format, maxline,&args);
    }
    else
    {
        data = new Lines2D(&filename, &format, maxline,&args);
    }
    data->startReadingThread();
 
    switch (set->getElementNb())
    {
    case 0:
        data->setColor(32,147,255);
        break;// blue
    case 1:
        data->setColor(71,255,102);
        break;// green
    case 2:
        data->setColor(255,252,32);
        break;// yellow
    case 3:
        data->setColor(255,49,13);
        break;// red
    case 4:
        data->setColor(73,240,255);
        break;// turcoise
    case 5:
        data->setColor(255,49,162);
        break;//pink
    default:
        data->setColor(1,1,1);
        break;
    }
    if (data==NULL)
        std::cout << "data error"  << std::endl;
    else
        set->add(data);
    return;
}
 
 
 
static int main_internal(std::unordered_map<std::string, std::string> & args)
{
    DrawableSet  set;
 
    std::string format;
    if (args.count("--format") == 0)
        format = std::string("%f;%f");
    else
        format = args["--format"];
 
    int maxline;
    if (args.count("--maxline") == 0)
        maxline = 5000;
    else
        maxline = atoi(args["--maxline"].c_str());
 
    if (args.count("--stdin") == 1)
    {
        std::string filename = std::string("/dev/stdin");
        std::string  datatype=args["--data"];
        load(&set,filename,datatype,maxline,format,args);
    }
 
    const int strSize = 10;
    char bufferfile [strSize];
    char bufferdata [strSize];
    for (int i=0; i<10; i++)
    {
        bzero(bufferfile,strSize);
        snprintf(bufferfile, strSize, "--file%01i", i);
 
 
        if( args.count(bufferfile) == 1)
        {
            std::string filename = args[bufferfile];
            bzero(bufferdata,strSize);
            snprintf(bufferdata, strSize, "--data%01i", i);
            std::string  datatype= args[bufferdata];
            load(&set,filename,datatype,maxline,format,args);
        }
    }
 
    //MathFunctionDrawable * mathFunc = new MathFunctionDrawable();
    //set.add(mathFunc);
    if (set.getElementNb()>0)
    {
        int width;
        int height;
        if (args.count("--width") == 1)
            width = atoi(args["--width"].c_str());
        else
            width = 1024;
 
        if (args.count("--height") == 1)
            height = atoi(args["--height"].c_str());
        else
            height = 400;
        if (args.count("--png") == 0)
        {
 
            bool debug = args.count("--debug") == 1;
            GlWindow *win;
            if (args.count("--glut") == 1)
                win = new GlutWin(width, height,debug);
            else
                win = new GlxWin(width, height,debug);
 
            win->init(&set);
 
            if (args.count("--title") == 0)
                win->setTitle("Graph");
            else
                win->setTitle(args["--title"].c_str());
 
            win->run();
        }
        else
        {
 
            GlImageRenderer * renderer = new GlImageRenderer(width, height, args["--png"].c_str());
            renderer->init(&set);
            set.waitUntilLoad();
            renderer->run();
        }
    }
    else
    {
        std::cout << "no input file" << std::endl;
    }
 
    return EXIT_SUCCESS;
}
 
 
 
int main(int argc, char *argv[])
{
    std::unordered_map<std::string, std::string> args;
    read_param(argc, argv, &args);
 
    int exit_status = EXIT_SUCCESS;
    if (args.count("--help") == 1)
        print_usage();
    else
        exit_status = main_internal(args);
 
    return exit_status;
}