Greetings everyone,

Below are my structs and the code that I am using in
my app to do pack and unpack of database record.

I always set my noteFlag if the note field points to
empty string (" ") or contains something meaningful
rather than NULL.  When I debug, I look at the
variable window and also the memory to make sure my
noteFlag get set to 0x5AA5 which it always DID and
packed into the database.

However, at unpacking time, my noteFlag always = 0 if
my note field points to " ".  Value 0x5AA5 set before
also disappears from the memory.  If there is
something meaningful notes in the field other than "
", then the noteFlag value seems to stay.

I am afraid of byte alignment and memory shifting so I
used MemMove, and DmWrite in my code, but this does
not solve the problem.

If anybody has any ideas as why this happens this way,
please let me know.
Any suggestions is truly appreciated.
Thanks.

typedef struct {
   UInt16 color;
   UInt16 size;
   UInt16 style;
   UInt16 item;
   UInt16 cost;
   UInt16 disc;
   Int16  qua0;
   Int16  qua1;
   Int16  qua2;
   Int16  qua3;
   Int16  qua4;
   UInt16 qua5;
   UInt32 totalOrder;
   UInt32 totalPurchase;
   char *name;
   char *address;
   char *company;
   char *note;
} SaleRec;

typedef struct {
   UInt16 color;
   UInt16 size;
   UInt16 style;
   UInt16 item;
   UInt16 cost;
   UInt16 disc;
   Int16  qua0;
   Int16  qua1;
   Int16  qua2;
   Int16  qua3;
   Int16  qua4;
   UInt16 qua5;
   UInt32 totalOrder;
   UInt32 totalPurchase;
   UInt16 nameFlag;
   UInt16 addressFlag;
   UInt16 companyFlag;
   UInt16 noteFlag;
   char name[1];

} PackedSaleRec;

void PackSaleRecord (SaleRec *saleRecord, MemHandle
saleHandle)
{
   UInt16 length = 0, len=0;
   Char *p, *srcP;
   UInt32 offset;
   PackedSaleRec *d=0;
   PackedSaleRec *temp;
   Err err = 0;
   UInt16 flag;

   length = SaleRecUnpackedSize(saleRecord);        
// Return the correct size

   // Resize the handle
   if (MemHandleResize(saleHandle, length) == 0) {
      // Copy the fields
      p = (Char *)MemHandleLock(saleHandle);
      temp = (PackedSaleRec *)p;
      offset = 0;
      DmWrite(p, offset, &saleRecord->color,
sizeof(saleRecord->color));
      offset += sizeof(saleRecord->color);
      DmWrite(p, offset, &saleRecord->size,
sizeof(saleRecord->size));
      offset += sizeof(saleRecord->size);
      DmWrite(p, offset, &saleRecord->style,
sizeof(saleRecord->style));
      offset += sizeof(saleRecord->style);
      DmWrite(p, offset, &saleRecord->item,
sizeof(saleRecord->item));
      offset += sizeof(saleRecord->item);
      DmWrite(p, offset, &saleRecord->cost,
sizeof(saleRecord->cost));
      offset += sizeof(saleRecord->cost);
      DmWrite(p, offset, &saleRecord->disc,
sizeof(saleRecord->disc));
      offset += sizeof(saleRecord->disc);
      DmWrite(p, offset, &saleRecord->qua0,
sizeof(saleRecord->qua0));
      offset += sizeof(saleRecord->qua0);
      DmWrite(p, offset, &saleRecord->qua1,
sizeof(saleRecord->qua1));
      offset += sizeof(saleRecord->qua1);
      DmWrite(p, offset, &saleRecord->qua2,
sizeof(saleRecord->qua2));
      offset += sizeof(saleRecord->qua2);
      DmWrite(p, offset, &saleRecord->qua3,
sizeof(saleRecord->qua3));
      offset += sizeof(saleRecord->qua3);
      DmWrite(p, offset, &saleRecord->qua4,
sizeof(saleRecord->qua4));
      offset += sizeof(saleRecord->qua4);
      DmWrite(p, offset, &saleRecord->qua5,
sizeof(saleRecord->qua5));
      offset += sizeof(saleRecord->qua5);
      DmWrite(p, offset, &saleRecord->totalOrder,
sizeof(saleRecord->totalOrder));
      offset += sizeof(saleRecord->totalOrder);
      DmWrite(p, offset, &saleRecord->totalPurchase,
sizeof(saleRecord->totalPurchase));
      offset += sizeof(saleRecord->totalPurchase);

      offset = (Int32)&d->name;
      DmStrCopy(p, offset, (Char *) saleRecord->name);
      offset += StrLen(saleRecord->name) + 1;
      flag = 2;
      DmWrite(p, (Int32) &d->nameFlag, &flag,
sizeof(flag));

      DmStrCopy(p, offset, (Char *)
saleRecord->address);
      offset += StrLen(saleRecord->address) + 1;
      flag = 3;
      DmWrite(p, (Int32) &d->addressFlag, &flag,
sizeof(flag));

      DmStrCopy(p, offset, (Char *)
saleRecord->company);
      offset += StrLen(saleRecord->company) + 1;
      flag = 4;
      DmWrite(p, (Int32) &d->companyFlag, &flag,
sizeof(flag));

      if (saleRecord->note != NULL) {
         srcP = saleRecord->note;
         len = StrLen(saleRecord->note);
         DmWrite(p, offset, srcP,
StrLen(saleRecord->note) + 1);
         offset += StrLen(saleRecord->note + 1);
         flag = 0x5AA5;
         DmWrite(p, (Int32) &d->noteFlag, &flag,
sizeof(flag));
       }

      MemHandleUnlock(saleHandle);

   }
}

