Added a few to the readme
[iomenu.git] / complete.c
blob5b6e9a0b67b7ebb34306f5a9faead3f435dfdc74
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 Line *last = malloc(sizeof(Line));
18 Buffer *buffer = malloc(sizeof(Buffer));
19 FILE *fp = fopen("complete.c", "r");
20 int number = 0;
22 /* Read the file into a doubly linked list of lines */
23 while (fgets(s, MAX_LINE_SIZE, fp)) {
24 number++;
26 line = malloc(sizeof(Line));
27 line = parse_line(s, separator);
29 printf("%s\n", line->content);
31 if (number == 1) {
32 line->prev = NULL;
33 buffer->first = line;
34 } else {
35 last->next = line;
36 line->prev = last;
39 last = line;
42 line->next = NULL;
43 buffer->total = number;
44 buffer->count = number;
45 buffer->current = buffer->first;
46 buffer->last = line;
48 free(last);
50 return buffer;
54 * Parse the line content to determine if it is a header and identify the
55 * separator if any.
57 Line *
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 */
72 if (sep != NULL) {
73 line->comment = malloc((strlen(s) - pos) * sizeof(char));
74 strcpy(line->comment, s + pos);
75 } else {
76 line->comment = "";
79 return line;
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
92 void
93 add_line(Buffer *buffer, Line *line, Line *previous, int number)
97 void
98 match_input()
103 * Return an array of lines that match.
105 Line * *
106 filter_lines()
111 * Print a line to stderr.
113 void
114 print_line(Line *line)
119 * Print a header title.
121 void
122 print_header()
127 * Print all the lines of a header.
129 void
130 print_lines()
135 * Listen for the user input and call the appropriate functions.
137 void
138 get_input()
143 * Print the prompt, before the input, with the number of candidates that
144 * match.
146 void
147 print_prompt()
152 * Print the user input to the screen, at the top of the candidates list
154 void
155 print_input()
160 main(int argc, char *argv[])
162 int i;
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);
177 return 0;