On 9/21/06, Larry Watkins <[EMAIL PROTECTED]> wrote:
OK, I still don't get this pointer business. But I am intrigued by your
suggestion. I think I understand that a string gets written to a memory
block and the pointer just points to that memory block location.
typedef struct {
Char GPSSourceStr[256];
Char *GPSLatCoor;
Char *GPSLatDir;
Char *GPSLonCoor;
Char *GPSLonDir;
} GPSDataType;
static GPSDataType GetCoordinates (Char *StrToSearch)
{
GPSDataType GPSData;
Char *pStr;
StrCopy(GPSData.GPSSourceStr, StrToSearch);
// Searches string for GPS data formatted as
// $GPGGA,[UTC],[Latitude],[N/S],[Longitude],[E/W],...
pStr = GPSData.GPSSourceStr;
// find the signature
pStr = StrStr(pStr, "$GPGGA");
pStr = StrStr(pStr, ","); *pStr++ = 0;
// pStr now points to UTC
// - you are not using this
pStr = StrStr(pStr, ","); *pStr++ = 0;
// pStr now points to latitude
GPSData.GPSLatCoor = pStr;
pStr = StrStr(pStr, ","); *pStr++ = 0;
// pStr now points to latitude direction
GPSData.GPSLatDir = pStr;
pStr = StrStr(pStr, ","); *pStr++ = 0;
// pStr now points to longitude
GPSData.GPSLongCoor = pStr;
pStr = StrStr(pStr, ","); *pStr++ = 0;
// pStr now points to longitudedirection
GPSData.GPSLongDir = pStr;
pStr = StrStr(pStr, ","); *pStr++ = 0;
//.. the other string contents
return GPSData;
}
thats without any error checking - you want to ensure that the pStr
never is NULL from a call to StrStr(). if it is, you have a badly sourced
string in the first place - in which you want to return an error.
secondly, NEVER return a structure from a function. this is bad, the
compiler cannot optimize it well and it makes error checking harder.
you sould try and use pointers, this way, only 4 bytes is passed around
instead of the compiler having to mess around with memory allocating
and doing it for you.
just look at the assembly code generated with your code :) you'll see.
this is what you should do:
static GPSDataType *GetCoordinates (Char *StrToSearch)
{
GPSDataType *result;
// default return value
result = NULL;
// check string format (error check)
// ...
// allocate memory for GPSData structure
result = MemPtrNew(sizeof(GPSDataType));
// existing code, but GPSData.item becomes GPSData -> item
return result;
}
what you can do then, is when the routine is called - check if the
result is NULL, if so, then you can detect a badly formatted GPS
string in the first place.
if you want to avoid the use of API's - you can make it even
cleaner by replacing
pStr = StrStr(pStr, ",");
with a simple:
while (pStr != ',') pStr++;
now - the routine can look much simpler and use the CPU much more
effectively. its important to have the *pStr = 0; as this is what creates
the terminators in the strings.
you can even optimize the use of the GPSData.GPSSourceStr by
allocating it on the heap and only using the memory you actually need.
of course - you need to manage the releasing of this memory as well.
a little piece of homework for you.
i am a traditional unix/gcc programmer - and a palmos veteran of 6+
years now, but you dont have to specifically listen to me. :) i like clean
and easy to understand code to be put in front of me, some people
panic when they see pointers - but, its really a simple concept.
--
// Aaron Ardiri
--
For information on using the PalmSource Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/support/forums/