Saturday, July 15, 2023

Check for int's validation in CPP & it's ".so" file


CValidator Class as shared library :-
----------------------------------------------

first open codeblocks IDE-> new project -> shared library -> next -> C++ :-

1. project title   : validator
2. folder project : /opt/so-file/validator
3. project name   : validator
4. resulting file name : automated by IDE


This "so" file name will be same as name of "project title ".


Now :-
----------
1. remove main.cpp
2. Project -> Build options -> Position Independent Code(-fPIC) -> right tick on it.
3. Have g++ follow the coming C++1y (aka C++14) ISO C++ language standard -> right tick on it.
4. Target x86_64 (64bit) -> right tick on it.

5. add header file validator.h

#ifndef VALIDATOR_H_INCLUDED
#define VALIDATOR_H_INCLUDED

static std::string  sint;

class CValidators
{
private:
//    static std::string  sint;
    int i;
    char ch;

public:
    CValidators();

    static bool intvalidator(std::string sint);
};

#endif // VALIDATOR_H_INCLUDED

6. add source file validator.cpp

#include "validator.h"

#include <iostream>
#include <string>

using std::string;

bool CValidators::intvalidator(string sint)
{
    int i; 

    if(sint.length() == 0)

    { return false; }


    char ch = sint.at(0);
    if(ch == '-')
        i = 1;
    else
        i = 0;
    for(; i < sint.length(); i++)
    {
        ch = sint.at(i);

        if(isdigit(ch) == false)
        {
            return false;
        }
    }
    return true;
}

--------------------------------------------------------------
compile it and build library file as named : libvalidator.so

now call this file in our new project "int-validation" :-

#include <iostream>
#include <string>
#include <validator.h>

using namespace std;

int main()
{

    string str;
    cout << "Enter number : ";
    getline(cin, str);

    bool tf = CValidator::intvalidator(str);

    if(tf == true)
        cout << "Valid integer" << endl;
    else if(tf == false)
        cout << "invalid integer" << endl;


/* now convert it from string to int. Because in place where we get input of int directly, if we give input of non int such as any character, program will crash. So we need to get it as string, which will get input of non ints and program will not crash. then check it via isdigit(char x) in loop, and then convert this string into int after checking of  validation. */


    return 0;
}

/*
output :-
---------

[rahul@C-Client Release]$ ./int-vallidation
Enter number : 123
Valid integer
[rahul@C-Client Release]$ ./int-vallidation
Enter number : 23e4
invalid integer
[rahul@C-Client Release]$ ./int-vallidation
Enter number : 23444r
invalid integer
[rahul@C-Client Release]$ ./int-vallidation
Enter number : e333333
invalid integer
[rahul@C-Client Release]$ ./int-vallidation
Enter number : 232323423234234234234
Valid integer
[rahul@C-Client Release]$
*/

--------------------------------------------------------------

to see how to build and call shared library files see mine other blogs, thanks.

No comments:

Post a Comment