Finished working on 'fetch_message_string' function

master
Aadhavan Srinivasan 2 years ago
parent d46d250b84
commit 6be0bd8ce2

@ -25,11 +25,18 @@ char* fetch_from_string(char* message, char* indicator) {
}
char* fetch_message_string(char* message) {
int num_of_terminators;
char* start = strstr(message,"START_OF_MESSAGE");
if (start == NULL) {
return NULL;
}
int start_index = start - message;
start_index += strlen("START_OF_MESSAGE");
while (*(message + start_index) == '\n' || *(message + start_index) == '\r') {
start_index++;
}
char* end = strstr(message,"END_OF_MESSAGE");
if (end == NULL) {
@ -37,9 +44,20 @@ char* fetch_message_string(char* message) {
}
int end_index = end - message;
if (*(message+ end_index-2) = '\r') {
num_of_terminators = 2;
} else {
num_of_terminators = 1;
}
int message_length = end_index - start_index;
char* message_string = malloc(message_length + 1);
for (int i=0;i < message_length; i++) {
*(message_string + i) = *(message_
for (int i=0;i < message_length-num_of_terminators; i++) { /* The reason the upper-bound is message_length-1 is because the last
character is a new-line, which the user would not have typed. */
*(message_string + i) = *(message + start_index + i);
}
*(message_string + (message_length - num_of_terminators)) = '\0';
return message_string;
}

Loading…
Cancel
Save