/** 0:server * 1:local */ char *change_same_name(char *localpath, int index, int flag) { printf("###################change same name...##################\n"); int i = 1; char *filename = NULL; char *new_path = NULL; char *path = NULL; char temp[256] = {0}; char suffix[6] = {0}; char *p = localpath; p = p + strlen(localpath); while(p[0] != '/' && strlen(p) < strlen(localpath)) p--; filename = my_malloc(strlen(p) + 1); sprintf(filename, "%s", p + 1); path = my_malloc(strlen(localpath) - strlen(p) + 1); snprintf(path, strlen(localpath) - strlen(p) + 1, "%s", localpath); //printf("%s, %s\n", path, filename); int exit = 1; while(exit) { int n = i; int j = 0; while((n = (n / 10))) { j++; } memset(temp, '\0', sizeof(temp)); snprintf(temp, 252 - j, "%s", filename); sprintf(suffix, "(%d)", i); char *new_name = insert_suffix(temp, suffix); //printf("new_name = %s\n", new_name); i++; new_path = my_malloc(strlen(path) + strlen(new_name) + 2); sprintf(new_path, "%s/%s", path, new_name); //printf("new_path = %s\n", new_path); if(flag == 0) exit = is_server_exist(new_path, index); else if(flag == 1){ if(access(new_path, F_OK) != 0) exit = 0; } free(new_name); } free(path); free(filename); printf("new_path = %s\n", new_path); return new_path; }
/* **************************************************************** routine for deep-sorting the suffixes a[0] ... a[n-1] knowing that they have a common prefix of length "depth" **************************************************************** */ void blind_ssort(Int32 *a, Int32 n, Int32 depth) { int neg_integer_cmp(const void *, const void *); node *find_companion(node *head, UChar *s); void insert_suffix(node *h, Int32 suf, int n, UChar mmchar); void traverse_trie(node *h); Int32 compare_suffixes(Int32 suf1, Int32 suf2, Int32 depth); void free_node_mem(); Int32 i,j,aj,lcp; node nh, *root, *h; // ---- sort suffixes in order of increasing length qsort(a,n, sizeof(Int32), neg_integer_cmp); // --- skip suffixes which have already reached the end-of-text for(j=0;j<n;j++) if(a[j]+depth < Text_size) break; if(j>=n-1) return; // everything is already sorted! // ------ init stack ------- Stack = (node **) malloc(n*sizeof(node *)); if(Stack==NULL) { fprintf(stderr,"Out of memory! (blind_ssort)\n"); exit(1); } // ------- init root with the first unsorted suffix nh.skip = -1; nh.right = NULL; nh.down = (void *) a[j]; root = &nh; // ------- insert suffixes a[j+1] ... a[n-1] for(i=j+1;i<n;i++) { h=find_companion(root, Text+a[i]); assert(h->skip==-1); assert(Stack_size<=i-j); aj=(Int32) h->down; assert(aj>a[i]); lcp = compare_suffixes(aj,a[i],depth); insert_suffix(root, a[i], lcp, Text[aj+lcp]); } // ---- traverse the trie and get suffixes in lexicographic order Aux=a; Aux_written = j; traverse_trie(root); assert(Aux_written==n); free_node_mem(); free(Stack); }
void blind_ssort(UInt32 *a, Int32 n, Int32 depth) { Int32 i,j,aj,lcp; node nh, *root, *h; /* ---- sort suffixes in order of increasing length */ qsort(a,n, sizeof(Int32), neg_integer_cmp); /* --- skip suffixes which have already reached the end-of-text */ for(j=0;j<n;j++) if(a[j]+(UInt32)depth < (UInt32)Text_size) break; if(j>=n-1) return; /* everything is already sorted! */ /* ------ init stack ------- */ Stack = (node **) malloc(n*sizeof(node *)); if(Stack==NULL) { fprintf(stderr,"Out of memory! (blind_ssort)\n"); exit(1); } /* ------- init root with the first unsorted suffix */ nh.skip = -1; nh.right = NULL; nh.down = (node *) a[j]; root = &nh; /* ------- insert suffixes a[j+1] ... a[n-1] */ for(i=j+1;i<n;i++) { h=find_companion(root, Text+a[i]); assert(h->skip==-1); assert(Stack_size<=i-j); aj=(Int32) h->down; assert((UInt32)aj>a[i]); lcp = compare_suffixes(aj,a[i],depth); insert_suffix(root, a[i], lcp, Text[aj+lcp]); } /* ---- traverse the trie and get suffixes in lexicographic order */ Aux=a; Aux_written = j; traverse_trie(root); assert(Aux_written==(UInt32)n); free_node_mem(); free(Stack); }
void build_tree (char *s, char *alphabet) // Build a suffix tree for the given string over the given alphabet // using McCreight's Suffix Link algorithm. { int index = 1; struct node *subtree = root, *lastleaf; idCnt = 1; if (s) { init_root (slen); if (slen > 0) { lastleaf = root -> leftchild; // Iterate over the input string s inserting each suffix into // the tree pointed to by root. while (s[index]) { root = insert_suffix (index++, root, &lastleaf); } } } }