Commits

Serge Zaitsev  committed 3fa88a7

added 'locate' command, changed 'find' command, added python wrapper

  • Participants
  • Parent commits e974787

Comments (0)

Files changed (3)

File codehound-find.py

+#!/usr/bin/python
+
+import sys
+import subprocess
+
+word = sys.argv[1]
+
+p = subprocess.Popen(["./codehound", "locate", word], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+out, err = p.communicate()
+
+for name in out.split('\n'):
+	try:
+		f = open(name, 'r')
+		prevline = None
+		thisline = None
+		for nextline in f.readlines():
+			if prevline != None and thisline != None and word in thisline:
+				print
+				print name+":"
+				print prevline, thisline, nextline
+			prevline = thisline
+			thisline = nextline
+	except IOError as e:
+		pass
+
 	return 0;
 }
 
-void search_cb(const char *file, void *arg) {
+void find_cb(const char *file, void *arg) {
 	char *word = arg;
 	char cmd[256];
 	sprintf(cmd, "grep --color=auto -H '%s' %s", word, file);
 	system(cmd);
+}
+
+void locate_cb(const char *file, void *arg) {
 	printf("%s\n", file);
 }
 
 	} else if (strcmp(argv[1], "find") == 0) {
 		struct index *index = index_open("index.cache");
 		uint32_t *trigrams = trigrams_create(argv[2]);
-		index_search(index, trigrams, search_cb, argv[2]);
+		index_search(index, trigrams, find_cb, argv[2]);
+		trigrams_destroy(trigrams);
+		index_destroy(index);
+	} else if (strcmp(argv[1], "locate") == 0) {
+		struct index *index = index_open("index.cache");
+		uint32_t *trigrams = trigrams_create(argv[2]);
+		index_search(index, trigrams, locate_cb, argv[2]);
 		trigrams_destroy(trigrams);
 		index_destroy(index);
 	}
 	index->entries_count = *(size_t *) p; p += sizeof(size_t);
 	index->entries = p;
 
-	printf("%d files, %d file ids, %d entries\n", index->files_count, 
-			index->fileids_count, index->entries_count);
+	//printf("%d files, %d file ids, %d entries\n", index->files_count, 
+	//		index->fileids_count, index->entries_count);
 	return index;
 }
 
 	for (i = 0; i < index->entries_count; i++) {
 		fwrite(&index->entries[i], sizeof(index->entries[i]), 1, f);
 	}
-	printf("%d files, %d file ids, %d entries\n", writer->files_count, 
-			index->fileids_count, index->entries_count);
+	//printf("%d files, %d file ids, %d entries\n", writer->files_count, 
+	//		index->fileids_count, index->entries_count);
 	fclose(f);
 	return 0;
 }