Nilorea Library

- Main Page
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Macros
n_misc.h
Go to the documentation of this file.
1 /*\file n_misc.h
2 *
3 *\author Mickael Castagnier
4 *
5 *\date 2014-04-04
6 */
7
8
9 #ifndef __MISC_HEADER_GUARD__
10
11 #define __MISC_HEADER_GUARD__
12
13 #ifdef __cplusplus
14 extern "C"
15 {
16 #endif
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <strings.h>
21 #include <sysexits.h>
22
23 #include "n_log.h"
24
26 #ifndef TRUE
27 #define TRUE 1
28 #endif
29
31 #ifndef FALSE
32 #define FALSE 0
33 #endif
34
35
36
40 #define Malloc(__ptr,__struct,__size) \
41 do \
42 { \
43 if ( !( __ptr = ( __struct *)calloc( __size , sizeof( __struct ) ) ) ) \
44 { \
45 log( ERROR , "( %s *)malloc( %s * sizeof( %d ) ) Error !", #__ptr , #__struct , __size ); \
46 exit( -1 ); \
47 } \
48 } while( 0 )
49
50
51
55 #define Malloc_s(__ptr,__struct,__size) \
56 do \
57 { \
58 if ( !( __ptr = ( __struct *)calloc( __size , sizeof( __struct ) ) ) ) \
59 { \
60 log( ERROR , "( %s *)malloc( %s * sizeof( %d ) ) Error !", #__ptr , #__struct , __size ); \
61 __ptr = NULL ; \
62 exit( EX_OSERR ); \
63 } \
64 } while( 0 )
65
66
67
71 #define Free( __ptr )\
72 do \
73 { \
74 if ( __ptr )\
75 {\
76 free( __ptr );\
77 __ptr = NULL;\
78 }\
79 else\
80 {\
81 log( DEBUG ,"Free( %s ) already done or NULL", #__ptr );\
82 } \
83 }while( 0 )
84
85
89 #define FreeNoLog( __ptr )\
90 do \
91 { \
92 if ( __ptr )\
93 {\
94 free( __ptr );\
95 __ptr = NULL;\
96 }\
97 }while( 0 )
98
99 /*
100 * program usage
101 */
102 void usage( char *name );
103
104
105 /*
106 * split helper
107 */
108 char** split(char* chaine,const char* delim,int vide);
109
110
111 /*
112 * free split results
113 */
114 int free_split_result( char ***tab );
115
116 static char *local_strdup( char *src )
117 {
118 char *str = NULL ;
119
120 if( !src )
121 {
122 log( ERROR , "attempting to duplicate a NULL string" );
123 return NULL ;
124 }
125
126 int len = strlen( src );
127
128 Malloc( str , char , len + 5 );
129
130 if( !str )
131 {
132 log( ERROR , "Couldn't allocate %d byte for duplicating \"%s\"" , src );
133 return NULL ;
134 }
135 for( int it = 0 ; it < len ; it ++ )
136 str[ it ] = src[ it ] ;
137
138 return str ;
139 }
140
141 #ifdef __cplusplus
142 }
143 #endif
144
145
146 #endif