root/trunk/search/search.d

Revision 59, 5.4 kB (checked in by lindquist, 1 year ago)

merged the dsssify branch into trunk noone has complained. some module identifiers have changed.
updated tinyjpeg to version 20070609

Line 
1 /*
2 This program is distributed without any warranty of any kind!
3
4 Author: Benjamin Shropshire (shro8822drop_this at vandals dot uidaho edu)
5
6 License: Freeware
7
8 Please give credit where credit is due. Please don't remove this notice.
9
10 */
11 module search;
12
13 import std.stdio;
14 import cgi.cgi;
15 import std.file;
16 import std.utf;
17 import std.stream;
18
19 const char[]
20     SOURCE_PATH = `/usr/include/phobos/`,
21     DOC_PATH = `/var/www/html/dmd/d/`,
22     HTTP_PATH = "/var/www/html"
23     ;
24 const char[][] roots = [
25     SOURCE_PATH,
26     DOC_PATH,
27     `/other/path/`
28     ];
29
30 void main(char[][] argv)
31 {
32     auto cgi = new HTML_CGI(argv);
33     bool search_d    = ("" != cgi.Value("D"));
34     bool search_di   = ("" != cgi.Value("DI"));
35     bool search_c    = ("" != cgi.Value("C"));
36     bool search_h    = ("" != cgi.Value("H"));
37     bool search_html = ("" != cgi.Value("HTML"));
38     char[] search = cgi.Value("search");
39
40     cgi.Head(
41     {
42         cgi.writef("<TITLE>Search: %s \"%s\" </TITLE>",cgi.Value("path"),cgi.Value("search"));
43     });
44     cgi.Body(
45     {
46         //cgi.Spill();
47
48         ////////////////// Build "search box" header
49
50         cgi.Form("/cgi-bin/search.cgi","post",
51         {
52             cgi.Heading4("Source Search:");
53             cgi.Table(
54             {
55                 cgi.TableRow(
56                 {
57                     cgi.TableData("Path:");
58                     cgi.TableData(
59                     {
60                        
61                         cgi.SelectList("path", cgi.Value("path"),roots);
62                     });
63                     cgi.writef(\n);
64                     cgi.TableData("Search Terms:");
65                     cgi.TableData({cgi.InputText("search",20, search);});
66                     cgi.writef(\n);
67                     cgi.TableData({cgi.InputSubmit("Go");});
68                 });
69             });
70             cgi.Table(
71             {
72                 cgi.TableRow(
73                 {
74                     cgi.TableData({cgi.writef("d");cgi.InputCheckbox("D",search_d);});
75                     cgi.TableData({cgi.writef("di");cgi.InputCheckbox("DI",search_di);});
76                     cgi.TableData({cgi.writef("c");cgi.InputCheckbox("C",search_c);});
77                     cgi.TableData({cgi.writef("h");cgi.InputCheckbox("H",search_h);});
78                     cgi.TableData({cgi.writef("html");cgi.InputCheckbox("HTML",search_html);});
79                 });
80             });
81         });
82
83         cgi.writef("<hr>");
84
85         ////////// begin scanning files
86
87         cgi.Table(
88         {
89             // test if we have anything to scan
90
91             char[] path = cgi.Value("path");
92             if(path != "")
93             {
94                 /*******
95                     Nested function to do recursive scanning.
96                     Scan "root" and call self on all dirs
97                 */
98                 void decend(char[] root)
99                 {
100                     // tag start of dir
101                     cgi.TableRow(
102                     {
103                         cgi.TableData(2,"<i>"~root~"</i>");
104                         cgi.TableData("dir");
105                     });
106
107                     // walk dirs
108                     foreach(DirEntry* di; root.WalkDir())
109                     {
110                         char[] type;
111                         char[] search = cgi.Value("search");
112
113                         try
114                         {
115                             if(di.isfile()) // test for file/dir
116                             {
117                                 bool look;
118                                 char[] str = di.name;
119
120                                 // test for file type
121
122                                 if(search_d && str.length > 2 && str[$-2..$] == ".d")
123                                 {   type = "<U>D source</u>";   look = true;    }
124                                 else if(search_di && str.length > 3 && str[$-3..$] == ".di")
125                                 {   type = "<U>D header</u>";   look = true;    }
126                                 else if(search_c && str.length > 2 && str[$-2..$] == ".c")
127                                 {   type = "<U>C source</u>";   look = true;    }
128                                 else if(search_h && str.length > 2 && str[$-2..$] == ".h")
129                                 {   type = "<U>C header</u>";   look = true;    }
130                                 else if(search_html && (str.length > 5 && str[$-5..$] == ".html") || (str.length > 4 && str[$-4..$] == ".htm"))
131                                 {   type = "<U>HTML</u>";       look = true;    }
132                                 else
133                                 {   look = false;           type = "file";  }
134
135                                 if(look)
136                                 {
137                                     char[][] lines = null;
138                                     int[] lineNum = null;
139
140                                     foreach(ulong i, char[] l; new File(di.name, FileMode.In))
141                                     {
142                                         foreach(str; l.Subs(search.length))
143                                             if(str == search)
144                                             {
145                                                 lines ~= l.dup;
146                                                 lineNum ~= i;
147                                             }
148                                     }
149                                     if(lines.length > 0)
150                                     {
151                                         cgi.TableRow(
152                                         {
153                                             cgi.TableData(1,{cgi.writef("<u>%d</u>",di.size);});
154                                             cgi.TableData(1,type);
155                                             if(str[0..HTTP_PATH.length] == HTTP_PATH)
156                                                 cgi.TableData(2,{cgi.Link(str[HTTP_PATH.length..$],str);});
157                                             else
158                                                 cgi.TableData(2,str);
159                                         });
160
161                                         foreach(i,l;lines)
162                                             cgi.TableRow(
163                                             {
164                                                 char[] tmp = cast(char[])read(di.name);
165                                                 if(tmp.length > 400) tmp.length = 400;
166                                                 cgi.TableData(1,{cgi.writef("%d:", lineNum[i]);});
167                                                 cgi.TableData(3,"<xmp>"~l~"</xmp>");
168                                             });
169                                     }
170                                 }
171                             }
172                             else
173                             {
174                                 decend(di.name);
175                             }
176                         }
177                         catch(Object o)
178                         {
179                             /+char[] str = di.name;
180                             cgi.TableRow(
181                             {
182                                 cgi.TableData(2,o.toString);
183                                 cgi.TableData(2,str);
184                             });+/
185                         }
186                     }// end of dir walk
187
188                 }
189                 decend(path);   // call function defined above
190             }
191             else    // nothing to scan
192                 cgi.writef("No path<br>");
193         });
194
195
196
197     });
198 }
199
200
201
202
203
204 int delegate(int delegate(inout DirEntry*)) WalkDir(char[] st)
205 {
206     struct RetC
207     {
208         char[] str;
209         int Ret(int delegate(inout DirEntry*) dl)
210         {
211             int i;
212             listdir(str, (DirEntry* de){
213                 i = dl(de);
214                 return 0 == i;
215                 });
216             return i;
217         }
218     }
219
220     auto ret = new RetC;
221     ret.str = st;
222     return &ret.Ret;
223 }
224
225
226 int delegate(int delegate (inout char[]) dg) Subs(char[] strin,uint I)
227 {
228     struct Ret
229     {
230         char[] str;
231         uint I;
232         int ret(int delegate (inout char[]) dg)
233         {
234             char[] tmp;
235             for(uint i=0; i<str.length-I; i++)
236             {
237                 tmp = str[i..i+I];
238                 if(int ex = dg(tmp))
239                     return ex;
240             }
241             return 0;
242         }
243     }
244     if(strin.length < I) return (int delegate (inout char[]) dg){return 0;};
245
246     auto ret = new Ret;
247     ret.str = strin;
248     ret.I = I;
249
250     return &ret.ret;
251 }
Note: See TracBrowser for help on using the browser.