Untitled

mail@pastecode.io avatar
unknown
c_cpp
7 months ago
11 kB
5
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <direct.h>
#include <time.h>
#include <windows.h>
#include <dirent.h> // Include this header for directory operations

// Function to clear the screen
void clearScreen() {
#ifdef _WIN32 // For Windows
    system("cls");
#else // For Unix-like systems (Linux, macOS)
    system("clear");
#endif
}

#define MAX_VARIABLES 100
#define MAX_NAME_LENGTH 50
#define MAX_FILES_IN_DIRECTORY 100

// Function to load data from a file
void loadFile(const char *fileName) {
    FILE *file = fopen(fileName, "r");

    if (file == NULL) {
        perror("Error opening file for reading");
        exit(EXIT_FAILURE);
    }

    printf("Data loaded from %s:\n", fileName);

    char line[MAX_NAME_LENGTH];
    while (fgets(line, sizeof(line), file) != NULL) {
        printf("%s", line);
    }

    fclose(file);
}

// Function to create a new directory
void createNewDirectory() {
    char directoryName[MAX_NAME_LENGTH];
    printf("Enter the name of the new directory: ");
    scanf("%s", directoryName);

    // Create the new directory using _mkdir.
    if (_mkdir(directoryName) != 0) {
        perror("Error creating directory");
        exit(EXIT_FAILURE);
    }

    printf("New directory '%s' created.\n", directoryName);
}

// Function to navigate directories
void navigateDirectories(char *currentDirectory) {
    // Display the contents of the current directory
    printf("Contents of directory '%s':\n", currentDirectory);

    // Open the current directory
    DIR *dir = opendir(currentDirectory);
    if (dir == NULL) {
        perror("Error opening directory");
        return;
    }

    struct dirent *entry;
    while ((entry = readdir(dir)) != NULL) {
        printf("%s\n", entry->d_name);
    }

    closedir(dir);

    // Prompt the user to select a directory to navigate into
    char targetDirectory[MAX_NAME_LENGTH];
    printf("Enter the name of the directory to navigate into (or '.' to stay in the current directory): ");
    scanf("%s", targetDirectory);

    if (strcmp(targetDirectory, ".") == 0) {
        // Stay in the current directory
        return;
    }

    // Check if the target directory exists
    char newDirectory[MAX_NAME_LENGTH];
    snprintf(newDirectory, sizeof(newDirectory), "%s/%s", currentDirectory, targetDirectory);
    if (_access(newDirectory, 0) != 0) {
        printf("Directory '%s' does not exist.\n", targetDirectory);
    } else {
        // Navigate into the target directory
        strcpy(currentDirectory, newDirectory);
        printf("Navigated into directory '%s'.\n", currentDirectory);
    }
}

// Function to encrypt or decrypt a string using XOR
void xorEncryptDecrypt(char *input, int key) {
    int len = strlen(input);
    for (int i = 0; i < len; i++) {
        input[i] ^= key;
    }
}

// Function to set the bit length for a variable
int setBitLength() {
    int bitLength;
    do {
        printf("Enter the bit length (4-8 bits) for the variable: ");
        scanf("%d", &bitLength);
        if (bitLength < 4 || bitLength > 8) {
            printf("Invalid bit length. Please enter a value between 4 and 8 bits.\n");
        }
    } while (bitLength < 4 || bitLength > 8);
    return bitLength;
}

// Function to select a file from the custom directory
void selectFileFromDirectory(const char *customDirectory) {
    while (1) {
        printf("Files in the custom directory:\n");

        char searchPath[MAX_NAME_LENGTH];
        snprintf(searchPath, sizeof(searchPath), "%s\\variables*.snet", customDirectory);

        WIN32_FIND_DATA findFileData;
        HANDLE hFind = FindFirstFile(searchPath, &findFileData);

        if (hFind == INVALID_HANDLE_VALUE) {
            printf("No files found in the directory.\n");
            return; // Return to the main menu
        } else {
            int fileCount = 0;
            char fileList[MAX_FILES_IN_DIRECTORY][MAX_NAME_LENGTH];

            do {
                if (fileCount >= MAX_FILES_IN_DIRECTORY) {
                    printf("Too many files in the directory.\n");
                    FindClose(hFind);
                    return; // Return to the main menu
                }

                snprintf(fileList[fileCount], sizeof(fileList[fileCount]), "%s", findFileData.cFileName);
                printf("%d. %s\n", fileCount + 1, fileList[fileCount]);
                fileCount++;
            } while (FindNextFile(hFind, &findFileData) != 0);

            FindClose(hFind);

            if (fileCount == 0) {
                printf("No files found in the directory.\n");
                return; // Return to the main menu
            }

            int choice;
            printf("Enter the number of the file to select (1-%d) or 0 to go back: ", fileCount);
            scanf("%d", &choice);

            if (choice == 0) {
                return; // Return to the main menu
            }

            if (choice < 1 || choice > fileCount) {
                printf("Invalid choice.\n");
            } else {
                char selectedFile[MAX_NAME_LENGTH];
                snprintf(selectedFile, MAX_NAME_LENGTH, "%s/%s", customDirectory, fileList[choice - 1]);

                // Load and display data from the selected file.
                loadFile(selectedFile);
            }
        }
    }
}

