Initial commit
authorStiletto <blasux@blasux.ru>
Sat, 12 Feb 2011 13:45:55 +0300
changeset 0 56281d90869c
child 1 5ef6ffc1bc7f
Initial commit
Makefile
config.mk
cos-dbupdate.cxx
cos-search.cxx
scan.cxx
walker.cxx
walker.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Makefile	Sat Feb 12 13:45:55 2011 +0300
@@ -0,0 +1,27 @@
+
+include config.mk
+
+all: cos-dbupdate cos-search scan
+
+scan: ${OBJS}
+	@echo CC -o $@
+	@g++ ${OBJS} ${LDFLAGS} -o $@
+
+clean:
+	rm -f ${OBJS} scan
+
+walker.o: walker.cxx walker.h
+	@g++ -c ${CFLAGS} walker.cxx
+
+#magi.o: magi.cxx walker.h
+#	@g++ -c ${CFLAGS} magi.cxx
+
+scan.o: scan.cxx magi.h
+	@g++ -c ${CFLAGS} scan.cxx
+
+
+cos-dbupdate: cos-dbupdate.cxx
+	g++ -O0 -g cos-dbupdate.cxx -o cos-dbupdate ${CFLAGS} ${LDFLAGS}
+
+cos-search: cos-search.cxx
+	g++ -O0 -g cos-search.cxx -o cos-search ${CFLAGS} ${LDFLAGS}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/config.mk	Sat Feb 12 13:45:55 2011 +0300
@@ -0,0 +1,25 @@
+# dwm version
+VERSION = 5.9
+
+# Customize below to fit your system
+
+# paths
+PREFIX = /usr/local
+MANPREFIX = ${PREFIX}/share/man
+
+# includes and libs
+INCS = -I. -I/usr/include
+LIBS = -L/usr/lib -lc -lmagic `xapian-config --libs` `pcre-config --libs-cpp`
+
+# flags
+CPPFLAGS = -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS} -D_BSD_SOURCE=1
+CFLAGS = -g -std=c++0x -pedantic -Wall -O0 ${INCS} ${CPPFLAGS}
+#CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS}
+LDFLAGS = -g ${LIBS}
+#LDFLAGS = -s ${LIBS}
+
+OBJS = walker.o scan.o
+# magi.o
+
+# compiler and linker
+CC = cc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cos-dbupdate.cxx	Sat Feb 12 13:45:55 2011 +0300
@@ -0,0 +1,66 @@
+/* quickstartindex.cc: Simplest possible indexer
+ *
+ * ----START-LICENCE----
+ * Copyright 1999,2000,2001 BrightStation PLC
+ * Copyright 2003,2004 Olly Betts
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
+ * USA
+ */
+
+#include <xapian.h>
+#include <iostream>
+#include <cstdlib>
+
+using namespace std;
+
+int main(int argc, char **argv)
+{
+    // Simplest possible options parsing: we just require three or more
+    // parameters.
+    if(argc < 2) {
+        cout << "usage: " << argv[0] <<
+                " <path to database>" << endl;
+        exit(1);
+    }
+
+    // Catch any Xapian::Error exceptions thrown
+    try {
+        // Make the database
+        Xapian::WritableDatabase database(argv[1], Xapian::DB_CREATE_OR_OPEN);
+        while (1) {
+            string fname;
+            getline(cin,fname);
+            if (cin.eof()||fname.empty())
+                break;
+            Xapian::Document newdocument;
+            newdocument.set_data(fname);
+            newdocument.add_posting("FILENAME="+fname, 0);
+            int i=1;
+            while (1) {
+                string term;
+                getline(cin,term);
+                if (cin.eof()||term.empty())
+                    break;
+                cout << "TERM <" << term << ">" << endl;
+                newdocument.add_posting(term, i++);
+            }
+            database.replace_document("FILENAME="+fname,newdocument);
+            cout << "EOFILE <" << fname << ">" << endl;
+        }
+    } catch(const Xapian::Error &error) {
+        cout << "Exception: "  << error.get_msg() << endl;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cos-search.cxx	Sat Feb 12 13:45:55 2011 +0300
@@ -0,0 +1,88 @@
+/* cos-search.cxx:
+ *   based on quickstartsearch.cc: Simplest possible searcher
+ *
+ * ----START-LICENCE----
+ * Copyright 1999,2000,2001 BrightStation PLC
+ * Copyright 2003,2004 Olly Betts
+ * Copyright 2010 Stiletto <blasux@blasux.ru>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
+ * USA
+ */
+
+#include <xapian.h>
+#include <iostream>
+#include <cstdlib>
+#include <algorithm>
+#include <pcrecpp.h>
+
+using namespace std;
+
+int main(int argc, char **argv)
+{
+    // Simplest possible options parsing: we just require two or more
+    // parameters.
+    if (argc < 3) {
+        cout << "usage: " << argv[0] <<
+                " <path to database> <search query>" << endl;
+        exit(1);
+    }
+
+    // Catch any Xapian::Error exceptions thrown
+    try {
+	Xapian::Database db(argv[1]);
+        Xapian::Enquire enquire(db);
+        Xapian::QueryParser qp;
+        string qstring(argv[2]);
+
+        pcrecpp::RE prereg("([A-Za-z0-9_-]+):");
+        pcrecpp::StringPiece input(qstring);  // Wrap in a StringPiece
+
+        string var;
+        while (prereg.Consume(&input, &var)) {
+            string upvar = var+"=";
+            std::transform(var.begin(), var.end(), var.begin(), ::tolower);
+            std::transform(upvar.begin(), upvar.end(), upvar.begin(), ::toupper);
+            cout << var << upvar << endl;
+            qp.add_boolean_prefix(var,upvar);
+        }
+
+
+        // Build the query object
+        Xapian::Query query = qp.parse_query(qstring); //Xapian::Query::OP_OR, argv + 2, argv + argc);
+        
+        cout << "Performing query `" << query.get_description() << "'" << endl;
+
+        // Give the query object to the enquire session
+        enquire.set_query(query);
+
+        // Get the top 10 results of the query
+        Xapian::MSet matches = enquire.get_mset(0, db.get_doccount());
+
+        // Display the results
+        cout << matches.size() << " results found" << endl;
+
+        for (Xapian::MSetIterator i = matches.begin();
+             i != matches.end();
+             ++i) {
+            Xapian::Document doc = i.get_document();
+            cout << "Document ID " << *i << "\t" <<
+                    i.get_percent() << "% [" <<
+                    doc.get_data() << "]" << endl;
+        }
+    } catch(const Xapian::Error &error) {
+        cout << "Exception: "  << error.get_msg() << endl;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scan.cxx	Sat Feb 12 13:45:55 2011 +0300
@@ -0,0 +1,6 @@
+#include "magi.h"
+
+int main(int argc, char *argv[]) {
+    MagicWalker f("/home/media/music");
+    f.Run();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/walker.cxx	Sat Feb 12 13:45:55 2011 +0300
@@ -0,0 +1,62 @@
+#include <sys/types.h>
+#include <sys/param.h>
+#include <dirent.h>
+#include <cerrno>
+#include <cassert>
+#include <cstdio>
+#include <cstring>
+#include <string>
+
+#include "walker.h"
+
+MagicWalker::MagicWalker(const std::string root) {
+    this->root = root;
+    magic = magic_open(MAGIC_MIME_TYPE);
+    magic_load(magic,NULL);
+}
+
+int MagicWalker::HandleDir(const char *dirname) {
+    DIR *d = opendir(dirname);
+    if(!d) return errno;
+
+    struct dirent *de;
+    while (de=readdir(d)) {
+        if(strcmp(de->d_name,".") && strcmp(de->d_name,"..")) {
+            //printf("%s/%s -- %d\n",dirname,de->d_name,de->d_type);
+            char next[MAXPATHLEN+1];
+            snprintf(next,MAXPATHLEN,"%s/%s",dirname,de->d_name);
+            if (de->d_type&DT_DIR)
+                HandleDir(next);
+            else if (de->d_type&DT_REG)
+                HandleFile(next);
+            else
+                printf("%d %d\n",DT_REG,de->d_type);
+        }
+    }
+    closedir(d);
+    return 0;
+}
+
+const char *MagicWalker::GetFileMagic(const char *fname) {
+    return magic_file(magic,fname);
+}
+
+int MagicWalker::HandleFile(const char *fname) {
+    const char *rest = GetFileMagic(fname);
+    if (rest)
+        printf("%s\nMIMETYPE=%s\n\n",fname,rest);
+    else
+        printf("%s\nMIMETYPE=\n\n",fname);
+    return 0;
+}
+        
+MagicWalker::~MagicWalker () {
+    magic_close(magic);
+}
+
+#ifdef MAIN_WALKER
+int main(int argc, char *argv[]) {
+    MagicWalker f("/home/media/music");
+    f.Run();
+}
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/walker.h	Sat Feb 12 13:45:55 2011 +0300
@@ -0,0 +1,20 @@
+#ifndef _WALKER_H_INCLUDED_
+#define _WALKER_H_INCLUDED_
+#include <string>
+#include <magic.h>
+
+class MagicWalker {
+    
+    public:
+        std::string root;
+        MagicWalker(const std::string root);
+        ~MagicWalker ();
+        void Run() { HandleDir(root.c_str()); }
+    protected:
+        virtual int HandleFile(const char *filename);
+        virtual int HandleDir(const char *dirname);
+        const char *GetFileMagic(const char *filename);
+        magic_t magic;
+};
+
+#endif