Linux仿QQ实验报告,Linux下仿QQ--eva0.4.1源码研究

没看到具体的关键实现,只是一个文件检索。保存了ip,国家,地区信息。忽略之。

#ifndef EVAIPADDRESS_H

#define EVAIPADDRESS_H

#include

#include

/*

this class is only for ipv4 addresses

this class can be used as below:

EvaIPAddress addr("255.255.255.255"); // or EvaIPAddress addr(0xffffffff) ;

uint ip = addr.IP();

std::string strIP = addr.toString();

*/

class EvaIPAddress{

public:

EvaIPAddress() {};

EvaIPAddress(const uint ip);

EvaIPAddress(const std::string &strIP);

EvaIPAddress(const EvaIPAddress &address);

void setAddress(const uint ip);

void setAddress(const std::string &strIP);

const bool isValid() const;

const uint IP() const;

const std::string toString();

EvaIPAddress &operator= (const EvaIPAddress &rhs);

private:

bool isValidIP;

uint mIP;

uint getIntIP(const std::string &strIP);

};

#endif

看着头文件,也只是一个工具类。

/***************************************************************************

*  Copyright (C) 2005 by yunfan                                          *

*  yunfan_zg@163.com                                                    *

*                                                                        *

*  This program is free software; you can redistribute it and/or modify  *

*  it under the terms of the GNU General Public License as published by  *

*  the Free Software Foundation; either version 2 of the License, or    *

*  (at your option) any later version.                                  *

*                                                                        *

*  This program is distributed in the hope that it will be useful,      *

*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *

*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *

*  GNU General Public License for more details.                          *

*                                                                        *

*  You should have received a copy of the GNU General Public License    *

*  along with this program; if not, write to the                        *

*  Free Software Foundation, Inc.,                                      *

*  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.            *

***************************************************************************/

#include "evaipaddress.h"

#include

EvaIPAddress::EvaIPAddress(const uint ip)

:isValidIP(false)

{

mIP = ip;

isValidIP = true;

}

EvaIPAddress::EvaIPAddress(const std::string &strIP)

:isValidIP(false)

{

mIP = getIntIP(strIP);

if(mIP)

isValidIP = true;

}

EvaIPAddress::EvaIPAddress(const EvaIPAddress &address)

:isValidIP(false)

{

mIP = address.IP();

isValidIP = address.isValid();

}

void EvaIPAddress::setAddress(const uint ip)

{

mIP = ip;

}

void EvaIPAddress::setAddress(const std::string &strIP)

{

mIP = getIntIP(strIP);

}

const bool EvaIPAddress::isValid() const

{

return isValidIP;

}

const uint EvaIPAddress::IP() const

{

return mIP;

}

const std::string EvaIPAddress::toString()

{

char strIP[16];

memset(strIP, 0, 16);

sprintf(strIP, "%d.%d.%d.%d", (mIP&0xFF000000)>>24, (mIP&0x00FF0000)>>16, (mIP&0x0000FF00)>>8, (mIP&0x000000FF));

return std::string(strIP);

}

EvaIPAddress &EvaIPAddress::operator= (const EvaIPAddress &rhs)

{

mIP = rhs.IP();

isValidIP = rhs.isValid();

return *this;

}

uint EvaIPAddress::getIntIP(const std::string &strIP)

{

int num = 0;

for(uint i=0; i< strIP.length(); i++)

if(strIP[i] == '.') num++;

// check if it consists of 4 parts

if(num != 3){

isValidIP = false;

return 0;

}

// get all 4 parts in

unsigned char parts[4];

int start = 0, end = 0;

for(int i=0; i<4; i++){

for(uint j= start; j

if(strIP[j] == '.'){

end = j;

break;

}

if(strIP[j] < '0' || strIP[j] > '9'){

isValidIP = false;

return 0;

}

}

//printf("3 strIP:%s\n",strIP.c_str());

std::string tmp = strIP.substr(start, end - start);

int tmpInt = atoi(tmp.c_str());

if(tmpInt< 0 || tmpInt > 255){

isValidIP = false;

return 0;

}

parts[i] = (unsigned char)tmpInt;

start = end + 1;

}

// put all 4 parts into one uint

return ((uint)parts[0])<<24 | ((uint)parts[1])<<16 | ((uint)parts[2])<<8 | parts[3];

}

不错,这个工具类,自己可以随时用到。 随时拿来主义。嘎嘎0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值