9 * Fill the buffer apropriately with the lines and headers
12 fill_buffer(char *separator
)
14 /* Fill buffer with string */
15 char s
[MAX_LINE_SIZE
];
17 Buffer
*buffer
= malloc(sizeof(Buffer
));
18 FILE *fp
= fopen("complete.c", "r");
20 while (fgets(s
, MAX_LINE_SIZE
, fp
)) {
21 line
= malloc(sizeof(Line
));
22 line
= parse_line(s
, separator
);
29 * Parse the line content to determine if it is a header and identify the
33 parse_line(char *s
, char *separator
)
35 Line
*line
= malloc(sizeof(Line
));
36 char *sep
= strstr(s
, separator
);
37 int pos
= (sep
== NULL
? strlen(s
) - 1 : sep
- s
);
39 /* Strip trailing newline */
40 s
[strlen(s
) - 1] = '\0';
42 /* Fill line->content */
43 line
->content
= malloc((pos
+ 1) * sizeof(char));
44 strncpy(line
->content
, s
, pos
);
46 /* Fill line->comment */
48 line
->comment
= malloc((strlen(s
) - pos
) * sizeof(char));
49 strcpy(line
->comment
, s
+ pos
);
59 * Add a line to the end of the current buffer.
61 * This requires to create a new line with a link to the previous line
62 * and to NULL as the next line.
64 * The previous line's 'next' should be relinked to this new line.
66 * The header's last line have to point to this last line
69 add_line(Buffer
*buffer
, Line
*line
, Line
*previous
, int number
)
79 * Return an array of lines that match.
87 * Print a line to stderr.
90 print_line(Line
*line
)
95 * Print a header title.
103 * Print all the lines of a header.
111 * Listen for the user input and call the appropriate functions.
119 * Print the prompt, before the input, with the number of candidates that
128 * Print the user input to the screen, at the top of the candidates list
136 main(int argc
, char *argv
[])
139 Buffer
*buffer
= NULL
;
140 char *separator
= "* ";
142 /* Command line arguments */
143 for (i
= 0; i
<= argc
; i
++)
146 /* Command line arguments */
147 buffer
= fill_buffer(separator
);