These are some macro definitions that can be useful for the c programmer.
My biggest headache on c has always been the error display: sigsegv segmentation fault.
/*
* Definitions
*/
#define TRUE 1
#define FALSE 0
#define OK 0
#define ERROR 1
/**
* Return the minimum value
*/
#define min(a,b) ((a)<(b)?a:b)
/**
* Return the maximum value
*/
#define max(a,b) ((a)>(b)?a:b)
/**
* Template to allocate memory with error display before terminating
*/
#define malloc2(Result,DataType,Count,FunctionName,VariableName) \
if(!(Result=(DataType *)malloc(sizeof(DataType)*(Count)))){\
printf("%s: memory error allocating %s\n",FunctionName,VariableName);\
exit(1);\
}
/**
* Template to allocate memory and set to 0 with error display before terminating
*/
#define calloc2(Result,DataType,Count,FunctionName,VariableName) \
if(!(Result=(DataType *)calloc((Count),sizeof(DataType)))){\
printf("%s: memory error allocating %s\n",FunctionName,VariableName);\
exit(1);\
}
/**
* Template to reallocate memory with error display before terminating
*/
#define realloc2(Result,DataType,Count,FunctionName,VariableName) \
if(!(Result=(DataType *)realloc(Result,sizeof(DataType)*(Count)))){\
printf("%s: memory error reallocating %s\n",FunctionName,VariableName);\
exit(1);\
}
/**
* Template to free the allocated memory
*/
#define free2(This) \
if(This){\
free(This);\
(This)=NULL;\
}\
/**
* Template to open a file for Reading, with error display before terminating
*/
#define fopen2r(File,Path,FunctionName) \
if(!(File=fopen(Path,"r"))){\
printf("%s: error while opening %s for reading\n",FunctionName,Path);\
exit(1);\
}
/**
* Template to open a file for Writing, with error display before terminating
*/
#define fopen2w(File,Path,FunctionName) \
if(!(File=fopen(Path,"w"))){\
printf("%s: error while opening %s for writing\n",FunctionName,Path);\
exit(1);\
}
/**
* Template to open a file for Writing, with error display before terminating
*/
#define fopen2a(File,Path,FunctionName) \
if(!(File=fopen(Path,"a"))){\
printf("%s: error while opening %s for appending\n",FunctionName,Path);\
exit(1);\
}
/**
* Template to open a file for Writing, with error display before terminating
*/
#define fopen2custom(File,Path,Argument,FunctionName) \
if(!(File=fopen(Path,Argument))){\
printf("%s: error while opening %s with the argument %s\n",FunctionName,Path,Argument);\
exit(1);\
}
Sample:
The malloc2 macro is use as:
int *this;
malloc2(this,int,30,"Chronometer_New","this");
Instead of the malloc fuction:
int *this=(int *)malloc(sizeof(int)*(30)));
and a nice error display will be shown if something fail, showing the function name and the variable name.
I hope this can be helpful for someone.
Thursday, February 26, 2009
Howto add a dll app.config file in setup project in Visual Studio
Steps to ship the config file with the dll in a setup project.
In my particular case I needed the dll config file for a WCF service client inside a MMC snapin.
For the library:
Hope can help someone with this.
Works for C# or VB.NET.
In my particular case I needed the dll config file for a WCF service client inside a MMC snapin.
For the library:
- Create a dll project. (e.g. myLibrary)
- Add a configuration file.
- Rename the configuration file as "projectName".dll.config. (e.g. myLibrary.config.dll)
- View the properties for the configuration file and set them to:
- Build action: Content (to reference later in setup)
- Copy to output: Copy always
- Create a setup project or a merge module.
- Select the Add the project output and then:
- Select primary output for the library project. (this includes the dll)
- Select project content for the library. (this includes the configuration file)
Hope can help someone with this.
Works for C# or VB.NET.
Subscribe to:
Posts (Atom)