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-10
Revision:
1:4ebb30d54be7
Parent:
0:359eba7592ca
Child:
2:4fc4593498d7

File content as of revision 1:4ebb30d54be7:

/*
sample code

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

int main()
{
    FILE *fp = fopen( "/local/out.txt", "r");
    MakeSequencer code(fp);
    
    code.getGcode(1,array);
}
*/



#include "MakeSequencer.h"


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

bool MakeSequencer::inherit(int _rows,int _columns,int _data[][CODE_LENGTH])
{
    _data[_rows][_columns] = (_columns == 0) ? 0 : _data[_rows][_columns-1];
    return false;
}

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

void MakeSequencer::getGcode(int _seq,int* _array)
{
    for(int _i = 0;_i < ARRAY_EREMENT;_i++)
    {
        _array[_i] = data[_i][_seq];   
    }
}

int MakeSequencer::getGcodeSize()
{
    return codesize;
}

bool MakeSequencer::ReadGcode()
{
    for(int i = 0;fgets(buf,sizeof(buf), _fp) != NULL;i++)
    {
        codesize = i+1;
        if(i > CODE_LENGTH)
            return false;
        
        for(int n = 0;buf[n] != '\0';n++)
            code[0][i] = code[0][i]+buf[n];
            
        int pos[3] = {code[0][i].find("x"),code[0][i].find("y"),code[0][i].find("z")};
        
        for(int p = 0;p < sizeof(pos)/sizeof(int);p++)
            find[p] = (pos[p] != code[0][i].npos) ? true:inherit(p,i,data);

        for(int p = 0;p < sizeof(pos)/sizeof(int);p++)
        {
            char trans[1] = {};
            string sw_code = find[p] ? code[0][i].substr(pos[p],1) : "I";
            sw_code.copy(trans,1);
            switch(trans[0])
            {
                case 'x':
                    if(find[1])
                        data[0][i] = atoi(code[0][i].substr(pos[0]+1,pos[1] - 1).c_str());   
                    else if(find[2])
                        data[0][i] = atoi(code[0][i].substr(pos[0]+1,pos[2] - 1).c_str());
                    else
                        data[0][i] = atoi(code[0][i].substr(pos[0]+1,code[0][i].size() - 1).c_str());
                break;
                
                case 'y':
                    if(find[2])
                        data[1][i] = atoi(code[0][i].substr(pos[1]+1,pos[2] - 1).c_str());
                    else
                        data[1][i] = atoi(code[0][i].substr(pos[1]+1,code[0][i].size() - 1).c_str());
                break;
                
                case 'z':
                    data[2][i] = atoi(code[0][i].substr(pos[2]+1,code[0][i].size() - 1).c_str());
                break;
                
                default:
                break;
            }    
        }
        printf("x=%d ,y=%d ,z=%d \n\r",data[0][i],data[1][i],data[2][i]);
    }
    return true;
}