Reading Gcode in LocalFiileSystem and encodeing int array. but this program has bug.probably

Fork of MakeSequencer by yuki sato

MakeSequencer.cpp

Committer:
satoyuki1111
Date:
2018-10-11
Revision:
2:4fc4593498d7
Parent:
1:4ebb30d54be7
Child:
3:09ee560a3007

File content as of revision 2:4fc4593498d7:

/*
sample

#include "mbed.h"
#include "MakeSequencer.h"

//A row is from 1

LocalFileSystem local("local");
int array[5] = {};

int main()
{
    FILE *fp = fopen( "/local/out.txt", "r");
    MakeSequencer code(fp);
    code.getGcode(3,sizeof(array)/sizeof(int),array);        //getGcode(int row,int aSize, int* array)
                                                             //array will get Gcode of a "int row" line
    for(int i = 0;i < sizeof(array)/sizeof(int);i++)
        printf("%d,",array[i]);
    printf("\n\r%d",code.getGcodeSize());                    //getGcodeSize()
                                                             //return Gcode row
}
*/


#include "MakeSequencer.h"


MakeSequencer::MakeSequencer(FILE *fp)
{
    _fp=fp;
    FileOpen();
}

bool MakeSequencer::FileOpen()
{
    if ( _fp == NULL )
    { 
        printf("Exception");
        return false;
    }
    else
        ReadGcode();
         
    fclose(_fp);
    return true;
}

void MakeSequencer::getGcode(int row,int aSize, int* array)
{
    for(int i = 0;i < aSize;i++)
    {
        array[i] = _data[i][row-1];
    }
}

int MakeSequencer::getGcodeSize()
{
    return _codesize+1;
}

bool MakeSequencer::ReadGcode()
{
    int _ele = 0;
    
    for(int i = 0;fgets(_buf,sizeof(_buf), _fp) != NULL;i++)
    {
        _codesize = i;
        
        if(i > CODE_LENGTH)
            return false;
        else
        {
            int    _tempnum[ARRAY_EREMENT];
            string _tempstr[ARRAY_EREMENT];
            char   _element[ARRAY_EREMENT][CODE_LENGTH];
            char   _char;
            int    _count = -1;
            bool   _check[ARRAY_EREMENT][CODE_LENGTH];
            
            for(int n = 0;n < ARRAY_EREMENT;n++)
            {
                for(int p = 0;p < CODE_LENGTH;p++)
                    _check[n][p] = true;
            }
            
            for(int n = 0;_buf[n] != '\0';n++)
            {
                _char = _buf[n];
                if((_char >= 'a'&& _char <= 'z') || (_char >= 'A' && _char <= 'Z'))
                {
                    _count++;                    
                    if(_char != _element[_count][0] && i != 0)
                    {
                        for(int p = 0;p < _ele;p++)
                        {
                            if(_char == _element[p][0])
                            {
                                _element[p][i] = _char;
                                _count = p;
                                _check[p][i] = false;
                                break;
                            }
                        }
                    }
                    _element[_count][i] = _char;
                    if(i == 0)
                        _ele++;
                }
                else if(_char >= '0'&& _char <= '9')
                {
                    _tempstr[_count] = _tempstr[_count] + _char;
                }   
                
                else if('\n' || '\r')
                {
                }
                else
                    return false;
            }
            
            for(int n = 0;n < _ele;n++)
            {
                _tempnum[n] = atoi(_tempstr[n].c_str());
                
                if(i != 0 && _check[n][i] && _element[n][i] != _element[n][0])
                {
                    //printf("{%d}-{%d},",n,i);
                    _data[n][i] = _data[n][i-1];
                }
                else
                {
                    _data[n][i] = _tempnum[n];
                }        
                printf("%c:%d ",_element[n][0],_data[n][i]);    
            }
            printf("\n\r");
        }
    }
    return true;
}