Re: Checking to see if field has data in it before passing it routine

2005-07-24 Thread Roger Stringer



Subject: RE: Checking to see if field has data in it before passing it routine
From: Jonathan King [EMAIL PROTECTED]
Date: Sat, 23 Jul 2005 18:41:31 -0400
X-Message-Number: 11

Here is a simple way to do what you are doing.

static void GetFieldData( UInt16 fld, Char *text ) {
FormPtr frm = FrmGetActiveForm();
FieldPtrfldP = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm,fld));

if(FldGetTextLegnth(fldP))  {
StrCopy(text, FldGetTextPtr(fldP);
}
}


Here's a better way that has error checking, default processing and takes 
advantage of a unique feature of the PalmOS StrNCat() function to prevent 
stack corruption:


  Boolean GetFieldData (UInt16 fldNbr, Char *text, UInt16 maxLen) {
FormPtr pForm = FrmGetActiveForm ();
FieldPtr  pField = FrmGetObjectPtr (pForm, FrmGetObjectIndex 
(pForm, fldNbr));

if  (text != NULL) *text = '\0'; // initialize
if (text == NULL || pField == NULL) return false; // I like error 
checking  !!

if (FldGetTextLength (pField))
StrNCat (text, FldGetTextPtr (pField), maxLen);
return true;
  }

Roger Stringer
Marietta Systems, Inc. (www.rf-tp.com)


--
For information on using the PalmSource Developer Forums, or to unsubscribe, 
please see http://www.palmos.com/dev/support/forums/


Re: Checking to see if field has data in it before passing it routine

2005-07-24 Thread druid
Here's a better way that has error checking, default processing and takes 
advantage of a unique feature of the PalmOS StrNCat() function to prevent 
stack corruption:

When using a functin like this where does the maxLen
value come from when passing the data to this function

Could I also make a change  


Boolean GetFieldData (UInt16 fldNbr, Char *text, UInt16 maxLen) {
FormPtr pForm = FrmGetActiveForm ();
FieldPtr pField = FrmGetObjectPtr (pForm, FrmGetObjectIndex 
(pForm, fldNbr));
if (text != NULL) *text = '\0'; // initialize
if (text == NULL || pField == NULL)
 {
  missing_field = true; 
  return false; // I like error checking !!
 } // use missing_field to set an aleart window and brake the 
   //static Boolean frmMain_saveButton_OnSelect(EventPtr event)
   // so as to not allow the save ??
if (FldGetTextLength (pField))
StrNCat (text, FldGetTextPtr (pField), maxLen);
return true;
}


-- 
For information on using the PalmSource Developer Forums, or to unsubscribe, 
please see http://www.palmos.com/dev/support/forums/


RE: Checking to see if field has data in it before passing it routine

