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 Line
*last
= malloc(sizeof(Line
));
18 Buffer
*buffer
= malloc(sizeof(Buffer
));
19 FILE *fp
= fopen("complete.c", "r");
22 /* Read the file into a doubly linked list of lines */
23 while (fgets(s
, MAX_LINE_SIZE
, fp
)) {
26 line
= malloc(sizeof(Line
));
27 line
= parse_line(s
, separator
);
29 printf("%s\n", line
->content
);
43 buffer
->total
= number
;
44 buffer
->count
= number
;
45 buffer
->current
= buffer
->first
;
54 * Parse the line content to determine if it is a header and identify the
58 parse_line(char *s
, char *separator
)
60 Line
*line
= malloc(sizeof(Line
));
61 char *sep
= strstr(s
, separator
);
62 int pos
= (sep
== NULL
? strlen(s
) - 1 : sep
- s
);
64 /* Strip trailing newline */
65 s
[strlen(s
) - 1] = '\0';
67 /* Fill line->content */
68 line
->content
= malloc((pos
+ 1) * sizeof(char));
69 strncpy(line
->content
, s
, pos
);
71 /* Fill line->comment */
73 line
->comment
= malloc((strlen(s
) - pos
) * sizeof(char));
74 strcpy(line
->comment
, s
+ pos
);
83 * Add a line to the end of the current buffer.
85 * This requires to create a new line with a link to the previous line
86 * and to NULL as the next line.
88 * The previous line's 'next' should be relinked to this new line.
90 * The header's last line have to point to this last line
93 add_line(Buffer
*buffer
, Line
*line
, Line
*previous
, int number
)
103 * Return an array of lines that match.
111 * Print a line to stderr.
114 print_line(Line
*line
)
119 * Print a header title.
127 * Print all the lines of a header.
135 * Listen for the user input and call the appropriate functions.
143 * Print the prompt, before the input, with the number of candidates that
152 * Print the user input to the screen, at the top of the candidates list
160 main(int argc
, char *argv
[])
163 Buffer
*buffer
= NULL
;
164 char *separator
= "* ";
166 /* Command line arguments */
167 for (i
= 0; i
<= argc
; i
++) {
168 if (argv
[i
][1] == '-') {
173 /* Command line arguments */
174 buffer
= fill_buffer(separator
);
175 printf("%s\n", buffer
->first
->next
->next
->prev
->prev
->content
);