Swego czasu bylo mi to potrzebne:
// Biblioteka obsługi haseł.
// Autor: Grzegorz(DzieX) Dziekan <[email protected]>
#ifndef _PASSWD_HPP_
#define _PASSWD_HPP_
#include <iostream>
#include <string>
#include <cctype> // dla ctype character functions
#include <conio.h> // dla getch()
namespace dx {
// enum prompt_list
// lista możliwych kursorów wyświetlanych podczas pobierania hasła
// dotted - kropkowane tło
// white - białe tło
// star - gwiazdka (domyślny)
// the_x - mała litera "x"
enum prompt_list {
dotted = -79,
white = -37,
star = 42,
the_x = 120
};
// enum mode_list
// lista możliwych trybów pobierania hasła
// stable - podczas pobierania hasła nie jest pokazywany kursor
// expand - podczas pobierania hasła pokazywany jest kursor (domyślny)
enum mode_list {
stable,
expand
};
// class passwd - białe znaki są ignorowane
//
// operatory:
// operator<< - wypisanie hasła na standardowe wyjście
// operator>> - pobieranie hasła (tylko ze standardowego wejścia !!)
// operator== - porównywanie dwóch obiektów passwd (porównuje tylko składowe "password" !!)
// operator== - porównywanie obiektu passwd oraz string'a (lub const char* lub char*)
// operator= - przypisanie string'a (lub const char* lub char*) do obiektu typu passwd
// operator= - przypisanie obiektu passwd do obiektu passwd
//
// składowe:
// password - hasło właściwe
// max_length - maksymalna długość hasła (domyślnie 12 znaków)
// prompt - kursor
// mode - tryb pracy
//
// konstruktor:
// passwd(unsigned _l=12, prompt_list _p=star, mode_list _m=expand)
//
// metody:
// set_prompt(prompt_list new_prompt) - ustawia kursor
// set_max_length(unsigned new_max_length) - ustawia maksymalną długość hasła
// set_mode(mode_list new_mode) - ustawia tryb
// erase() - opróżnia składowe: buffor i password
//------------------------------------------------------------------------------
class passwd {
friend std::ostream &operator<<(std::ostream&, passwd&);
friend std::istream &operator>>(std::istream&, passwd&);
std::string password;
unsigned max_length;
char buffer;
prompt_list prompt;
mode_list mode;
public:
passwd(unsigned , prompt_list, mode_list);
bool operator==(const passwd&) const;
bool operator==(const std::string&) const;
std::string &operator=(const std::string&);
passwd &operator=(const passwd&);
void set_prompt(prompt_list new_prompt) {prompt=new_prompt;};
void set_max_length(unsigned new_max_length) {max_length=new_max_length;};
void set_mode(mode_list new_mode) {mode=new_mode;};
void erase() {buffer=0; password.erase();};
bool empty() const {return password.empty();};
};
//------------------------------------------------------------------------------
passwd::passwd(unsigned _length=12, prompt_list _prompt=star, mode_list _mode=expand)
:max_length(_length), prompt(_prompt), mode(_mode) {
}
//---------------
bool passwd::operator==(const passwd &temp) const {
return password == temp.password;
}
//---------------
bool passwd::operator==(const std::string &str) const {
return password == str;
}
//---------------
std::string &passwd::operator=(const std::string &str) {
password = str;
if(str.size() > max_length)
set_max_length(str.size());
return password;
}
//---------------
passwd &passwd::operator=(const passwd &temp) {
if (this != &temp) {
this->~passwd();
new(this) passwd(temp);
}
return *this;
}
//---------------
std::ostream &operator<<(std::ostream &out, passwd &temp) {
return out << temp.password;
}
//---------------
std::istream &operator>>(std::istream &in, passwd &temp) {
do {
temp.buffer=getch();
if(isgraph(temp.buffer)) {
temp.password.append(1, temp.buffer);
if(temp.mode != stable)
std::cout << (char)temp.prompt;
}
else if(temp.buffer == 8 && temp.password.size() >= 1) {
temp.password.erase(temp.password.size()-1);
if(temp.mode != stable)
std::cout << "\b" << std::ends << "\b";
}
else if(temp.buffer == 27 ) {
temp.password.erase();
break;
}
else if(isspace(temp.buffer)) {
if(temp.buffer != 13) {
temp.buffer=0;
continue;
}
std::cout << std::endl;
break;
}
} while(temp.password.size() < temp.max_length);
if(temp.password.size() > 0)
return in;
else {
std::cout << "Exeption: password is to short.\n";
temp.erase();
return in;
}
}
//---------------
} // koniec namespace dx
#endif // _PASSWD_HPP_
Moze Tobie tez sie przyda jesli dobrze Cie zrozumialem... :p