I decide to embark finally on a large project. So I made a group on sourceforge, got CVS up and running, got models, and now started to code. Our code looks like a bunch of modules (both for Linux and for Windows) that need to be able to request files writing.
So I decided not to play around with Inheritance and create a simple class that will grant each module an int value that will represent it's file access handle.
For example, Display_Win will call GetFileAcc(filename), and it will get an int that it will then pass to the class. So, if it gets an error it could just do WriteToMyFile(int thatHandle, char *msg,...).
But I realized that if I want to have scalability in my modules, I would need to have more than just 5 file handles. And I knew that a simple array would be too wasteful, so I embarked on making a linked list. After all, a linked list is perfect... I would always have only 5-15 file handles in a list, and I would need to remove file handles from the list handle quite often.
But I am not quite sure I got it right. This crashes often for no reason at all :(. I think I made a mistake in actually creating the linked list, and not the file access. Can somebody please tell me what I did wrong?
/*
writeLog.h
HEADER
*/
#include <cstdio>
#include <cstdarg>
#include <cstdlib>
class writeLog{
public:
writeLog(); // Will initialize the begin node.
~writeLog();// Will free all memory
int requestNode(char* filename); // will make space and return a unique ID
void killNode(int ID); //Will free that node.
void record(int ID,char *msg,...); // Actual record thingy.
private:
struct fileNode{
int ID; // Sigh, lets hope 2million should be enough.
FILE *file; // IDs WILL _NOT_ BE IN ORDER!!!!!!!!
fileNode *Next;
}*begin;
int cID; // CURRENT ID number.
fileNode* MakeNode(int ID,char *filename);
fileNode* NextNode(fileNode *Node); //return Node->Next
fileNode* GetNode(int ID); //searches through array looking for Node->ID
fileNode* GetLastNode(); //looks for last array piece
fileNode* PrevNode(fileNode *Node); // gets Node-1
void flushAll();
};
#include "writeLog.h"
#define null NULL
writeLog::writeLog()
{ cID=0;
begin=(fileNode*) malloc( sizeof(fileNode));
begin->ID=0; // ONLY 0
begin->file=NULL;
}
int writeLog::requestNode(char *filename)
{
cID++;
fileNode* Node=MakeNode(cID,filename);
fileNode* temp=GetLastNode();
temp->Next=Node;
return cID;
}
writeLog::fileNode* writeLog::MakeNode(int ID,char* filename)
{
fileNode* Node = (fileNode*) malloc( sizeof(fileNode));
Node->ID=ID;
Node->file=fopen(filename,"w");
Node->Next=NULL;
return Node;
}
void writeLog::record(int ID, char *msg,...)
{
fileNode* Node = GetNode(ID);
va_list args;
va_start (args, msg);
vfprintf (Node->file, msg, args);
va_end (args);
fflush(Node->file);
}
writeLog::fileNode* writeLog::GetLastNode()
{
fileNode* Node = begin;
for(;Node->Next!=NULL;Node=NextNode(Node) ); // Woosh. It works like a charm
return Node; // ...I think.
}
writeLog::fileNode* writeLog::NextNode(fileNode *Node){
return Node->Next;}
writeLog::fileNode* writeLog::GetNode(int ID)
{ fileNode* temp;
for(;temp->ID!=ID;temp==NextNode(temp));
return temp;
}
void writeLog::killNode(int ID) // let the nasty stuff start
{
fileNode* temp = GetNode(ID);
fileNode* prev = PrevNode(temp);
fileNode* next = temp->Next;
fclose(temp->file);
free(temp);
prev->Next=next;
next->Next=NULL;
}
writeLog::fileNode* writeLog::PrevNode(fileNode* Node)
{
fileNode* prevNode=begin;
fileNode* temp = begin;
while(1)
{
if(temp==Node)
break;
prevNode=temp;
temp=NextNode(temp);
}
return prevNode;
}
writeLog::~writeLog()
{
while(1)
{
fileNode* Node = GetLastNode();
if(Node==NULL)
break;
fileNode* prev = PrevNode(Node);
free(Node);
prev->Next=NULL;
}
}