2005-07-24 Thread Jonathan King
maxLen would be the length of the text buffer pointed to by text. For
instance, if text was char text[64], you would call
GetFieldData(FieldID, text, 63).

 -Original Message-
 From: [EMAIL PROTECTED] [mailto:bounce-450125-
 [EMAIL PROTECTED] On Behalf Of druid
 Sent: Sunday, July 24, 2005 10:41 AM
 To: Palm Developer Forum
 Subject: Re: Checking to see if field has data in it before passing it
 routine
 
 Here's a better way that has error checking, default processing and
takes
 advantage of a unique feature of the PalmOS StrNCat() function to
 prevent
 stack corruption:
 
 When using a functin like this where does the maxLen
 value come from when passing the data to this function
 
 Could I also make a change
 
 
 Boolean GetFieldData (UInt16 fldNbr, Char *text, UInt16 maxLen) {
 FormPtr pForm = FrmGetActiveForm ();
 FieldPtr pField = FrmGetObjectPtr (pForm, FrmGetObjectIndex
 (pForm, fldNbr));
 if (text != NULL) *text = '\0'; // initialize
 if (text == NULL || pField == NULL)
  {
   missing_field = true;
   return false; // I like error checking !!
  } // use missing_field to set an aleart window and brake the
//static Boolean frmMain_saveButton_OnSelect(EventPtr event)
// so as to not allow the save ??
 if (FldGetTextLength (pField))
 StrNCat (text, FldGetTextPtr (pField), maxLen);
 return true;
 }
 
 
 --
 For information on using the PalmSource Developer Forums, or to
 unsubscribe, please see http://www.palmos.com/dev/support/forums/



-- 
For information on using the PalmSource Developer Forums, or to unsubscribe, 
please see http://www.palmos.com/dev/support/forums/


Re: Checking to see if field has data in it before passing it routine

2005-07-24 Thread Chris Tutty
From: druid [EMAIL PROTECTED]
 Here's a better way that has error checking, default processing and takes
 advantage of a unique feature of the PalmOS StrNCat() function to
prevent
 stack corruption:

Are you sure it's StrNCat you want and not StrNCopy?  I'm just
asking because the old version copied to text so you seem to
have changed the way the function works.

 When using a functin like this where does the maxLen
 value come from when passing the data to this function

It depends on how the buffer is defined.  If you're defining it
using a constant then pass that constant - 1.  It gets more
complicated if you're building a string up and concatenating
because you need to take the existing length of the string into
account but for StrNCopy it's generally just a matter of passing
the defined length -1.

Chris Tutty

 Boolean GetFieldData (UInt16 fldNbr, Char *text, UInt16 maxLen) {
 FormPtr pForm = FrmGetActiveForm ();
 FieldPtr pField = FrmGetObjectPtr (pForm, FrmGetObjectIndex
 (pForm, fldNbr));
 if (text != NULL) *text = '\0'; // initialize
 if (text == NULL || pField == NULL)
  {
   missing_field = true;
   return false; // I like error checking !!
  } // use missing_field to set an aleart window and brake the
file://static Boolean frmMain_saveButton_OnSelect(EventPtr event)
// so as to not allow the save ??
 if (FldGetTextLength (pField))
 StrNCat (text, FldGetTextPtr (pField), maxLen);
 return true;
 }


 --
 For information on using the PalmSource Developer Forums, or to
unsubscribe, please see http://www.palmos.com/dev/support/forums/


-- 
For information on using the PalmSource Developer Forums, or to unsubscribe, 
please see http://www.palmos.com/dev/support/forums/


Re: Checking to see if field has data in it before passing it routine

2005-07-24 Thread Chris Tutty
From: Chris Tutty [EMAIL PROTECTED]
 From: druid [EMAIL PROTECTED]
  Here's a better way that has error checking, default processing and
takes
  advantage of a unique feature of the PalmOS StrNCat() function to
 prevent
  stack corruption:
 
 Are you sure it's StrNCat you want and not StrNCopy?  I'm just
 asking because the old version copied to text so you seem to
 have changed the way the function works.

Ah, my mistake, I didn't realise that Roger had stepped in and
rewritten your function (of course if I'd actually read the whole
function I'd have seen that).

And he's added a technique for handling the missing fields.
Yeesh, I should just go back to bed.

Chris Tutty


-- 
For information on using the PalmSource Developer Forums, or to unsubscribe, 
please see http://www.palmos.com/dev/support/forums/


Re: Checking to see if field has data in it before passing it routine

2005-07-23 Thread Chris Tutty
From: druid [EMAIL PROTECTED]
 I need to check to see if the field has data in it before I pass it to
GetFieldData.
 What is the best way to do that and assign a text value to name
 if the field is NULL

Do you mean if the field is NULL or if the field contents are NULL?

If the contents are NULL GetFieldData will handle it acceptably,
although it would perhaps be better to clear text before copying any
value in fld.  Clearing it first gaurantees that there isn't old data in
text after GetFieldData returns.

Unfortunately you're not passing enough information to GetFieldData
for it to gaurantee robust results.  All functions that pass a buffer as a
char * must also pass a buffer length.  WIthout this length it's impossible
for the function to either clear the buffer if fld is NULL, or to gaurantee
that buffer overflow isn't occuring.  StrCopy is actually very dangerous
- your code will be much more robust in the long term if you remove
every StrCopy and replace it with a StrNCopy using the appropriate
defined buffer length AND terminate the string properly.  Since the
standard Palm OD StrNCopy doesn't gaurantee string termination it's
better to wrap this in a function that always terminates the string.

It's a bit of work to make sure that you always handle strings robustly,
but it'll save you time in the long run.  Tryng to track down buffer
overruns after release is insanely expensive in times of time, money
and product credibility.

So
 GetFieldData( fldName, name );

should be GetFieldData( fldName, name, nameLen );

 static void GetFieldData( UInt16 fld, Char *text )
 {
static void GetFieldData( UInt16 fld, Char *text, length )
{


 FormPtr frm = FrmGetActiveForm();
 FieldPtrfldP = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm,fld));

 MemHandle hText = FldGetTextHandle(fldP);
 if (hText)
 {
   MemPtr *pMem = MemHandleLock( hText );
   StrCopy( text, (Char*)pMem );

StrNCopy( text, (Char*)pMem, length );
text[length - 1] = '\0';

   MemHandleUnlock( hText );
 }
} else
{
MemSet(text, length, '\0');
}
 }

