Mercurial > algorithms
changeset 12:5b67bf33fccc tip
Added comments. Broke out file read toa function slurp_file().
author | Eris Caffee <discordia@eldalin.com> |
---|---|
date | Tue, 18 Aug 2015 14:26:12 -0500 |
parents | b8cfd6af18b3 |
children | |
files | string_search/kmp.c |
diffstat | 1 files changed, 61 insertions(+), 25 deletions(-) [+] |
line diff
1.1 --- a/string_search/kmp.c Tue Aug 18 12:22:22 2015 -0500 1.2 +++ b/string_search/kmp.c Tue Aug 18 14:26:12 2015 -0500 1.3 @@ -17,7 +17,7 @@ 1.4 /* Knuth-Morris-Pratt algorithm 1.5 * 1.6 * As described on Wikipedia 1.7 - * https://duckduckgo.com/l/?kh=-1&uddg=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FKnuth-Morris-Pratt_algorithm 1.8 + * https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm 1.9 */ 1.10 1.11 size_t kmp_search( char *haystack, char *needle ) { 1.12 @@ -94,6 +94,12 @@ 1.13 } 1.14 1.15 /******************************************************************************/ 1.16 +/* Calculate the difference between two struct timeval's. 1.17 + * 1.18 + * OUT: struct timeval *elapsed The calculated difference. 1.19 + * IN: struct timeval *start The starting time; 1.20 + * IN: struct timeval *end The ending time; 1.21 + */ 1.22 1.23 void timevaldiff( struct timeval *elapsed, struct timeval *start, struct timeval *end ) { 1.24 time_t sec; 1.25 @@ -112,6 +118,54 @@ 1.26 } 1.27 1.28 /******************************************************************************/ 1.29 +/* Read the entire contents of a file into memory. 1.30 + * 1.31 + * IN: char *filename The name of the file to read. It will be open in "rb" 1.32 + * mode. 1.33 + * IN: char **buffer The address of a char * which will be set to the start 1.34 + * of the memory buffer containing the contents of the 1.35 + * file. You should call free() on this when you are done. 1.36 + * 1.37 + * Return value: The amount of data read. Note that the data in the 1.38 + * buffer will be null terminated, but this may be 1.39 + * meaningless if the file is a binary file, so you should 1.40 + * use the return value when iterating through the data. 1.41 + */ 1.42 + 1.43 +size_t slurp_file( char *filename, char **buffer ) { 1.44 + 1.45 + FILE * file; 1.46 + char temp[65536]; 1.47 + size_t bytes_read; 1.48 + size_t buffer_top = 0; 1.49 + size_t buffer_max = 1; 1.50 + 1.51 + *buffer = malloc( sizeof( char ) ); 1.52 + 1.53 + file = fopen( filename, "rb" ); 1.54 + while ( bytes_read = fread( temp, sizeof( char ), sizeof( temp ), file ) ) { 1.55 + while ( buffer_max - buffer_top < bytes_read ) { 1.56 + size_t new_buffer_max = 2 * buffer_max; 1.57 + char * new_buffer = malloc( new_buffer_max ); 1.58 + for ( size_t i = 0; i < buffer_max; i++ ) { 1.59 + new_buffer[i] = (*buffer)[i]; 1.60 + } 1.61 + free( *buffer ); 1.62 + *buffer = new_buffer; 1.63 + buffer_max = new_buffer_max; 1.64 + } 1.65 + memcpy( &(*buffer)[ buffer_top ], temp, bytes_read ); 1.66 + buffer_top += bytes_read; 1.67 + } 1.68 + 1.69 + (*buffer)[ buffer_top + 1 ] = 0; 1.70 + 1.71 + fclose( file ); 1.72 + 1.73 + return buffer_top; 1.74 + } 1.75 + 1.76 +/******************************************************************************/ 1.77 1.78 int main( int argc, char **argv ) { 1.79 char *def_needle = "ABCDABD"; 1.80 @@ -121,8 +175,6 @@ 1.81 char * haystack; 1.82 size_t haystack_max; 1.83 size_t haystack_top = 0; 1.84 - haystack = malloc( sizeof( char ) ); 1.85 - haystack_max = 1; 1.86 1.87 if ( argc >= 2 ) { 1.88 needle = argv[1]; 1.89 @@ -131,33 +183,15 @@ 1.90 needle = def_needle; 1.91 } 1.92 1.93 + size_t filesize; 1.94 if ( argc >= 3 ) { 1.95 - FILE * file; 1.96 - char buffer[65536]; 1.97 - size_t bytes_read; 1.98 - 1.99 - file = fopen( argv[2], "r" ); 1.100 - while ( bytes_read = fread( buffer, sizeof( char ), sizeof( buffer ), file ) ) { 1.101 - while ( haystack_max - haystack_top < bytes_read ) { 1.102 - size_t new_haystack_max = 2 * haystack_max; 1.103 - char * new_haystack = malloc( new_haystack_max ); 1.104 - for ( size_t i = 0; i < haystack_max; i++ ) { 1.105 - new_haystack[i] = haystack[i]; 1.106 - } 1.107 - free( haystack ); 1.108 - haystack = new_haystack; 1.109 - haystack_max = new_haystack_max; 1.110 - } 1.111 - memcpy( &haystack[ haystack_top ], buffer, bytes_read ); 1.112 - } 1.113 - 1.114 - fclose( file ); 1.115 + filesize = slurp_file( argv[2], &haystack ); 1.116 } 1.117 else { 1.118 - haystack = def_haystack; 1.119 + haystack = strdup( def_haystack ); 1.120 + filesize = strlen( def_haystack ); 1.121 } 1.122 1.123 - 1.124 struct timeval start; 1.125 struct timeval end; 1.126 struct timeval elapsed; 1.127 @@ -185,5 +219,7 @@ 1.128 printf( "found >%s<\n", found ); 1.129 printf( "brute force found in %d.%06d seconds\n", elapsed.tv_sec, elapsed.tv_usec ); 1.130 1.131 + free(haystack); 1.132 + 1.133 exit( EXIT_SUCCESS ); 1.134 }