Click here to Skip to main content
16,004,882 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: What does this line of code really do? Pin
Mike Dimmick21-Apr-04 1:02
Mike Dimmick21-Apr-04 1:02 
GeneralSorting contents of CObList array Pin
Still learning how to code20-Apr-04 10:04
Still learning how to code20-Apr-04 10:04 
GeneralRe: Sorting contents of CObList array Pin
David Crow20-Apr-04 10:23
David Crow20-Apr-04 10:23 
GeneralCast/Conversation Pin
Irish_GUI20-Apr-04 10:02
Irish_GUI20-Apr-04 10:02 
GeneralRe: Cast/Conversation Pin
David Crow20-Apr-04 10:45
David Crow20-Apr-04 10:45 
GeneralRe: Cast/Conversation Pin
Irish_GUI20-Apr-04 10:55
Irish_GUI20-Apr-04 10:55 
GeneralRe: Cast/Conversation Pin
toxcct20-Apr-04 10:59
toxcct20-Apr-04 10:59 
GeneralRe: Cast/Conversation Pin
toxcct20-Apr-04 10:58
toxcct20-Apr-04 10:58 
maybe a standard fonction does this (inet_addr() i think, but i don't remember), the folowing works fine, even if it can seem "heavy" :

<font style="color:blue;">#include</font> <stdlib.h>    <font style="color:green;">// atoi()</font>
<font style="color:blue;">#include</font> <ctype.h>     <font style="color:green;">// isdigit()</font>
<font style="color:blue;">#include</font> <string.h>    <font style="color:green;">// strlen()</font>
 
<font style="color:blue;">enum </font>IPValidate {
    IP_ERR = 0,
    IP_OK  = 1
};

 
<font style="color:blue;">int</font> str2bytesIPAddr (<font style="color:blue;">char </font>pcDestTab[], <font style="color:blue;">const char</font>* pcIPAddr) {
    <font style="color:green;">// An IP address is between 0.0.0.0 and 255.255.255.255
</font>
    <font style="color:blue;">int </font>iCptCharIntoIPAddr = 0;
    <font style="color:blue;">int </font>iCptCharIntoByte = 0;
    <font style="color:blue;">int </font>iResult = IP_OK;
    <font style="color:blue;">short </font>sNbBytes = 0;
    <font style="color:blue;">char </font>pstrByte[16];<font style="color:green;">	// To prevent argument size error
</font> 
    <font style="color:blue;">if </font>(strlen(pcIPAddr) > 15) {<font style="color:green;">// the max length of an IP address can't exceed 15 caracters
</font>         iResult = IP_ERR;
    }
    <font style="color:blue;">else </font>{
        <font style="color:blue;">do </font>{
            <font style="color:blue;">if </font>(isdigit(pcIPAddr[iCptCharIntoIPAddr])) {
                <font style="color:green;">// A byte of an IP address mustn't begin with a '0', except if '0' is alone</font>
                <font style="color:blue;">if </font>((iCptCharIntoByte == 0) && (pcIPAddr[iCptCharIntoIPAddr] == '0') && (isdigit(pcIPAddr[iCptCharIntoIPAddr+1]))) {
                    iResult = IP_ERR;
                }
                <font style="color:blue;">else </font>{
                    pstrByte[iCptCharIntoByte] = pcIPAddr[iCptCharIntoIPAddr];
                    iCptCharIntoIPAddr++;
                    iCptCharIntoByte++;
                }
            }
            <font style="color:blue;">else </font>{
                <font style="color:blue;">if </font>(pcIPAddr[iCptCharIntoIPAddr] == '.') {
                    <font style="color:green;">// If we are at the end of a byte
</font>
                    pstrByte[iCptCharIntoByte] = '\0';
                    iCptCharIntoByte = 0;
                    iCptCharIntoIPAddr++;
                    pcDestTab[sNbBytes] = atoi(pstrByte);
                    sNbBytes++;
                    <font style="color:blue;">if </font>((atoi(pstrByte) > 255) || (atoi(pstrByte) < 0)) {
                        <font style="color:green;">// Given that we are on 8 bits, the value must be into [0; 255]</font>
                        iResult = IP_ERR;
                    }
                }
                <font style="color:blue;">else </font>{
                    <font style="color:green;">// Any character different from a digit or a dot ('.')</font>
                    iResult = IP_ERR;
                }
            }
        } <font style="color:blue;">while </font>((pcIPAddr[iCptCharIntoIPAddr] != '\0') && (sNbBytes <= 3) && (iResult == IP_OK));
        <font style="color:blue;">if </font>((pcIPAddr[iCptCharIntoIPAddr] == '\0') && (sNbBytes <= 3) && (iResult == IP_OK)) {
            pstrByte[iCptCharIntoByte] = '\0';
            pcDestTab[sNbBytes] = atoi(pstrByte);
            sNbBytes++;
            <font style="color:blue;">if </font>((atoi(pstrByte) > 255) || (atoi(pstrByte) < 0)) {
                iResult = IP_ERR;
            }
        }
        <font style="color:green;">// IP Address must contain 4 bytes
</font>
        <font style="color:blue;">if </font>(sNbBytes != 4) {
            iResult = IP_ERR;
        }
    }
    <font style="color:blue;">return </font>iResult;
}



TOXCCT >>> GEII power


GeneralRe: Cast/Conversation Pin
Irish_GUI20-Apr-04 11:10
Irish_GUI20-Apr-04 11:10 
GeneralRe: Cast/Conversation Pin
toxcct21-Apr-04 5:10
toxcct21-Apr-04 5:10 
GeneralRe: Cast/Conversation Pin
Anonymous20-Apr-04 21:22
Anonymous20-Apr-04 21:22 
GeneralRe: Cast/Conversation Pin
toxcct20-Apr-04 22:14
toxcct20-Apr-04 22:14 
GeneralStructure Initialization Pin
aman200620-Apr-04 9:46
aman200620-Apr-04 9:46 
GeneralRe: Structure Initialization Pin
toxcct20-Apr-04 10:30
toxcct20-Apr-04 10:30 
GeneralRe: Structure Initialization Pin
Rick York20-Apr-04 11:07
mveRick York20-Apr-04 11:07 
GeneralAppend exported data to MatLab file Pin
Stemple20-Apr-04 9:12
Stemple20-Apr-04 9:12 
Generalfast data access Pin
Cyberizen20-Apr-04 9:02
Cyberizen20-Apr-04 9:02 
Generalinvoking internet explorer with html/jsp Pin
VirtualRelease20-Apr-04 9:00
VirtualRelease20-Apr-04 9:00 
GeneralRe: invoking internet explorer with html/jsp Pin
22491720-Apr-04 19:31
22491720-Apr-04 19:31 
GeneralRe: invoking internet explorer with html/jsp Pin
VirtualRelease26-Apr-04 13:33
VirtualRelease26-Apr-04 13:33 
QuestionSkinMagic? Pin
Krugger40420-Apr-04 8:53
Krugger40420-Apr-04 8:53 
AnswerRe: SkinMagic? Pin
Prakash Nadar20-Apr-04 17:58
Prakash Nadar20-Apr-04 17:58 
GeneralTreeControl in View Class Pin
Krugger40420-Apr-04 8:51
Krugger40420-Apr-04 8:51 
GeneralModeless dialog problem with Handle Pin
Krugger40420-Apr-04 8:47
Krugger40420-Apr-04 8:47 
GeneralRe: Modeless dialog problem with Handle Pin
Prakash Nadar20-Apr-04 15:54
Prakash Nadar20-Apr-04 15:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.