Chris Tutty


-- 
For information on using the PalmSource Developer Forums, or to unsubscribe, 
please see http://www.palmos.com/dev/support/forums/


Re: Checking to see if field has data in it before passing it routine

2005-07-23 Thread druid
First im concerned that the field is not NULL, in other words
when the form is opened, the field exists with nothing in it.
If I have if figured correctly that is NULL
if all contents are removed from the field that means the fields
contents are NULL
Im concerned that both situations are handled
-- 
For information on using the PalmSource Developer Forums, or to unsubscribe, 
please see http://www.palmos.com/dev/support/forums/


Re: Checking to see if field has data in it before passing it routine

2005-07-23 Thread druid
since fldName is a non database assigned field
this means its NULL i think
So I need to check it for that before I use the GetFieldData
the name length is set to 255 when its declared so passing the 
nameLen would always be 256
and it is NULL because nothing has been assigned
Char name[256];
The problem is I need to know what is in fldName before I try and use 
GetFieldData and if the contents or the field is NULL
not do the call to the GetFieldData routine

-- 
For information on using the PalmSource Developer Forums, or to unsubscribe, 
please see http://www.palmos.com/dev/support/forums/


RE: Checking to see if field has data in it before passing it routine

2005-07-23 Thread Jonathan King
Here is a simple way to do what you are doing.

static void GetFieldData( UInt16 fld, Char *text )
{
FormPtr frm = FrmGetActiveForm();
FieldPtrfldP = FrmGetObjectPtr(frm,
FrmGetObjectIndex(frm,fld));

if(FldGetTextLegnth(fldP))
{
StrCopy(text, FldGetTextPtr(fldP);
}
}

 -Original Message-
 From: [EMAIL PROTECTED] [mailto:bounce-449914-
 [EMAIL PROTECTED] On Behalf Of druid
 Sent: Friday, July 22, 2005 8:53 PM
 To: Palm Developer Forum
 Subject: Checking to see if field has data in it before passing it
routine
 
  I need to check to see if the field has data in it before I pass it
to
 GetFieldData.
 What is the best way to do that and assign a text value to name
 if the field is NULL
 
 
 GetFieldData( fldName, name );
 
 
 
 
 static void GetFieldData( UInt16 fld, Char *text )
 {
 FormPtr frm = FrmGetActiveForm();
 FieldPtrfldP = FrmGetObjectPtr(frm,
FrmGetObjectIndex(frm,fld));
 
MemHandle hText = FldGetTextHandle(fldP);
   if (hText)
   {
 MemPtr *pMem = MemHandleLock( hText );
 StrCopy( text, (Char*)pMem );
 MemHandleUnlock( hText );
 }
 }
 --
 For information on using the PalmSource Developer Forums, or to
 unsubscribe, please see http://www.palmos.com/dev/support/forums/



-- 
For information on using the PalmSource Developer Forums, or to unsubscribe, 
please see http://www.palmos.com/dev/support/forums/


re: Checking to see if field has data in it before passing it routine

2005-07-23 Thread druid
I tried you code change and it does work
Now for the wierd thing

I added this


static void GetFieldData( UInt16 fld, Char *text )
{
FormPtr frm = FrmGetActiveForm();
FieldPtr fldP = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm,fld));

 if(FldGetTextLength(fldP))
  {
   StrCopy(text, FldGetTextPtr(fldP));
  }
   else
  {
   StrCopy(text, UNKNOWN);
  }
}

