This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.c
148 lines (125 loc) · 3.29 KB
/
search.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "cache.h"
#include "config.h"
#include "main.h"
#include "notebook.h"
#include "search.h"
// Strategy for searching:
// Open every post and determine whether or not a certain query
// is found within it. If so, get basic information about it and
// add it to the list.
struct search_result *search(struct notebook notebook, const char query[100], size_t *result_count)
{
if (strcmp(query, "") == 0) {
return NULL;
}
static struct search_result results[5000];
size_t result_index = 0;
char path[512];
FILE *cache_file = fopen(cache_path(notebook.id), "r");
if (cache_file == NULL)
return NULL;
while (fgets(path, 512, cache_file)) {
path[strlen(path) - 1] = '\0'; // remove \n
// skip blank lines and those starting with "#"
if (strcmp(path, "") == 0 || strncmp(path, "#", 1) == 0)
continue;
char filepath[512];
strncpy(filepath, notebook.path, 256);
strncat(filepath, path, 256);
filepath[256] = '\0';
FILE *fp = fopen(filepath, "r");
if (fp == NULL) {
fprintf(stderr, "File not found but is listed in posts.txt: %s\n", filepath);
continue;
}
int c;
size_t count = 1;
while ((c = getc(fp)) != EOF) {
count++;
}
char *contents = see_you_later_allocator(count);
size_t i = 0;
fseek(fp, 0, SEEK_SET);
while ((c = getc(fp)) != EOF && i < count) {
contents[i] = (char)c;
i++;
}
contents[i] = '\0';
if (strstr(contents, query) != NULL) {
struct search_result r;
strncpy(r.path, notebook.path, 256);
strncat(r.path, path, 256);
r.path[256] = '\0';
results[result_index] = r;
result_index++;
}
free(contents);
fclose(fp);
}
*result_count = result_index;
return results;
}
struct search_hashtag *search_list_hashtags(struct notebook notebook, size_t *result_count) {
static struct search_hashtag results[5000];
size_t result_index = 0;
char path[512];
FILE *cache_file = fopen(cache_path(notebook.id), "r");
if (cache_file == NULL)
return NULL;
while (fgets(path, 512, cache_file)) {
path[strlen(path) - 1] = '\0'; // remove \n
// skip blank lines and those starting with "#"
if (strcmp(path, "") == 0 || strncmp(path, "#", 1) == 0)
continue;
char filepath[512];
strncpy(filepath, notebook.path, 256);
strncat(filepath, path, 256);
filepath[256] = '\0';
FILE *fp = fopen(filepath, "r");
if (fp == NULL) {
fprintf(stderr, "File not found but is listed in posts.txt: %s\n", filepath);
continue;
}
bool in_tag = false;
size_t tag_i = 0;
int c = 0;
size_t i = 0;
char tag[512];
while ((c = getc(fp)) != EOF) {
if (in_tag) {
if (c == ' ' || c == '\n' || ispunct(c)) {
in_tag = false;
tag[tag_i] = '\0';
size_t j = 0;
for (; j < result_index; ++j) {
if (strcmp(tag, results[j].tag) == 0) {
break;
}
}
// j will equal result_index if no results were already found
if (j != result_index) {
results[j].occurances += 1;
} else {
strcpy(results[result_index].tag, tag);
results[result_index].occurances += 1;
result_index += 1;
}
tag_i = 0;
} else {
tag[tag_i] = (char)c;
tag_i += 1;
}
} else if (c == '#') {
in_tag = true;
}
i++;
}
fclose(fp);
}
*result_count = result_index;
return results;
}