void UnpacksaleRecord(SaleRec *saleRecord,
PackedSaleRec *packedsaleRecord)
{
   UInt16 nameFlag, addressFlag, companyFlag,
noteFlag;
   Char *tempP;

   tempP = (Char *)packedSaleRecord;

   MemMove(&saleRecord->color, tempP, sizeof(UInt16));
   tempP += sizeof(UInt16);

   MemMove(&saleRecord->size, tempP, sizeof(UInt16));
   tempP += sizeof(UInt16);

   MemMove(&saleRecord->style, tempP, sizeof(UInt16));
   tempP += sizeof(UInt16);

   MemMove(&saleRecord->item, tempP, sizeof(UInt16));
   tempP += sizeof(UInt16);

   MemMove(&saleRecord->cost, tempP, sizeof(UInt16));
   tempP += sizeof(UInt16);

   MemMove(&saleRecord->disc, tempP, sizeof(UInt16));
   tempP += sizeof(UInt16);

   MemMove(&saleRecord->qua0, tempP, sizeof(Int16));
   tempP += sizeof(Int16);

   MemMove(&saleRecord->qua1, tempP, sizeof(Int16));
   tempP += sizeof(Int16);

   MemMove(&saleRecord->qua2, tempP, sizeof(Int16));
   tempP += sizeof(Int16);

   MemMove(&saleRecord->qua3, tempP, sizeof(Int16));
   tempP += sizeof(Int16);

   MemMove(&saleRecord->qua4, tempP, sizeof(Int16));
   tempP += sizeof(Int16);

   MemMove(&saleRecord->qua5, tempP, sizeof(UInt16));
   tempP += sizeof(UInt16);

   MemMove(&saleRecord->totalOrder, tempP,
sizeof(UInt32));
   tempP += sizeof(UInt32);

   MemMove(&saleRecord->totalPurchase, tempP,
sizeof(UInt32));
   tempP += sizeof(UInt32);

   MemMove(&nameFlag, tempP, sizeof(nameFlag));
   tempP += sizeof(nameFlag);

   MemMove(&addressFlag, tempP, sizeof(addressFlag));
   tempP += sizeof(addressFlag);

   MemMove(&companyFlag, tempP, sizeof(companyFlag));
   tempP += sizeof(companyFlag);

   MemMove(&noteFlag, tempP, sizeof(noteFlag));
   tempP += sizeof(noteFlag);

   saleRecord->name = tempP;
   tempP += StrLen(tempP) + 1;
   saleRecord->address = tempP;
   tempP += StrLen(tempP) + 1;
   saleRecord->company = tempP;
   tempP += StrLen(tempP) + 1;

   if (noteFlag) {
      saleRecord->note = tempP;
      p += StrLen(p) + 1;
   } else
      saleRecord->note = NULL;

}

 




=====
Best regards,

Van Nguyen.

__________________________________________________
Do You Yahoo!?
Yahoo! Photos - 35mm Quality Prints, Now Get 15 Free!
http://photos.yahoo.com/


-- 
For information on using the ACCESS Developer Forums, or to unsubscribe, please 
see http://www.access-company.com/developers/forums/

Reply via email to