Untitled
unknown
plain_text
2 years ago
5.8 kB
10
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h> // Include this for access() function.
// Structure to store item information.
struct Item {
int id;
char name[100];
char model[100];
char config[100];
char hash[100];
};
// Function to create a directory in the user's Documents folder.
void createCustomDirectory(const char *directoryName) {
char documentsPath[200];
const char *homeDirectory = getenv("HOME");
if (homeDirectory == NULL) {
fprintf(stderr, "ERROR: -(Unable to get user's home directory)-\n");
exit(1);
}
snprintf(documentsPath, sizeof(documentsPath), "%s/Documents", homeDirectory);
char customDirPath[250];
snprintf(customDirPath, sizeof(customDirPath), "%s/%s", documentsPath, directoryName);
// Split the path and create parent directories if they don't exist.
char *dir = customDirPath;
while ((dir = strchr(dir + 1, '/')) != NULL) {
*dir = '\0';
if (access(customDirPath, F_OK) == -1) {
if (mkdir(customDirPath, 0700) == -1) {
fprintf(stderr, "ERROR: -(Unable to create custom directory)-\n");
exit(1);
}
}
*dir = '/';
}
// Create the final directory.
if (access(customDirPath, F_OK) == -1) {
if (mkdir(customDirPath, 0700) == -1) {
fprintf(stderr, "ERROR: -(Unable to create custom directory)-\n");
exit(1);
}
}
}
// Function to display the menu.
void displayMenu() {
printf("Menu:\n");
printf("1. Add Item\n");
printf("2. View Items\n");
printf("3. Search Item by ID\n");
printf("4. Save Items to .snet File\n");
printf("5. Exit Program\n");
}
// Function to add an item.
void addItem(struct Item **items, int *itemCount) {
struct Item item;
printf("Enter Item ID: ");
scanf("%d", &item.id);
printf("Enter Item Name: ");
scanf("%s", item.name);
printf("Enter Item Model: ");
scanf("%s", item.model);
printf("Enter Item Config: ");
scanf("%s", item.config);
printf("Enter Item Hash: ");
scanf("%s", item.hash);
(*itemCount)++;
*items = (struct Item *)realloc(*items, (*itemCount) * sizeof(struct Item));
if (*items == NULL) {
fprintf(stderr, "ERROR: -(Unable to allocate memory)-\n");
exit(1);
}
(*items)[(*itemCount) - 1] = item;
printf("SUCCESS: -(Item added)-.\n");
}
// Function to compare items for sorting by ID.
int compareItems(const void *a, const void *b) {
return ((struct Item *)a)->id - ((struct Item *)b)->id;
}
// Function to view all items sorted by ID.
void viewItems(struct Item *items, int itemCount) {
if (itemCount == 0) {
printf("ERROR: -(No items to display)-\n");
return;
}
qsort(items, itemCount, sizeof(struct Item), compareItems);
printf("Items:\n");
printf("ID\tName\tModel\tConfig\tHash\n");
for (int i = 0; i < itemCount; i++) {
printf("%d\t%s\t%s\t%s\t%s\n", items[i].id, items[i].name, items[i].model, items[i].config, items[i].hash);
}
}
// Function to search for an item by ID
void searchItemByID(struct Item *items, int itemCount, int searchID) {
for (int i = 0; i < itemCount; i++) {
if (items[i].id == searchID) {
printf("SUCCESS: -(Item Found)-\n");
printf("ID\tName\tModel\tConfig\tHash\n");
printf("%d\t%s\t%s\t%s\t%s\n", items[i].id, items[i].name, items[i].model, items[i].config, items[i].hash);
return;
}
}
printf("ERROR: -(Item with ID %d not found)-\n", searchID);
}
// Function to save items to a custom .snet file.
void saveItemsToFile(struct Item *items, int itemCount, const char *fileName) {
FILE *file = fopen(fileName, "w");
if (file == NULL) {
fprintf(stderr, "ERROR: -(Unable to open file for writing: %s)-\n", fileName);
return;
}
for (int i = 0; i < itemCount; i++) {
fprintf(file, "%d\t%s\t%s\t%s\t%s\n", items[i].id, items[i].name, items[i].model, items[i].config, items[i].hash);
}
fclose(file);
printf("SUCCESS: -(Items have been saved to %s)-\n", fileName);
}
int main() {
// Specify the custom directory name
const char *customDirectoryName = "Documents/Projects/bcs1_c_l_hello_world_1";
// Creates a custom directory in the user's Documents folder.
createCustomDirectory(customDirectoryName);
// Pointer to store items.
struct Item *items = NULL;
// Counter for the number of items.
int itemCount = 0;
int choice;
// To store the filename for saving items.
char fileName[110];
do {
displayMenu();
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addItem(&items, &itemCount);
break;
case 2:
viewItems(items, itemCount);
break;
case 3:
printf("Enter ID to search: ");
int searchID;
scanf("%d", &searchID);
searchItemByID(items, itemCount, searchID);
break;
case 4:
printf("Enter file name to save (.snet extension will be added): ");
scanf("%s", fileName);
strcat(fileName, ".snet");
saveItemsToFile(items, itemCount, fileName);
break;
case 5:
printf("Exiting program...\n");
break;
default:
printf("ERROR: -(Invalid choice)-\n");
break;
}
} while (choice != 5);
free(items); // Free dynamically allocated memory before exiting.
return 0;
}Editor is loading...