/* * Fills a table of paths to characters in the tree. * Takes in a pointer to a huffmantree and a pointer to character pointers. */ void fill(huffmantree* tree, char** array){ int slot_size=((tree->size-1)/2+1)*sizeof(char); //maximum length of a path from root to leaf char* path=malloc(slot_size); path[0]=0; filltable(tree, array, path); free(path); }
/* * Helper method of fill. * It assigns a string that represents the path to the character in the huffmantree * to the corresponding slot in the table, an array of strings. * Takes in a pointer to a huffmantree, a pointer to character pointers, which is the * table, and a character array, which is the collector in the tail recursion. */ void filltable(huffmantree* tree, char** array, char* path){ if (huffmantree_isleaf(tree)){ int c=tree->c; memcpy(array[c],path,strlen(path)+1); } else{ char* left_path=mystrdup(path,strlen(path)+2); char* right_path=mystrdup(path, strlen(path)+2); strcat(left_path,"0"); strcat(right_path, "1"); filltable(tree->left, array, left_path); filltable(tree->right, array, right_path); free(left_path); free(right_path); } }
main() { char c; char d; int sign; int i; char buf[80]; int pos; static char digit[] = "0123456789"; /* Create output files */ if ((cfile = fopen("syntax.c", "w")) == NULL) { perror("syntax.c"); exit(2); } if ((hfile = fopen("syntax.h", "w")) == NULL) { perror("syntax.h"); exit(2); } fputs(writer, hfile); fputs(writer, cfile); /* Determine the characteristics of chars. */ c = -1; if (c < 0) sign = 1; else sign = 0; for (nbits = 1 ; ; nbits++) { d = (1 << nbits) - 1; if (d == c) break; } printf("%s %d bit chars\n", sign? "signed" : "unsigned", nbits); if (nbits > 9) { fputs("Characters can't have more than 9 bits\n", stderr); exit(2); } size = (1 << nbits) + 1; base = 1; if (sign) base += 1 << (nbits - 1); digit_contig = 1; for (i = 0 ; i < 10 ; i++) { if (digit[i] != '0' + i) digit_contig = 0; } /* Generate the #define statements in the header file */ fputs("/* Syntax classes */\n", hfile); for (i = 0 ; synclass[i].name ; i++) { sprintf(buf, "#define %s %d", synclass[i].name, i); fputs(buf, hfile); for (pos = strlen(buf) ; pos < 32 ; pos = pos + 8 &~ 07) putc('\t', hfile); fprintf(hfile, "/* %s */\n", synclass[i].comment); } putc('\n', hfile); fputs("/* Syntax classes for is_ functions */\n", hfile); for (i = 0 ; is_entry[i].name ; i++) { sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i); fputs(buf, hfile); for (pos = strlen(buf) ; pos < 32 ; pos = pos + 8 &~ 07) putc('\t', hfile); fprintf(hfile, "/* %s */\n", is_entry[i].comment); } putc('\n', hfile); fprintf(hfile, "#define SYNBASE %d\n", base); fprintf(hfile, "#define PEOF %d\n\n", -base); putc('\n', hfile); fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile); fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile); fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile); putc('\n', hfile); output_type_macros(); /* is_digit, etc. */ putc('\n', hfile); /* Generate the syntax tables. */ fputs("#include \"shell.h\"\n", cfile); fputs("#include \"syntax.h\"\n\n", cfile); init(); fputs("/* syntax table used when not in quotes */\n", cfile); add("\n", "CNL"); add("\\", "CBACK"); add("'", "CSQUOTE"); add("\"", "CDQUOTE"); add("`", "CBQUOTE"); add("$", "CVAR"); add("}", "CENDVAR"); add("<>();&| \t", "CSPCL"); print("basesyntax"); init(); fputs("\n/* syntax table used when in double quotes */\n", cfile); add("\n", "CNL"); add("\\", "CBACK"); add("\"", "CENDQUOTE"); add("`", "CBQUOTE"); add("$", "CVAR"); add("}", "CENDVAR"); add("!*?[=", "CCTL"); print("dqsyntax"); init(); fputs("\n/* syntax table used when in single quotes */\n", cfile); add("\n", "CNL"); add("'", "CENDQUOTE"); add("!*?[=", "CCTL"); print("sqsyntax"); filltable("0"); fputs("\n/* character classification table */\n", cfile); add("0123456789", "ISDIGIT"); add("abcdefghijklmnopqrstucvwxyz", "ISLOWER"); add("ABCDEFGHIJKLMNOPQRSTUCVWXYZ", "ISUPPER"); add("_", "ISUNDER"); add("#?$!-*@", "ISSPECL"); print("is_type"); if (! digit_contig) digit_convert(); exit(0); }