You are on page 1of 4

enum CommandType getCommandType(char *command)

{
if (command == NULL || command[0] == '\0')
{
return INVALID;
}

char firstWord[20];
sscanf(command, "%s", firstWord);
if (strcmp(firstWord, "Quit") == 0)
{
return QUIT;
}
else if (strcmp(firstWord, "Delete") == 0)
{
return DELETE;
}
else if (strcmp(firstWord, "Add") == 0)
{
return ADD;
}
else if (strcmp(firstWord, "Edit") == 0)
{
return EDIT;
}
else if (strcmp(firstWord, "Show") == 0)
{
return SHOW;
}
else
{
return INVALID;
}
}
void getTitleFromAdd(char *command, char *out_title)
{
char *titleStart = strstr(command, "[");
char *titleEnd = strstr(titleStart, "]");

if (titleStart != NULL && titleEnd != NULL)


{
titleStart++;
int length = titleEnd - titleStart;
strncpy(out_title, titleStart, length);
out_title[length] = '\0';
}
else
{
strcpy(out_title, "");
}
}
void getDescriptionFromAdd(char *command, char *out_description)
{
char *descriptionStart = strstr(command, "[");
descriptionStart = strstr(descriptionStart + 1, "[");
char *descriptionEnd = strstr(descriptionStart, "]");

if (descriptionStart != NULL && descriptionEnd != NULL)


{
descriptionStart++;
int length = descriptionEnd - descriptionStart;
strncpy(out_description, descriptionStart, length);
out_description[length] = '\0';
}
else
{
strcpy(out_description, "");
}
}
void getTimeFromAdd(char *command, char *out_time)
{
char *timeStart = strstr(command, "[");
timeStart = strstr(timeStart + 1, "[");
timeStart = strstr(timeStart + 1, "[");
char *timeEnd = strstr(timeStart, "]");

if (timeStart != NULL && timeEnd != NULL)


{
timeStart++;
int length = timeEnd - timeStart;
strncpy(out_time, timeStart, length);
out_time[length] = '\0';
}
else
{
strcpy(out_time, "");
}
}
bool checkCurrent(char cur_char)
{
if (!((cur_char >= 'a' && cur_char <= 'z') ||
(cur_char >= 'A' && cur_char <= 'Z') ||
(cur_char >= '0' && cur_char <= '9') ||
cur_char == ' ' || cur_char == ',' ||
cur_char == '.' || cur_char == '-' ||
cur_char == ':' || cur_char == '|' ||
cur_char == '/'))
{
return true;
}
return false;
}
int checkTitle(char *raw_title)
{
int max_length = MAX_LENGTH_TITLE;
int length = strlen(raw_title);
if (length > max_length)
{
return length;
}
bool valid = true;
for (int i = 0; i < length; i++)
{
char cur_char = raw_title[i];
if (checkCurrent(cur_char))
{
valid = false;
break;
}

if (i == 0 && cur_char == ' ')


{
return 0;
}
if (i == length - 1 && cur_char == ' ')
{
return length - 1;
}
}

if (valid)
{
return -1;
}
else
{
for (int i = 0; i < length; i++)
{
char cur_char = raw_title[i];
if (checkCurrent(cur_char))
{
return i;
}
}
}

return -1;
}
int checkDescription(char *raw_description)
{
int max_length = MAX_LENGTH_DESCRIPTION;
int length = strlen(raw_description);

if (length > max_length)


{
return length;
}
bool valid = true;
for (int i = 0; i < length; i++)
{
char cur_char = raw_description[i];

if (checkCurrent(cur_char))
{
valid = false;
break;
}

if (i == 0 && cur_char == ' ')


{
return 0;
}
if (i == length - 1 && cur_char == ' ')
{
return length - 1;
}
}
if (valid)
{
return -1;
}
else
{
for (int i = 0; i < length; i++)
{
char cur_char = raw_description[i];
if (checkCurrent(cur_char))
{
return i;
}
}
}

return -1;
}

You might also like