Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » Gate() or flow control problem
icon5.gif  Gate() or flow control problem [message #10451] Tue, 10 July 2007 02:08 Go to next message
pntkl is currently offline  pntkl
Messages: 4
Registered: July 2007
Junior Member
Hi,
I am very new to programming (especially C++). I am having trouble with some code I'm writing to create an XML file that stores user information. (Is XML a preferred method for such a thing?)

Problem:
I'm trying to use Gate() in an if statement, to determine if there exists a duplicate entry in an ArrayCtrl. When I run the application, the flow control never hits the for() loop contained in User::Validate(). Can anyone help me understand what it is I am doing wrong?

Test Case:

User.cpp
#include "TrayApp.h"
#include "User.h"


void
User::Add()
{
	if(!IsNull(~modify.EditFName) && !IsNull(~modify.EditLName) && !IsNull(~modify.EditUser) && !IsNull(~modify.EditPass) && !IsNull(~modify.DropGroup))
	{
		if(Gate(Validate()))
		{
			sha.Put(~modify.EditPass);
			String hashpass = sha.Finish();
			userarray.Add(~modify.EditFName, ~modify.EditLName, ~modify.EditUser, hashpass, ~modify.DropGroup);
			userarray.GoEnd();
			modify.EditFName <<= modify.EditLName <<= modify.EditUser <<= modify.EditPass <<= modify.DropGroup <<= Null;
			ActiveFocus(modify.EditFName);
			sha.Put("", 0);
		}
		else
			PromptOK("First and last name or user already exists!");
	}
	else
		PromptOK("Please complete all fields!");
}

bool
User::Validate()
{
	bool match;
	int rcount = userarray.GetCount();
	if(rcount > 0)
	{
		for(int i = rcount; i >= 1; i--)
		{
			PromptOK("For loop entered");
			String strFirstName = ~modify.EditFName;
			String strLastName = ~modify.EditLName;
			String strUser = ~modify.EditUser;
			String strColFirstName = userarray.GetColumn(i, 0); // Get the First Name
			String strColLastName = userarray.GetColumn(i, 1); // Get the Last Name
			String strColUser = userarray.GetColumn(i, 2); // Get the User Name

			if(CompareNoCase(strFirstName, strColFirstName) && CompareNoCase(strLastName, strColLastName) || CompareNoCase(strUser, strColUser))
			{
				PromptOK("CompareNoCase match found");
				match = true;
				break;
			}
			else
			{
				PromptOK("CompareNoCase does not match");
				match = false;
			}
		}
	}
	else
	{
		PromptOK("Rcount Else");
		return false;
	}
	if(!match)
	{
		PromptOK("Match return false");
		return false;
	}
	else
	{
		PromptOK("Match return true");
		return true;
	}
}

void
User::Change()
{
	;
}

void
User::Delete()
{
	if(userarray.IsCursor() && PromptOKCancel("Are you sure?"))
	{
		int row = userarray.GetCursor();
		userarray.Remove(row);
	}
	else
		PromptOK("Please select which user to delete.");
}

void
User::Open()
{
	;
}

void
User::Save()
{
	;
}

void
User::Exit()
{
	;
}
	
//void
//User::Serialize()
//{
//	;
//}

User::User()
{
	CtrlLayout(*this, "APR User Management");
	CtrlLayout(modify);
	tab.Add(modify, "Manage");
	userarray.AddColumn("First Name");
	userarray.AddColumn("Last Name");
	userarray.AddColumn("User");
	userarray.AddColumn("Password");
	userarray.AddColumn("Group");
	modify.EditPass.Password();
	modify.DropGroup.Add("Tech");
	modify.DropGroup.Add("Admin");
	modify.add <<= THISBACK(Add);
	//modify.change <<= THISBACK(Change)
	modify.del <<= THISBACK(Delete);
	//fs.AllFilesType();
}


User.h
#ifndef _APR_User_h_
#define _APR_User_h_

#include <CtrlLib/CtrlLib.h>
#include <Crypto/Crypto.h>
#include <Report/Report.h>

using namespace Upp;

#define LAYOUTFILE <APR/User.lay>
#include <CtrlCore/lay.h>

class User : public WithUserLayout<TopWindow> {
	WithModifyLayout<ParentCtrl> modify;
	Sha1 sha;
	FileSel fs;
	String filename;
		
	void Add();
	bool Validate();
	void Change();
	void Delete();
	void Open();
	void Save();
	void Exit();
	
	typedef User CLASSNAME;
	
public:
	//void Serialize(Stream& s);
	
	User();
};

#endif


User.lay
LAYOUT(UserLayout, 520, 360)
	ITEM(TabCtrl, tab, LeftPosZ(8, 504).TopPosZ(20, 132))
	ITEM(ArrayCtrl, userarray, LeftPosZ(8, 504).TopPosZ(160, 192))
END_LAYOUT

LAYOUT(ModifyLayout, 496, 96)
	ITEM(Label, dv___0, SetLabel(t_("First Name")).LeftPosZ(4, 56).TopPosZ(20, 19))
	ITEM(Label, dv___1, SetLabel(t_("Last Name")).LeftPosZ(92, 56).TopPosZ(20, 19))
	ITEM(Label, dv___2, SetLabel(t_("User")).LeftPosZ(180, 56).TopPosZ(20, 19))
	ITEM(Label, dv___3, SetLabel(t_("Password")).LeftPosZ(268, 56).TopPosZ(20, 19))
	ITEM(Label, dv___4, SetLabel(t_("Group")).LeftPosZ(356, 56).TopPosZ(20, 19))
	ITEM(EditString, EditFName, LeftPosZ(4, 72).TopPosZ(48, 19))
	ITEM(EditString, EditLName, LeftPosZ(92, 72).TopPosZ(48, 19))
	ITEM(EditString, EditUser, LeftPosZ(180, 72).TopPosZ(48, 19))
	ITEM(EditString, EditPass, LeftPosZ(268, 72).TopPosZ(48, 19))
	ITEM(DropList, DropGroup, LeftPosZ(356, 72).TopPosZ(48, 19))
	ITEM(Button, add, SetLabel(t_("Add")).LeftPosZ(444, 42).TopPosZ(12, 15))
	ITEM(Button, change, SetLabel(t_("Change")).LeftPosZ(444, 42).TopPosZ(40, 15))
	ITEM(Button, del, SetLabel(t_("Delete")).LeftPosZ(444, 42).TopPosZ(68, 15))
END_LAYOUT


P.S. - Any pointers on more appropriate methods of writing this? :)
Re: Gate() or flow control problem [message #10460 is a reply to message #10451] Tue, 10 July 2007 17:14 Go to previous messageGo to next message
pntkl is currently offline  pntkl
Messages: 4
Registered: July 2007
Junior Member
Hi Again,
I changed 'if(Gate(Validate())){}' to if(!Gate(Validate())){}, which seems to be working okay. Now, the CompareNoCase() statments always match, even if the compared strings are completely different. Can someone help me understand what I'm missing?

Also, I can't find stricmp(), nor CompareNoCase() in Assist++. Is there an easy way to search through the UPP source code (with TheIDE), in order to find out how they are written? (I'd normally use grep/sed/awk, but I was hoping TheIDE had similar functionality)

