你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux下仿QQ--eva0.4.1源码研究

[日期:2013-10-31] 来源:Linux社区  作者:zz_yun [字体:大 中 小]

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

#ifndef EVAIPADDRESS_H
 #define EVAIPADDRESS_H
                       
 #include <inttypes.h>
 #include <string>
 /*
    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 <stdlib.h>
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<strIP.length(); 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];
}

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

linux
相关资讯       EVA QQ  eva 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款