Click here to Skip to main content
15,915,869 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: C help needed Pin
Neville Franks19-Jun-05 19:19
Neville Franks19-Jun-05 19:19 
GeneralRe: C help needed Pin
Christian Graus19-Jun-05 20:03
protectorChristian Graus19-Jun-05 20:03 
GeneralRe: C help needed Pin
Christian Graus19-Jun-05 20:10
protectorChristian Graus19-Jun-05 20:10 
GeneralRe: C help needed Pin
Jack Squirrel Jr.20-Jun-05 0:12
sussJack Squirrel Jr.20-Jun-05 0:12 
GeneralRe: C help needed Pin
David Crow20-Jun-05 3:12
David Crow20-Jun-05 3:12 
GeneralRe: C help needed Pin
Bob Stanneveld19-Jun-05 20:40
Bob Stanneveld19-Jun-05 20:40 
GeneralRe: C help needed Pin
Tom Archer20-Jun-05 1:27
Tom Archer20-Jun-05 1:27 
GeneralRe: C help needed Pin
John R. Shaw20-Jun-05 6:08
John R. Shaw20-Jun-05 6:08 
Laugh | :laugh: You forgot!

Back when I used to write strictly C code, I would create a general purpose
modeule to hadle things like this.

/* myinclude.h */
#define LISTHANDLE (void*);
LISTHANDLE List_Create(size_t record_size);
size_t List_Size(LISTHANDLE hList);
int List_Append(LISTHANDLE hList, void* pRecord);
int List_ReadNext(LISTHANDLE hList, void* pRecord);
int List_Insert(LISTHANDLE hList, void* pRecord);
int List_Sort(int (*callback)(void*,void*));
/*
 etc...
*/
/* mysource.c */
#ifndef BYTE
#define BYTE unsigned char
#endif
typedef struct st_NodeHeader
{
    struct st_NodeHeader* pNext;
} NODE_HEADER;

typedef struct st_Header
{
    size_t nRecSize;
    NODE_HEADER* pHead;
    NODE_HEADER* pTail;
    NODE_HEADER* pCurrent;
} HEADER;

LISTHANDLE List_Create(size_t record_size)
{
    HEADER* pHeader = (HEADER*)malloc(sizeof(HEADER));
    if( pHeader )
    {
        memset(pHeader,0,sizeof(HEADER);
        pHeader->nRecSize = record_size;
    }
    return(pHeader);
};

int List_Insert(LISTHANDLE hList, void* pRecord)
{
    if( hList && pRecord )
    {
        HEADER* pHeader = (HEADER*)hList;
        size_t nNodeSize = sizeof(NODE_HEADER) + pHeader->nRecSize;
        NODE_HEADER* pNode = (NODE_HEADER*)malloc(nNodeSize);
        if( pNode )
        {
            BYTE* pRec = ((BYTE*)pNode) + sizeof(NODE_HEADER);
            pNode->pNext = (NODE_HEADER*)0;
            memcpy(pRec,pRecord,pHeader->nRecSize);
            /*
            Add node to list like normal
            .......
            */
            return(1);
        }
    }
    return(0);
}

int List_ReadNext(LISTHANDLE hList, void* pRecord)
{
    if( hList && pRecord )
    {
        HEADER* pHeader = (HEADER*)hList;
        if( pHeader->pCurrent )
        {
            BYTE* pRec = ((BYTE*)pNode) + sizeof(NODE_HEADER);
            memcpy(pRecord,pRec,pHeader->nRecSize);
            pHeader->pCurrent = pHeader->pCurrent->pNext;
            return(1);
        }
    }
    return(0);
}


Well you get the idea. Once it is written, you don't have to worry about how
it works anymore. You can expand it to include file I/O for saving to disk,
and you can change how it works interanly, without having to modify the code
that uses it.


INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
GeneralRe: C help needed Pin
Tom Archer20-Jun-05 21:24
Tom Archer20-Jun-05 21:24 
GeneralRe: C help needed Pin
Bob Stanneveld20-Jun-05 20:41
Bob Stanneveld20-Jun-05 20:41 
Generalwindows hicon problem Pin
c. s.19-Jun-05 14:33
c. s.19-Jun-05 14:33 
GeneralRe: windows hicon problem Pin
Rage19-Jun-05 22:02
professionalRage19-Jun-05 22:02 
GeneralRe: windows hicon problem Pin
c. s.21-Jun-05 18:08
c. s.21-Jun-05 18:08 
Questionplease can any one help me ???? Pin
kosamoza19-Jun-05 13:33
kosamoza19-Jun-05 13:33 
AnswerRe: please can any one help me ???? Pin
Christian Graus19-Jun-05 20:11
protectorChristian Graus19-Jun-05 20:11 
GeneralRe: please can any one help me ???? Pin
toxcct19-Jun-05 20:44
toxcct19-Jun-05 20:44 
AnswerRe: please can any one help me ???? Pin
Bob Stanneveld19-Jun-05 20:46
Bob Stanneveld19-Jun-05 20:46 
GeneralRe: please can any one help me ???? Pin
kosamoza20-Jun-05 6:57
kosamoza20-Jun-05 6:57 
GeneralRe: please can any one help me ???? Pin
Bob Stanneveld20-Jun-05 20:36
Bob Stanneveld20-Jun-05 20:36 
GeneralRe: please can any one help me ???? Pin
Anonymous21-Jun-05 4:42
Anonymous21-Jun-05 4:42 
GeneralRe: please can any one help me ???? Pin
Bob Stanneveld21-Jun-05 20:34
Bob Stanneveld21-Jun-05 20:34 
AnswerRe: please can any one help me ???? Pin
FlyingTinman20-Jun-05 10:25
FlyingTinman20-Jun-05 10:25 
GeneralRe: please can any one help me ???? Pin
kosamoza20-Jun-05 10:47
kosamoza20-Jun-05 10:47 
GeneralRe: please can any one help me ???? Pin
FlyingTinman20-Jun-05 12:08
FlyingTinman20-Jun-05 12:08 
GeneralRe: please can any one help me ???? Pin
kosamoza20-Jun-05 13:57
kosamoza20-Jun-05 13:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.