You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.8 KiB
C

#include <easysock.h>
#include "message_helpers.h"
char* fetch_from_string(char* message, char* indicator) {
char* message_copy = malloc(strlen(message));
strcpy(message_copy,message);
char* token = malloc (sizeof(char) * strlen(message_copy));
char* string_to_search = malloc(strlen(indicator) + 1);
strcpy(string_to_search,indicator);
strcat(string_to_search,":");
token = strtok(message_copy," r\n");
while (strcmp(token,string_to_search) != 0) {
token = strtok(NULL," \r\n");
if (token == NULL) {
return NULL;
}
}
token = strtok(NULL," \r\n");
return token;
}
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) {
return NULL;
}
int end_index = end - message;
if (*(message + end_index-2) = '\r') {
num_of_terminators = 2;
} else {
num_of_terminators = 1;
}
end_index -= num_of_terminators;
int message_length = end_index - start_index;
char* message_string = malloc(message_length + 2);
printf("Message length is %d\n",message_length);
for (int i=0; i<message_length; 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);
printf("%c",*(message_string + i));
}
strcat(message_string,"\0");
// printf("%s\n",message_string);
// *(message_string + (message_length - num_of_terminators)) = '\0';
return message_string;
}