jeudi 5 septembre 2019

How to extract different datatype data from an ASCII array using templates

I'm trying to extract different datatype data from an ASCII array using templates where each datatype data is separated by a delimiter(':' in this case) also each datatype data can be preceded and succeeded by whitespaces which needs to removed. But I'm getting template instantiation error. Can somebody enlighten me how to accomplish this task. This is my sample code:

#include<iostream>
#include<string>
#include<cstring>


class Extract {
public:
    inline Extract() {
        length = 0;
        std::memset(recvBuff, '\0', sizeof(recvBuff));
    }

    inline ~Extract() {
    }

    int length;
    unsigned char recvBuff[1024];

    template<typename T>
    void extractData(short& start, short& end, T& val, char delimiter, short dataType) {
        while(end <= length) {
            if(recvBuff[end] == delimiter)
                break;

            end++;
        }

        if(end >= length)
            return;

        std::string token(recvBuff[start + 1], end - start - 1);
        std::string trimmedToken;

        std::string::size_type first = token.find_first_not_of(" \n\r\t\f\v");
        std::string::size_type last = token.find_last_not_of(" \n\r\t\f\v");

        if((first == std::string::npos) || (last == std::string::npos))
            return;
        else
            trimmedToken.assign(token, first, (last - first + 1));

        if(dataType == 1) { //char
            if(trimmedToken.size() == 1)
                val = trimmedToken.at(0);
        }
        else if(dataType == 2) { //int
            if(trimmedToken.size() > 0)
                val = std::stoi(trimmedToken);
        }
        else if(dataType == 3) { //float
            if(trimmedToken.size() == 1)
                val = std::stod(trimmedToken);
        }
        else if(dataType == 4) { //std::string
            val.assign(trimmedToken);
        }
    }
};


int main(int argc, char* argv[]) {
     //"contains mixed ascii data of char, int, float separated by :" for example
     //" CMD : 123 : 3453.45646 : REP : T : 2424 : 3424 : 803424.2312 "
    char buff[1024];

    Extract objExtract;
    objExtract.length = 60; // some dummy value for time being
    std::memcpy(objExtract.recvBuff, buff, objExtract.length);

    short start = 0;
    short end = 1;

    std::string data1;
    objExtract.extractData(start, end, data1, ':', 4);

    int data2;
    objExtract.extractData(start, end, data2, ':', 2);

    double data3;
    objExtract.extractData(start, end, data3, ':', 3);

    std::string data4;
    objExtract.extractData(start, end, data4, ':', 4);

    char data5;
    objExtract.extractData(start, end, data5, ':', 1);

    int data6;
    objExtract.extractData(start, end, data6, ':', 2);

    int data7;
    objExtract.extractData(start, end, data7, ':', 2);

    double data8;
    objExtract.extractData(start, end, data8, ':', 3);

    std::cout << data1 << "\t" << data2 << std::endl;

    return 0;   
}

Aucun commentaire:

Enregistrer un commentaire