Monday, September 26, 2011

String processing in C++ using string library

[1] String library in C++ library.
[2] Get decimalhttp://www.blogger.com/img/blank.gif value from string.
[3] iStringStream Class in C++ library.
[4] iStringStream operator.
[5] Print hex value in C++
[6] IP address validation using GNU C++ library.
[7]String to number and vice versa in C++/C Lib.



IP ADDRESS VALIDATION [6]
-------------------------
inet_ntop(), inet_pton().



//------------------------------------------------------

// IPv4 demo of inet_ntop() and inet_pton()

struct sockaddr_in sa;
char str[INET_ADDRSTRLEN];

// store this IP address in sa:
inet_pton(AF_INET, "192.0.2.33", &(sa.sin_addr));

// now get it back and print it
inet_ntop(AF_INET, &(sa.sin_addr), str, INET_ADDRSTRLEN);

printf("%s\n", str); // prints "192.0.2.33"


//-----------------------------

// IPv6 demo of inet_ntop() and inet_pton()
// (basically the same except with a bunch of 6s thrown around)

struct sockaddr_in6 sa;
char str[INET6_ADDRSTRLEN];

// store this IP address in sa:
inet_pton(AF_INET6, "2001:db8:8714:3a90::12", &(sa.sin6_addr));

// now get it back and print it
inet_ntop(AF_INET6, &(sa.sin6_addr), str, INET6_ADDRSTRLEN);

printf("%s\n", str); // prints "2001:db8:8714:3a90::12"


//--------------------------------------------------------

// Helper function you can use:

//Convert a struct sockaddr address to a string, IPv4 and IPv6:

char *get_ip_str(const struct sockaddr *sa, char *s, size_t maxlen)
{
switch(sa->sa_family) {
case AF_INET:
inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr),
s, maxlen);
break;

case AF_INET6:
inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)sa)->sin6_addr),
s, maxlen);
break;

default:
strncpy(s, "Unknown AF", maxlen);
return NULL;
}

return s;
}




STRING PROCESSING IN C 'atoi','atof','sscanf' [2]
--------------------------
The old C way (deprecated):



const char* str_int = "777";
const char* str_float = "333.3";
int i = atoi(str_int);
float f = atof(str_float);


A better way:

const char* str_int = "777";
const char* str_float = "333.3";
int i;
float f;

if(EOF == sscanf(str_int, "%d", &i))
{
//error
}

if(EOF == sscanf(str_float, "%f", &f))
{
//error
}





STRING PROCESSING IN C++ USING 'istringstream' CLASS[2][3]
--------------------------


#include
#include
#include

template
bool from_string(T& t,
const std::string& s,
std::ios_base& (*f)(std::ios_base&))
{
std::istringstream iss(s);
return !(iss >> f >> t).fail();
}

int main()
{
int i;
float f;

// the third parameter of from_string() should be
// one of std::hex, std::dec or std::oct
if(from_string(i, std::string("ff"), std::hex))
{
std::cout << i << std::endl;
}
else
{
std::cout << "from_string failed" << std::endl;
}

if(from_string(f, std::string("123.456"), std::dec))
{
std::cout << f << std::endl;
}
else
{
std::cout << "from_string failed" << std::endl;
}
return 0;
}

/* output:
255
123.456
*/




STRING PROCESSING IN C++ BUILDER
-----------------------------------


//Text.SubString(1,2).c_str()
sprintf(lcTemp, "%s", Pan1Edit2PacMac->Text.SubString(1,2).c_str());

//Text.ToInt()
lsPacHwRev.HwRevChkSum=Pan1Edit4NewHwRev->Text.ToInt()+0xFF;

//AnsiString::IntToHex(addr,2)
Memo1ScreenPrint->Lines->Add("Mac Address Retrieved from PAC: "+
AnsiString(SPACPacketPtr->Source));
Memo1ScreenPrint->Lines->Add(AnsiString::IntToHex(ptrlsPacMacAddr->macAddr1,2)+
"-"+AnsiString::IntToHex(ptrlsPacMacAddr->macAddr2,2)+
"-"+AnsiString::IntToHex(ptrlsPacMacAddr->macAddr3,2)+
"-"+AnsiString::IntToHex(ptrlsPacMacAddr->macAddr4,2)+
"-"+AnsiString::IntToHex(ptrlsPacMacAddr->macAddr5,2)+
"-"+AnsiString::IntToHex(ptrlsPacMacAddr->macAddr6,2));
Memo1ScreenPrint->Lines->Add(" at: "+AnsiString(asctime(tblock)));

//AnsiString(text).AnsiPos("KeyWords")
lcTemp=AnsiString(lsStringBuffer).AnsiPos("OK");
if (lcTemp==0)
lcTemp=AnsiString(lsStringBuffer).AnsiPos("0");





//validate and decode ip address string
#include sys/types.h
#include sys/socket.h
#include arpa/inet.h



//To validate and decode ip address
std::string str3="";
size_t pos,posLast;
struct sockaddr_in sa;
char str[INET_ADDRSTRLEN];
int liRet;


//Check whether it's valid IP address
liRet=inet_pton(AF_INET,pszVal,&(sa.sin_addr));
if(liRet<0){
std::cout<<"Error: Only IpV4 address is supported"<}else if(liRet==0){
std::cout<<"Illegal IpV4 address string"<}else{
inet_ntop(AF_INET,&(sa.sin_addr),str,INET_ADDRSTRLEN);
std::cout<}


posLast=0;
pos=0;
//for(int j=0;j<4;j++){
// pos = std::string(pszVal).find(".",pos); // position of "." in str
// str3 = std::string(pszVal).substr (posLast,pos-posLast); // get from f"live" to the end
// from_string(lRealValue, str3, std::dec);
// std::cout<<"part "<// posLast=pos+1;
// pos++;
}





No comments: