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