Untitled

 avatar
unknown
plain_text
3 years ago
5.9 kB
2
Indexable
#include <stdio.h>
#include <string.h>
#include <dirent.h>

const char *get_filename_ext(const char *filename) {
    const char *dot = strrchr(filename, '.');
    if(!dot || dot == filename) return "";
    return dot + 1;
}


void traverseDirectories(const char* directory_name, char gender[1], char firstname[20], char lastname[20]){

    DIR* dir = opendir(directory_name);
    int totalidx, startidx, endidx, z;

    if(dir == NULL){
        return;
    }

    struct dirent* entity;
    entity = readdir(dir);
    while(entity != NULL){
        
        if(entity->d_type == DT_DIR && strcmp(entity->d_name, ".") != 0 && strcmp(entity->d_name, "..") != 0){
            char path[100] = {0};
            strcat(path, directory_name);
            strcat(path, "/");
            strcat(path, entity->d_name);
            traverseDirectories(path, gender, firstname, lastname);
            //printf("%s\n", entity->d_name);
        }

        else if(entity->d_type == DT_REG && strcmp(get_filename_ext(entity->d_name), "txt") == 0 && !(strcmp(entity->d_name, "database.txt") == 0 && strcmp(directory_name, ".") == 0)){
            FILE *entity_file;
            char allpath[50] = {0};
            strcat(allpath, directory_name);
            strcat(allpath, "/");
            strcat(allpath, entity->d_name);
            //printf("%s\n", allpath);
            entity_file = fopen(allpath, "r+");
            char buffer[256];
            char prev[256];
            char prevprev[256];
            int isname = 0;
            totalidx = 0;

            
            while(fscanf(entity_file, "%s", buffer) != EOF){
                //printf("%s\n", buffer);
                if(strcmp(buffer, firstname) == 0){
                    isname = 1;
                }
                if(isname == 1){
                    if(gender[0] == 'm'){
                        //printf("%d", ftell(entity_file));
                        fseek(entity_file, -strlen(buffer) - 4, SEEK_CUR);
                        fputs("Mr.", entity_file);
                        fseek(entity_file, strlen(buffer) + 2, SEEK_CUR);
                        fputs(lastname, entity_file);
                    }
                    if(gender[0] == 'f'){
                        fseek(entity_file, -strlen(buffer) - 4, SEEK_CUR);
                        fputs("Ms.", entity_file);
                        fseek(entity_file, strlen(buffer) + 2, SEEK_CUR);
                        fputs(lastname, entity_file);
                    }
                }
                isname = 0;
                //strcpy(prevprev, prev);
                //strcpy(prev,  buffer);
                totalidx = totalidx + strlen(buffer) + 1; 
                
                /*
                startidx = 0;
                endidx = 0;

                for (int i = 0; i < strlen(buffer); i++){

                    if(buffer[i] == ' '){

                        char currentword[20];
                        char currentword2[20];
                        z = 0;

                        for(int j = startidx; j < i; j++){
                            currentword[z] = buffer[j];
                            z++;
                        }
                        if(strcmp(currentword, firstname) == 0){
                            if(gender[0] == 'm' && buffer[startidx - 3] == 's'){
                                fseek(entity_file, totalidx + startidx - 3, SEEK_SET);
                                fputs('r', entity_file);
                            }
                            if(gender[0] == 'f' && buffer[startidx - 3] == 'r'){
                                fseek(entity_file, totalidx + startidx - 3, SEEK_SET);
                                fputs('s', entity_file);
                            }
                            z = 0;
                            for(int k = i + 1; i < i + 1 + strlen(lastname); k++){
                                currentword2[z] = buffer[k];
                                z++;
                            }

                            if(strcmp(currentword2, lastname) != 0){
                                fseek(entity_file, totalidx + i + 1, SEEK_SET);
                                fputs(lastname, entity_file);
                            }
                        }
                        startidx = i + 1;
                    }
                }

                totalidx += strlen(buffer);
                */
            } 
            
            fclose(entity_file);
        }

        entity = readdir(dir);
    }
    closedir(dir);
}

int main(int argc, char* argv[]){

    FILE *database_file;
    database_file = fopen("database.txt", "r");
    char buffer[256];
    int startidx, endidx, j, counter;
    char firstname[20];
    char lastname[20];
    char gender[5];
    
    counter = 0;
    while(fscanf(database_file, "%s", buffer) != EOF){
        if(counter == 0){
            strcpy(gender, buffer);
            counter++;
        }
        else if(counter == 1){
            strcpy(firstname, buffer);
            counter++;
        }
        else if(counter == 2){
            strcpy(lastname, buffer);
            traverseDirectories(".", gender, firstname, lastname);
            counter = 0;
        }
        


        /*
        char gender[1];
        char firstname[20];
        char lastname[20];

        gender[0] = buffer[0];
        startidx = 2;
        endidx = 2;
        while(buffer[endidx] != ' '){
            endidx++;
        }

        j = 0;
        for (int i = startidx; i < endidx; i++)
        {
            firstname[j] = buffer[i];
            j++;
        }

        j = 0;
        for (int i = endidx + 1; i < strlen(buffer); i++){
            lastname[j] = buffer[i];
            j++;
        }
        traverseDirectories(".", gender, firstname, lastname);
        */



    }
    fclose(database_file);
return 0;
}