// Function to create a new file in the custom directory with an incremented number
void createNewFileInDirectory(const char *customDirectory, const char *fileName) {
    char filePath[MAX_NAME_LENGTH];
    snprintf(filePath, sizeof(filePath), "%s/%s", customDirectory, fileName);

    FILE *file = fopen(filePath, "w");
    if (file == NULL) {
        perror("Error creating new file");
        exit(EXIT_FAILURE);
    }

    fclose(file);
    printf("New file '%s' created in %s.\n", fileName, customDirectory);
}

int main() {
     // Define variables to store custom directory name and variables.
    char customDirectory[MAX_NAME_LENGTH];
    char variables[MAX_VARIABLES][MAX_NAME_LENGTH];
    int bitLengths[MAX_VARIABLES];
    int numVariables;
    int choice;

    // Initialize the current directory to the custom directory
    strcpy(customDirectory, ".");

    int fileCounter = 1; // To keep track of the file number

    while (1) {
        // Display the menu options.
        printf("\nMenu:\n");
        printf("1. Add Variables\n");
        printf("2. Load and Display Variables\n");
        printf("3. Navigate Directories\n"); // Add the navigate directories option
        printf("4. Select a File from Directory\n");
        printf("5. Create a New File\n");
        printf("6. Clear Screen\n");
        printf("7. Create New Directory\n");
        printf("8. Exit\n");
        printf("Enter your choice (1/2/3/4/5/6/7/8): ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                // Prompt the user to enter the number of variables.
                printf("Enter the number of variables (up to %d): ", MAX_VARIABLES);
                scanf("%d", &numVariables);

                // Prompt the user to enter variable names and set bit lengths.
                printf("Enter the variable names and bit lengths:\n");
                for (int i = 0; i < numVariables; i++) {
                    printf("Variable %d name: ", i + 1);
                    scanf("%s", variables[i]);
                    bitLengths[i] = setBitLength();
                }

                // Create the file name within the custom directory with a ".snet" extension.
                char fileName[MAX_NAME_LENGTH];
                snprintf(fileName, sizeof(fileName), "%s/variables%d.snet", customDirectory, fileCounter);

                // Open the file for writing (append mode).
                FILE *file = fopen(fileName, "a"); // Use "a" to append to an existing file
                if (file == NULL) {
                    perror("Error opening file for writing");
                    exit(EXIT_FAILURE);
                }

                // Write the variables and bit lengths to the file.
                for (int i = 0; i < numVariables; i++) {
                    fprintf(file, "Variable %d (Bits %d): %s\n", i + 1, bitLengths[i], variables[i]);
                }

                // Close the file.
                fclose(file);

                // Inform the user where the variables have been saved.
                printf("Variables saved to %s/variables%d.snet\n", customDirectory, fileCounter);

                fileCounter++; // Increment the file number
                break;

            case 2:
                // Display the list of available .snet files for loading
                selectFileFromDirectory(customDirectory);
                break;

            case 3:
                // Navigate directories
                navigateDirectories(customDirectory);
                break;

            case 4:
                // Select a file from the custom directory
                selectFileFromDirectory(customDirectory);
                break;

            case 5:
                // Prompt the user to enter the name of the new file.
                char newFileName[MAX_NAME_LENGTH];
                printf("Enter the name of the new file: ");
                scanf("%s", newFileName);

                // Create a new file in the custom directory.
                createNewFileInDirectory(customDirectory, newFileName);
                break;

            case 6:
                // Clear the screen
                clearScreen();
                break;

            case 7:
                // Create a new directory
                createNewDirectory();
                break;

            case 8:
                printf("Exiting the program.\n");
                exit(EXIT_SUCCESS);
                break;

            default:
                printf("Invalid choice. Please select a valid option.\n");
        }
    }

    return 0;
}

// Function to load data from a file if it exists
void loadFileIfExists(const char *fileName) {
    FILE *file = fopen(fileName, "r");

    if (file == NULL) {
        perror("Error opening file for reading");
        return; // Return without loading if the file doesn't exist
    }

    printf("Data loaded from %s:\n", fileName);

    char line[MAX_NAME_LENGTH];
    while (fgets(line, sizeof(line), file) != NULL) {
        printf("%s", line);
    }

    fclose(file);
}