Sunday, August 16, 2009

String processing functions in C library

[1] http://www.cplusplus.com/reference/clibrary/cstdlib/strtol/

/* String functions in C library debugged in VC6 IDE

strncmp, strtol, strtok,strncpy,sprintf,atoi,

*/

#include "stdafx.h"
#include
#include //strncmp,strtok
#include //strtol

typedef unsigned char GSNTYPES_BYTE; /* 0 to 255 */
typedef char GSNTYPES_CHAR; /* -128 to 127 */


#define MAX_NUM_BYTES_HTML_TAG_VALUE (256) //limited by ConnectOne WiFi module.



int main()
{
//unsigned int len;

//len = strspn("this is a test","siht ");
//len = strspn("this is a test","se");
//printf("%d\n",len);


const char DASHSTRING[]="-";

const char CMD2TARGETREAD[]="Cmd2Target_Rd_\x31";
GSNTYPES_CHAR lcTagsValue[MAX_NUM_BYTES_HTML_TAG_VALUE];
GSNTYPES_BYTE lcTemp,lcWeb2TargetCmdCode;
char stringTemp[11];
char FakedMiMe_01[]="HTTP/1.1\xb7 200\xb7 OK\r\nServer:\xb7 CableTrackerIChip\r\nContent-Type:\xb7 application/octet-stream\r\n\r\n";
char word[]="\x6d=\x64=";



int i;


sprintf(lcTagsValue,"I/(-1)\r\n");
sprintf(stringTemp,"%s",strtok(lcTagsValue, "(:):\r\n"));
sprintf(stringTemp,"%s",strtok(NULL, "(:):\r\n"));
printf("decimal value of %s is %d \n", stringTemp,atoi(stringTemp));

sprintf(lcTagsValue,"I/(001)\r\n");
sprintf(stringTemp,"%s",strtok(lcTagsValue, "(:):\r\n"));
sprintf(stringTemp,"%s",strtok(NULL, "(:):\r\n"));
printf("decimal value of %s is %d \n", stringTemp,atoi(stringTemp));

sprintf(lcTagsValue,"I/(000)\r\n");
sprintf(stringTemp,"%s",strtok(lcTagsValue, "(:):\r\n"));
sprintf(stringTemp,"%s",strtok(NULL, "(:):\r\n"));
printf("decimal value of %s is %d \n", stringTemp,atoi(stringTemp));


printf (word);
printf(FakedMiMe_01);


// char string []="abc def ghi";
// char * word;


/* char num[15];

// Test a valid number
strcpy(num,"09012345");

printf("%s(Oct) is %i(Dec)\n", num, strtol(num, NULL, 8));
printf("%s(Dec) is %i(Dec)\n", num, strtol(num, NULL, 10));
printf("%s(hex) is %i(Dec)\n", num, strtol(num, NULL, 16));

puts("----------------------------------");

*/

printf("string is %s \n", CMD2TARGETREAD);


for (i=0;i {

printf("[%i]=%2X \n",i, CMD2TARGETREAD[i]);
}


sprintf(lcTagsValue,"I/(u got me)\r\nW");

sprintf(stringTemp,"%s",strtok(lcTagsValue, "(:):\r\n"));
printf("before bracket %s \n",stringTemp);

sprintf(stringTemp,"%s",strtok(NULL, "(:):\r\n"));
printf("in bracket %s \n",stringTemp);

sprintf(stringTemp,"%s",strtok(NULL, "(:):\r\n"));
printf("After CR/LR %s \n",stringTemp);
printf("After CR/LR 0x%02X \n",stringTemp[0]);

printf("len in bracket %u \n",strlen(stringTemp));


sprintf(lcTagsValue,"I/(u got me)\r\n");

sprintf(stringTemp,"%s",strtok(lcTagsValue, "(:):\r\n"));
printf("before bracket %s \n",stringTemp);

sprintf(stringTemp,"%s",strtok(NULL, "(:):\r\n"));
printf("in bracket %s \n",stringTemp);

sprintf(stringTemp,"%s",strtok(NULL, "(:):\r\n"));
printf("After CR/LR %s \n",stringTemp);
printf("After CR/LR 0x%02X \n",stringTemp[0]);

printf("len in bracket %u \n",strlen(stringTemp));



//sprintf(lcTagsValue,"Cmd2Target_Rd_0A");
sprintf(lcTagsValue,"11-22-33-AA-BB-CF");

printf("strlen(CMD2TARGETREAD) %d\n", strlen(CMD2TARGETREAD));


sprintf(stringTemp,"%s",strtok(lcTagsValue, DASHSTRING));
lcTemp=(GSNTYPES_BYTE)strtol(stringTemp,NULL,16);

printf("Mac Addr 1 %02x\n",lcTemp);

sprintf(stringTemp,"%s",strtok(NULL, DASHSTRING));
lcTemp=(GSNTYPES_BYTE)strtol(stringTemp,NULL,16);

printf("Mac Addr 2 %02x\n",lcTemp);


sprintf(stringTemp,"%s",strtok(NULL, DASHSTRING));
lcTemp=(GSNTYPES_BYTE)strtol(stringTemp,NULL,16);

printf("Mac Addr 3 %02x\n",lcTemp);

sprintf(stringTemp,"%s",strtok(NULL, DASHSTRING));
lcTemp=(GSNTYPES_BYTE)strtol(stringTemp,NULL,16);

printf("Mac Addr 4 %02x\n",lcTemp);

sprintf(stringTemp,"%s",strtok(NULL, DASHSTRING));
lcTemp=(GSNTYPES_BYTE)strtol(stringTemp,NULL,16);

printf("Mac Addr 5 %02x\n",lcTemp);

sprintf(stringTemp,"%s",strtok(NULL, DASHSTRING));
lcTemp=(GSNTYPES_BYTE)strtol(stringTemp,NULL,16);

printf("Mac Addr 6 %02X\n",lcTemp);


/* if (strncmp(CMD2TARGETREAD,lcTagsValue,strlen(CMD2TARGETREAD))==0)
{
//Read command -- 'Cmd2Target_Rd_4F'
//Change ASCII ('0' ~ 'F' )to HEX and refill the gcU2_RxBuffer[]

strncpy(stringTemp,&lcTagsValue[strlen(CMD2TARGETREAD)],2);

lcWeb2TargetCmdCode=(GSNTYPES_BYTE)strtol(stringTemp,NULL,16);



}
*/

printf("%d\n",lcWeb2TargetCmdCode);



/*
word=strtok(string, " ");

printf("%s\n",word);

//word=strtok(NULL, " ");
word=strtok(string, " ");

printf("%s\n",word);

*/

}

No comments: