in source/huffman_generator/generator.c [43:106]
int read_code_points(const char *input_path) {
memset(code_points, 0, sizeof(code_points));
FILE *file = aws_fopen(input_path, "r");
if (!file) {
printf("Failed to open file '%s' for read.", input_path);
return 1;
}
static const char HC_KEYWORD[] = "HUFFMAN_CODE";
static const size_t HC_KW_LEN = sizeof(HC_KEYWORD) - 1;
int is_comment = 0;
char line[120];
while (fgets(line, sizeof(line), file) != NULL) {
const size_t line_length = strlen(line);
if (line[0] == '#') {
/* Ignore preprocessor directives */
continue;
}
for (size_t i = 0; i < line_length - 1; ++i) {
if (!is_comment) {
if (line[i] == '/' && line[i + 1] == '*') {
is_comment = 1;
} else if (strncmp(&line[i], HC_KEYWORD, HC_KW_LEN) == 0) {
/* Found code, parse it */
/* Skip macro */
const char *current_char = &line[i + HC_KW_LEN];
current_char += skip_whitespace(current_char);
/* Skip ( */
assert(*current_char == '(');
++current_char;
/* Parse symbol */
uint8_t symbol = (uint8_t)atoi(current_char);
struct huffman_code_point *code_point = &code_points[symbol];
assert(!code_point->symbol && "Symbol already found!");
code_point->symbol = symbol;
current_char += read_past_comma(current_char);
/* Skip the binary string form */
current_char += read_past_comma(current_char);
/* Parse bits */
code_point->code.bits = (uint32_t)strtol(current_char, NULL, 16);
current_char += read_past_comma(current_char);
code_point->code.num_bits = (uint8_t)atoi(current_char);
}
} else if (line[i] == '*' && line[i + 1] == '/') {
is_comment = 0;
}
}
}
fclose(file);
return 0;
}