Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 104 lines (83 sloc) 2.065 kB
e653ec9 @abhinav-upadhyay apropos.c: Wrote a bare bones implementation of apropos.
authored
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "sqlite3.h"
6
7 //#TODO implement a function for removing stopwords
8 //static void remove_stopwords(char *);
9 static int search(const char *);
10 static void usage(void);
11
12 int
13 main(int argc, char *argv[])
14 {
15 char *query = NULL; // the user query
16
17 if (argc < 2)
18 usage();
19
20 query = argv[1];
21 //remove_stopwords(query);
22 if (search(query) < 0)
23 return -1;
24 return 0;
25
26 }
27
28 /*
29 * search --
30 * Opens apropos.db and performs the search for the keywords entered by the user
31 */
32 static int
33 search(const char *query)
34 {
35 sqlite3 *db = NULL;
36 int rc = 0;
37 int idx = -1;
38 char *sqlstr = NULL;
39 char *name = NULL;
40 char *snippet = NULL;
41 sqlite3_stmt *stmt = NULL;
42
43 sqlite3_initialize();
44 rc = sqlite3_open_v2("apropos.db", &db, SQLITE_OPEN_READONLY, NULL);
45 if (rc != SQLITE_OK) {
46 fprintf(stderr, "Database does not exist. Try running makemandb and "
47 "then try again\n");
48 sqlite3_close(db);
49 sqlite3_shutdown();
50 return -1;
51 }
52
53 sqlite3_extended_result_codes(db, 1);
54
55 sqlstr = "select name, snippet(mandb, \"\033[1m\", \"\033[0m\", \"...\" )"
56 "from mandb where mandb match :query limit 10 OFFSET 0";
57
58 rc = sqlite3_prepare_v2(db, sqlstr, -1, &stmt, NULL);
59 if (rc != SQLITE_OK) {
60 fprintf(stderr, "%s\n", sqlite3_errmsg(db));
61 sqlite3_close(db);
62 sqlite3_shutdown();
63 return -1;
64 }
65
66 idx = sqlite3_bind_parameter_index(stmt, ":query");
67 rc = sqlite3_bind_text(stmt, idx, query, -1, NULL);
68 if (rc != SQLITE_OK) {
69 fprintf(stderr, "%s\n", sqlite3_errmsg(db));
70 sqlite3_finalize(stmt);
71 sqlite3_close(db);
72 sqlite3_shutdown();
73 return -1;
74 }
75
76 while (sqlite3_step(stmt) == SQLITE_ROW) {
77 name = (char *) sqlite3_column_text(stmt, 0);
78 snippet = (char *) sqlite3_column_text(stmt, 1);
79 printf("%s\n%s\n\n", name, snippet);
80 }
81
82 sqlite3_finalize(stmt);
83 sqlite3_close(db);
84 sqlite3_shutdown();
85
86 return 0;
87
88 }
89
90
91
92 /*
93 * usage --
94 * print usage message and die
95 */
96 static void
97 usage(void)
98 {
99
100 (void)fprintf(stderr,
101 "usage: %s query\n", getprogname());
102 exit(1);
103 }
Something went wrong with that request. Please try again.