Thread: Backup Search
View Single Post
Old Jul 3rd, 2006, 5:49 PM   #3
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 403
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Works with text files now

I updated the search engine so that now it works with text files meaning that anyone can use it. Basically all you've gotta do is structure a text file with various entries as follows:
Location of file/folder
Description of file/folder
Keywords related to file/folder
Location of file/folder
Description of file/folder
Keywords related to file/folder
Location of file/folder
Description of file/folder
Keywords related to file/folder
Keep going like that without any whitespaces in between lines. Therefore, every file/folder/CD/DVD you enter into the text file should have three relevant pieces of information:
1 - its location: I use a particular format of the following type [physical storage device] >> {folder} >> {folder} >> file
2 - its description: A useful sentence or so regarding what the actual file is for your own benefit.
3 - keywords: these are just various words (do not separate with commas) that are relevant to what the file is about.
Here is a sample of what the text file would look like:
[Backup 1] >> {Folder 1} >> file1.txt
This is a text file that contains some experimental data.
experimental data txt
[Backup 1] >> {Folder 1} >> file2.txt
This is a text file that contains more experimental data.
experimental data txt
As you can see this particular database only has two entries, mine has 208.
Warnings: Do not deviate from the 3 line format and do not type very long descriptions. Furthermore, the text file must be named database.txt and it must be ANSI encoded NO UNICODE allowed. Here is the source code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (void)
{
	unsigned long i;
	unsigned long ENTRIES;
	unsigned long count;
	int perform;
	char search[80];
	char ***database;
	FILE *fp;

	fp = fopen("database.txt", "r");

	if(fp == NULL)
	{
		printf("Error opening file.\n");
		return(0);
	}

	ENTRIES = 0;

	do
	{
		fgets(search, 512, fp);
		ENTRIES++;
	}while(!feof(fp));

	ENTRIES /= 3;

	database = (char ***) calloc(ENTRIES, sizeof(char **));
	for(i = 0; i < ENTRIES; i++)
	{
		database[i] = (char **) calloc(ENTRIES * 3, sizeof(char*));
	}

	fsetpos(fp, 0);

	for(i = 0; i < ENTRIES; i++)
	{
		fgets(search, 512, fp);
		database[i][0] = (char *) calloc(strlen(search) + 1, sizeof(char));
		strncpy(database[i][0], search, strlen(search));

		fgets(search, 512, fp);
		database[i][1] = (char *) calloc(strlen(search) + 1, sizeof(char));
		strncpy(database[i][1], search, strlen(search));

		fgets(search, 512, fp);
		database[i][2] = (char *) calloc(strlen(search) + 1, sizeof(char));
		strncpy(database[i][2], search, strlen(search));
	}

	printf("Grim Pirate's Backup Archive Search Engine\n\n");
	printf("COMMANDS");
	printf("\n  !help  - displays the available commands");
	printf("\n  !quit  - terminate program");
	printf("\n  !list  - lists all entries in the database");
	printf("\n  !count - lists the total amount of entries in the database");
	printf("\n  !about - program info\n\n");
	while(1)
	{
		perform = 1;
		count = 0;
		printf("Please enter your search terms\n-> ");
		gets(search);
		if(strstr(search, "!quit"))
		{
			break;
		}
		if(strstr(search, "!help"))
		{
			printf("\nCOMMANDS");
			printf("\n  !help  - displays the available commands");
			printf("\n  !quit  - terminate program");
			printf("\n  !list  - lists all entries in the database");
			printf("\n  !count - lists the total amount of entries in the database");
			printf("\n  !about - program info\n\n");
			perform = 0;
		}
		if(strstr(search, "!about"))
		{
			printf("\nAuthor:    The Grim Pirate");
			printf("\nHomepage:  http://grimpirate.t35.com/");
			printf("\nHistory:   2006.07.02 - Did not work with text files.");
			printf("\nVersion:   2006.07.03\n\n");
			perform = 0;
		}
		if(strstr(search, "!count"))
		{
			printf("\n%li entries in database.\n\n", ENTRIES);
			perform = 0;
		}
		if(strstr(search, "!list"))
		{
			printf("\n--------------------------------------------------------------------------------");
			printf("                                   RESULTS\n");
			printf("--------------------------------------------------------------------------------\n");

			for(i = 0; i < ENTRIES; i++)
			{
				printf("Location:\n");
				printf("%s", database[i][0]);
				printf("Description:\n");
				printf("%s", database[i][1]);
				printf("\n");
				count++;
			}

			printf("--------------------------------------------------------------------------------");
			printf("%li entries in database.\n\n", count);

			perform = 0;
		}

		if(perform)
		{
			printf("\n--------------------------------------------------------------------------------");
			printf("                                   RESULTS\n");
			printf("--------------------------------------------------------------------------------\n");

			for(i = 0; i < ENTRIES; i++)
			{
				if(strstr(database[i][2], search))
				{
					count++;
					printf("ENTRY %74li", count);
					printf("Location:\n");
					printf("%s", database[i][0]);
					printf("Description:\n");
					printf("%s", database[i][1]);
					printf("\n");
				}
			}

			if(count == 0)
			{
				printf("NONE\n\n");
			}

			printf("--------------------------------------------------------------------------------\n");
		}
	}

	return (0);
}
This console application should work for anyone running any version of windows, but if it doesn't please let me know. If the program doesn't show the prompt, then there's probably a problem with your database file. Try it after each entry you plan to input until you find the one producing the error and modify it so the program works. Lemme know if the program worked for you or not. For those of you who can't compile it or don't know how I'm uploading a compiled version with a sample text file.
Attached Files
File Type: zip engine.zip (15.8 KB, 12 views)
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote