0% found this document useful (0 votes)
4 views5 pages

Command Parsing and Validation Functions

The document contains C code functions for handling commands related to tasks, including converting strings to uppercase, extracting titles, descriptions, and times from commands, and validating date and time formats. It also includes functions for checking the validity of titles and descriptions, comparing datetime values, and copying strings. The code appears to be part of a larger program for managing tasks or events with specific command types and validation rules.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Command Parsing and Validation Functions

The document contains C code functions for handling commands related to tasks, including converting strings to uppercase, extracting titles, descriptions, and times from commands, and validating date and time formats. It also includes functions for checking the validity of titles and descriptions, comparing datetime values, and copying strings. The code appears to be part of a larger program for managing tasks or events with specific command types and validation rules.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

//1

void toUpperCase(char *str) {


int len = strlen(str);
for (int i = 0; i < len; i++) {
str[i] = toupper((unsigned char)str[i]);
}
}

enum CommandType getCommandType(char *command) {


char temp[MAX_LENGTH_COMMAND];
strcpy(temp, command);
char *token = strtok(temp, " ");
if (token == NULL) {
return INVALID;
}
toUpperCase(token);
enum CommandType commandType = INVALID;
for (int i = 0; i < 6; i++) {
if (strcmp(token, command_name[i]) == 0) {
commandType = (enum CommandType)i;
break;
}
}
return commandType;
}

//2
void getTitleFromAdd(char *command, char *out_title) {
char temp[MAX_LENGTH_COMMAND];
strcpy(temp, command);
char a[100][100];
int n = 0;
char *word = strtok(temp, "[]");
while (word != NULL) {
strcpy(a[n], word);
++n;
word = strtok(NULL, "[]");
}
strcpy(out_title, a[1]);
}

void getDescriptionFromAdd(char *command, char *out_description) {


char temp[MAX_LENGTH_COMMAND];
strcpy(temp, command);
char a[100][100];
int n = 0;
char *word = strtok(temp, "[]");
while (word != NULL) {
strcpy(a[n], word);
++n;
word = strtok(NULL, "[]");
}
strcpy(out_description, a[3]);
}

void getTimeFromAdd(char *command, char *out_time) {


char temp[MAX_LENGTH_COMMAND];
strcpy(temp, command);
char a[100][100];
int n = 0;
char *word = strtok(temp, "[]");
while (word != NULL) {
strcpy(a[n], word);
++n;
word = strtok(NULL, "[]");
}
strcpy(out_time, a[5]);
}

//3
int checkTitle(char *out_title) {
int length = strlen(out_title);
if(length > MAX_LENGTH_TITLE) return length;
if(out_title[0] == ' ' || out_title[length - 1] == ' ') return 0;
for (int i = 0; i < length; i++) {
char c = out_title[i];
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') || c == ' ' || c == ',' ||
c == '.' || c == '-' || c == ':' || c == '|' || c == '/'))
return i;
}
return -1;
}
//4
int checkDescription(char *out_description) {
int len = strlen(out_description);
if(len > MAX_LENGTH_DESCRIPTION) return len;
if(out_description[0] == ' ' || out_description[len - 1] == ' ') return 0;
for (int i = 0; i < len; i++) {
char c = out_description[i];
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') || c == ' ' || c == ',' ||
c == '.' || c == '-' || c == ':' || c == '|' || c == '/'))
return i + 1;
}
return -1;
}
//5
int getTime(char *datetime1, char *datetime2) {
int hh1, mm1, dd1, mo1, yyyy1;
char temp1[101];
strcpy(temp1, datetime1);
char *token1 = strtok(temp1, ":");
hh1 = atoi(token1);
token1 = strtok(NULL, "|");
mm1 = atoi(token1);
token1 = strtok(NULL, "/");
dd1 = atoi(token1);
token1 = strtok(NULL, "/");
mo1 = atoi(token1);
token1 = strtok(NULL, "|");
yyyy1 = atoi(token1);

int hh2, mm2, dd2, mo2, yyyy2;


char temp2[101];
strcpy(temp2, datetime2);
char *token2 = strtok(temp2, ":");
hh2 = atoi(token2);
token2 = strtok(NULL, "|");
mm2 = atoi(token2);
token2 = strtok(NULL, "/");
dd2 = atoi(token2);
token2 = strtok(NULL, "/");
mo2 = atoi(token2);
token2 = strtok(NULL, "|");
yyyy2 = atoi(token2);

//printf("%d %d %d %d %d\n%d %d %d %d %d\n",hh1, mm1, dd1, mo1, yyyy1, hh2,


mm2, dd2, mo2, yyyy2);

if (yyyy2 < yyyy1) return 0;


