]> git.donarmstrong.com Git - biopieces.git/blob - code_c/Maasha/src/inc/common.h
41096bcbd73e66aa58de4b580f9159770a904caa
[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 /* Zero and then free memory from a given pointer. */
86 void  mem_free( void *pt );
87
88 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ARRAYS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
89
90
91 /* Binary search an array of integers for an integer value. */
92 bool binary_search_array( int *array, int array_size, int val );
93
94
95 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> MISC <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
96
97
98 /* Remove the last char from a string. */
99 void  chop( char *string );
100
101 /* Remove the last char from a string if the char is a newline (safer than chop). */
102 void  chomp( char *string );
103
104 /* Split a given line and a delimiter return the split result as a list. */
105 void  split( char *string, char delimit, struct list **fields );
106     
107 /* Mockup version of Perl substr. */
108 char *substr( char *string, int offset, int len );
109
110 /* Return a binary number as a string of 1's and 0's. */
111 char *bits2string( uint bin );
112
113
114 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
115
116