#ifndef _FROMFILE_H
#define _FROMFILE_H

#include "pri.h"
#include <queue>
#include <fstream>
#include  "docommand.h"

class FromFile {
      
    queue<string> myfifo;       //fronta pro prikazy a arg
    fstream is;                 //promena pro operace se souborem
    Prikaz p;                   //Modul prikaz, prirazeni cisla prikazu
    DoCommand *dc;              //ukazatel - provedeni prikazu, zpracovani arg
    Stg* stg;                   //ukazatel na struktur STG
    
public:      
FromFile(const char* name, Stg* st){             
     is.open (name);                       //otevreni souboru
     if (!is.is_open())
     {
        is.close();
        cout << "soubor neexistuje" << endl;
        system("PAUSE");
        exit(1);    
      }
     stg = st;                             //prirazeni ukazatele STG
     dc = new DoCommand(&myfifo,st);       //inicializace DoCommand
     readFile();                           //fce - cte soubor a zpracovava
             }     

//-----------------------------------------------------------------------------
~FromFile(){             
     is.close();                           //uzavreni souboru
     delete dc;                            //dealokace 
         }

//-----------------------------------------------------------------------------
//cteni souboru
void readFile(){
     char buf[256];                        
     while(is.getline(buf,256,'\n')){      //cte radek
     doFifo(buf);                          //fce - deleni redku na prikazy a arg      
     if(!myfifo.empty())
     make(p.command(myfifo.front()));      //fce - vybere prikaz
     }               

     }

//-----------------------------------------------------------------------------
//maze frontu
void delteFifo(){
     while(!myfifo.empty()){               
     myfifo.pop();                         //vymazani fronty
     }                                
}

//-----------------------------------------------------------------------------
// plni frontu                
void doFifo(string s){

    string str = "";
    bool vloz = false;                
    
    for(unsigned int i = 0; i < s.size();++i){
    
    if(s[i] == '\n' || s[i] == '\t') s[i] = ' ';            //nahrada mezerou
    if(s[i] != ' ') { str += s[i]; vloz = true; }           //vlozeni do pomocne promene cast arg
    if(s[i] == ' ')                                         //vyhodnoti vlozeni do fronty
    {  

    if(vloz)
    myfifo.push(str);
    str.clear();
    vloz = false;
    }
    
    }
    
    if(vloz)
    myfifo.push(str);
    
}

//-----------------------------------------------------------------------------
//vyhodnoceni prikazu a jeho provedeni                
void make(int i){
    
    string str = myfifo.front();
    myfifo.pop();                                     
     
    switch(i){          
    case 1:dc->command128();               //inputs
         break;
    case 2:dc->command128(1);              //outputs
         break;
    case 3:dc->command3();                  //init_state
         break;
    case 4:dc->command4();                  //aktivace grafu
         break;
    case 5:dc->command5();                  //marking
         break;
    case 6:dc->command6();delteFifo();      //end grafu
         break;
    case 7:dc->commandName();delteFifo();   //jmeno vystupniho souboru
         break;
    case 8:dc->command128(2);               //internal
         break;
    case 9:delteFifo();                     //komnetar
         break;
    default:
            dc->commandGraf(str);           //graf
    } 
}                        
      
      
}; 

#endif

