matmatmat
September 25th, 2009, 04:33 PM
I have this code:
#include <libglademm/xml.h>
#include <gtkmm.h>
#include <iostream>
#include <string.h>
#include <cstdlib>
#include <ctime>
#ifdef ENABLE_NLS
# include <libintl.h>
#endif
/* For testing propose use the local (not installed) glade file */
/* #define GLADE_FILE PACKAGE_DATA_DIR"/passwrdgenerator/glade/passwrdgenerator.glade" */
#define GLADE_FILE "passwrdgenerator.glade"
using namespace std;
Gtk::Window* main_win = 0;
Gtk::CheckButton* letters = 0;
Gtk::CheckButton* numbers = 0;
Gtk::CheckButton* symbols = 0;
Gtk::SpinButton* lengthb = 0;
int length;
int randn;
string passwrd;
string choice;
void on_generate()
{
choice = "";
passwrd = "";
length = 0;
randn = 0;
srand((unsigned)time(0));
if (letters->get_active())
{
choice += "qwertyuiopasdfghjklzxcvbnm";
}
if (numbers->get_active())
{
choice += "1234567890";
}
if (symbols->get_active())
{
choice += "!$%^&*~#@<>";
}
length = lengthb->get_value_as_int();
if (length == 0){
cout << length;
return;
}
int i;
const char* cchoice = choice.c_str();
cout << sizeof(cchoice) << "\n";
for(i=0;i<=length;i++){
randn = (rand()%sizeof(cchoice)/sizeof(char))+1;
passwrd += cchoice[randn];
}
cout << passwrd << "\n";
cout << choice << "\n";
}
void on_quit()
{
main_win->hide();
}
int
main (int argc, char *argv[])
{
Gtk::Main kit(argc, argv);
Gtk::Button* generate = NULL;
Gtk::Button* quit = NULL;
//Load the Glade file and instiate its widgets:
Glib::RefPtr<Gnome::Glade::Xml> refXml;
try
{
refXml = Gnome::Glade::Xml::create(GLADE_FILE);
}
catch(const Gnome::Glade::XmlError& ex)
{
std::cerr << ex.what() << std::endl;
return 1;
}
refXml->get_widget("main_window", main_win);
refXml->get_widget("generate", generate);
refXml->get_widget("quit", quit);
refXml->get_widget("letters", letters);
refXml->get_widget("numbers", numbers);
refXml->get_widget("symbols", symbols);
refXml->get_widget("length", lengthb);
if (quit){
quit->signal_clicked().connect(sigc::ptr_fun(&on_quit));
}
if (generate){
generate->signal_clicked().connect(sigc::ptr_fun(&on_generate));
}
if (main_win)
{
kit.run(*main_win);
}
return 0;
}
the sizeof doesn't work, it is always 4.
#include <libglademm/xml.h>
#include <gtkmm.h>
#include <iostream>
#include <string.h>
#include <cstdlib>
#include <ctime>
#ifdef ENABLE_NLS
# include <libintl.h>
#endif
/* For testing propose use the local (not installed) glade file */
/* #define GLADE_FILE PACKAGE_DATA_DIR"/passwrdgenerator/glade/passwrdgenerator.glade" */
#define GLADE_FILE "passwrdgenerator.glade"
using namespace std;
Gtk::Window* main_win = 0;
Gtk::CheckButton* letters = 0;
Gtk::CheckButton* numbers = 0;
Gtk::CheckButton* symbols = 0;
Gtk::SpinButton* lengthb = 0;
int length;
int randn;
string passwrd;
string choice;
void on_generate()
{
choice = "";
passwrd = "";
length = 0;
randn = 0;
srand((unsigned)time(0));
if (letters->get_active())
{
choice += "qwertyuiopasdfghjklzxcvbnm";
}
if (numbers->get_active())
{
choice += "1234567890";
}
if (symbols->get_active())
{
choice += "!$%^&*~#@<>";
}
length = lengthb->get_value_as_int();
if (length == 0){
cout << length;
return;
}
int i;
const char* cchoice = choice.c_str();
cout << sizeof(cchoice) << "\n";
for(i=0;i<=length;i++){
randn = (rand()%sizeof(cchoice)/sizeof(char))+1;
passwrd += cchoice[randn];
}
cout << passwrd << "\n";
cout << choice << "\n";
}
void on_quit()
{
main_win->hide();
}
int
main (int argc, char *argv[])
{
Gtk::Main kit(argc, argv);
Gtk::Button* generate = NULL;
Gtk::Button* quit = NULL;
//Load the Glade file and instiate its widgets:
Glib::RefPtr<Gnome::Glade::Xml> refXml;
try
{
refXml = Gnome::Glade::Xml::create(GLADE_FILE);
}
catch(const Gnome::Glade::XmlError& ex)
{
std::cerr << ex.what() << std::endl;
return 1;
}
refXml->get_widget("main_window", main_win);
refXml->get_widget("generate", generate);
refXml->get_widget("quit", quit);
refXml->get_widget("letters", letters);
refXml->get_widget("numbers", numbers);
refXml->get_widget("symbols", symbols);
refXml->get_widget("length", lengthb);
if (quit){
quit->signal_clicked().connect(sigc::ptr_fun(&on_quit));
}
if (generate){
generate->signal_clicked().connect(sigc::ptr_fun(&on_generate));
}
if (main_win)
{
kit.run(*main_win);
}
return 0;
}
the sizeof doesn't work, it is always 4.