#include "head.h"

char buff_input(char *inbuff, char *_buff){
    
    *inbuff = 0;
    { // INPUT
        char ch;
        while((ch=getchar())>' '&&*inbuff<BUFF_MAX){
            *(_buff+*inbuff)=(int)ch;
            (*inbuff)++;
        }
        if(ch<= 4)return -1;
    }


    if (_buff[0]>='a' && _buff[0] <= 'z')
        {   // Is input - command?
        const char NUM_OF_CHAR_COMMANDS = 15;
        char * _charCommands[NUM_OF_CHAR_COMMANDS]; // _charCommands[*]["char"]
        _charCommands[0]    = (char[]){"input"};
        _charCommands[1]    = (char[]){"getmatrix"};
        _charCommands[2]    = (char[]){"logic"};
        _charCommands[3]    = (char[]){"edit"};
        _charCommands[4]    = (char[]){"random"};
        _charCommands[5]    = (char[]){"copy"};
        _charCommands[6]    = (char[]){"destruct"};
        _charCommands[7]    = (char[]){"getcolum"};
        _charCommands[8]    = (char[]){"getrow"};
        _charCommands[9]    = (char[]){"transp"};
        _charCommands[10]   = (char[]){"determ"};
        _charCommands[11]   = (char[]){"obr"};
        _charCommands[12]   = (char[]){"create"};
        _charCommands[13]   = (char[]){"n"};
        _charCommands[14]   = (char[]){"exit"};


        char cmd = cmd_buff(_buff,_charCommands,NUM_OF_CHAR_COMMANDS);
        if(cmd==15)return -1; //exit  
        return cmd; //Вернуть комману    
    }

    return 0;
}

char cmd_buff(char* _buff,char** _charCommands,char NUM_OF_CHAR_COMMANDS){
    for (int i = 0;i<NUM_OF_CHAR_COMMANDS;i++){
        char flag = 1;
        for(int j = 0;*(_charCommands[i]+j)!='\0';j++){
            if (_buff[j]!=*(_charCommands[i]+j)){
                flag = 0;
                break;
            }
        }
        if(flag) return i+1;//Вернуть комманду
    }
    return 0;
}

char get_matrix_num(void){
    char inbuff;
    char _buff[BUFF_MAX];
    while(buff_input(&inbuff,_buff)>0&&*_buff>' ');

    long int num = 0;
    char flagIsDec = 1;
    { // GetNum
        int i = 0;
        if(_buff[0]=='-'){i++;flagIsDec = -1;}
        
        for(; i<inbuff;i++)
            if(_buff[i]>= '0' && _buff[i] <= '9')
                num = (num*10) + (_buff[i]-'0');
        num *= flagIsDec;
    }
        if (num>MAX_MATRIX_ALIVE)num = MAX_MATRIX_ALIVE;
        if (num<0)num = 0;
        return num;
}


int get_num(void){
    char inbuff;
    char _buff[BUFF_MAX];
    while(buff_input(&inbuff,_buff)>0&&*_buff>' ');

    long int num = 0;
    char flagIsDec = 1;
    { // GetNum
        int i = 0;
        if(_buff[0]=='-'){i++;flagIsDec = -1;}
        
        for(; i<inbuff;i++)
            if(_buff[i]>= '0' && _buff[i] <= '9')
                num = (num*10) + (_buff[i]-'0');
        num *= flagIsDec;
    }
        if (num>INT_MAX)num = INT_MAX;
        if (num<INT_MIN)num = INT_MIN;
        return (int)num;
}
/*
void outputInt(int n){

}

void outputStr(char* str){

}*/ // Подрузамевалось подобный io но он это нецелесообразная трата времени
