tree_x.c (2059B)
1 // tree replay step routine 2 // input string is output from tree.c 3 4 #include "../lash_tree.h" 5 #include "../lash_tree_dump.h" 6 #include <regex.h> 7 #include <stdlib.h> 8 #include <stdio.h> 9 #include <string.h> 10 11 12 int main() { 13 14 int count = 0; 15 char *input = "(i1,h6)37 (i2,h8)46 (i3,h2)40 (i4,h3)61 (i5,h9)46 (i6,h1)92 (i7,h5)86 (i8,h0)95 (i9,h4)83 \0"; 16 char *cursor = input; 17 //strcat(cursor, "\0"); 18 long int *tmpheap = (long int*)malloc(sizeof(long int) * 255); 19 unsigned int *tmpidx = (unsigned int*)malloc(sizeof(unsigned int) * 255); 20 lash_tree_t *tree; 21 tree = lash_treeInit(tree, 255); 22 if (tree == NULL) 23 return 1; 24 lash_treeDumpInit(1); 25 lash_treeDumpAdd(tree, "heap"); 26 27 int rc = 100; 28 regex_t rx; 29 int re = 0; 30 31 regmatch_t rp[rc]; 32 char *rb = (char*)malloc(255); 33 //re = regcomp(&rx, "/\(i([:digit:]+),h([:digit:]+))[:digit:]+ "); 34 re = regcomp(&rx, "i([0-9]+),h([0-9]+))([0-9]+) ", REG_EXTENDED); 35 36 while (re != REG_NOMATCH) { 37 unsigned int result_h, result_i = 0; 38 long int result_r = 0; 39 char *tmpstring = (char*)malloc(16); 40 re = regexec(&rx, cursor, rc, rp, 0); 41 regerror (re, &rx, rb, 255); 42 if (re == REG_NOMATCH) 43 continue; 44 strncpy(tmpstring, cursor + rp[2].rm_so, rp[2].rm_eo - rp[2].rm_so); 45 *(tmpstring + rp[2].rm_eo - rp[2].rm_so) = 0; 46 result_h = (unsigned int)atoi(tmpstring); 47 strncpy(tmpstring, cursor + rp[1].rm_so, rp[1].rm_eo - rp[1].rm_so); 48 *(tmpstring + rp[1].rm_eo - rp[1].rm_so) = 0; 49 result_i = (unsigned int)atoi(tmpstring); 50 strncpy(tmpstring, cursor + rp[3].rm_so, rp[3].rm_eo - rp[3].rm_so); 51 *(tmpstring + rp[3].rm_eo - rp[3].rm_so) = 0; 52 result_r = (long int)atoi(tmpstring); 53 54 *(tmpidx + result_i - 1) = result_h; 55 *(tmpheap + result_h) = result_r; 56 cursor += rp[0].rm_eo; 57 count++; 58 printf("%d: %i %i (n%li), cursor is now '%s'\n", count, (int)rp[0].rm_so, (int)rp[0].rm_eo, result_r, cursor); 59 } 60 61 tree->idx = tmpidx; 62 tree->heap = tmpheap; 63 tree->position = count; 64 65 long int n = -1; 66 unsigned int l = 0; 67 lash_treeRemove(tree, 0, &n, &l); 68 lash_treeDump(NULL, NULL); 69 return 0; 70 }