Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   Backup Search (http://www.programmingforums.org/showthread.php?t=10600)

grimpirate Jul 2nd, 2006 3:38 PM

Backup Search
 
My LCD laptop screen messed up again, and this prompted me to find a solution to keeping track of the backups I've made of my computer. I'm a habitual reformatter and start over from scratch. Needless to say, I have quite a few backup CDs and DVDs of my data. So I've created a search engine for the files I have archived.
:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ENTRIES 1

int main (void)
{
        int i;
        int n;
        int perform;
        char search[80];
        char ***database;

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

        n = 0;

        // database[n][0] = (char *) calloc(strlen("\0"), sizeof(char));
        // strcpy(database[n][0], "\0");
        // database[n][1] = (char *) calloc(strlen("\0"), sizeof(char));
        // strcpy(database[n][1], "\0");
        // database[n][2] = (char *) calloc(strlen("\0"), sizeof(char));
        // strcpy(database[n][2], "\0");
        // n++;

        /////////////////
        // DATABASE //
        ////////////////
        database[n][0] = (char *) calloc(strlen("Backup 01\0"), sizeof(char));
        strcpy(database[n][0], "Backup01\0");
        database[n][1] = (char *) calloc(strlen("Contains a backup of various things.\0"), sizeof(char));
        strcpy(database[n][1], "Contains a backup of various things.\0");
        database[n][2] = (char *) calloc(strlen("backup, applications, games, etc.\0"), sizeof(char));
        strcpy(database[n][2], "backup, applications, games, etc.\0");
        n++;
        ////////////////////////////////////////////////////////////////////////////////////////////


        printf("Grim Pirate's Backup Archive Search Engine\n\n");
        printf("COMMANDS");
        printf("\n  !quit - terminate program");
        printf("\n  !about - program info\n\n");
        while(1)
        {
                perform = 1;
                printf("Please enter your search terms\n-> ");
                gets(search);
                if(strstr(search, "!quit"))
                {
                        break;
                }
                if(strstr(search, "!about"))
                {
                        printf("\nAuthor:    The Grim Pirate");
                        printf("\nHomepage:  http://grimpirate.t35.com/");
                        printf("\nVersion:  2006.06.02\n\n");
                        perform = 0;
                }

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

                        for(i = 0; i < ENTRIES; i++)
                        {
                                if(strstr(database[i][2], search))
                                {
                                        printf("Location:    ");
                                        puts(database[i][0]);
                                        printf("Description:  ");
                                        puts(database[i][1]);
                                }
                                else
                                {
                                        printf("NONE\n");
                                }
                        }

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

        return (0);
}

Eventually I plan it to work with text files. That way anybody can create the backups by following a standard text file format. For the time being though this is what I came up with and it works well enough for my needs. The database array stores three key things: location of the file, a description of the file, and some keywords relevant to the file. It only displays the location and description on the screen. To add more entries to the database, copy the commented database code, paste it in the database section, input the relevant info, and update the ENTRIES constant to the number of entries in your database. Of course, it's better to use this program sooner rather than later, 'cause then like myself you have to go back through all your backups and start indexing them. Which is gonna suck, but better late than never. I hope someone can find a use for the app.

Prm753 Jul 2nd, 2006 5:50 PM

Hmm. Intresting. Shouldn't this have been in the "Current Project Work" section of the forums?

grimpirate Jul 3rd, 2006 6:49 PM

Works with text files now
 
1 Attachment(s)
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.

grimpirate Jul 3rd, 2006 8:49 PM

console output fix
 
1 Attachment(s)
Yeah I just realized while searching my own database that the console window doesn't have enough buffer room to display certain entries if the number of results gets too high. Therefore, I rewrote the program to output the results of the search to a file called results.txt which it automatically generates and overwrites if it doesn't exist. Everything else works the same.
:

#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*));
        }

        rewind(fp);

        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));
        }

        fclose(fp);

        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  !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(strlen(search) == 0)
                {
                        printf("\nYou did not enter anything to search for.\n\n");
                        perform = 0;
                }
                if(strstr(search, "!help"))
                {
                        printf("\nCOMMANDS");
                        printf("\n  !help  - displays the available commands");
                        printf("\n  !quit  - terminate program");
                        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("\n          2006.07.03a - no !list command, outputs to file.");
                        printf("\nVersion:  2006.07.03b\n\n");
                        perform = 0;
                }
                if(strstr(search, "!count"))
                {
                        printf("\n%li entries in database.\n\n", ENTRIES);
                        perform = 0;
                }

                if(perform)
                {
                        fp = fopen("results.txt", "w");
                        for(i = 0; i < ENTRIES; i++)
                        {
                                if(strstr(database[i][2], search))
                                {
                                        count++;
                                        fprintf(fp, "Location:\n");
                                        fprintf(fp, database[i][0]);
                                        fprintf(fp, "Description:\n");
                                        fprintf(fp, database[i][1]);
                                        fprintf(fp, "\n");
                                }
                        }

                        if(count == 0)
                        {
                                printf("\nNO RESULTS\n\n");
                        }
                        else
                        {
                                printf("\nOUTPUT SUCCESSFUL\n\n");
                        }
                        fclose(fp);
                }

        }

        return (0);
}

I'm only including the search engine executable this time. For the database example file download the previous post's zip file.


All times are GMT -5. The time now is 8:02 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC