To fix #1
parent
ff6e233855
commit
db80c4920b
1
config.h
1
config.h
|
@ -55,7 +55,6 @@ typedef enum
|
|||
#define max(a, b) (a > b) ? a : b
|
||||
#endif
|
||||
|
||||
#define MAX_COMMAND_LEN 200
|
||||
#define COMMAND_COUNT 6
|
||||
|
||||
const char allowedCommands[][200] = {"cd", "ls", "rm", "mkdir", "touch", "find"};
|
||||
|
|
151
handler.h
151
handler.h
|
@ -3,6 +3,10 @@
|
|||
|
||||
#define MAX_ARG_LEN 200
|
||||
|
||||
#define OBJECT_WRONG 0
|
||||
#define OBJECT_FOLDER 1
|
||||
#define OBJECT_FILE 2
|
||||
|
||||
int checkCommandValid(char* command)
|
||||
{
|
||||
for (size_t i = 0; i < COMMAND_COUNT; i++)
|
||||
|
@ -52,13 +56,129 @@ int checkMinorArgumentValid(char* command, char* argument)
|
|||
return FAILURE;
|
||||
}
|
||||
|
||||
int checkMajorArgumenntValid(char* command, char* argument)
|
||||
int checkMajorArgumenntValid(char* argument, Folder* RootFolder, Folder* CurrentFolder, Folder* ResultFolder, File* ResultFile)
|
||||
{
|
||||
return SUCCESS;
|
||||
Folder* ptr;
|
||||
char* path = (char*) malloc(MAX_COMMAND_LEN);
|
||||
|
||||
strcpy(path, argument);
|
||||
|
||||
for (size_t i = 0; i < strlen(path); i++)
|
||||
{
|
||||
if(path[i] == '\n')
|
||||
{
|
||||
path[i] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
if (argument[0] == '/')
|
||||
{
|
||||
// Абсолютный путь
|
||||
ptr = RootFolder;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
// Относительный путь
|
||||
ptr = CurrentFolder;
|
||||
}
|
||||
|
||||
char *sep = "/", *istr;
|
||||
istr = strtok(path, sep);
|
||||
|
||||
int foundFlag;
|
||||
while (istr != NULL)
|
||||
{
|
||||
foundFlag = 1;
|
||||
for (size_t i = 0; i < CurrentFolder->folders_count_cur; i++)
|
||||
{
|
||||
if (!strcmp(CurrentFolder->folders[i].filename, istr))
|
||||
{
|
||||
foundFlag = 0;
|
||||
ptr = &CurrentFolder->folders[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(foundFlag)
|
||||
{
|
||||
int isFileFlag = 0;
|
||||
for (size_t i = 0; i < strlen(istr); i++)
|
||||
{
|
||||
if(istr[i] == '.')
|
||||
{
|
||||
isFileFlag = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isFileFlag)
|
||||
{
|
||||
char* filename = (char*) malloc(filename_lenth + 1);
|
||||
char* extension = (char*) malloc(extinsion_lenth + 1);
|
||||
int extFlag = 0;
|
||||
|
||||
int writeCount = 0;
|
||||
|
||||
for (size_t i = 0; i < strlen(istr); i++) // Отделяем имя файла от расширения
|
||||
{
|
||||
if (istr[i] == '.')
|
||||
{
|
||||
extFlag = 1;
|
||||
filename[writeCount + 1] = '\0';
|
||||
writeCount = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(extFlag)
|
||||
{
|
||||
filename[writeCount] = istr[i];
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
extension[writeCount] = istr[i];
|
||||
}
|
||||
|
||||
writeCount++;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < ptr->files_count_cur; i++)
|
||||
{
|
||||
if (!strcmp(ptr->files[i].filename, filename) && !strcmp(ptr->files[i].extension, extension))
|
||||
{
|
||||
free(path);
|
||||
free(filename);
|
||||
free(extension);
|
||||
|
||||
ResultFile = &ptr->files[i];
|
||||
return OBJECT_FILE;
|
||||
}
|
||||
}
|
||||
|
||||
free(filename);
|
||||
free(extension);
|
||||
}
|
||||
|
||||
printf("Cannot resolve %s\n", istr);
|
||||
return WRONG_ARGUMENT_FAILURE;
|
||||
}
|
||||
|
||||
istr = strtok(NULL, sep);
|
||||
}
|
||||
|
||||
free(path);
|
||||
ResultFolder = ptr;
|
||||
return OBJECT_FOLDER;
|
||||
}
|
||||
|
||||
int checkArgumentValid(char* command, char* argument)
|
||||
int checkArgumentValid(char* command, char* argument, Folder* RootFolder, Folder* CurrentFolder, Folder* resultFolder, File* resultFile)
|
||||
{
|
||||
Folder* _ResultFolder;
|
||||
File* _ResultFile;
|
||||
|
||||
int iResult;
|
||||
|
||||
if(argument == NULL)
|
||||
{
|
||||
return WRONG_ARGUMENT_FAILURE;
|
||||
|
@ -75,18 +195,28 @@ int checkArgumentValid(char* command, char* argument)
|
|||
else
|
||||
{
|
||||
// Обрабатываем объект
|
||||
if (checkMajorArgumenntValid(command, argument))
|
||||
iResult = checkMajorArgumenntValid(argument, RootFolder, CurrentFolder, _ResultFolder, _ResultFile);
|
||||
if (iResult == WRONG_ARGUMENT_FAILURE)
|
||||
{
|
||||
return WRONG_ARGUMENT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
if (iResult == OBJECT_FILE)
|
||||
{
|
||||
resultFile = _ResultFile;
|
||||
}
|
||||
|
||||
if (iResult == OBJECT_FOLDER)
|
||||
{
|
||||
resultFolder = _ResultFolder;
|
||||
}
|
||||
}
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int commandParserHandler(char* input)
|
||||
int commandParserHandler(char* input, Folder* RootFolder, Folder* CurrentFolder)
|
||||
{
|
||||
int result = SUCCESS; // Если что-то пойдет не так, то он изменится
|
||||
|
||||
|
@ -104,6 +234,9 @@ int commandParserHandler(char* input)
|
|||
|
||||
int optionFlag = 0;
|
||||
|
||||
File* ResultFile = NULL;
|
||||
Folder* ResultFolder = NULL;
|
||||
|
||||
while(istr != NULL)
|
||||
{
|
||||
// Обрабатываем команду/аргумент
|
||||
|
@ -121,7 +254,7 @@ int commandParserHandler(char* input)
|
|||
|
||||
case 1:
|
||||
// аргумент 1
|
||||
if (checkArgumentValid(command, istr))
|
||||
if (checkArgumentValid(command, istr, RootFolder, CurrentFolder, ResultFolder, ResultFile))
|
||||
{
|
||||
result = WRONG_OPTION;
|
||||
goto cleanup;
|
||||
|
@ -141,7 +274,7 @@ int commandParserHandler(char* input)
|
|||
goto cleanup;
|
||||
}
|
||||
|
||||
if (checkArgumentValid(command, istr))
|
||||
if (checkArgumentValid(command, istr, RootFolder, CurrentFolder, ResultFolder, ResultFile))
|
||||
{
|
||||
result = WRONG_OPTION;
|
||||
goto cleanup;
|
||||
|
@ -166,4 +299,4 @@ int commandParserHandler(char* input)
|
|||
free(arg2);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -4,7 +4,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// #include "config.h"
|
||||
#define MAX_COMMAND_LEN 200
|
||||
|
||||
#define GETSTRING_ERR_RE -1
|
||||
#define GETSTRING_ERR_OVERFLOW -2
|
||||
|
|
45
main.c
45
main.c
|
@ -1,27 +1,46 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#include "structs.h"
|
||||
// #include "structs.h"
|
||||
#include "include/ui.h"
|
||||
#include "handler.h"
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
Folder* CurrentFolder;
|
||||
Folder RootFolder;
|
||||
|
||||
createFolder("root", &RootFolder);
|
||||
CurrentFolder = &RootFolder;
|
||||
|
||||
int iResult;
|
||||
char* buf;
|
||||
|
||||
addFolder("test1", &RootFolder);
|
||||
addFolder("test2", &RootFolder);
|
||||
addFolder("test3", &RootFolder);
|
||||
|
||||
addFolder("test11", &RootFolder.folders[0]);
|
||||
|
||||
Folder* ResultFolder;
|
||||
File* ResultFile;
|
||||
|
||||
iResult = checkArgumentValid("ls", "/test1/", &RootFolder, CurrentFolder, ResultFolder, ResultFile);
|
||||
printf("%d\n", iResult);
|
||||
|
||||
// char* buf;
|
||||
|
||||
while (1)
|
||||
{
|
||||
printf("> ");
|
||||
buf = commandCallHandler();
|
||||
// while (1)
|
||||
// {
|
||||
// printf("> ");
|
||||
// buf = commandCallHandler();
|
||||
|
||||
if (buf == NULL)
|
||||
{
|
||||
printf("Input too long!\n");
|
||||
}
|
||||
// if (buf == NULL)
|
||||
// {
|
||||
// printf("Input too long!\n");
|
||||
// }
|
||||
|
||||
iResult = commandParserHandler(buf);
|
||||
printf("result: %d\n", iResult);
|
||||
}
|
||||
// iResult = commandParserHandler(buf, &RootFolder, CurrentFolder);
|
||||
// printf("result: %d\n", iResult);
|
||||
// }
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
#include "structs.h"
|
||||
#include "config.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
int main()
|
||||
|
|
Loading…
Reference in New Issue