else if (yyyy2 == yyyy1 && mo2 < mo1) return 0;
else if (yyyy2 == yyyy1 && mo2 == mo1 && dd2 < dd1) return 0;
else if (yyyy2 == yyyy1 && mo2 == mo1 && dd2 == dd1 && hh2 < hh1) return 0;
else if (yyyy2 == yyyy1 && mo2 == mo1 && dd2 == dd1 && hh2 == hh1 && mm2 < mm1)
return 0;
else return -1;
}

//ham check xem tra ve gia tri loi


int createInvalidValue(int value, char number, char type) {
char invalid[5];
if (type == 'h') {
snprintf(invalid, sizeof(invalid), "1%c%d", number, value);
}
else if (type == 'p') {
snprintf(invalid, sizeof(invalid), "2%c%d", number, value);
}
else if (type == 'd') {
snprintf(invalid, sizeof(invalid), "3%c%d", number, value);
}
else if (type == 'm') {
snprintf(invalid, sizeof(invalid), "4%c%d", number, value);
}
else if (type == 'y') {
snprintf(invalid, sizeof(invalid), "5%c%d", number, value);
}
return atoi(invalid);
}

int checkDateTime(char *datetime, char type) {


int hh, mm, dd, mo, yyyy;
char temp[101];
strcpy(temp, datetime);
char *token = strtok(temp, ":");
hh = atoi(token);
token = strtok(NULL, "|");
mm = atoi(token);
token = strtok(NULL, "/");
dd = atoi(token);
token = strtok(NULL, "/");
mo = atoi(token);
token = strtok(NULL, "|");
yyyy = atoi(token);

if (yyyy <= 0) return createInvalidValue(yyyy, type, 'y');


else if (mo < 1 || mo > 12) return createInvalidValue(mo, type, 'm');
else if (dd < 1 || dd > 31) return createInvalidValue(dd, type, 'd');
else if (hh < 0 || hh > 23) return createInvalidValue(hh, type, 'h');
else if (mm < 0 || mm > 59) return createInvalidValue(mm, type, 'p');
return -1;
}

int compareDatetime1and2(char *datetime1, char *datetime2) {


int invalid1 = checkDateTime(datetime1, '1');
int invalid2 = checkDateTime(datetime2, '2');

if (invalid1 != -1 && invalid2 != -1) {


return invalid1;
}
else if (invalid1 != -1) {
return invalid1;
}
else if (invalid2 != -1) {
return invalid2;
}
else {
return -1;
}
}
//ham copy chuoi
void copyString(char *source, char *destination) {
while (*source != '\0') {
*destination = *source;
source++;
destination++;
}
*destination = '\0';
}
//ham check tat ca dieu kien
int checkTime(char *raw_time) {
char copy_command[100];
copyString(raw_time, copy_command);
char datetime1[MAX_LENGTH_TIME];
char datetime2[MAX_LENGTH_TIME];
char *token = strtok(copy_command, "-");
if (token != NULL) {
strcpy(datetime1, token);
token = strtok(NULL, "-");
if (token != NULL) {
strcpy(datetime2, token);
}
}

int hh1, mm1, dd1, mo1, yyyy1;


int hh2, mm2, dd2, mo2, yyyy2;

if (sscanf(datetime1, "%d:%d|%d/%d/%d", &hh1, &mm1, &dd1, &mo1, &yyyy1) != 5) {


return createInvalidValue(1, '1', '1');
}

if (sscanf(datetime2, "%d:%d|%d/%d/%d", &hh2, &mm2, &dd2, &mo2, &yyyy2) != 5) {


return createInvalidValue(1, '2', '1');
}
int datetime1CheckResult = checkDateTime(datetime1, '1');
int datetime2CheckResult = checkDateTime(datetime2, '2');

if (datetime1CheckResult != -1) {
return datetime1CheckResult;
}

if (datetime2CheckResult != -1) {
return datetime2CheckResult;
}

if (yyyy2 < yyyy1) {


return 0;
}

if (yyyy2 == yyyy1 && mo2 < mo1) {


return createInvalidValue(mo2, '2', 'm');
}

if (yyyy2 == yyyy1 && mo2 == mo1 && dd2 < dd1) {


return createInvalidValue(dd2, '2', 'd');
}

if (yyyy2 == yyyy1 && mo2 == mo1 && dd2 == dd1 && hh2 < hh1) {
return createInvalidValue(hh2, '2', 'h');
}

if (yyyy2 == yyyy1 && mo2 == mo1 && dd2 == dd1 && hh2 == hh1 && mm2 < mm1) {
return createInvalidValue(mm2, '2', 'p');
}

return -1;
}
//8
int getFieldFromEdit(char *edit_cmd) {
char *token = strtok(edit_cmd, " ");
token = strtok(NULL, ":");

if (strcmp(token, "title") == 0) {
return 1;
} else if (strcmp(token, "description") == 0) {
return 2;
} else if (strcmp(token, "time") == 0) {
return 3;
} else if (strcmp(token, "status") == 0) {
return 4;
} else {
return 0;
}
}

You might also like