<Edit1>
I forgot to mention: It seems as if the instance of Sha1 is simply compounded, when it is called again. E.G. the same password strings are showing different Sha1.Finish() strings when I use Add(). I tried using Sha1.Put("", 0), and it seemed to work initially. I'm not sure what changed, but it doesn't work now.

If I'm posting in the wrong forum, please, let me know... Wink
</Edit2>

<Edit2>
I used PromptOK(strColFirstName, etc), and it seems that userarray.GetColumn(i, N) isn't returning the column value. Get(i, N) didn't work for me either. I was thinking about using a char array, but that won't help me--when I can't get a column's value... Razz
</Edit2>

[Updated on: Tue, 10 July 2007 18:41]

Report message to a moderator

Re: Gate() or flow control problem [message #10463 is a reply to message #10460] Tue, 10 July 2007 22:25 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
I am not quite sure what you want to achieve, however

if(Gate(Validate()))


This constructs a Gate object with default value obtained by calling Validate(), then it evaluates Gate by operator bool (which results in the value returned by Validate) and then destructs the Gate instance.

In other words, quite nonsense Smile
Re: Gate() or flow control problem [message #10464 is a reply to message #10463] Tue, 10 July 2007 22:27 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
P.S.: Now seing the code more carefuly, there is no need for Gate IMO. Just call Validate... or what did you expect the Gate is able to give to you?

[Updated on: Tue, 10 July 2007 22:30]

Report message to a moderator

Re: Gate() or flow control problem [message #10465 is a reply to message #10460] Tue, 10 July 2007 22:34 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
pntkl wrote on Tue, 10 July 2007 11:14


Also, I can't find stricmp(), nor CompareNoCase() in Assist++. Is there an easy way to search through the UPP source code (with TheIDE), in order to find out how they are written? (I'd normally use grep/sed/awk, but I was hoping TheIDE had similar functionality)



Ctrl+Shift+F (Find in files).

Quote:


<Edit2>
I used PromptOK(strColFirstName, etc), and it seems that userarray.GetColumn(i, N) isn't returning the column value. Get(i, N) didn't work for me either. I was thinking about using a char array, but that won't help me--when I can't get a column's value... Razz
</Edit2>


Yes, I have just noticed another trouble:

for(int i = rcount; i >= 1; i--)

In C/C++/Java/C# etc... array indexes start at 0, which is adapted in ArrayCtrl as well. Therefore I think this should be

for(int i == rcount - 1; i >= 0; i++)

Mirek
Re: Gate() or flow control problem [message #10466 is a reply to message #10465] Tue, 10 July 2007 22:49 Go to previous messageGo to next message
pntkl is currently offline  pntkl
Messages: 4
Registered: July 2007
Junior Member
Thank you so much, Mirek. You're right, I didn't account for the 0 based index. Shocked

I think the only problem I'm having now has to do with why I am using Gate(). I was initially using if(Validate())..., but when I use that; it seems to go straight to the else statement. Then, I tried if(THISBACK(Validate))..; that went into the if(Validate())..., but never actually called Validate(). I think its just a lack of understanding, I'm just not sure what I'm missing... Wink
icon7.gif  Re: Gate() or flow control problem [message #10471 is a reply to message #10466] Wed, 11 July 2007 01:59 Go to previous messageGo to next message
pntkl is currently offline  pntkl
Messages: 4
Registered: July 2007
Junior Member
Ok,

I understand what you're saying, Mirek. I'm not sure what gave me the impression I needed to use THISBACK(); in the condition... Wink

Thanks again, Mirek. Now I just need to find out what my Sha1 instance is doing... Smile

<EDIT>
I was calling the instance in the header file, instead of in its proper scope (User::Add()).
</EDIT>

Is crypto.h going to include other algorithms and will it be documented at all? It took me a while to realize crypto. Confused

These are the relevant code changes:

void
User::Add()
{
	if(!IsNull(~modify.EditFName) && !IsNull(~modify.EditLName) && !IsNull(~modify.EditUser) && !IsNull(~modify.EditPass) && !IsNull(~modify.DropGroup))
	{
		//if(!Gate(Validate())) <-- Removed.
		match = false;
		Validate();
		if(!match) // <-- bool match; added to User.h
		{
			Sha1 sha; // <-- Removed from User.h
			sha.Put(~modify.EditPass);
			String hashpass = sha.Finish();
			userarray.Add(~modify.EditFName, ~modify.EditLName, ~modify.EditUser, hashpass, ~modify.DropGroup);
			userarray.GoEnd();
			modify.EditFName <<= modify.EditLName <<= modify.EditUser <<= modify.EditPass <<= modify.DropGroup <<= Null;
			ActiveFocus(modify.EditFName);
			// sha.Put("", 0); <-- Removed. What was I thinking?!
		}
		else
			PromptOK("First and last name or user already exists!");
	}
	else
		PromptOK("Please complete all fields!");
}

void
User::Validate()
{
	int rcount = userarray.GetCount();
	if(rcount > 0)
	{
		for(int i = rcount - 1; i >= 0; i--)
		{
			String strFirstName = ~modify.EditFName;
			String strColFirstName = userarray.GetColumn(i, 0); // Get the First Name
			String strLastName = ~modify.EditLName;
			String strColLastName = userarray.GetColumn(i, 1); // Get the Last Name
			String strUser = ~modify.EditUser;
			String strColUser = userarray.GetColumn(i, 2); // Get the User Name
				
			if(CompareNoCase(strFirstName, strColFirstName) && CompareNoCase(strLastName, strColLastName) || CompareNoCase(strUser, strColUser))
			{
				match = false; // <-- Reversed, I guess CompareNoCase returns true--with no match?
			}
			else
			{
				match = true;
				break;
			}
		}
	}
	else
	{
		match = false;
	}
}

[Updated on: Wed, 11 July 2007 02:45]

Report message to a moderator

Re: Gate() or flow control problem [message #10475 is a reply to message #10471] Wed, 11 July 2007 07:38 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
pntkl wrote on Tue, 10 July 2007 19:59


Is crypto.h going to include other algorithms and will it be documented at all? It took me a while to realize crypto. Confused



I rather think Sha1 should be moved somewhere (e.g. where MD5 is...)

Previous Topic: asm int 3, abort();
Next Topic: Problem using Split on carriage return
Goto Forum:
  


Current Time: Mon Apr 29 00:51:37 CEST 2024

Total time taken to generate the page: 0.07132 seconds