Skip to content

Instantly share code, notes, and snippets.

@jsfaint
Created May 27, 2014 03:26
Show Gist options
  • Save jsfaint/b4a29f4902b3b70b1e4c to your computer and use it in GitHub Desktop.
Save jsfaint/b4a29f4902b3b70b1e4c to your computer and use it in GitHub Desktop.
#include <stdio.h>
#define NO_OF_PORTS 8
#define TRUE 1
#define FALSE 0
static long pow(int x, int y)
{
int ii;
long result = 1;
for (ii=0; ii<y; ii++)
result *= x;
return result;
}
int atoi_fake(const char* string)
{
int result = 0;
int ii = 0, cnt = 0;
for(ii = 0; ii<strlen(string); ii++) {
if(string[ii] >= '0' && string[ii] <= '9')
cnt++;
else
break;
}
for (ii=0; ii<cnt; ii++) {
result = result + (string[ii]-'0')*pow(10, cnt-ii-1);
}
return result;
}
unsigned char trim2int(unsigned char* PortList, const char* szbuf, unsigned short iLen)
{
unsigned char port = 0;
unsigned short cnt = 0;
unsigned short j = 0;
short iStart;
iStart = 0;
for (j=0; j<iLen; j++) {
if ((szbuf[j] >= '0' && szbuf[j] <= '9') || szbuf[j] == ',' || szbuf[j] == '-') {
if (szbuf[j] == ',' || (j == iLen - 1) ) {
port = atoi_fake(szbuf + iStart);
iStart = j + 1;
if (port == 0 || port > NO_OF_PORTS || cnt>=NO_OF_PORTS) {
return FALSE;
}
PortList[cnt] = port;
cnt++;
}
}
}
return TRUE;
}
int main(void)
{
char str[] = "5";
char portlist[8] = {0};
char i;
trim2int(portlist, str, strlen(str));
for (i=0; i<8; i++)
{
if (portlist[i] != 0)
printf("%d, ", portlist[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment