]> git.donarmstrong.com Git - biopieces.git/blob - code_c/Maasha/src/inc/common.h
fcc01bf2b0f557530505928f810f03074c0672e0
[biopieces.git] / code_c / Maasha / src / inc / common.h
1 /* Including standard libraries */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <stdarg.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <ctype.h>
8 #include <math.h>
9
10 /* Define a shorthand for unsigned int */
11 #define uint unsigned int
12
13 /* Define a boolean type */
14 #define bool char
15 #define TRUE 1
16 #define FALSE 0
17
18 /* Macro for resetting a pointer to all \0's. */
19 #define ZERO( pt ) ( memset( pt, '\0', sizeof( *pt ) ) )
20
21 /* Macro for dynamic allocation of memory. */
22 #define MEM_GET( pt ) ( pt = mem_get( sizeof( *pt ) ) ) 
23
24 /* Macro for cloning a structure in memroy. */
25 #define MEM_CLONE( pt ) mem_clone( pt, sizeof( ( pt )[ 0 ] ) )
26
27 /* Macros for determining min or max of two given values. */
28 #define MAX( a, b ) a < b ? b : a
29 #define MIN( a, b ) a > b ? b : a
30
31 /* Macros for abs and int functions. */
32 #define ABS( x ) ( ( x ) < 0 ) ? -( x ) : ( x )
33 #define INT( x ) ( int ) x
34
35
36 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> STRUCTURE DECLARATIONS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
37
38
39 /* Singly linked list with a pointer to the next element and a pointer to a value. */
40 struct list
41 {
42     struct list *next;
43     void        *val;
44 };
45
46 /* Singly linked list with a pointer to the next element and an integer value. */
47 struct list_int
48 {
49     struct list *next;
50     int          val;
51 };
52
53
54 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ERROR HANDLING <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
55
56
57 /* Print error message to stderr and exit. */
58 void  die( char *error_msg );
59
60 /* Print warning message to stderr. */
61 void warn( char *warn_msg );
62
63
64 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> MEMORY HANDLING <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
65
66
67 /* Get a pointer with a given size of allocated memory. */
68 void         *mem_get( size_t size );
69
70 /* Get a pointer with a given size of allocated and zero'ed memory. */
71 void    *mem_get_zero( size_t size );
72
73 /* Resize allocated memory for a given pointer. */
74 void      *mem_resize( void* pt, size_t size );
75
76 /* Resize allocated memory for a given pointer with extra memory zero'ed. */    
77 void *mem_resize_zero( void* pt, size_t old_size, size_t new_size );
78
79 /* Clone a structure in memory and return a pointer to the clone. */
80 void       *mem_clone( void *old_pt, size_t size );
81
82 /* Free memory from a given pointer. */
83 void         mem_free( void *pt );
84
85
86 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ARRAYS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
87
88
89 /* Binary search an array of integers for an integer value. */
90 bool binary_search_array( int *array, int array_size, int val );
91
92
93 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> MISC <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
94
95
96 /* Remove the last char from a string. */
97 void    chop( char *string );
98
99 /* Remove the last char from a string if the char is a newline (safer than chop). */
100 void   chomp( char *string );
101
102 /* Split a given line and a delimiter return the split result as a list. */
103 void   split( char *string, char delimit, struct list **fields );
104     
105 /* Mockup version of Perl substr. */
106 char *substr( char *string, int offset, int len );
107
108 /* Return a binary number as a string of 1's and 0's. */
109 char *bits2string( uint bin );
110
111
112 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
113
114