Code:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <ctype.h>
#define MAX_TIMESTR 1024
/* Data Structures */
struct message {
time_t time;
char location[24];
int device_id;
double frequency;
int burst_size;
int msg_len;
int *msg;
};
struct node {
struct node* next;
struct message data;
};
/* Function Prototypes */
char *timestamp(time_t);
void sorted_insert(struct node** headRef, time_t, char *, int, double, int, int, int[]);
/* Debug Function Prototypes - Remove once Release */
void print_list(struct node* root)
{
struct node* current = root;
while (current != NULL){
printf("%s %s/%d %s\n",timestamp((current->data).time),
(current->data).location, (current->data).device_id,
(current->data.msg));
current = current->next;
}
}
int main(int argc, char **argv)
{
int i = 1, a = 1;
time_t data_time;
char data_location[24];
int data_deviceid, burst_size, msg_len;
double data_frequency;
int *data_message;
struct node *empty = NULL;
/* Check for arguments if argc == 1 display usage */
if (argc == 1){
printf("usage: codebreak messagefile\n");
printf("where: messagefile contains intercepts in the correct binary format\n");
return 0;
}
/* Open a File for reading */
FILE *fp = fopen(argv[i],"r");
/* Place Data into Variables ready for insertion into SLL */
while ((a = fseek(fp,1,SEEK_CUR)) > 0){
fseek(fp,0,SEEK_CUR);
fscanf("%d %s %d %ld %d %d",&data_time,data_location,data_deviceid,
&data_frequency, &burst_size, &msg_len);
fseek(fp,1,SEEK_CUR);
data_message = malloc(msg_len*sizeof(int));
fgets(data_message,msg_len,fp);
/* Debug */
printf("%d %s %d %ld %d %d",data_time,data_location,data_deviceid,
data_frequency,burst_size,msg_len);
sorted_insert(&empty, data_time, data_location, data_deviceid,
data_frequency, burst_size, msg_len, data_message);
}
print_list(empty);
return 0;
}
void sorted_insert(struct node** headRef, time_t data_time, char data_location[],
int data_deviceid, double data_frequency, int burst_size, int msg_len,
int data_msg[])
{
int j;
struct node** currentRef = headRef;
struct node* new = (struct node*)malloc(sizeof(struct node));
/* Traverse until right node for insertion */
while(*currentRef != NULL && ((int)(data_time) < (int)((*currentRef)->data.time)))
currentRef = &((*currentRef)->next);
/* Insert data and re-link structures */
new->next = *currentRef;
*currentRef = new;
new->data.time = data_time;
for (j = 0; j < 24; j++){
new->data.location[j] = data_location[j];
}
new->data.device_id = data_deviceid;
new->data.frequency = data_frequency;
new->data.burst_size = burst_size;
new->data.msg_len = msg_len;
new->data.msg = malloc(sizeof(msg_len * sizeof(int)));
for (j = 0; j < msg_len; j++){
new->data.msg[j] = data_msg[j];
}
}
char *timestamp(time_t tval)
{
static char t_buff[MAX_TIMESTR];
t_buff[MAX_TIMESTR-1] = '\0';
strftime(t_buff, MAX_TIMESTR-1, "%Y-%m-%d",gmtime(&tval));
return t_buff;
}
Is there anyway to obtain the correct structure of the file and put them into their respective variables.