Could split string according to a separator.
[iomenu.git] / complete.c
blob2e611604cba5a834b71aeda9e470e70ccf2a2160
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
5 #include "complete.h"
6 #include "config.h"
8 /*
9 * Fill the buffer apropriately with the lines and headers
11 Buffer *
12 fill_buffer(char *separator)
14 /* Fill buffer with string */
15 char s[MAX_LINE_SIZE];
16 Line *line = NULL;
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);
25 return buffer;
29 * Parse the line content to determine if it is a header and identify the
30 * separator if any.
32 Line *
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 */
47 if (sep != NULL) {
48 line->comment = malloc((strlen(s) - pos) * sizeof(char));
49 strcpy(line->comment, s + pos);
50 } else {
51 line->comment = "";
52 printf("\n");
55 return line;
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
68 void
69 add_line(Buffer *buffer, Line *line, Line *previous, int number)
73 void
74 match_input()
79 * Return an array of lines that match.
81 Line * *
82 filter_lines()
87 * Print a line to stderr.
89 void
90 print_line(Line *line)
95 * Print a header title.
97 void
98 print_header()
103 * Print all the lines of a header.
105 void
106 print_lines()
111 * Listen for the user input and call the appropriate functions.
113 void
114 get_input()
119 * Print the prompt, before the input, with the number of candidates that
120 * match.
122 void
123 print_prompt()
128 * Print the user input to the screen, at the top of the candidates list
130 void
131 print_input()
136 main(int argc, char *argv[])
138 int i;
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);
149 return 0;