'Bitfields' in Pascal 
Author Message
 'Bitfields' in Pascal

I am trying to port some file structures from C to Pascal, but am
running into some problems with the 'bitfields'.  Here is the C code
that I need to convert to pascal (Only parts dealing with bitfields):

enum MCONFDATABITFIELDS {
        NetworkedConferenceFlag, PrivateConferenceFlag,
        FidoStyleNetMailFlag, AliasConferenceFlag,
        UserDeleteFlag, TriPackBackupFlag,
        DisablePrivateMessagesFlag, DisablePublicMessagesFlag,
        MCLastBit };

typedef struct {
        char Name[41];
        SHORT ReadSecurityLevel;
        SHORT PostSecurityLevel;
        SHORT TriPackNumberOfDaysToSave;
        LONG HighestMessageNumber;
        unsigned char NetworkFlags[2];
        unsigned char BitFields[10];
        unsigned char ReservedDataArea[961];

Quote:
} MCONFDATA;

=: That is a structure for one of the files :=

=: These are procs for changing the fields I guess :=

inline void SetBit(unsigned char *s, SHORT n)
{
        s[n / 8] |= (unsigned char)(1 << (n % 8));

Quote:
}

inline void ResetBit(unsigned char *s, SHORT n)
{
        s[n / 8] &= (unsigned char)(~(1 << (n % 8)));

Quote:
}

inline SHORT GetBit(unsigned char *s, SHORT n)
{
        return (s[n / 8] & (1 << (n % 8))) ? 1 : 0;

Quote:
}

inline void ModifyBit(unsigned char *s, SHORT n, SHORT f)
{
        if (f)
                SetBit(s, n);
        else
                ResetBit(s, n);

Quote:
}

void ZeroBits(unsigned char *s, SHORT n, SHORT l);

inline void ZeroField(void *s, SHORT n)
{
        memset(s, 0, n);

Quote:
}

=: This is something dealing with the same structure above :=
class MCONF_DAT {
private:
        DATAFILE <MCONFDATA> Mconf_Dat;
public:
        void Open(void);
        void Close(void) { Mconf_Dat.Close(); }
        void Read(SHORT n) { Mconf_Dat.Read(n); }
        void Write(SHORT n) {
                ZeroBits(Mconf_Dat.Data.BitFields, MCLastBit,
                        sizeof(Mconf_Dat.Data.BitFields));
                ZeroField(Mconf_Dat.Data.ReservedDataArea,
                        sizeof(Mconf_Dat.Data.ReservedDataArea));
                Mconf_Dat.Write(n); }
        LONG Length(void) { return Mconf_Dat.Length(); }
        void OpenRead(SHORT n) { Open(); Read(n); }
        void WriteClose(SHORT n) { Write(n); Close(); }
        void OpenReadClose(SHORT n) { OpenRead(n); Close(); }
        void OpenWriteClose(SHORT n) { Open(); WriteClose(n); }
        void ClearIt(void) { Mconf_Dat.ClearIt(); }
        char *Name(void) { return Mconf_Dat.Data.Name; }
        void Name(char *s) { strcpy(Mconf_Dat.Data.Name, s); }
        SHORT ReadSecurityLevel(void) { return
Mconf_Dat.Data.ReadSecurityLevel; }
        void ReadSecurityLevel(SHORT n) { Mconf_Dat.Data.ReadSecurityLevel =
n; }
        SHORT PostSecurityLevel(void) { return
Mconf_Dat.Data.PostSecurityLevel; }
        void PostSecurityLevel(SHORT n) { Mconf_Dat.Data.PostSecurityLevel =
n; }
        SHORT TriPackNumberOfDaysToSave(void) {
                return Mconf_Dat.Data.TriPackNumberOfDaysToSave; }
        void TriPackNumberOfDaysToSave(SHORT n) {
                Mconf_Dat.Data.TriPackNumberOfDaysToSave = n; }
        LONG HighestMessageNumber(void) {
                return Mconf_Dat.Data.HighestMessageNumber; }
        void HighestMessageNumber(LONG n) {
                Mconf_Dat.Data.HighestMessageNumber = n; }
        unsigned char *NetworkPtr(void) { return Mconf_Dat.Data.NetworkFlags;
Quote:
}

