Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define CliMaxSize 1024
#define PacketMaxSize 999  
#define MaxNumberofPackets 5 


#if 1

static char InData[CliMaxSize] = "\r\nRCV\r\nSOME:DATA40\r\n24Ab2bfsdfa46jKjj936100dsasgdfsdf1240s0x\r\nOK\r\n\r\nRCV\r\nSOME:DATA50\r\n24Ab246324400123543gfdsas4sdaasd22231fn67sdff46ngk\r\nOK\r\n\r\nRCV\r\nSOME:DATA0\r\n\r\nOK\r\n";
#endif


#if 0

static char InData[CliMaxSize] = "\r\nRCV\r\nSOME:DATA40\r\n24Ab2bfsdfa46jKjj936100dsasgdfsdf1240s0x\r\nOK\r\n\r\nRCV\r\nSOME:DATA60\r\n24Ab246324400123543gfdsas4sdaasd";
#endif

static char PacketBytes[PacketMaxSize];

typedef struct {
    unsigned char ValidPackets;
    unsigned char DataSizeStartAddress[MaxNumberofPackets];
    unsigned char ValidPacketsStartAddress[MaxNumberofPackets];
    unsigned int DataSize[MaxNumberofPackets];
}_CLI;


static unsigned char AvailableValidPackets(_CLI *const PtrCLI) {
    char *pch;
    PtrCLI->ValidPackets = 0;
    pch = strstr(InData,"OK\r\n");
    while (pch != NULL) {
        PtrCLI->ValidPackets++;
        pch = strstr(++pch,"OK\r\n");
    }
    return PtrCLI->ValidPackets;
}

static void GetStartAddressOfPackets(_CLI *const PtrCLI) {
    char *pch = &InData[0];
    unsigned char LocalCounter = 0;
    /* find the location of ":" in Source */
    while (*pch++ != '\0') {
        if (*pch == ':') {
            PtrCLI->ValidPacketsStartAddress[LocalCounter] = pch - &InData[0] + 9;
            PtrCLI->DataSizeStartAddress[LocalCounter] = pch - &InData[0] + 5;
            LocalCounter++;
        }
    }
}

static void ExtractDataSizeInIntegerForm(_CLI *const PtrCLI) {
    char LocArray[MaxNumberofPackets] = { 0,0,0,0,0};
    char LocCounter = 0, LocalDummy = 0, localMultiplier = 1;

    for (char LocLoop = 0; LocLoop < PtrCLI->ValidPackets; LocLoop++) {
        char *LocPtr = (char*)&InData[PtrCLI->DataSizeStartAddress[LocLoop]];
        while (*LocPtr != '\r') {
            LocArray[LocCounter++] = *(LocPtr++) - '0';
            localMultiplier *= 10;
        }
        for (char c = 0; c < LocCounter; c++){
            localMultiplier = localMultiplier / 10;
            LocalDummy += LocArray[c] * localMultiplier;
            LocArray[c] = 0;
        }
        PtrCLI->DataSize[LocLoop] = LocalDummy;
        LocCounter = 0;
        LocalDummy = 0;
    }
}

static void CopyString(const int StartAddress, const int size) {
    char *ptr = &InData[0] + StartAddress;
        memset(PacketBytes, '\0', sizeof(PacketBytes));
        for(char i = 0; i < size; i++) {
            PacketBytes[i] = *ptr++;
        }

}

static void ExtractPackets (_CLI *const PtrCLI){
    for(char i = 0; i< PtrCLI->ValidPackets; i++) {
	if (PtrCLI->DataSize[i] != 0) {
	CopyString(PtrCLI->ValidPacketsStartAddress[i], PtrCLI->DataSize[i]);
        printf("Packet %d> size: %d data: ",i+1, PtrCLI->DataSize[i]);
		for (char j = 0; j< PtrCLI->DataSize[i]; j++)
			{
				printf("%c", PacketBytes[j]);
			}
		printf("\n");
    }
else {
	printf("Packet %d> size: %d data: \n",i+1, PtrCLI->DataSize[i]);
	}
}
}

int main() {
    _CLI CommandLineInterface;
    AvailableValidPackets(&CommandLineInterface);
    GetStartAddressOfPackets(&CommandLineInterface);
    ExtractDataSizeInIntegerForm(&CommandLineInterface);
    ExtractPackets(&CommandLineInterface);
    return 0;
}


What I have tried:

I am making this parser and the output is as required:
Packet 1> size: 40 data: 24Ab2bfsdfa46jKjj936100dsasgdfsdf1240s0x
Packet 2> size: 50 data: 24Ab246324400123543gfdsas4sdaasd22231fn67sdff46ngk
Packet 3> size: 0 data:
but my supervisor is asking me to make it more efficient. can anyone of you give some suggestions to make it better and short?
any suggestion will be appreciated.
Posted
Updated 11-Nov-18 23:01pm

You can combine AvailableValidPackets and GetStartAddressOfPackets easily. Also to optimized it more, have one single iteration over all input data instead of using strstr and do parsing, counting etc. in one go.
 
Share this answer
 
Quote:
static void CopyString(const int StartAddress, const int size) {
char *ptr = &InData[0] + StartAddress;
memset(PacketBytes, '\0', sizeof(PacketBytes));
for(char i = 0; i < size; i++) {
PacketBytes[i] = *ptr++;
}

}

I see no purpose in zeroing the PacketBytes buffer, the code is immediately overwriting it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900