+ // There must be a more elegant way of moving the cursor at a
+ // location met during display
+
+ int cursor_x = 0;
+
+ if(title) {
+ addstr(title);
+ addstr(" ");
+ cursor_x += strlen(title) + 1;
+ }
+
+ sprintf(buffer, "%d/%d ", nb_printed_lines, nb_lines);
+ addstr(buffer);
+ cursor_x += strlen(buffer);
+
+ addnstr(pattern, cursor_position);
+ cursor_x += cursor_position;
+
+ if(pattern[cursor_position]) {
+ addstr(pattern + cursor_position);
+ } else {
+ addstr(" ");
+ }
+
+ if(use_regexp || case_sensitive) {
+ addstr(" [");
+ if(use_regexp) {
+ addstr("regexp");
+ }
+
+ if(case_sensitive) {
+ if(use_regexp) {
+ addstr(",");
+ }
+ addstr("case");
+ }
+ addstr("]");
+ }
+
+ move(0, cursor_x);
+
+ attroff(attr_modeline);
+
+ // We are done
+
+ refresh();
+ free_matcher(&matcher);
+}
+
+//////////////////////////////////////////////////////////////////////
+
+void read_file(const char *input_filename,
+ int nb_lines_max, int *nb_lines, char **lines,
+ int hash_table_size, int *hash_table) {
+
+ char raw_line[buffer_size];
+
+ FILE *file = fopen(input_filename, "r");
+
+ if(!file) {
+ fprintf(stderr, "Can not open `%s'.\n", input_filename);
+ exit(1);
+ }
+
+ int start = 0, end = 0;
+
+ while(*nb_lines < nb_lines_max && (end > start || !feof(file))) {
+ int eol = start;
+ while(eol < end && raw_line[eol] != '\n') eol++;
+
+ if(eol == end) {
+ for(int k = 0; k < end - start; k++) {
+ raw_line[k] = raw_line[k + start];
+ }
+ end -= start;
+ eol -= start;
+ start = 0;
+ end += fread(raw_line + end, sizeof(char), buffer_size - end, file);
+ while(eol < end && raw_line[eol] != '\n') eol++;
+ }
+
+ if(eol == buffer_size) {
+ raw_line[buffer_size - 1] = '\0';
+ fprintf(stderr, "Line too long:\n");
+ fprintf(stderr, raw_line);
+ fprintf(stderr, "\n");
+ exit(1);
+ }
+
+ raw_line[eol] = '\0';
+
+ char *t = raw_line + start;
+
+ // Remove the zsh history prefix
+
+ if(zsh_history && *t == ':') {
+ while(*t && *t != ';') t++;
+ if(*t == ';') t++;
+ }
+
+ // Remove the bash history prefix
+
+ if(bash_history) {
+ while(*t == ' ') t++;
+ while(*t >= '0' && *t <= '9') t++;
+ while(*t == ' ') t++;
+ }
+
+ // Check for duplicates with the hash table and insert the line
+ // in the list if necessary
+
+ int dup;
+
+ if(hash_table) {
+ dup = test_and_add(t, *nb_lines, lines, hash_table, hash_table_size);
+ } else {
+ dup = -1;
+ }
+
+ if(dup < 0) {
+ lines[*nb_lines] = new char[strlen(t) + 1];
+ strcpy(lines[*nb_lines], t);
+ } else {
+ // The string was already in there, so we do not allocate a
+ // new string but use the pointer to the first occurence of it
+ lines[*nb_lines] = lines[dup];
+ lines[dup] = 0;
+ }
+
+ (*nb_lines)++;
+
+ start = eol + 1;
+ }