1 |
/* |
2 |
$Id: libtest.c,v 1.2 2002/08/22 22:58:39 whmoseley Exp $ |
3 |
** |
4 |
** Copyright (C) 1995, 1996, 1997, 1998 Hewlett-Packard Company |
5 |
** |
6 |
** Originally by Kevin Hughes, kev@kevcom.com, 3/11/94 |
7 |
** |
8 |
** This program and library is free software; you can redistribute it and/or |
9 |
** modify it under the terms of the GNU (Library) General Public License |
10 |
** as published by the Free Software Foundation; either version 2 |
11 |
** of the License, or any later version. |
12 |
** |
13 |
** This program is distributed in the hope that it will be useful, |
14 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
** GNU (Library) General Public License for more details. |
17 |
** |
18 |
** You should have received a copy of the GNU (Library) General Public License |
19 |
** along with this program; if not, write to the Free Software |
20 |
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21 |
**--------------------------------------------------------- |
22 |
* |
23 |
* Example program for interfacing a C program with the Swish-e C library. |
24 |
* |
25 |
* ./libtest [optional index file] |
26 |
* |
27 |
* use quotes for more than one file |
28 |
* ./libtest index.swish-e |
29 |
* ./libtest 'index1 index2 index3' |
30 |
* |
31 |
* See the perl/SWISHE.xs file for more detail |
32 |
* |
33 |
*/ |
34 |
|
35 |
|
36 |
#include <stdio.h> |
37 |
#include "swish.h" |
38 |
#include "error.h" |
39 |
#include "mem.h" // for mem_summary only |
40 |
|
41 |
#define DISPLAY_COUNT 10 // max to display |
42 |
|
43 |
|
44 |
void show_results( SWISH *swish_handle, int ); |
45 |
|
46 |
int main(int argc, char **argv) |
47 |
{ |
48 |
SWISH *swish_handle; |
49 |
int num_results; |
50 |
int structure = IN_FILE; |
51 |
char *properties = NULL; |
52 |
char *sortspec = NULL; |
53 |
char input_buf[200]; |
54 |
|
55 |
|
56 |
|
57 |
|
58 |
/* Connect to the indexes specified */ |
59 |
|
60 |
swish_handle = SwishInit( argv[1] && *(argv[1]) ? argv[1] : "index.swish-e"); |
61 |
|
62 |
/* Now, let's send all warnings and error messages to stderr instead of stdout (the default) */ |
63 |
SwishErrorsToStderr(); /* Global. Must be set after calling SwishInit() */ |
64 |
|
65 |
|
66 |
|
67 |
/* how to check for errors */ |
68 |
|
69 |
if ( SwishError( swish_handle ) ) |
70 |
{ |
71 |
/* Now, there's a few ways to do this */ |
72 |
|
73 |
SwishAbortLastError( swish_handle ); // The easy way. Format, print message and aborts. |
74 |
|
75 |
/* Or for more control */ |
76 |
|
77 |
{ |
78 |
int number = SwishError( swish_handle ); |
79 |
char *message = SwishErrorString( swish_handle ); |
80 |
char *comment = SwishLastErrorMsg( swish_handle ); |
81 |
|
82 |
fprintf(stderr, "err: Number [%d], Type [%s], Optional String [%s]\n", number, message, comment ); |
83 |
} |
84 |
|
85 |
/* Now if you want to exit */ |
86 |
exit(1); |
87 |
|
88 |
/* Otherwise, to continue you need to clean up memory */ |
89 |
SwishClose( swish_handle ); |
90 |
// return; // for example |
91 |
} |
92 |
|
93 |
|
94 |
while ( 1 ) |
95 |
{ |
96 |
printf("Enter search words: "); |
97 |
if ( !fgets( input_buf, 200, stdin ) ) |
98 |
break; |
99 |
|
100 |
|
101 |
num_results = SwishSearch(swish_handle, input_buf,structure,properties,sortspec); |
102 |
|
103 |
/* SwishSearch return: |
104 |
* Number of hits if positive |
105 |
* Zero on no results |
106 |
* A negative number (the error number) on fail. |
107 |
*/ |
108 |
|
109 |
if ( num_results >= 0 ) |
110 |
{ |
111 |
printf("Total Results: %d\n", num_results ); |
112 |
|
113 |
if ( num_results > 0 ) |
114 |
show_results( swish_handle, DISPLAY_COUNT ); |
115 |
} |
116 |
|
117 |
|
118 |
/* Deal with errors */ |
119 |
|
120 |
if ( num_results < 0 && SwishError( swish_handle ) ) |
121 |
{ |
122 |
if ( SwishCriticalError( swish_handle ) ) |
123 |
SwishAbortLastError( swish_handle ); |
124 |
else |
125 |
printf("Error: %s %s\n", SwishErrorString( swish_handle ), SwishLastErrorMsg( swish_handle ) ); |
126 |
} |
127 |
|
128 |
Mem_Summary("At end of loop", 0); |
129 |
|
130 |
} |
131 |
|
132 |
Mem_Summary("Before Free", 0); |
133 |
SwishClose( swish_handle ); |
134 |
|
135 |
/* Look for memory leaks -- must change settings in mem.h and recompile swish */ |
136 |
Mem_Summary("At end of program", 1); |
137 |
|
138 |
return 0; |
139 |
|
140 |
} |
141 |
|
142 |
/* Display some standard properties -- see perl/SWISHE.xs for how to get at the data */ |
143 |
|
144 |
void show_results( SWISH *swish_handle, int max_display ) |
145 |
{ |
146 |
RESULT *result; |
147 |
|
148 |
|
149 |
while ( max_display-- && (result = SwishNext( swish_handle )) ) |
150 |
{ |
151 |
|
152 |
/* This SwishResultPropertyStr() will work for all types of props */ |
153 |
/* But SwishResultPropertyULong() can be used to return numeric types */ |
154 |
/* Should probably check for errors after every call */ |
155 |
/* SwishResultPropertyULong will return ULONG_MAX if the value cannot be returned */ |
156 |
/* that could mean an error, or just that there was not a property assigned (which is not an error) */ |
157 |
|
158 |
printf("%lu %s '%s' %lu %s\n", |
159 |
SwishResultPropertyULong (swish_handle, result, "swishrank" ), |
160 |
SwishResultPropertyStr (swish_handle, result, "swishdocpath" ), |
161 |
SwishResultPropertyStr (swish_handle, result, "swishtitle"), |
162 |
SwishResultPropertyULong (swish_handle, result, "swishdocsize" ), |
163 |
SwishResultPropertyStr (swish_handle, result, "swishlastmodified" ) |
164 |
); |
165 |
} |
166 |
|
167 |
|
168 |
} |
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|