You are on page 1of 3

void getTitleFromEdit(char *command, char *out_title)

{
char *title_start = strstr(command, "title:[");
if (title_start != NULL)
{
title_start = title_start + 7;
char *title_end = strstr(title_start, "]");
if (title_end != NULL)
{
strncpy(out_title, title_start, title_end - title_start);
out_title[title_end - title_start] = '\0';
}
}
}
void getDescriptionFromEdit(char *command, char *out_description)
{
char *description_start = strstr(command, "description:[");
if (description_start != NULL)
{
description_start = description_start + 13;
char *description_end = strstr(description_start, "]");
if (description_end != NULL)
{
strncpy(out_description, description_start, description_end -
description_start);
out_description[description_end - description_start] = '\0';
}
}
}
void getTimeFromEdit(char *command, char *out_time)
{
char *time_start = strstr(command, "time:[");
if (time_start != NULL)
{
time_start = time_start + 6;
char *time_end = strstr(time_start, "]");
if (time_end != NULL)
{
strncpy(out_time, time_start, time_end - time_start);
out_time[time_end - time_start] = '\0';
}
}
}
int getNumFromCommand(char *command)
{
int num;
char *numStart = strchr(command, '#');
if (numStart == NULL)
{
return -1;
}

if (sscanf(numStart + 1, "%d", &num) != 1)


{
return 0;
}

if (num < 1)
{
return 0;
}

return num;
}
bool checkGetFieldFromEdit(char description_pos, char title_pos, char time_pos,
char status_pos)
{
if ((description_pos == NULL || title_pos < description_pos) &&
(time_pos == NULL || title_pos < time_pos) &&
(status_pos == NULL || title_pos < status_pos))
{
return true;
}
return false;
}
int getFieldFromEdit(char *edit_cmd)
{
char *title_pos = strstr(edit_cmd, "title:");
char *description_pos = strstr(edit_cmd, "description:");
char *time_pos = strstr(edit_cmd, "time:");
char *status_pos = strstr(edit_cmd, "status:");

if (title_pos != NULL)
{
if (checkGetFieldFromEdit(description_pos, title_pos, time_pos,
status_pos))
{
return 1;
}
}

if (description_pos != NULL)
{
if ((title_pos == NULL || description_pos < title_pos) &&
(time_pos == NULL || description_pos < time_pos) &&
(status_pos == NULL || description_pos < status_pos))
{
return 2;
}
}

if (time_pos != NULL)
{
if ((title_pos == NULL || time_pos < title_pos) &&
(description_pos == NULL || time_pos < description_pos) &&
(status_pos == NULL || time_pos < status_pos))
{
return 3;
}
}

if (status_pos != NULL)
{
if ((title_pos == NULL || status_pos < title_pos) &&
(description_pos == NULL || status_pos < description_pos) &&
(time_pos == NULL || status_pos < time_pos))
{
return 4;
}
}
return 0;
}
enum Status getStatusFromEdit(char *edit_cmd)
{
char *status_pos = strstr(edit_cmd, "status:[");
if (status_pos != NULL)
{
status_pos = status_pos + 8;
char status_char = status_pos[0];

switch (status_char)
{
case 'I':
case 'i':
return IN_PROGRESS;
case 'D':
case 'd':
return DONE;
case 'A':
case 'a':
return ARCHIVED;
}
}

return -1;
}

You might also like