now if I dont fill in any of the fields and it writes this to
the record, the very first field has the first three characters
cut off

OWN,UNKNOWN,Select,,Select,Select,NO,NO,NO,NO,NO,NO,YES,NO,,UNKNOWN,UNKNOWN

however if I actualy put data into the field

this is a test,21,Fair,test desc,good,A Gnoll 
Cave,YES,NO,NO,NO,NO,NO,YES,NO,test comment,2,NPC 
test

this all is ok
now it only does this for the very first field all the others get the full word 
UNKNOWN

-- 
For information on using the PalmSource Developer Forums, or to unsubscribe, 
please see http://www.palmos.com/dev/support/forums/


Re: Checking to see if field has data in it before passing it routine

2005-07-23 Thread Chris Tutty
From: Jonathan King [EMAIL PROTECTED]
 Here is a simple way to do what you are doing.

 static void GetFieldData( UInt16 fld, Char *text )
 {
 FormPtr frm = FrmGetActiveForm();
 FieldPtrfldP = FrmGetObjectPtr(frm,
 FrmGetObjectIndex(frm,fld));

 if(FldGetTextLegnth(fldP))
which needs to be:
if(fldP   FldGetTextLegnth(fldP))
if the point about the field itself being null is to be handled,
but since the code already tests the field handle for NULL
the only other thing you're doing is checking for an empty
string which, from memory, StrCopy will handle acceptably
so I'm not sure that this code adds much and...

 {
 StrCopy(text, FldGetTextPtr(fldP);
... you're not testing the FldGetTextPtr() return value for
null and, as far as I'm aware, this can return null so your
code is more fragile than the original druid proposed.
No?


  -Original Message-
  From: [EMAIL PROTECTED] [mailto:bounce-449914-
  [EMAIL PROTECTED] On Behalf Of druid
  Sent: Friday, July 22, 2005 8:53 PM
  To: Palm Developer Forum
  Subject: Checking to see if field has data in it before passing it
 routine
 
   I need to check to see if the field has data in it before I pass it
 to
  GetFieldData.
  What is the best way to do that and assign a text value to name
  if the field is NULL
 
 
  GetFieldData( fldName, name );
 
 
 
 
  static void GetFieldData( UInt16 fld, Char *text )
  {
  FormPtr frm = FrmGetActiveForm();
  FieldPtrfldP = FrmGetObjectPtr(frm,
 FrmGetObjectIndex(frm,fld));
 
  MemHandle hText = FldGetTextHandle(fldP);
  if (hText)
  {
MemPtr *pMem = MemHandleLock( hText );
StrCopy( text, (Char*)pMem );
MemHandleUnlock( hText );
  }
  }
  --
  For information on using the PalmSource Developer Forums, or to
  unsubscribe, please see http://www.palmos.com/dev/support/forums/



 --
 For information on using the PalmSource Developer Forums, or to
unsubscribe, please see http://www.palmos.com/dev/support/forums/


-- 
For information on using the PalmSource Developer Forums, or to unsubscribe, 
please see http://www.palmos.com/dev/support/forums/


Re: Checking to see if field has data in it before passing it routine

2005-07-23 Thread Chris Tutty
From: druid [EMAIL PROTECTED]
 The problem is I need to know what is in fldName before I try 
 and use GetFieldData and if the contents or the field is NULL
 not do the call to the GetFieldData routine
 
Nope, I think you're doing the right thing bu checking for NULL 
data within the function.  This means that the function is safe
regardless of what's passed to it.  You just need to check the
fldP for null and I think the function as written is fine.

Chris Tutty


-- 
For information on using the PalmSource Developer Forums, or to unsubscribe, 
please see http://www.palmos.com/dev/support/forums/


Re: Checking to see if field has data in it before passing it routine

2005-07-23 Thread Chris Tutty
From: druid [EMAIL PROTECTED]
 I tried you code change and it does work
 Now for the wierd thing
 
 I added this
 
StrCopy(text, UNKNOWN);
   }
 now if I dont fill in any of the fields and it writes this to
 the record, the very first field has the first three characters
 cut off
 OWN,UNKNOWN,Select,,

Hmm, that's the first four characters which is significant because
it's the size of a long int, suggesting that a char pointer has been
offset by one (stack corruption, mishandled pointer or something
strange about the way the constant is being stored).  It's
difficult to say why this might work for good data and fail for
the constant.  

Try checking to see if the data *before* the first field has been 
overwritten (generally requiring some work with the debugger
or a hex editor).  You want to know whether the full text has been
written to an address four bytes before the start of the field or
whether the start of the constant is wrong by four bytes but the
data has been written to the wrong location.  There are some
compiler settings for how constants are handled but I wouldn't
have thought they'd cause this sort of problem.

Chris Tutty


-- 
For information on using the PalmSource Developer Forums, or to unsubscribe, 
please see http://www.palmos.com/dev/support/forums/


re: Checking to see if field has data in it before passing it routine

2005-07-23 Thread druid
found it
If I clicked save button before I entered data
this 

size = StrLen(name)+1+ StrLen(level)+1+ StrLen(expFld)+1+ StrLen(desc)+1+
   StrLen(mItems)+1+ StrLen(startz)+1+ StrLen(heritage)+1+ StrLen(citytask)+1+
   StrLen(sabatoge)+1+ StrLen(repet)+1+ StrLen(access)+1+ StrLen(freeport)+1+
   StrLen(qeynos)+1+ StrLen(complete)+1+ StrLen(comment)+1+ StrLen(spoints)+1+
   StrLen(npc)+1;

would end up with null lengths for some of the StrLen
It manifested itself by cutting off the first 4 characters

So i added this in the 
case frmOpenEvent:
// Repaint form on open
form = FrmGetActiveForm();
FrmDrawForm(form);
StrCopy(heritage, NO);  
StrCopy(citytask, NO);  
StrCopy(sabatoge, NO);  
StrCopy(complete, NO);  
StrCopy(access, NO);  
StrCopy(freeport, NO);  
StrCopy(qeynos, YES);
StrCopy(repet, NO);
StrCopy(desc, NONE);
StrCopy(comment, NONE);
StrCopy(name, NONE);
StrCopy(npc, NONE);
StrCopy(level, 0);
StrCopy(spoints, 0);

to be sure all of it had a value incase the record was saved
with out the user putting data in
However what I really want to do it not allow a save unless
all fields are completed

-- 
For information on using the PalmSource Developer Forums, or to unsubscribe, 
please see http://www.palmos.com/dev/support/forums/


Checking to see if field has data in it before passing it routine

2005-07-22 Thread druid
 I need to check to see if the field has data in it before I pass it to 
GetFieldData.
What is the best way to do that and assign a text value to name
if the field is NULL


GetFieldData( fldName, name );



   
static void GetFieldData( UInt16 fld, Char *text )
{
FormPtr frm = FrmGetActiveForm();
FieldPtrfldP = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm,fld));

 MemHandle hText = FldGetTextHandle(fldP);
if (hText)
{
  MemPtr *pMem = MemHandleLock( hText );
  StrCopy( text, (Char*)pMem );
  MemHandleUnlock( hText );
}
}
-- 
For information on using the PalmSource Developer Forums, or to unsubscribe, 
please see http://www.palmos.com/dev/support/forums/