/*
  idvalidation.cpp
  
  Author: Sergi Diaz Planas (2017)
*/
#include "idvalidation.h"

// Return true if DNI defined by input digits string and control
// letter is valid. Return false otherwise.
bool validDNI(std::string digits, char letter)
{
    std::string DNI_CONTROL = "TRWAGMYFPDXBNJZSQVHLCKE";

    // Return false if number does not have exactly 8 digits.
    if (digits.length() != 8)
        return false;

    // Try to parse digits string into an integer.
    try {
        // Check control letter.
        long int number = atoi(digits.c_str());
        return DNI_CONTROL[number % 23] == letter;
    }
    catch (int e)
    {
        return false;
    }
}
