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