        SHORT Network(SHORT nw) {
                return GetBit(Mconf_Dat.Data.NetworkFlags, nw); }
        void Network(SHORT nw, SHORT n) {
                ModifyBit(Mconf_Dat.Data.NetworkFlags, nw, n); }
        SHORT NetworkedConference(void) {
                return GetBit(Mconf_Dat.Data.BitFields, NetworkedConferenceFlag);       }
        void NetworkedConference(SHORT n) {
                ModifyBit(Mconf_Dat.Data.BitFields, NetworkedConferenceFlag, n); }
        SHORT PrivateConference(void) {
                return GetBit(Mconf_Dat.Data.BitFields, PrivateConferenceFlag); }
        void PrivateConference(SHORT n) {
                ModifyBit(Mconf_Dat.Data.BitFields, PrivateConferenceFlag, n); }
        SHORT FidoStyleNetMail(void) {
                return GetBit(Mconf_Dat.Data.BitFields, FidoStyleNetMailFlag);  }
        void FidoStyleNetMail(SHORT n) {
                ModifyBit(Mconf_Dat.Data.BitFields, FidoStyleNetMailFlag, n); }
        SHORT AliasConference(void) {
                return GetBit(Mconf_Dat.Data.BitFields, AliasConferenceFlag);   }
        void AliasConference(SHORT n) {
                ModifyBit(Mconf_Dat.Data.BitFields, AliasConferenceFlag, n); }
        SHORT UserDelete(void) {
                return GetBit(Mconf_Dat.Data.BitFields, UserDeleteFlag);        }
        void UserDelete(SHORT n) {
                ModifyBit(Mconf_Dat.Data.BitFields, UserDeleteFlag, n); }
        SHORT TriPackBackup(void) {
                return GetBit(Mconf_Dat.Data.BitFields, TriPackBackupFlag);     }
        void TriPackBackup(SHORT n) {
                ModifyBit(Mconf_Dat.Data.BitFields, TriPackBackupFlag, n); }
        SHORT DisablePrivateMessages(void) {
                return GetBit(Mconf_Dat.Data.BitFields,
DisablePrivateMessagesFlag);    }
        void DisablePrivateMessages(SHORT n) {
                ModifyBit(Mconf_Dat.Data.BitFields, DisablePrivateMessagesFlag, n);
Quote:
}

        SHORT DisablePublicMessages(void) {
                return GetBit(Mconf_Dat.Data.BitFields,
DisablePublicMessagesFlag);     }
        void DisablePublicMessages(SHORT n) {
                ModifyBit(Mconf_Dat.Data.BitFields, DisablePublicMessagesFlag, n); }

Quote:
};

Anybody have any help?

---
Joshua Ecklund
BBS Development
http://www.*-*-*.com/

---
"The reason we come up with new versions is not
to fix bugs. It's absolutely not. It's the
stupidest reason to buy a new version I ever heard."
  -- Bill Gates, CEO, Microsoft Corporation

BOYCOTT MICROSOFT: http://www.*-*-*.com/
---



Wed, 18 Jun 1902 08:00:00 GMT  
 'Bitfields' in Pascal

Quote:

> I am trying to port some file structures from C to Pascal, but am
> running into some problems with the 'bitfields'.  Here is the C code
> that I need to convert to pascal (Only parts dealing with bitfields):

> enum MCONFDATABITFIELDS {
>         NetworkedConferenceFlag, PrivateConferenceFlag,
>         FidoStyleNetMailFlag, AliasConferenceFlag,
>         UserDeleteFlag, TriPackBackupFlag,
>         DisablePrivateMessagesFlag, DisablePublicMessagesFlag,
>         MCLastBit };

     One way to do this is to use the SET type.  Most Pascal
implementations handle sets as bits, using as few bytes as possible to
hold all of the set members.  I notice the above
"set of bits" has 9 bits, which means it would take 2 bytes.  If you
actually want to represent (hardware) bits, you should do a little
experimentation to see how your implementation handles the bits,
including whether the first bit is "high" or "low".

TYPE
  confdatabits = (NetworkedConferenceBit, PrivateConferenceBit,
        FidoStyleNetMailBit, AliasConferenceBit,
        UserDeleteBit, TriPackBackupBit,
        DisablePrivateMessagesBit, DisablePublicMessagesBit,
        MCLastBit };
  confdatabitfieldtype = SET OF confdatabits;
VAR
  confdatabitfield : confdatabitfieldtype;

Here are ways to do things to this.

Initialize (clear all bits)     :  confdatabitfield := [];
Initialize (specific pattern):  confdatabitfield :=
[FidoStyleNetMailBit, UserDeleteBit];
Set a bit                          :  confdatabitfield :=
confdatabitfield + [UserDeleteBit];
Clear a bit                       :  confdatabitfield :=
confdatabitfield - [UserDeleteBit];
Test a bit                         :  IF UserDeleteBit IN
confdatabitfield THEN ...
Test each bit                    :  FOR bit := NetworkedConferenceBit TO
MCLastBit DO
                                            IF bit IN confdatabitfield
THEN ...

     There are other ways to do this, but I personally like this
method.  Its only drawback is that it is too "transparent", too easy to
see what you are doing, and thus too easy to avoid the obscure bug.  Oh,
well.  (I forget how to make the "Tongue-in-Cheek smiley).

Bob Schor
Pascal Enthusiast



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. type field ''money''

2. IndexDefs.Add('Help','Please',[ixD2WorksGreat, ixD3Errors])

3. 'C' to PASCAL translator

4. Pascal to 'C' FAQ

5. Looking for equivalent to 'friend'(C++) in object pascal

6. TmT Pascal Lite question 'Speed Bumps'

7. Playing CD's or MIDI's in pascal

8. Playing AVI's and WAV's Through Turbo Pascal

9. pascal 'real' data to vb 'double'

10. 'Incompatible types 'string' and 'Tdbedit''

11. Replace 'keystack''

12. Don't trust in 'trunc'

 

 
Powered by phpBB® Forum Software